Given a number N. Check if it is perfect or not. A number is perfect if sum of factorial of its digit is equal to the given number.
Example 1:
Input: N = 23
Output: 0
Explanation: The sum of factorials of 
digits of 23 is 2! + 3! = 2 + 6 = 8 
which is not equal to 23. Thus, answer 
is 0.
Example 2:
Input: N = 145
Output: 1
Explanation: The sum of factorials of digits 
of 145 is 1! + 4! + 5! = 1 + 24 + 120 = 145 
which is equal to 145.Thus, answer is 1.
Your Task:
You don't need to read input or print anything.Your task is to complete the function isPerfect() which takes a number N as input parameter and returns 1 if N is perfect.Otherwise, it returns 0.
Expected Time Complexity: O(Log_{10}N)
Expected Auxillary Space: O(constant)
Constraints:
1<=N<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def isPerfect(self,N):

        #code here

Solutions:
import math

class Solution:

	def isPerfect(self, N):
		s = 0
		for i in str(N):
			s += math.factorial(int(i))
		if s == N:
			return 1
		return 0


class Solution:

	def isPerfect(self, N):
		s = sum([self.count_factorial(int(x)) for x in str(N)])
		res = 0 if s != N else 1
		return res

	def count_factorial(self, N):
		f = 1
		for i in range(2, N + 1):
			f *= i
		return f


class Solution:

	def isPerfect(self, N):
		sum = 0
		temp = N
		while N > 0:
			rem = N % 10
			sum = sum + self.fact(rem)
			N = N // 10
		if sum == temp:
			return 1
		return 0

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


class Solution:

	def isPerfect(self, N):
		arr = str(N)
		fact = 0
		for i in arr:
			sum = 1
			k = int(i)
			while k > 0:
				sum = sum * k
				k -= 1
			fact = fact + sum
		if fact == N:
			return 1
		return 0


class Solution:

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

	def isPerfect(self, N):
		tempN = N
		totalN = 0
		while N > 0:
			digit = N % 10
			totalN = totalN + self.factorial(digit)
			N = N // 10
		if tempN == totalN:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		if N == 0:
			return 0
		else:
			sum = 0
			t = [int(a) for a in str(N)]
			for i in t:
				pr = 1
				while i > 0:
					pr = pr * i
					i = i - 1
				sum = sum + pr
			if sum == N:
				return 1
			else:
				return 0


import math

class Solution:

	def isPerfect(self, N):
		sum = 0
		temp = N
		while N > 0:
			sum += math.factorial(N % 10)
			N //= 10
		if sum == temp:
			return 1
		return 0


class Solution:

	def isPerfect(self, N):

		def fact(num):
			memo = [1] * (num + 1)
			for i in range(2, num + 1):
				memo[i] = i * memo[i - 1]
			return memo
		memo = fact(10)
		temp = N
		_sum = 0
		while temp:
			_sum += memo[temp % 10]
			temp //= 10
		if _sum == N:
			return 1
		return 0


class Solution:

	def isPerfect(self, N):

		def fact(n):
			s = 1
			for i in range(1, n + 1):
				s *= i
			return s
		a = 0
		for j in str(N):
			a += fact(int(j))
		return 1 if a == N else 0


class Solution:

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

	def isPerfect(self, N):
		temp = N
		total = 0
		while temp != 0:
			digit = temp % 10
			total += self.fact(digit)
			temp //= 10
		if total == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfect(self, N):
		x = 0
		for i in str(N):
			x += math.factorial(int(i))
		if x == N:
			return 1
		else:
			return 0

	def fact(self, N):
		s = 1
		for i in range(N + 1):
			s = s * i
		return s


