Project Euler——Problem 10 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 9
Project Euler——Problem 11

Project Euler——Problem 10

daybreakcx posted @ 2009年7月17日 22:00 in Prject Euler , 1028 阅读

原题与答案:

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位整数的,如下是程序源代码:

 

  1. #include<stdio.h>
  2. #include<string.h>
  3. bool nprime[2000001];
  4. long long ans=0;
  5. int i,j;
  6. int main()
  7. {
  8.         memset(nprime,false,sizeof(nprime));
  9.         for (i=2;i<=1414;i++)
  10.                 if (!nprime[i])
  11.                 {
  12.                         ans+=i;
  13.                         for (j=i*i;j<=2000000;j+=i)
  14.                                 nprime[j]=true;
  15.                 }
  16.         for (i=1415;i<=2000000;i++)
  17.                 if (!nprime[i]) ans+=i;
  18.         printf("%lld\n",ans);
  19.         return 0;
  20. }

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter