Given a user can select any 3 non-repeating numbers n
from a range of 1-10, identify any prime numbers where 1 <= prime_numbers <= n
. n
is an integer of all the 4 selected numbers put together in the order they were chosen. So selecting numbers 1, 12, 8
is 1128
.
Return the middle most prime number. If there are an even number of prime numbers, get the lower number.
def middle_prime(*args):
maximum = 10
try:
if len(args) > 3:
raise Exception('Only 3 numbers are allowed.')
else:
for i in args:
if not 0 < i <= maximum:
raise Exception('Only type numbers from 1 to {}.'.format(maximum))
num = int(''.join([str(i) for i in args]))
primes = [i for i in range(2, num + 1) if is_prime(i)]
if primes:
mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
return primes[mididx]
return -1
except Exception as e:
return e
def is_prime(int:int):
for i in range(2, int):
if not int % i:
return False
return True
test.assert_equals(middle_prime(2, 3, 8), 101)
test.assert_equals(middle_prime(10, 9, 8), 479)
test.assert_equals(middle_prime(4, 1, 3), 173)
test.assert_equals(middle_prime(9, 10, 3), 4099)