A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. Given a number N as input. Print "YES" if it's a Krishnamurthy Number, else Print "NO".
 
Example 1: 
Input:
N = 145
Output:
YES
Explanation:
1! + 4! + 5! = 145 So, 145 is a 
Krishnamurthy Number and therefore 
the Output "YES".
Example 2: 
Input:
N = 14
Output:
NO
Explanation:
1! + 4! = 25 So, 14 isn't a 
Krishnamurthy Number and therefore 
the Output "NO".
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKrishnamurthy() 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^{8}

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

Starter code:
#User function Template for python3

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

Solutions:
class Solution:

	def isKrishnamurthy(self, N):

		def fact(n):
			if n == 1 or n == 0:
				return 1
			return n * fact(n - 1)
		sumo = 0
		n = N
		while n != 0:
			r = n % 10
			sumo = sumo + fact(r)
			n = n // 10
		if sumo == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		import math
		list = [int(x) for x in str(N)]
		sum = 0
		for ele in list:
			sum = sum + math.factorial(ele)
		if sum == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, n):
		sumi = 0
		a = n
		while n > 0:
			rem = n % 10
			fact = 1
			for i in range(1, rem + 1):
				fact *= i
			sumi += fact
			n //= 10
		if sumi == a:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		rr = N
		while N != 0:
			f = 1
			r = N % 10
			for i in range(1, r + 1):
				f *= i
			s += f
			N //= 10
		if s == rr:
			return 'YES'
		else:
			return 'NO'


from functools import reduce

class Solution:

	def isKrishnamurthy(self, N):
		t = N
		s = 0
		while N >= 1:
			r = N % 10
			fac = reduce(lambda a, b: a * b, range(1, r + 1), 1)
			s += fac
			N = N // 10
		if t == s:
			return 'YES'
		return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		mstr = str(N)
		for num in mstr:
			sum += math.factorial(int(num))
		return 'YES' if N == sum else 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		a = []
		n = [int(i) for i in str(N)]
		for i in n:
			a.append(math.factorial(i))
		return 'YES' if sum(a) == N else 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		d = [math.factorial(int(x)) for x in str(N)]
		s = sum(d)
		if s == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		t = N
		s = 0
		while N >= 1:
			r = N % 10
			fac = 1
			for i in range(1, r + 1):
				fac *= i
			s += fac
			N = N // 10
		if t == s:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		x = N

		def fact(n):
			if n == 1 or n == 0:
				return 1
			return n * fact(n - 1)
		while N:
			s += fact(N % 10)
			N = N // 10
		if s == x:
			return 'YES'
		else:
			return 'NO'


from math import factorial

class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		p = N
		while N != 0:
			r = N % 10
			s = s + factorial(r)
			N = N // 10
		if p == s:
			c = 'YES'
		else:
			c = 'NO'
		return c


def fact(n):
	if n == 0:
		return 1
	return n * fact(n - 1)

class Solution:

	def isKrishnamurthy(self, n):
		N = n
		s = 0
		while n:
			dig = n % 10
			s += fact(dig)
			n //= 10
		ans = int(s == N)
		if ans:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		result = 0
		while temp > 0:
			rem = temp % 10
			fact = 1
			for i in range(1, rem + 1):
				fact = fact * i
			result += fact
			temp = temp // 10
		if result == N:
			return 'YES'
		else:
			return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		ans = str(N)
		for i in ans:
			sum = sum + math.factorial(int(i))
		if sum == N:
			return 'YES'
		return 'NO'


from math import factorial

class Solution:

	def isKrishnamurthy(self, N):
		x = N
		y = 0
		while x != 0:
			rem = x % 10
			y = y + factorial(rem)
			x = x // 10
		if y == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		l = []
		s = 0
		temp = N
		while N > 0:
			rem = N % 10
			l.append(rem)
			N = N // 10
		for i in l:
			s = s + math.factorial(i)
		if s == temp:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		h = N
		s = 0
		while N > 0:
			r = N % 10
			f = math.factorial(r)
			s += f
			N = N // 10
		if s == h:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		m = str(N)
		h = 0
		for i in m:
			k = int(i)
			fact = 1
			for j in range(1, k + 1):
				fact *= j
			h += fact
		if h == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		c = 0
		for i in str(N):
			p = 1
			for j in range(1, int(i) + 1):
				p *= j
			c += p
		if c == N:
			return 'YES'
		return 'NO'


