Given a number N, check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
 
Example 1:
Input:
N = 6
Output:
1 
Explanation:
Factors of 6 are 1, 2, 3 and 6.
Excluding 6 their sum is 6 which
is equal to N itself. So, it's a
Perfect Number.
Example 2:
Input:
N = 10
Output:
0
Explanation:
Factors of 10 are 1, 2, 5 and 10.
Excluding 10 their sum is 8 which
is not equal to N itself. So, it's
not a Perfect Number.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isPerfectNumber() which takes an Integer N as input and returns 1 if N is a Perfect number else returns 0.
 
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{12}

Difficulty: EASY
Tags: Mathematics
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
    def isPerfectNumber(self, N):
        # code here 

Solutions:
class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		s = 1
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				s += i + N / i
			if s > N:
				return 0
		if s == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfectNumber(self, N):
		l = [6, 28, 496, 8128]
		if N in l:
			return 1
		return 0


def isPerfect(n):
	if n % 2 == 0:
		for i in range(1, 20):
			if n == pow(2, i) * (pow(2, i + 1) - 1):
				return 1
		return 0
	else:
		t = int(n ** 0.5) + 1
		result = 1
		for i in range(2, t):
			if n % i == 0:
				result += i + n // i
		if result == n:
			return 1
		return 0

class Solution:

	def isPerfectNumber(self, n):
		if n == 1:
			return 0
		if isPerfect(n):
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, num):
		pefectnum = [6, 28, 496, 8128, 33550336]
		if num in pefectnum:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		sum = 1
		i = 2
		while i <= math.sqrt(N) and sum < N:
			if N % i == 0:
				sum = sum + i + N / i
			i = i + 1
		return 1 if sum != 1 and sum == N else 0


class Solution:

	def isPerfectNumber(self, N):
		assert 1 <= N <= 10 ** 12
		sum = 0
		x = 1
		while x > 0 and x < 100000:
			if N % x == 0 and N != x:
				sum = sum + x
			else:
				sum = sum
			x = x + 1
		if sum == N:
			return 1
		else:
			return 0


from math import floor, sqrt

class Solution:

	def isPerfectNumber(self, N):
		if N <= 1:
			return 0
		(i, total) = (2, 1)
		while i <= floor(sqrt(N)) and total <= N:
			if N % i == 0:
				total += i
				div = N // i
				if i != div:
					total += div
			i += 1
		return 1 if total == N else 0


class Solution:

	def isPerfectNumber(self, N):
		i = 1
		s = 0
		while i * i <= N:
			if N % i == 0:
				s += i
				s += N // i
				if s - N > N:
					return 0
			i += 1
		return 1 if s - N == N and N != 1 else 0


from math import sqrt

class Solution:

	def isPerfectNumber(self, N):
		s = 1
		n = int(sqrt(N)) + 1
		for i in range(2, n):
			if N % i == 0:
				s += i
				if i != N // i:
					s += N // i
			if s > N:
				return 0
		return 1 if s == N and N != 1 else 0


class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		sum = 1
		i = 2
		while i * i <= N and sum < N:
			if N % i == 0:
				sum = sum + i + N // i
			i += 1
		if sum == N:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, N):
		total = 1
		sqrtN = int(N ** (1 / 2))
		for i in range(2, sqrtN + 1):
			if N % i == 0:
				total = total + i + N / i
				if total > N:
					return 0
		if total == N and total != 1:
			return 1
		return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		sum = 0
		p = 1
		while p > 0 and p < 100000:
			if N % p == 0 and N != p:
				sum = sum + p
			else:
				sum = sum
			p = p + 1
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfectNumber(self, N):
		sum = 0
		x = 1
		while x > 0 and x < 100000:
			if N % x == 0 and N != x:
				sum = sum + x
			else:
				sum = sum
			x = x + 1
		if sum == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectNumber(self, n):
		if n <= 1:
			return 0
		(sum, i) = (1, 2)
		while i <= int(n ** 0.5) and sum < n:
			if n % i == 0:
				sum += i
				if i * i != n:
					sum += n // i
			i += 1
		if n == sum:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, N):
		import math
		if N <= 1:
			return 0
		total = 1
		for i in range(2, int(math.sqrt(N)) + 1):
			if total > N:
				return 0
			if N % i == 0:
				total += i
				if N // i != i:
					total += N // i
		if total == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfectNumber(self, N):
		if N == 6 or N == 28 or N == 496 or (N == 8128) or (N == 33550336) or (N == 8589869056) or (N == 137438691328):
			return 1
		return 0
		(s, i) = (1, 2)
		while i * i < N:
			k = N // i
			if k * i == N:
				s += i + k
			i += 1
		if i * i == N:
			s += i
		if s == N:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, N):
		if N in [6, 28, 496, 8128, 33550336, 8589869056]:
			return 1
		return 0


from math import *

class Solution:

	def isPerfectNumber(self, N):
		i = 1
		a = []
		sum = 0
		if N < 100000:
			for i in range(1, N + 1):
				if N % i == 0:
					a.append(i)
			for num in a:
				sum += num
			sum -= a[-1]
			if sum == N:
				return 1
			else:
				return 0
		else:
			return 0


