I've been set an assignment to find all the prime numbers smaller than N
We've been told to first create a function to test if it is divisble by any numbers in my primes list
and then second to create a function to test each integer from 2 to N, then using the previous function find all the primes and print them.
i've created this, however my code outputs all the numbers from 2 to N rather than just the primes.
I can't figure out why.
def is_divisible(n, primes):
"""
Tests to see if a number is divisible by any number in the list of primes
"""
for p in primes:
if n % p == 0:
return False
return True
def find_primes(N):
"""
Returns list of primes smaller than N
"""
primes=[]
for n in range(2, N+1):
is_divisible(n,primes)
if True:
primes.append(n)
print(primes)
find_primes(20)