Problem
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example
Input: 16
Output: true
Input: 14
Output: false
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPerfectSquare(int num) {
long temp = num;
while (temp*temp > num)
temp = (temp + num/temp) / 2;
return temp*temp == num;
}
};