I gave a fairly naive implementation on this, the last iteration took a while to return results.
def return_primes(upto=100):
primes = []
for i in range(2, upto+1):
if not any(not i % p for p in primes):
primes.append(i)
return primes
And usage:
>>> sum(return_primes(upto=20000))
21171191
>>> sum(return_primes(upto=200000))
1709600813
>>> sum(return_primes(upto=2000000))
142913828922
Here's another implementation using the Sieve of Eratosthenes:
def return_primes(upto=100):
primes = []
sieve = set()
for i in range(2, upto+1):
if i not in sieve:
primes.append(i)
sieve.update(range(i, upto+1, i))
return primes
This is much faster, running in about a second or two, compared to minutes for the above:
>>> sum(return_primes(200000))
1709600813
>>> sum(return_primes(2000000))
142913828922
To attempt to better diagnose your problem with this information, we see that
>>> 142913828922 % 2**32
1179908154
Which (thank you Will Ness) leads one to conclude that you're summing with 32bit integers. If we change your code to the following:
import math
import numpy as np
def sum_primes(limit):
potential = 2
primes = [potential]
potential += 1
primes.append(potential)
while potential < limit:
potential+=2
test = True
sqrt_potential = math.sqrt(potential)
for a in primes:
if a > sqrt_potential:
break
if potential%a == 0:
test = False
break
if test and potential <= limit:
primes.append(potential)
print np.sum(primes, dtype=np.int64)
return
then you should get the correct output.
And I do replicate your behavior if I instead use:
print np.sum(primes, dtype=np.int32)