Project Euler——Problem 10 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 10
daybreakcx
posted @ 2009年7月17日 22:00
in Prject Euler
, 1046 阅读
原题与答案:
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
分析与解答:
题目的意思很容易理解,就是求2000000以下的素数的和,这个题目又是一个素数题,我们很容易写出C代码,但是要注意,结果是超出32位整数的,如下是程序源代码:
-
#include<stdio.h>
-
#include<string.h>
-
bool nprime[2000001];
-
long long ans=0;
-
int i,j;
-
int main()
-
{
-
memset(nprime,false,sizeof(nprime));
-
for (i=2;i<=1414;i++)
-
if (!nprime[i])
-
{
-
ans+=i;
-
for (j=i*i;j<=2000000;j+=i)
-
nprime[j]=true;
-
}
-
for (i=1415;i<=2000000;i++)
-
if (!nprime[i]) ans+=i;
-
return 0;
-
}