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:

 1procedure TfrmMain.Problem10;
 2var
 3	x : integer;
 4	primeSum : int64;
 5begin
 6	GetPrimeList(2000000);
 7	primeSum := 0;
 8	for x := 0 to lstPrimes.Count - 1 do
 9		inc(primeSum, GetPrime(x));
10	ShowMessage(IntToStr(primeSum));
11end;

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.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

2 Responses to Project Euler: Problem 10

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

  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).

Leave a Reply

Your email address will not be published. Required fields are marked *