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 1:

Input: 16
Returns: True



Example 2:

Input: 14
Returns: False



Credits:Special thanks to @elmirap for adding this problem and creating all test cases.

Difficulty: introductory

Starter code:

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        

Solutions:
class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         n = num**0.5
         if n == int(n):
             return True
         else:
             return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         yy = math.sqrt(num)
         ans_str = str(yy)
         ans = ans_str.split(".")
         if (int(ans[1]) == 0 ):
             return True
         else:
             return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         start = 1
         while num > 0:
             num = num - start
             start = start + 2
         
         if num == 0:
             return True
         
         return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
 
         num = num ** 0.5
         if num == int(num):
             return True
         return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         i = 0
         while i*i <=num:
             if i*i == num:
                 return True
             i +=1
         
         return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         x = 0
         for i in range(num):
             x += 1 + 2*i
             if x == num: return True
             if x > num: return False
         return False

class Solution:
     def isPerfectSquare(self, num):
         sign = True
         i = 1
         while(sign):
             if(num == i * i):
                 return True
             elif(num > i * i):
                 i = i + 1
             else:
                 return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num < 1:
             return False
         n = 100
         n = int(n/2 + num/2/n)
         while n**2 > num:
             n = int(n/2 + num/2/n)
         if n**2 == num:
             return True
         else:
             return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         p, r = 1, (num>>1) + 1
         while p <= r:
             mid = (p + r) >> 1
             sq = mid*mid
             if sq == num:
                 return True
             if sq >= num:
                 r = mid - 1
             if sq <= num:
                 p = mid + 1
         return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         
         if num < 1 :
             return False
         root = 1
         while (root*root < num):
             root = root+1
         if root*root == num:
             return True
         return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         middle = num//2+1
         for i in range(0, middle+1):
             if i*i == num:
                 return True
             if i*i > num:
                 return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         for i in range(1, num + 1):
             if i * i == num:
                 return True
             elif i * i > num:
                 return False

class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         low = 1
         high = num
         while low <= high:
             mid = (low + high) // 2
             if mid **2 == num:
                 return True
             elif mid **2 < num:
                 low = int(mid) + 1
             elif mid **2 > num:
                 high = int(mid) - 1
         return False


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num < 1:
             return False
         
         i = 1
         while True:
             pow2 = i**2
             i += 1
             if pow2 > num:
                 return False
             if pow2 == num:
                 return True
 


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         
         i = 1        
         while(True):
             if num / i < i:
                 # print num / i
                 return False
             else:
                 if num / i == i:
                     return True
                 else:
                     i += 1


class Solution:
     def isPerfectSquare(self, num):
         """
         :type num: int
         :rtype: bool
         """
         a = num
         while a * a > num:
             a = (a + num//a) // 2
         return a * a == num