Given an integer N, check if it is possible to represent it as a function(a, b) such that : a^{2} + b^{2} = N where "a" and "b" are whole numbers. Print 1 if it's possible, else Print 0.
 
Example 1:
Input:
N = 1
Output:
1
Explanation:
1^{2} + 0^{2} = 1. So, the Output is 1
Example 2:
Input:
N = 2
Output:
1
Explanation:
1^{2} + 1^{2} = 2. So, the Output is 1
Example 3:
Input:
N = 7
Output:
0
Explanation:
We don't have a or b, which when summed
after squaring gives 7. So, Output is 0.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkSquares() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def checkSquares(self, N):

        # code here 

Solutions:
import math

class Solution:

	def checkSquares(self, N):
		flag = 0
		for a in range(int(math.sqrt(N)) + 1):
			for b in range(int(math.sqrt(N)) + 1):
				if a * a + b * b == N:
					flag = 1
					break
			if flag == 1:
				break
		return flag


class Solution:

	def checkSquares(self, N):
		for i in range(N):
			d = N - i * i
			if d < 0:
				return 0
			k = int(d ** 0.5)
			if k * k == d:
				return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.checkSquares(N))


import math

class Solution:

	def checkSquares(self, N):
		i = int(math.sqrt(N))
		while i >= 0:
			j = i
			while j >= 0:
				if i * i + j * j == N:
					return 1
				j -= 1
			i -= 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.checkSquares(N))


from math import sqrt

class Solution:

	def checkSquares(self, N):
		for i in range(int(sqrt(N)) + 1):
			for j in range(int(sqrt(N)) + 1):
				if i * i + j * j == N:
					return 1
		return 0


class Solution:

	def checkSquares(self, N):
		import math
		for i in range(int(math.sqrt(N)) + 1):
			if N - i ** 2 in [x ** 2 for x in range(int(math.sqrt(N)) + 1)]:
				return 1
		return 0