class Solution:

	def isPerfect(self, N):
		temp = N
		sum = 0
		while temp > 0:
			quo = temp % 10
			temp = temp // 10
			facto = 1
			for i in range(1, quo + 1, 1):
				facto = facto * i
			sum += facto
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):

		def fact(x):
			if x == 0:
				return 1
			else:
				return x * fact(x - 1)
		numstr = str(N)
		sum = 0
		for i in range(0, len(numstr)):
			digi = int(numstr[i])
			sum = sum + fact(digi)
		if sum == N:
			return 1
		else:
			return 0


class Solution:

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

	def isPerfect(self, N):
		total = 0
		n = N
		while n > 0:
			a = n % 10
			total += self.factorial(a)
			n = n // 10
		return 1 if total == N else 0


from math import factorial

class Solution:

	def isPerfect(self, N):
		s = 0
		p = N
		while N != 0:
			r = N % 10
			s = s + factorial(r)
			N = N // 10
		if p == s:
			c = 1
		else:
			c = 0
		return c


import math

class Solution:

	def isPerfect(self, N):
		ln = [int(i) for i in str(N)]
		sum_fac = 0
		for i in ln:
			sum_fac += math.factorial(i)
		if sum_fac == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfect(self, N):
		sum1 = 0
		T = N
		while N > 0:
			rem = N % 10
			sum1 += math.factorial(rem)
			N //= 10
		if sum1 == T:
			return 1
		return 0


class Solution:

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

	def isPerfect(self, N):
		str_n = str(N)
		sum = 0
		for i in str_n:
			sum += self.fact(int(i))
		if sum == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfect(self, N):
		q = N
		res = 0
		N = str(N)
		for i in range(len(N)):
			num = int(N[i])
			res = res + math.factorial(num)
		if res == q:
			return 1
		else:
			return 0


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

class Solution:

	def isPerfect(self, N):
		a = N
		sum = 0
		while N != 0:
			sum += fact(N % 10)
			N = N // 10
		if sum == a:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		import math
		p = [math.factorial(int(q)) for q in str(N)]
		if sum(p) == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		import math
		s = [math.factorial(int(x)) for x in str(N)]
		if sum(s) == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		e = N
		N = str(N)
		k = len(N)
		N = int(N)
		s = 0
		d = 0
		for b in range(k):
			b = N % 10
			N = N - b
			N = N / 10
			l = 1
			for i in range(1, int(b + 1)):
				l = l * i
			for j in range(1):
				d = d + l
		d = d
		if int(d) == e:
			return '1'
		else:
			return '0'


class Solution:

	def isPerfect(self, N):
		import math
		s = str(N)
		t = 0
		for i in range(len(s)):
			t += math.factorial(int(s[i]))
		if t == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		total = 0
		num = N
		while num:
			temp = num % 10
			total += self.factorial(temp)
			num = int(num / 10)
		if total == N:
			return 1
		else:
			return 0

	def factorial(self, num):
		res = 1
		for i in range(1, num + 1):
			res *= i
		return res


class Solution:

	def isPerfect(self, n):
		temp = n
		sum = 0
		for i in range(len(str(n))):
			fact = 1
			rem = temp % 10
			while rem > 0:
				fact = fact * rem
				rem -= 1
			temp = temp // 10
			sum += fact
		if sum == n:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		num = N
		sum = 0

		def Fact(n):
			g = 1
			for i in range(2, n + 1):
				g *= i
			return g
		while N > 0:
			remainder = N % 10
			N = N // 10
			sum += Fact(remainder)
		if sum == num:
			return 1
		return 0


from math import factorial

class Solution:

	def isPerfect(self, N):
		num = str(N)
		summa = 0
		for item in num:
			summa += factorial(int(item))
		return int(N == summa)


class Solution:

	def isPerfect(self, N):
		l = []
		temp = N
		while N > 0:
			fact = 1
			dig = N % 10
			for i in range(1, dig + 1):
				fact = fact * i
			l.append(fact)
			N = N // 10
		if sum(l) == temp:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		a = list(map(int, str(N)))
		sum = 0
		for i in a:
			fact = 1
			for j in range(2, i + 1):
				fact *= j
			sum += fact
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		k = N
		s = 0
		while k:
			r = k % 10
			m = 1
			for i in range(1, r + 1):
				m = m * i
			s += m
			k = k // 10
		if s == N:
			return 1
		else:
			return 0


