I was solving Euler problem no. 10 that asks to find sum of all primes below 2million.
I am getting different results on using sqrt
and pow
functions.
Using sqrt
gives the correct answer, plus using pow function takes more time. Here is my code.
for(sum=0,i=3;i<=2000000;i+=2)
{
for(j=3;j<=sqrt(i);j++)
if(i%j==0)
break;
if(j>sqrt(i))
sum+=i;
}
sum+=2;
std::cout << "\nSum = " << sum;
pow
? Show us your code. – Djon Jun 2 '13 at 10:09pow()
? – Daniel Fischer Jun 2 '13 at 10:09pow(i,0.5)
in place ofsqrt(i)
And usingpow()
gives a wrong answer and takes longer time. – Shubham Jun 2 '13 at 10:12break
statement.pow
orsqrt
, you get a wrong value either way. – Sergey Kalinichenko Jun 2 '13 at 10:12