project euler #10: optimization with primes
baseball.mjp at gmail.com
Tue Mar 31 18:11:45 PDT 2009
Hey, I've started to do some of the problems on Project Euler to practice my programming skills. I'm doing #10 right now, and I think I've got a working solution. It's just that it's way too slow.
Here's the link:
The code works fine with sum of primes below 10. It output 17.
But for 2 million, it takes quite a while, and an FAQ on the page said most problems are made with being run within a minute. Mine was gonna quite a bit longer.
Here is my code:
int sum = 2;
long bigNum = 10; //Or 2 million
for ( int i = 3; i < bigNum; i += 2 )
if( isPrime( i ) )
sum += i;
writefln( sum );
bool isPrime( long n )
for( int i = 2; i < n; i++ )
if ( n % i == 0 )
I'm pretty sure the isPrime() function can be done better.
More information about the Digitalmars-d-learn