from math import factorial as fac

class Solution:

	def isPerfect(self, N):
		sum = 0
		for ele in str(N):
			sum += fac(int(ele))
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		sum = 0
		fact = [0] * 10
		fact[0] = 1
		for i in range(1, 10):
			fact[i] = fact[i - 1] * i
		store = N
		while N:
			digit = N % 10
			sum += fact[digit]
			N //= 10
		if sum == store:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		(t, l) = (N, [])
		(s, m) = (0, 1)
		while N > 0:
			f = N % 10
			l.append(f)
			N //= 10
		for i in l:
			f = 1
			for j in range(i, 0, -1):
				f *= j
			s += f
		if t == s:
			return 1
		else:
			return 0


from math import factorial

class Solution:

	def isPerfect(self, N):
		s = sum([factorial(int(x)) for x in str(N)])
		return int(s == N)


class Solution:

	def isPerfect(self, N):

		def factorial(n):
			if n == 0:
				return 1
			else:
				return n * factorial(n - 1)
		digit_sum = sum((factorial(int(digit)) for digit in str(N)))
		if digit_sum == N:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfect(self, N):
		sum = 0
		arr = []
		for num in str(N):
			arr.append(int(num))
		for i in arr:
			sum = sum + math.factorial(i)
		if sum == N:
			return '1'
		else:
			return '0'


class Solution:

	def factorial(self, N):
		res = 1
		for i in range(1, N + 1):
			res *= i
		return res

	def isPerfect(self, N):
		res = 0
		for i in str(N):
			res += self.factorial(int(i))
		return 1 if res == N else 0


import math

class Solution:

	def isPerfect(self, N):
		x = 0
		f = 1
		y = N
		while N != 0:
			z = int(N % 10)
			for i in range(1, z + 1):
				f = f * i
			i = 0
			x = x + f
			f = 1
			N = int(N / 10)
		if x == y:
			return 1
		else:
			return 0


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

class Solution:

	def isPerfect(self, N):
		dig = str(N)
		sum = 0
		for i in range(len(dig)):
			sum = sum + fact(int(dig[i]))
		if sum == N:
			return '1'
		else:
			return '0'


class Solution:

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


class Solution:

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


import math

class Solution:

	def isPerfect(self, N):
		l = []
		rem = 0
		temp = N
		ans = 0
		while N > 0:
			rem = N % 10
			l.append(rem)
			N = N // 10
		for i in l:
			ans += math.factorial(i)
		if temp == ans:
			return 1
		else:
			return 0


class Solution:

	def fact(self, N):
		facto = 1
		for i in range(1, N + 1):
			facto = facto * i
		return facto

	def isPerfect(self, N):
		perf = 0
		for i in str(N):
			perf = perf + self.fact(int(i))
		if N == perf:
			return '1'
		else:
			return '0'


class Solution:

	def isPerfect(self, N):
		s = 0
		a = list(map(int, str(N)))
		for i in a:
			p = i
			for j in range(1, i):
				p *= j
			if i == 0:
				p = 1
			s += p
		if s == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):

		def fact(n):
			if n < 1:
				return 1
			else:
				return n * fact(n - 1)
		output = 0
		for i in str(N):
			output += fact(int(i))
		if output == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, n):
		import math
		l = list(map(int, str(n)))
		s = 0
		for i in l:
			k = math.factorial(i)
			s += k
		if s == n:
			return 1
		return 0


import math

class Solution:

	def isPerfect(self, N):
		if N == 1:
			return 1
		else:
			v = []
			for i in str(N):
				f = math.factorial(int(i))
				v.append(f)
			if sum(v) == N:
				return 1
			else:
				return 0


