Euler Project Problem #10 Solution
Posted by oxaric on November 24, 2008
It uses C.
Click to directly download euler-solution-10.c
// filename 'euler-solution-10.c' // By: Louis Casillas, oxaric@gmail.com
// Euler Problem #10 // Find the sum of all the primes below two million.
#include <stdio.h> #include <math.h>
char isPrime( int num ) { if ((num % 3) == 0 ) { return 0; }
int i = 0;
int r = floor ( sqrt( num ) ); int f = 5;
while (f <= r) { if ((num % f) == 0) { return 0; }
if ((num % (f + 2)) == 0) { return 0; }
f += 6; }
return 1; }
#define UPTO 2000000
int main() { int i = 5; double sum_of_primes = 5;
while (i < UPTO) { if ( isPrime( i ) ) { sum_of_primes += i; }
i += 2; }
printf( "The sum of the primes is: %f", sum_of_primes );
return 0; }
|
Advertisement
Like this:
Be the first to like this post.
This entry was posted on November 24, 2008 at 7:50 and is filed under C/C++, Programming, Project Euler.
Tagged: 10, answer, c++, euler, project, solution, ten. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.