The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
In [13]:
def primes_beneath(N):
primes_found = [2]
i = 2
while primes_found[-1] < N:
i += 1
d = 1
sqrt_i = sqrt(i)
for p in primes_found:
if i % p == 0:
d = i / p
break
if p > sqrt_i:
break
if d == 1:
primes_found.append(i)
i += 1 # can skip evens.
return primes_found[:-1]
pr = primes_beneath(10)
print pr, sum(pr)
In [14]:
pr = primes_beneath(2e6)
print sum(pr)
In [ ]: