Given a positive integer, N. Find the factorial of N.
 
Example 1:
Input:
N = 5
Output:
120
Explanation:
5*4*3*2*1 = 120
Example 2:
Input:
N = 4
Output:
24
Explanation:
4*3*2*1 = 24
Your Task:
You don't need to read input or print anything. Your task is to complete the function factorial() which takes an integer N as input parameters and returns an integer, the factorial of N.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
0 <= N <= 18

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3


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

Solutions:
class Solution:

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


class Solution:

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


class Solution:

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


import math

class Solution:

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


import math

class Solution:

	def factorial(self, N):
		if N == 1 or N == 0:
			return 1
		return math.factorial(N)


class Solution:

	def factorial(self, N):

		def recur(total, num):
			if num == 1:
				return total
			if num == 0:
				return 1
			return recur(total * num, num - 1)
		res = recur(1, N)
		return res


class Solution:

	def factorial(self, N):
		if not 0 <= N <= 18:
			Exception()
		x = 1
		for i in range(N, 1, -1):
			x = x * i
		return x


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:
	fact = [1] * 20

	def factorial(self, N):
		if self.fact[19] == 1:
			for i in range(2, 20):
				self.fact[i] = i * self.fact[i - 1]
		return self.fact[N]


class Solution:

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


class Solution:

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


class Solution:

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


from math import factorial as f

class Solution:

	def factorial(self, N):
		return f(N)


import functools

class Solution:

	def factorial(self, N):
		if N >= 1 and N <= 18:
			return functools.reduce(lambda a, b: a * b, [a for a in range(1, N + 1)])
		else:
			return 1


class Solution:

	def factorial(self, N):
		f = 1
		while N:
			f = f * N
			N -= 1
		return f


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def factorial(self, N):

		def fac(n):
			if n == 1 or n == 0:
				return 1
			return n * fac(n - 1)
		return fac(N)


import math as m

class Solution:

	def factorial(self, N):
		a = m.factorial(N)
		return a


import math

class Solution:

	def factorial(self, N):
		if N < 0:
			return 0
		elif N == 0 or N == 1:
			return 1
		else:
			fac = 1
			while N > 1:
				fac *= N
				N -= 1
			return fac


import math

class Solution:

	def factorial(self, n):
		if n == 0:
			return 1
		elif n == 1:
			return 1
		else:
			return n * math.factorial(n - 1)


class Solution:

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


import math

class Solution:

	def factorial(self, N):
		for i in range(N + 1):
			return math.factorial(N)


class Solution:

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


class Solution:

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


class Solution:

	def factorial(self, n):
		dp = [1, 1]
		m = 10 ** 9 + 7
		for i in range(1, n + 1):
			dp[i % 2] = dp[(i - 1) % 2] * i
		return dp[n % 2]


class Solution:

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


class Solution:

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