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
long long is an integer type. So, your +0.5 is lost in truncation.
long long