For a given number N, find whether it is a factorial number or not. A Factorial number is a number which is equal to the factorial value of other numbers.
 
Example 1:
Input:
N = 6
Output:
1
Explanation:
6 is factorial of 3
Example 2:
Input:
N = 5
Output:
0
Explanation:
no number's factorial is 5.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isFactorial() which takes an integer N as input parameters and returns 1 if N is a factorial number, or 0 otherwise.
 
Expected Time Complexity: O(log N)
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 100000

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def isFactorial(self, N):
		sum = 1
		if N == 1 or N == 0 or N == 2:
			return 1
		for i in range(2, N + 1):
			if sum == N:
				return 1
			elif sum > N:
				return 0
			sum = sum * i


k = 1

class Solution:

	def isFactorial(self, N):
		global k
		if N == 1:
			k = 1
			return 1
		k = k + 1
		if N % k == 0:
			return self.isFactorial(N / k)
		else:
			k = 1
			return 0


import math

class Solution:

	def isFactorial(self, N):
		a = 2
		while N != 1:
			if N % a != 0:
				return 0
			N /= a
			a += 1
		return 1


class Solution:

	def isFactorial(self, N):
		import math
		i = 0
		while math.factorial(i) <= N:
			i = i + 1
			if math.factorial(i) == N:
				return 1
				break
		if math.factorial(i) > N:
			return 0


class Solution:

	def isFactorial(self, N):
		i = 2
		while N != 1:
			if N % i != 0:
				return 0
			N = N // i
			i += 1
		return 1


class Solution:

	def isFactorial(self, N):
		a = 1
		for i in range(1, N + 1):
			a *= i
			if a == N:
				return 1
			if a > N:
				return 0


class Solution:

	def isFactorial(self, N):
		f = 1
		i = 1
		while 1:
			f *= i
			if f == N:
				return 1
				break
			elif f > N:
				return 0
				break
			i += 1


class Solution:

	def isFactorial(self, n):
		i = 1
		while True:
			if n % i == 0:
				n //= i
			else:
				break
			i += 1
		if n == 1:
			return 1
		else:
			return 0


class Solution:

	def isFactorial(self, N):
		y = 1
		for x in range(1, N + 1):
			y = y * x
			if y == N:
				return 1
			elif y > N:
				return 0


class Solution:

	def isFactorial(self, N):
		x = 1
		ctr = 1
		pr = 1
		while pr < N:
			pr = pr * ctr
			ctr = ctr + 1
			if pr == N:
				return 1
		if pr == 1:
			return 1
		return 0


class Solution:

	def isFactorial(self, N):
		p = 1
		i = 1
		while p < N:
			p = p * i
			i += 1
		if p == N:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isFactorial(N))


class Solution:

	def isFactorial(self, N):
		(val, idx) = (1, 1)
		while val < N:
			val *= idx
			idx += 1
		if val == N:
			return 1
		else:
			return 0


class Solution:

	def isFactorial(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact *= i
			if fact == N:
				return 1
			if fact > N:
				break
		return 0


class Solution:

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


class Solution:

	def isFactorial(self, N):
		x = 1
		if N == 1 or N == 0 or N == 2:
			return 1
		else:
			i = 2
			while i <= N:
				if x == N:
					return 1
				elif x > N:
					return 0
				x = x * i
				i = i + 1


from math import sqrt

class Solution:

	def isFactorial(self, N):
		fact = 1
		x = N
		for i in range(1, round(sqrt(N) + 0.5) + 1):
			fact = fact * i
			if fact == x:
				return 1
		return 0


import math

class Solution:

	def isFactorial(self, n):
		b = int(math.sqrt(n)) + 1
		k = 1
		for i in range(1, b + 1):
			k *= i
			if k == n:
				return 1
		return 0


class Solution:

	def isFactorial(self, N):
		i = 1
		k = 1
		while i < N:
			i = i * k
			k += 1
		if i == N:
			return 1
		else:
			return 0