// you’re reading...

Project Euler Solutions

Project Euler Problem 10 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Analysis

Sieve the prime numbers less than 2,000,000 and sum them. Having a fast algorithm to sieve primes helps a solution run under a few seconds.

Solution

Runs < 2 seconds in Python.

limit = 2000000
sieve = range(limit)
for n in xrange(2, int(limit**.5) + 1):
  if sieve[n]: 
    for i in xrange(n ** 2, limit, n): 
      sieve[i] = 0
print "answer to PE10 = ",sum(sieve)

Comments

This problem changed from a limit of one million in 2008.

Discussion

No comments for “Project Euler Problem 10 Solution”

Post a comment