Home Project Euler Teaching Projects Gallery

Problem 10: Summation of primes

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

Find the sum of all the primes below two million.


Algorthim:

Finally, I use the Sieve of Erathosene method. I sugguest everyone look this up But, essentially, sieve method is as follows:

  1. Create a list of integers you want to go up to
  2. Start from p = 2 as it is the smallest prime
  3. Mark all numbers up to N that is a multiple of this prime (2)
  4. Find the next smallest number that is not marked (this is your next prime)
  5. Repeat step 2 but with this new prime

It's actaully sufficient to start from p^2 with every new prime we encounter, but that's not too important. Well, it will be for future cases. My code does not run optimally even though I understood the method and incorporated. I made some adjustment in later problems...

Anyhow, Wikipedia has a good demonstration of how the sieve method works:

From: Wikipedia

Code:

Result:

Answer: 142913828922
Runtime: 15.9 seconds