【题目】
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
【翻译】10以下的质数的和是2 + 3 + 5 + 7 = 17.
找出两百万以下所有质数的和。
【思路】依旧暴力法【代码】
bool isPrime( int value)
{
if(value==2)
return true;
if(value<2 || (!(value&1)))
return false;
for(int i=2;i<(int)sqrt(value)+1;i++)
{
if(value%i==0)
{
return false;
}
}
return true;
}
void test10()
{
long long int sum=2;
for(int i=3;i<2000000;i+=2)
if(isPrime(i))
sum+=i;
cout<<sum<<endl;
}
【答案】本题答案为 142913828922。