class Solution:

	def isPerfectNumber(self, N):
		i = 1
		a = []
		sum = 0
		if N < 1000000:
			for i in range(1, N + 1):
				if N % i == 0:
					a.append(i)
			for num in a:
				sum = sum + num
			sum = sum - a[-1]
			if sum == N:
				return 1
			else:
				return 0
		else:
			return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		if N <= 2:
			return 0
		sm = 1
		for i in range(2, int(N ** 0.5) + 1, 1):
			if N % i == 0:
				ad = N // i
				sm += i
				if ad != i:
					sm += ad
				if sm > N:
					return 0
		if sm == N:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, N):
		if N < 2:
			return 0
		import math as g
		s = 1
		for i in range(2, g.ceil(g.sqrt(N))):
			if N % i == 0:
				if i * i == N:
					s += i
				else:
					s = s + i + int(N // i)
			if s > N:
				return 0
		return 1 if s == N else 0


import math

class Solution:

	def isPerfectNumber(self, n):
		if n < 2:
			return 0
		s = 1
		m = int(math.sqrt(n))
		for i in range(2, m + 1):
			if n % i == 0:
				if i * i == n:
					s += i
				else:
					s += i + n // i
			if s > n:
				return 0
		if s == n:
			return 1
		return 0


from math import sqrt

class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		sum = 1
		for i in range(2, round(sqrt(N // 2)) + 1):
			if N % i == 0:
				sum += i
				sum += N // i
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfectNumber(self, N):
		k = int(N ** 0.5)
		s = {}
		c = 0
		for i in range(1, k + 1):
			if N % i == 0:
				c += i
				if i != N // i:
					c += N // i
			if c - N > N:
				return 0
		if c - N == N:
			return 1
		return 0


import math

class Solution:

	def isPerfectNumber(self, n):
		s = 1
		m = int(math.sqrt(n))
		for i in range(2, m + 1):
			if n % i == 0:
				if i * i == n:
					s += i
				else:
					s += i + n // i
			if s > n:
				return 0
		if s == n and n != 1:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPerfectNumber(N))


import math

class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		summ = 1
		n = math.floor(math.sqrt(N))
		for i in range(2, n + 1):
			if N % i == 0:
				summ += i
				summ += N // i
				if summ > N:
					return 0
		if summ == N:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPerfectNumber(N))


import math

class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		summ = 1
		n = math.floor(math.sqrt(N))
		for i in range(2, n + 1):
			if N % i == 0:
				summ += i
				summ += N // i
				if summ > N:
					return 0
		if summ == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		sum = 1
		n = math.floor(math.sqrt(N))
		for x in range(2, n + 1):
			if N % x == 0:
				sum += x
				sum += N / x
				if sum > N:
					return 0
		if sum == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		if N < 2:
			return 0
		comp = int(math.floor(math.sqrt(N))) + 1
		s = 1
		i = 2
		while i < comp and s < N:
			if N % i == 0:
				s += i
				s += N // i
			i += 1
		if s == N:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, n):
		if n == 1:
			return 0
		s = 1
		i = 2
		while i * i <= n:
			if n % i == 0:
				s = s + i + n / i
			i = i + i
		return int(s == n)


import math

class Solution:

	def isPerfectNumber(self, n):
		if n == 1:
			return 0
		s = 1
		i = 2
		while i <= int(math.sqrt(N)):
			if N % i == 0:
				s = s + i + N / i
			i = i + i
		return int(s == n)


import math

class Solution:

	def isPerfectNumber(self, N):
		r = int(math.sqrt(N)) + 1
		s = 0
		for i in range(1, r):
			if N % i == 0:
				s += i if i != N else 0
				s += N // i if i != 1 else 0
			if s > N:
				return 0
		if s < N or s > N:
			return 0
		return 1


import math

class Solution:

	def isPerfectNumber(self, N):
		if N == 1:
			return 0
		root = math.ceil(math.sqrt(N))
		divisors = [1]
		for i in range(2, root):
			if N % i == 0:
				divisors.append(i)
				divisors.append(N // i)
		if sum(divisors) == N:
			return 1
		return 0


import math

class Solution:

	def isPerfectNumber(self, N):
		if N < 2:
			return 0
		s = 1
		i = 2
		while i * i <= N:
			if N % i == 0:
				s += i + N // i
			i += 1
		if s == N:
			return 1
		return 0


class Solution:

	def isPerfectNumber(self, N):
		upper_limit = int(N ** 0.5)
		factors_list = []
		for i in range(1, upper_limit + 1):
			(quotient, remainder) = divmod(N, i)
			if remainder == 0:
				factors_list.append(quotient)
				factors_list.append(int(N / quotient))
		factors_list = set(factors_list)
		factors_list.remove(N)
		if sum(factors_list) == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfectNumber(self, n):
		sum = 1
		i = 2
		while i * i <= n:
			if n % i == 0:
				sum = sum + i + n / i
			i += 1
		return 1 if sum == n and n != 1 else 0


import math

class Solution:

	def isPerfectNumber(self, n):
		sum = 0
		for i in range(1, int(math.sqrt(n)) + 1, 1):
			if n % i == 0 and i < n:
				sum += i
				sum += n // i
		return int(2 * n == sum)