Given a positive integer n, check if it is perfect square or not.
NOTE: Try to solve the question using only addition and subtraction operation.
Example 1:
Input: n = 35
Output: 0 
Explanation: 35 is not perfect
square because sqrt(35) = 5 but
5*5 !=35.
Example 2:
Input: n = 49
Output: 1
Explanation: sqrt(49) = 7 and 
7*7 = 49, Hence 49 is perfect square. 
Your Task:  
You dont need to read input or print anything. Complete the function isPerfectSquare() which takes n as input parameter and returns 1 if n is a perfect square else return 0.
Expected Time Complexity: O(sqrt(n))
Expected Auxiliary Space: O(1)
Constraints:
1<= n <=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



#User function Template for python3

class Solution:

    def isPerfectSquare (self, n):

        # code here 

Solutions:
class Solution:

	def isPerfectSquare(self, n):
		k = n ** 0.5
		if int(k) * int(k) == n:
			return 1
		return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		if math.ceil(math.sqrt(n)) == math.floor(math.sqrt(n)):
			return 1
		else:
			return 0


from math import sqrt

class Solution:

	def isPerfectSquare(self, n):
		j = sqrt(n)
		if j % 1 == 0:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		sq = int(math.sqrt(n))
		if sq * sq == n:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		sqRoot = int(math.sqrt(n))
		if sqRoot * sqRoot == n:
			return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		beg = 1
		end = n
		while beg <= end:
			mid = (beg + end) // 2
			if mid * mid < n:
				beg = mid + 1
			elif mid * mid > n:
				end = mid - 1
			else:
				return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		for i in range(1, n):
			if i * i == n:
				return 1
			if i * i > n:
				return 0


class Solution:

	def isPerfectSquare(self, n):
		if n ** 0.5 * 10 % 10 == 0:
			return 1
		return 0


from math import sqrt

class Solution:

	def isPerfectSquare(self, n):
		a = sqrt(n)
		if n == int(a) * int(a):
			return 1
		else:
			return 0


class Solution:

	def isPerfectSquare(self, n):
		left = 1
		right = n // 2
		while left <= right:
			mid = (left + right) // 2
			if mid ** 2 == n:
				return 1
			elif mid ** 2 > n:
				right = mid - 1
			else:
				left = mid + 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		i = 1
		while i * i < n:
			i += 1
		if i * i == n:
			return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		import math as m
		a = int(m.sqrt(n))
		if a ** 2 == n:
			return 1
		else:
			return 0


class Solution:

	def isPerfectSquare(self, n):
		r = int(n ** 0.5)
		return int(r * r == n)


class Solution:

	def isPerfectSquare(self, n):
		c = int(n ** (1 / 2))
		if c ** 2 == n:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		z = math.sqrt(n)
		while n > 0:
			n -= z
		if n == 0:
			return 1
		else:
			return 0


class Solution:

	def isPerfectSquare(self, n):
		lim = int(n ** 0.5)
		for i in range(1, lim + 1):
			if i * i == n:
				return 1
		return 0


class Solution:

	def isPerfectSquare(self, num):
		l = 0
		h = num
		m = l + h >> 1
		while l <= h:
			if m * m == num:
				return 1
			elif m * m > num:
				h = m - 1
			else:
				l = m + 1
			m = l + h >> 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		if n == 1:
			return 1
		elif n == 2 or n == 3:
			return 0
		for i in range(2, int(n ** 0.5) + 1):
			if i * i == n:
				return 1
		return 0


import math

class Solution:

	def isPerfectSquare(self, N):
		if N >= 0:
			n = int(math.sqrt(N))
			s = n * n
		if s == N:
			return '1'
		else:
			return '0'


class Solution:

	def binary_search(self, n):
		l = 0
		r = n // 2
		while l <= r:
			mid = (l + r) // 2
			if mid * mid == n:
				return 1
			elif mid * mid > n:
				r = mid - 1
			else:
				l = mid + 1
		return 0

	def isPerfectSquare(self, n):
		ans = self.binary_search(n)
		return ans


class Solution:

	def isPerfectSquare(self, n):
		r = int(n ** 0.5)
		return 1 if n == r * r else 0


import math as m

class Solution:

	def isPerfectSquare(self, n):
		x = pow(n, 0.5)
		return 1 if m.ceil(x) == m.floor(x) else 0


class Solution:

	def isPerfectSquare(self, n):
		import math
		ans = math.sqrt(n)
		if ans.is_integer():
			return 1
		else:
			return 0


class Solution:

	def isPerfectSquare(self, n):
		num = int(n ** 0.5)
		if num ** 2 == n:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		for i in range(1, n):
			if math.sqrt(n) == i:
				return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		for x in range(0, 200000):
			if n == x ** 2:
				return 1
			elif x ** 2 > n:
				return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		int_sqr_root = int(math.sqrt(n))
		float_sqr_root = math.sqrt(n)
		if int_sqr_root == float_sqr_root:
			return 1
		return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		k = math.sqrt(n)
		p = int(k)
		if p * p == n:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		p = n ** 0.5
		if math.floor(p) == p:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectSquare(self, n):
		for x in range(0, int(math.sqrt(n)) + 1):
			if x * x == n:
				return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		if n == 1:
			return 1
		a = n ** 0.5
		b = int(n ** 0.5)
		if a == b:
			return 1
		return 0


class Solution:

	def isPerfectSquare(self, n):
		a = int(n ** 0.5)
		if a ** 2 == n:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.isPerfectSquare(n))


import math

class Solution:

	def isPerfectSquare(self, n):
		for i in range(1, int(math.sqrt(n)) + 1):
			if math.pow(i, 2) == n:
				return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.isPerfectSquare(n))


class Solution:

	def isPerfectSquare(self, n):
		S = int(n ** (1 / 2))
		if S * S == n:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.isPerfectSquare(n))


class Solution:

	def isPerfectSquare(self, n):
		g = int(n ** 0.5)
		if g * g == n:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.isPerfectSquare(n))