The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
A little sieve of Eratosthenes…
1 2 3 4 5 6 7 8 9 10 11 | # sieve of Eratosthenes def sieve(n): primes = range(2,n) x = 0 while primes[x] < n**0.5: primes = [y for y in primes if y==primes[x] or y%primes[x]] x = x+1 return primes >>> sum(sieve(2000000)) 142913828922L |
Tags: primes, problem10, project euler, sieve of eratosthenes
[...] python sets, the sieve of Eratosthenes from problem 10 and some pre-filtering for primes with even digits. ?View Code PYTHON# sieve of Eratosthenes def [...]