The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
#include <stdio.h>
#define NUM 2000000
int main()
{
int i, j;
unsigned long int sum = 2;
char arr[NUM] = { 0 };
for ( i = 3; i < NUM; i+=2 )
{
if ( 0 == arr[i] )
{
sum += i;
for ( j = 2; i*j < NUM; j++ )
{
arr[i * j] = 1;
}
}
}
printf( "%ld\n", sum );
return(0);
}
解出答案是:142913828922