I'm trying to make a binary search of all prime numbers up to num
, but the search()
function won't return
the booleans so that the prime_maker()
function will print
'It's a prime!'.
def split_list(x):
spliting = len(x)//2
return x[:spliting], x[spliting:]
def search (A, a):
B, C = split_list(A)
if a <= A[-1] and a >= A[0]:
if A[len(A)//2] > a:
A = B
if A[len(A)//2] != a:
search(A,a)
else:
# print('It is a prime!')
return True
# This won't return
elif A[len(A)//2] < a:
A = C
if A[len(A)//2] != a:
search(A,a)
else:
# print('It is a prime!')
return True
# This won't return
else:
# print('It is not a prime.')
return False
def prime_maker():
a = int(input('Is it a prime?: '))
num = 100
prim = []
for x in range(2, num + 1):
isPrime = True
for y in range(2, int(x**0.5)+1):
if not (x % y):
isPrime = False
break
if isPrime:
prim.append(x)
if a < 0 or a > num:
print ('Insert a number between 0 og %d' % num)
prime_maker()
else:
if search(prim,a):
print('It\'s a prime!')
else:
print('It\'s not a prime')
prime_maker()
Why doesn't the search()
function return
the booleans
so that the prime_maker()
function prints the right if-statment
in the last few lines?