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;
var
x : integer;
primeSum : int64;
begin
GetPrimeList(2000000);
primeSum := 0;
for x := 0 to lstPrimes.Count - 1 do
inc(primeSum, GetPrime(x));
ShowMessage(IntToStr(primeSum));
end;
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: Mathematics, Programming, Project Euler