Given an integer N, find its factorial. return a list of integers denoting the digits that make up the factorial of N.
Example 1:
Input: N = 5
Output: 120
Explanation : 5! = 1*2*3*4*5 = 120
Example 2:
Input: N = 10
Output: 3628800
Explanation :
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800
Your Task:
You don't need to read input or print anything. Complete the function factorial() that takes integer N as input parameter and returns a list of integers denoting the digits that make up the factorial of N.
Expected Time Complexity : O(N^{2})
Expected Auxilliary Space : O(1)
Constraints:
1 ≤ N ≤ 1000

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Number theory

Combinatorics

Data structures

String algorithms

Starter code: #User function Template for python3



class Solution:

    def factorial(self, N):

        #code here

Solutions:
class Solution:

	def factorial(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact = fact * i
		l = []
		l.append(fact)
		return l


import math as m

class Solution:

	def factorial(self, N):
		r = ''
		s = str(m.factorial(N))
		return s


class Solution:

	def factorial(self, N):
		result = [1]
		for i in range(2, N + 1):
			carry = 0
			for j in range(len(result)):
				product = result[j] * i + carry
				result[j] = product % 10
				carry = product // 10
			while carry > 0:
				result.append(carry % 10)
				carry //= 10
		result.reverse()
		return result


class Solution:

	def factorial(self, n):
		val = 1
		for i in range(1, n + 1):
			val *= i
		return [int(i) for i in str(val)]


class Solution:

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


class Solution:

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


class Solution:

	def factorial(self, N):
		arr = [1]
		for i in range(1, N + 1):
			co = 0
			for j in range(len(arr)):
				arr[j] = arr[j] * i + co
				co = arr[j] // 10
				arr[j] = arr[j] % 10
			while co != 0:
				arr.append(co % 10)
				co = co // 10
		arr.reverse()
		return arr


import math

class Solution:

	def factorial(self, N):
		return str(math.factorial(N))


class Solution:

	def factorial(self, N):
		fact = 1
		for j in range(1, N + 1, 1):
			fact *= j
		return str(fact)


class Solution:

	def factorial(self, N):
		factorial = 1
		if N == 0:
			factorial = 1
		else:
			for i in range(1, N + 1):
				factorial = factorial * i
		res = [int(x) for x in str(factorial)]
		return res


class Solution:

	def factorial(self, N):
		fact = 1
		for i in range(N, 0, -1):
			fact *= i
		return [fact]


class Solution:

	def factorial(self, N):
		fact = 1
		for i in range(N):
			fact = fact * (i + 1)
		sol = []
		for i in str(fact):
			sol.append(i)
		return sol


class Solution:

	def factorial(self, N):
		fact = 1
		k = []
		while N > 0:
			fact = fact * N
			N -= 1
		while fact > 0:
			x = fact % 10
			k.append(x)
			fact = fact // 10
		k.reverse()
		return k


class Solution:

	def factorial(self, N):
		prod = 1
		l = []
		for i in range(1, N + 1):
			prod *= i
		res = str(prod).split()
		return res[-1]


class Solution:

	def factorial(self, N):
		f = 1
		a = []
		for i in range(1, N + 1):
			f = f * i
		a.append(f)
		return a


class Solution:

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


class Solution:

	def factorial(self, N):

		def fact(N):
			if N == 1:
				return 1
			else:
				return N * fact(N - 1)
		return str(fact(N))


class Solution:

	def factorial(self, N):
		n = int(N)
		if N == 0:
			return 1
		ans = 1
		for i in range(1, n + 1):
			ans = ans * i
		lst = []
		lst.append(ans)
		return lst


import math

class Solution:

	def factorial(self, N):
		if N == 0:
			return [1]
		ans = []
		fact = math.factorial(N)
		while fact:
			rem = fact % 10
			fact //= 10
			ans.insert(0, rem)
		return ans


class Solution:

	def factorial(self, N):
		m = 1
		for i in range(1, N + 1):
			m = m * i
		k = []
		k.append(m)
		return k


class Solution:

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

	def factorial(self, N):
		res = self.Facto(N)
		return [int(x) for x in str(res)]


class Solution:

	def factorial(self, N):
		s = 1
		for i in range(1, N + 1):
			s *= i
		return list(map(int, str(s)))


class Solution:

	def factorial(self, N):
		pro = 1
		for i in range(2, N + 1):
			pro = pro * i
		result = ''
		for i in str(pro):
			result += i
		return result


class Solution:

	def factorial(self, N):
		c = 1
		while N > 0:
			c = c * N
			N = N - 1
		return str(c)


class Solution:

	def factorial(self, N):
		f = 1
		for i in range(2, N + 1):
			f *= i
		l = []
		while f > 0:
			l.append(f % 10)
			f //= 10
		return l[::-1]


class Solution:

	def factorial(self, N):
		op = []
		fact = 1
		for i in range(1, N + 1):
			fact = fact * i
		fact = str(fact)
		for i in fact:
			op.append(int(i))
		return op


class Solution:

	def factorial(self, N):
		fact = N
		for x in range(1, N - 1):
			temp = fact * x
			fact += temp
		return str(fact)


from math import factorial

class Solution:

	def factorial(self, N):
		fact = str(factorial(N))
		return fact


class Solution:

	def factorial(self, N):
		sum = 1
		for j in range(1, N + 1):
			sum = sum * j
		res = str(sum)
		a = []
		a[:0] = res
		return a


class Solution:

	def factorial(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact *= i
		k = str(fact)
		a = []
		a[:] = k
		return a


class Solution:

	def factorial(self, N):

		def fac(x):
			if x == 1:
				return x
			return x * fac(x - 1)
		num = fac(N)
		return str(num).split()


class Solution:

	def factorial(self, N):
		s = 1
		for i in range(1, N + 1):
			s = s * i
		s1 = str(s)
		li = []
		for i in s1:
			li.append(i)
		return li


class Solution:

	def factorial(self, N):
		if N == 0:
			return [1]
		fact = 1
		for i in range(1, N + 1):
			fact = i * fact
		ans = []
		while fact != 0:
			rem = fact % 10
			fact = fact // 10
			ans.insert(0, rem)
		return ans


import sys
sys.setrecursionlimit(10000)

class Solution:

	def factorial(self, N):
		dp = [-1] * (N + 1)

		def fun(N):
			if N == 1:
				return 1
			if N == 0:
				return 1
			if dp[N] != -1:
				return dp[N]
			dp[N] = N * fun(N - 1)
			return dp[N]
		return [fun(N)]
0


class Solution:

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


class Solution:

	def factorial(self, N):
		output = 1
		for i in range(1, N + 1, 1):
			output = output * i
		return str(output).split()


class Solution:

	def factorial(self, N):
		l = []
		fact = 1
		while N > 0:
			fact = fact * N
			N = N - 1
		for i in str(fact):
			l.append(i)
		return l


class Solution:

	def factorial(self, N):
		if N == 0:
			return 1
		result = 1
		for i in range(N, 0, -1):
			result *= i
		return str(result)


class Solution:

	def factorial(self, N):
		fact = 1
		ls = []
		for i in range(1, N + 1):
			fact *= i
		ls[:] = str(fact)
		return ls


class Solution:

	def factorial(self, N):

		def fact(n):
			if n == 1:
				return 1
			return n * fact(n - 1)
		ans = fact(N)
		return [ans]


class Solution:

	def factorial(self, N):
		p = 1
		for i in range(1, N + 1):
			p *= i
		list_of_digits = [int(i) for i in str(p)]
		return list_of_digits


class Solution:

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


class Solution:

	def factorial(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact = fact * i
		digits = []
		while fact > 0:
			digits.insert(0, fact % 10)
			fact = fact // 10
		return digits


class Solution:

	def factorial(self, N):
		res = 1
		for i in range(1, N + 1):
			res *= i
		ans = [int(x) for x in str(res)]
		return ans


class Solution:

	def factorial(self, N):
		res = []
		res.append(1)
		i = 1
		carry = 0
		while i <= N:
			j = 0
			length = len(res)
			while j < length:
				product = res[j] * i + carry
				res[j] = product % 10
				carry = product // 10
				j += 1
			i += 1
			if carry != 0:
				while carry > 0:
					res.append(carry % 10)
					carry = carry // 10
		return res[::-1]


class Solution:

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

	def factorial(self, N):
		a = str(ob.fact(N))
		return a


class Solution:

	def factorial(self, N):
		res = 1
		if N == 1:
			return '1'
		else:
			for i in range(2, N + 1):
				res = res * i
		return str(res)


class Solution:

	def factorial(self, N):
		fact = 1
		while N > 0:
			fact = fact * N
			N = N - 1
		return str(fact)


class Solution:

	def factorial(self, N):
		fact = 1
		i = 1
		while i <= N:
			fact = fact * i
			i = i + 1
		return str(fact)


from math import factorial

class Solution:

	def factorial(self, N):
		r = factorial(N)
		return str(r)