class Solution:

	def isPerfect(self, N):
		s = 0
		for i in str(N):
			s = s + Solution.factorial(int(i))
		if s == N:
			return 1
		return 0

	def factorial(n):
		f = 1
		while n > 0:
			f = f * n
			n = n - 1
		return f


class Solution:

	def isPerfect(self, N):
		sum = 0
		a = N
		while N > 0:
			f = 1
			rem = N % 10
			for i in range(1, rem + 1):
				f *= i
			sum += f
			N = N // 10
		if a == sum:
			return 1
		else:
			return 0


class Solution:

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

	def isPerfect(self, N):
		l = list(str(N))
		sum = 0
		for i in l:
			sum = sum + self.fact(int(i))
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		from math import factorial
		a = str(N)
		sum = 0
		for i in a:
			b = int(i)
			sum = sum + factorial(b)
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):

		def fac(n):
			if n <= 1:
				return n
			return fac(n - 1) * n

		def j(n):
			Sum = 0
			while n > 0:
				r = n % 10
				n = n // 10
				Sum += fac(r)
			return Sum
		return 1 if j(N) == N else 0


class Solution:

	def isPerfect(self, N):

		def fac(n):
			c = 1
			for i in range(n, 0, -1):
				c = c * i
			return c

		def j(n):
			Sum = 0
			while n > 0:
				r = n % 10
				n = n // 10
				Sum += fac(r)
			return Sum
		return 1 if j(N) == N else 0


import math

class Solution:

	def isPerfect(self, N):
		num = N
		s = 0
		temp = 0
		while N > 0:
			temp = N % 10
			s = s + math.factorial(temp)
			N //= 10
		if s == num:
			return 1
		else:
			return 0


import math

class Solution:

	def isPerfect(self, N):
		n = str(N)
		y = 0
		for i in n:
			x = 1
			for j in range(2, int(i) + 1):
				x = x * j
			y = y + x
		if y == N:
			return 1
		return 0


import math

class Solution:

	def isPerfect(self, N):
		n = str(N)
		y = 0
		for i in n:
			x = math.factorial(int(i))
			y = y + x
		if y == N:
			return 1
		return 0


class Solution:

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

	def isPerfect(self, N):
		s_l = list(str(N))
		sum1 = 0
		for i in s_l:
			sum1 = sum1 + self.fact(int(i))
		if sum1 == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		sum = 0
		x = N
		while N > 0:
			rem = N % 10
			N //= 10
			p = 1
			for i in range(1, rem + 1):
				p *= i
			sum += p
		return 1 if sum == x else 0


class Solution:

	def isPerfect(self, N):
		s = 0
		n = N
		while n:
			f = 1
			for i in range(1, n % 10 + 1):
				f *= i
			s += f
			n //= 10
		return int(s == N)


class Solution:

	def isPerfect(self, N):
		s = 0
		ss = N
		while N > 0:
			n = N % 10
			N = N // 10
			s = s + Solution.fac(n)
		if ss == s:
			return 1
		return 0

	def fac(n):
		if n < 1:
			return 1
		else:
			return n * Solution.fac(n - 1)


class Solution:

	def isPerfect(self, N):

		def fact_digit(num):
			f = 1
			for i in range(1, num + 1):
				f *= i
			return f
		sum = 0
		Number = N
		while N > 0:
			digit = N % 10
			sum += fact_digit(digit)
			N //= 10
		if sum == Number:
			return 1
		return 0


class Solution:

	def isPerfect(self, N):
		sum = 0

		def factor(n):
			fact = 1
			for i in range(1, n + 1):
				fact = fact * i
			return fact
		for i in str(N):
			sum = sum + factor(int(i))
		if sum == N:
			return 1
		else:
			return 0


class Solution:

	def isPerfect(self, N):
		l = [int(x) for x in str(N)]
		sumFact = 0
		for d in l:
			sumFact += self.fact(d)
		if sumFact == N:
			return 1
		else:
			return 0

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