Project Euler: Problem 10

This is problem 10 on Project Euler:

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

Find the sum of all the primes below two million.

This is another problem where I cannot see any other solution than the brute-force approach of simply adding primes until we find a prime greater than two million.

This is the Delphi code:

procedure TfrmMain.Problem10;
  x : integer;
  primeSum : int64;
  primeSum := 0;
  for x := 0 to lstPrimes.Count - 1 do
    inc(primeSum, GetPrime(x));

Line 6 populates our list of prime numbers with all primes up to 2 000 000. Line 7 initialises the result (primeSum) to zero. We then go through the list adding each entry to the result.

On my PC this code takes 62 milliseconds to execute.

Tags: , ,

2 Responses to “Project Euler: Problem 10”

  1. Nice! Just wanted to respond. I thoroughly loved your post. Keep up the great work on .

  2. With 1 excluded, the smallest prime is therefore 2. However, since 2 is the only even prime (which, ironically, in some sense makes it the “oddest” prime), it is also somewhat special, and the set of all primes excluding 2 is therefore called the ” odd primes .” Note also that while 2 is considered a prime today, at one time it was not (Tietze 1965, p. 18; Tropfke 1921, p. 96).