I have two blocks of code, both of which I have written to apply the sieve of eratosthenes to sum all primes up to 2000000. This first block, which is just raw code not wrapped in any function, is this:
N = 2000000
is_prime = (N + 1) * [True]
for candidate in range(2, N + 1):
if is_prime[candidate]:
print(candidate)
for witness in range(2 * candidate, N + 1, candidate):
is_prime[witness] = False
The second block of code has split this functionality into a function which check for primality, and then a for loop which specifies the upper bound. It is as follows:
def is_prime(n):
is_prime = (n + 1) * [True]
for candidate in range(2, int(sqrt(n)) + 1):
if is_prime[candidate]:
for witness in range(2 * candidate, n+1, candidate):
is_prime[witness] = False
return is_prime[n]
for candidate in range(2, LIMIT):
if is_prime(candidate):
print(candidate)
However, the block of code split into the function which checks primality is infinitely slower. I cannot for the life of me figure out what the difference between these blocks of code is. What am I doing wrong?