Problem10

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…

?View Code PYTHON
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: , , ,

One Response to “Problem10”

  1. [...] 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 [...]

Leave a Reply