from math import factorial

class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		for i in str(N):
			s += factorial(int(i))
		return 'YES' if s == N else 'NO'


def fact(n):
	c = 1
	while n >= 1:
		c *= n
		n = n - 1
	return c

class Solution:

	def isKrishnamurthy(self, N):
		n = N
		k = n
		p = 0
		while n > 0:
			r = n % 10
			p = p + fact(r)
			n = n // 10
		if p == k:
			return 'YES'
		else:
			return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		sum1 = 0
		a = N
		while a != 0:
			r = a % 10
			x = math.factorial(r)
			sum1 = sum1 + x
			a = a // 10
		if sum1 == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		a = N
		while a:
			r = a % 10
			s = math.factorial(r)
			sum = sum + s
			a = a // 10
		if sum == N:
			return 'YES'
		else:
			return 'NO'


from math import factorial

class Solution:

	def isKrishnamurthy(self, N):
		string = str(N)
		arr_sum = []
		for num in range(len(string)):
			digit = int(string[num])
			arr_sum.append(factorial(digit))
		if sum(arr_sum) == N:
			return 'YES'
		else:
			return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		(s, dn) = (0, N)
		while dn != 0:
			s += math.factorial(dn % 10)
			dn = dn // 10
		if s == N:
			return 'YES'
		return 'NO'


def factorial(n):
	ans = 1
	for i in range(1, n + 1):
		ans *= i
	return ans

class Solution:

	def isKrishnamurthy(self, n):
		s = str(n)
		ans = 0
		for i in s:
			ans += factorial(int(i))
		if ans == n:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		n = N
		s = 0
		t = n
		while n:
			i = 1
			f = 1
			r = n % 10
			while i <= r:
				f = f * i
				i = i + 1
			s = s + f
			n = n // 10
		if s == t:
			return 'YES'
		else:
			return 'NO'


class Solution:
	cache = {}

	def factorial(self, n):
		fact = 1
		while n != 0:
			fact = fact * n
			n = n - 1
		return fact

	def isKrishnamurthy(self, n):
		sum = 0
		temp = int(n)
		while temp != 0:
			sum += self.factorial(temp % 10)
			temp = temp // 10
		if sum == n:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		tmp = N
		while tmp > 0:
			dig = tmp % 10
			fac = 1
			for i in range(2, dig + 1):
				fac *= i
			sum += fac
			tmp //= 10
		if N == sum:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		a = []
		x = list(str(N))
		for i in x:
			b = 1
			for j in range(1, int(i) + 1):
				b *= int(j)
			a.append(b)
		if sum(a) == N:
			return 'YES'
		else:
			return 'NO'


def fact(i):
	pd = 1
	for j in range(1, i + 1):
		pd *= j
	return pd

class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		sum1 = 0
		while N > 0:
			d = N % 10
			sum1 += fact(d)
			N //= 10
		if sum1 == temp:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):

		def fact(n):
			if n == 0 or n == 1:
				return 1
			return n * fact(n - 1)
		n = str(N)
		x = 0
		for i in n:
			x += fact(int(i))
		if x == N:
			return 'YES'
		return 'NO'


