I think there is a precision problem with the following code:
bool isPerfectSquare(long long n){
long long squareRootN=(long long)(sqrt(n)+0.5);
return
You might use std::sqrt as a guess and test with multiplication:
#include
#include
#include
bool isPerfectSquare(long long n){
double guess = sqrt(n);
long long r = std::floor(guess);
if(r*r == n) return true;
else {
r = std::ceil(guess);
return r*r == n;
}
}
int main() {
const long long Limit = std::pow(10, std::numeric_limits::digits10 / 2);
std::cout << " Limit: " << Limit << '\n';
for (long long i = 0; i < Limit; ++i) {
if( ! isPerfectSquare(i*i)) {
std::cout << "Failure: " << i << '\n';
return 0;
}
}
std::cout << "Success\n";
}