Question:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
tips:注意结果的值可能大于32位机器中long的表示范围!!!
C Code:
#include <stdio.h>
int IsPrim(long n)
{
int i;
for(i = 2;i*i <= n;i++)
{
if(0 == n%i)
return 0;
}
return 1;
}
void main()
{
long i = 3;
_int64 sum = 2;
while(i <= 2000000)
{
if(IsPrim(i))
{
sum += i;
}
i++;
}
printf("%I64d\n",sum);
}
Answer:
142913828922