【Project Euler】【Problem 10】Summation of primes

来源:互联网 发布:linux解压命令zip 编辑:程序博客网 时间:2024/05/16 09:43

Summation of primes

Problem 10

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
翻译

质数的和

问题 10

10以内所有质数的和是: 2 + 3 + 5 + 7 = 17.

求2000000以内所有质数的和。


答案:
142913828922
解法一:
#include <stdio.h>#include <math.h>bool isPrime(long num){    int _sqrt = (long)sqrt(num);    if (num == 2 || num == 3)    {        return true;    }    for (int i = 2; i <= _sqrt; i++ )    {        if (num % i == 0)        {            return false;        }    }    return true;}int main(int argc, char *argv[]){long long result = 0;long maxNum = 2000000;int _count = 1;for (long i = 2; i<maxNum; i++){if (isPrime(i)){result += i;_count++;}}printf("result is %I64d \n",result);return 0;}