blog.vi-kan.net One thought after another

Project Euler, Problem 10

Yet another problem involving primes. Guess I have to make a better prime generator soon…

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

Find the sum of all the primes below two million.

Soon, but not yet…

Using the same generator from problem 7, I go for an easy, but slow, solution for this one. There is one thing, though. Our generator generates up to the nth prime, but in this problem, we need to find every prime below 2000000. Already decided not to make a new prime generator, we cheat by asking it generate a lot of primes, and then jump out as soon as we hit a prime to big.

sum := 0;
for prime in PrimeGenerator(500000) do
begin
  if prime >= 2000000 then
    break;
  sum := sum + prime;
end;

I choose to generate 500000 primes, pretty sure one prime out of four should be enough.

And that’s it – it’s all there at http://svn.vi-kan.net/euler.

comments powered by Disqus