Problem 10
08 February 2002
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Answer:
142913828922
code:
#include
#include
int primer(long int n)
{
long long int flag=1,i;
for(i=2;i<=sqrt(n)&&flag==1;i++)
if(n%i==0)
flag=0;
return(flag);
}
int main(void)
{
long long j=3,sum=2;/*忽略了2,所以要加上2*/
for(j=3;j<2000000;j+=2)
{
if(primer(j))
sum+=j;
}
printf("%lld\n",sum);
return 0;
}
Test
13 年前
0 评论:
发表评论