class Solution:

	def fact(self, N):
		if N == 1 or N == 0:
			return 1
		else:
			return N * self.fact(N - 1)

	def isKrishnamurthy(self, N):
		a = str(N)
		l = []
		suma = 0
		for i in range(len(a)):
			l.append(self.fact(int(a[i])))
		for i in range(len(l)):
			suma += l[i]
		if N == suma:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		from math import factorial as F
		res = 0
		for x in str(N):
			res += F(int(x))
		return 'YES' if N == res else 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		fact = [1] * 10
		for i in range(2, 10):
			fact[i] = fact[i - 1] * i
		t = 0
		x = N
		while N:
			t += fact[N % 10]
			N //= 10
		return 'YES' if t == x else 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		(res, X) = (0, N)
		while N > 0:
			rem = N % 10
			res += math.factorial(rem)
			N //= 10
		if res == X:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		num = str(N)
		s = 0
		for i in num:
			f = 1
			for j in range(1, int(i) + 1):
				f = f * j
			s += f
		if s == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		import math
		ans = 0
		arr = list(str(N))
		for v in arr:
			ans += math.factorial(int(v))
		if ans == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		l = list(str(N))
		s = 0
		for i in l:
			s = s + math.factorial(int(i))
		if s == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		sum = 0
		while N > 0:
			i = N % 10
			fac = 1
			if i == 0 or i == 1:
				fac = 1
			else:
				for j in range(1, i + 1):
					fac = fac * j
			sum = fac + sum
			N = N // 10
		if sum == temp:
			return 'YES'
		else:
			return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		sum = 0
		while N:
			re = N % 10
			sum += math.factorial(re)
			N = N // 10
		if sum == temp:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		r = N
		s = 0
		from math import factorial
		while N != 0:
			t = N % 10
			s += factorial(t)
			N = N // 10
		if r == s:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		r = 0
		num = N
		while num > 0:
			r = num % 10
			s = s + self.fact(r)
			num = num // 10
		if s == N:
			return 'YES'
		return 'NO'

	def fact(self, n):
		if n == 1 or n == 0:
			return 1
		f = self.fact(n - 1) * n
		return f


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f = f * i
	return f

class Solution:

	def isKrishnamurthy(self, N):
		res = 0
		t = N
		while N != 0:
			res += fact(N % 10)
			N = N // 10
		if res == t:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		a = 1
		b = 0
		l = [1]
		for x in range(1, 10):
			a = a * x
			l.append(a)
		for y in str(N):
			y = int(y)
			b = b + l[y]
		if b == N:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		temp = N
		while N != 0:
			fact = 1
			d = N % 10
			for i in range(1, d + 1):
				fact *= i
			s += fact
			N //= 10
		if s == temp:
			return 'YES'
		else:
			return 'NO'


import math

class Solution:

	def isKrishnamurthy(self, N):
		c = 0
		s = str(N)
		for ch in s:
			c += math.factorial(int(ch))
		if c == N:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, n):
		temp = n
		k = 0
		while n != 0:
			d = n % 10
			s = 1
			for i in range(1, d + 1):
				s = s * i
			k = k + s
			n = n // 10
		if k == temp:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		s = 0
		while N != 0:
			s += math.factorial(N % 10)
			N //= 10
		return 'YES' if temp == s else 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		M = N
		while N > 0:
			rem = N % 10
			pro = 1
			for i in range(1, rem + 1):
				pro *= i
			sum += pro
			N = int(N / 10)
		if sum == M:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, n):
		a = 0
		x = n
		while n > 0:
			w = 1
			s = n % 10
			for i in range(1, s + 1):
				w = w * i
			a = a + w
			n = n // 10
		if a == x:
			return 'YES'
		else:
			return 'NO'


def calc(n):
	a = 1
	while n > 1:
		a = a * n
		n -= 1
	return a

class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		for i in str(N):
			s += calc(int(i))
		if s == N:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):

		def fact(n):
			f = 1
			for i in range(1, n + 1):
				f *= i
			return f
		s = 0
		for i in str(N):
			s += fact(int(i))
		if s == N:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		stri = str(N)
		for n in stri:
			sum += math.factorial(int(n))
		return 'YES' if N == sum else 'NO'


import math as m

class Solution:

	def isKrishnamurthy(self, N):
		if sum([m.factorial(int(i)) for i in str(N)]) == N:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		sum1 = 0
		while N:
			i = 1
			f = 1
			r = N % 10
			while i <= r:
				f = f * i
				i = i + 1
			sum1 = sum1 + f
			N = N // 10
		if sum1 == temp:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def factorial(self, num):
		ans = 1
		while num:
			ans *= num
			num -= 1
		return ans

	def isKrishnamurthy(self, N):
		num = N
		sum = 0
		while num:
			sum += self.factorial(num % 10)
			num //= 10
		if sum == N:
			return 'YES'
		return 'NO'


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f = f * i
	if n == 0:
		return 1
	else:
		return f

