歐拉計劃(10)Summation of primes

【題目】

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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章