我一直得到 1179908154 的错误答案。起初我将其归咎于我的求和变量是 int 类型,而不是 long。我给了它长类型,但我得到了相同的答案。想法?
// Project Euler
// Problem 10
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
int p = 3;
long sum = 2;
bool isPrime;
for (p; p < 2000000; p++)
{
isPrime = true;
for (int i = 2; i <= sqrt(static_cast<double>(p)); i++) // cast into double for sqrt function
{
if (p % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime == true)
{
cout << p << endl; // show each prime
sum += p; // add prime to sum
}
}
cout << sum << endl; // show sum
system("pause");
}