class Solution:

	def isKrishnamurthy(self, N):
		a = []
		f = 1
		s = 0
		b = N
		while N > 0:
			a.append(int(N % 10))
			N = N // 10
		for i in range(len(a)):
			f = fact(a[i])
			s = s + f
		if s == b:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		fact_lst = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 0]
		for i in str(N):
			fact_lst[10] += fact_lst[int(i)]
		if fact_lst[10] == N:
			return 'YES'
		return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):
		if N == 0:
			return 'YES'
		m = 0
		for i in str(N):
			i = int(i)
			c = 1
			for j in range(1, i + 1):
				c *= j
			m += c
		if m == N:
			return 'YES'
		return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def fact(self, n):
		if n == 0 or n == 1:
			return 1
		return n * self.fact(n - 1)

	def isKrishnamurthy(self, n):
		s = str(n)
		res = 0
		for x in s:
			res += self.fact(int(x))
		if res == n:
			return 'YES'
		return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):
		fac = []
		fac.append(1)
		for i in range(1, 10):
			fac.append(fac[i - 1] * i)
		co = N
		xsum = 0
		while co:
			xsum += fac[co % 10]
			co = co // 10
		if xsum == N:
			return 'YES'
		else:
			return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


import math

class Solution:

	def isKrishnamurthy(self, N):
		t = N
		s = 0
		while N != 0:
			r = N % 10
			s = s + math.factorial(r)
			N = N // 10
		if s == t:
			return 'YES'
		else:
			return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def fact(self, n):
		prod = 1
		for itr in range(1, n + 1):
			prod *= itr
		return prod

	def isKrishnamurthy(self, N):
		total = 0
		lst = list(map(lambda x: int(x), list(str(N))))
		for itr in lst:
			total += self.fact(itr)
		if total == N:
			return 'YES'
		return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):
		m = str(N)
		r = []
		for i in m:
			fact = 1
			i = int(i)
			for j in range(1, i + 1):
				fact = fact * j
			r.append(fact)
		m = sum(r)
		if m == N:
			return 'YES'
		else:
			return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):

		def fact(n):
			if n <= 1:
				return 1
			return n * fact(n - 1)
		num = N
		smu = 0
		while num > 0:
			smu += fact(num % 10)
			num = num // 10
		if N == smu:
			return 'YES'
		else:
			return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):
		s = {0: 1}
		p = 1
		for i in range(1, 10):
			p = p * i
			s[i] = p
		d = N
		m = 0
		while d != 0:
			m += s[d % 10]
			d = d // 10
		if m == N:
			return 'YES'
		return 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


class Solution:

	def isKrishnamurthy(self, N):
		sum = 0
		mstr = str(N)
		for num in mstr:
			sum += math.factorial(int(num))
		return 'YES' if N == sum else 'NO'
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isKrishnamurthy(N))


import math

class Solution:

	def isKrishnamurthy(self, N):
		str_num = str(N)
		sum = 0
		for digit in str_num:
			sum += math.factorial(int(digit))
		if N == sum:
			return 'YES'
		return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		aux = N
		while aux > 0 and N > 0:
			N -= self.fact(aux % 10)
			aux = int(aux / 10)
		if aux > 0 or N != 0:
			return 'NO'
		return 'YES'

	def fact(self, n):
		x = 1
		while n > 1:
			x *= n
			n -= 1
		return x


class Solution:

	def isKrishnamurthy(self, N):
		temp = N
		sum = 0
		fact = 1
		while N > 0:
			r = N % 10
			fact = 1
			for i in range(2, r + 1):
				fact *= i
			sum += fact
			N //= 10
		if sum == temp:
			return 'YES'
		else:
			return 'NO'


class Solution:

	def isKrishnamurthy(self, N):
		s = 0
		i = N
		while i > 0:
			j = i % 10
			mS = 1
			while j > 0:
				mS *= j
				j -= 1
			s += mS
			i = i // 10
		return 'YES' if s == N else 'NO'