I'm a novice C++ programmer. Recently I've been practicing using programming to solve problems at projecteuler.net. I am stuck on a particular problem that involves finding the sum of all prime numbers below 2,000,000. here is my code:
#include <iostream>
#include <cmath>
usingnamespace std;
bool isPrime(int num)
{
if(num == 0)
returntrue;
for(int i = 2; i <= sqrt(num); i++)
{
if(num % i == 0)
{
returnfalse;
}
}
returntrue;
}
int main()
{
unsignedint sum=2;
for(int x=3;x<2000000;x+=2)
{
if (isPrime(x))
{
sum += x;
}
}
cout << sum;
return 0;
}
The code runs fine, but is apparently outputting the wrong answer. I have checked my logic multiple times and feel it is correct (though I may be wrong.) So my conclusion now is that there is something in my code that isn't working the way I expect it to. Any help would be appreciated. Thanks :)