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 could use a range for your return boolean, although it may lead to unprecise outputs depending on how stringent your requirements are:
double threshold = 0.01; return (squareRootN*squareRootN > n-threshold) && (squareRootN*squareRootN < n+threshold);