The problem reads as follows:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.Right from the start I can think of two methods:
Find the sum of all the primes below two million.
- Using a similar solution as for PE-7 but adding the primes instead of storing them
- Sieve of Eratosthenes
The third way I can think of is to combine the first two methods. As with PE-7, automatically exclude all multiples of 2 and 3 for a 2/3 reduction in numbers considered. Then find all the primes below the square root of 2 million (so anything below and including 1415) and sieve on the remaining 1/3 with those prime numbers. Could result in a nice speedup over the first method. As usual, another thing you could do is to remove the use of the modulus operand - it's expensive.
The code can be found here