不用别人认可也不用别人理解,尽情去做自己认为应该做的事……

2009年5月6日星期三

Problem 10--(c)

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;
}

0 评论: