Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n.
For example factorial of 6 is 6*5*4*3*2*1 which is 720.
Find the Factorial of a Number Using Recursive approach
This Python program uses a recursive function to calculate the factorial of a given number. The factorial is computed by multiplying the number with the factorial of its preceding number.
python3
def factorial(n):
return 1 if (n = = 1 or n = = 0 ) else n * factorial(n - 1 )
num = 5
print ( "Factorial of" ,num, "is" ,factorial(num))
|
Output:
Factorial of 5 is 120
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Factorial of a Number Using Iterative approach
Example 1:
python3
def factorial(n):
if n < 0 :
return 0
elif n = = 0 or n = = 1 :
return 1
else :
fact = 1
while (n > 1 ):
fact * = n
n - = 1
return fact
num = 5
print ( "Factorial of" ,num, "is" ,
factorial(num))
|
Output:
Factorial of 5 is 120
Time Complexity: O(n)
Auxiliary Space: O(1)
Example 2:
Python3
def factorial(n):
res = 1
for i in range ( 2 , n + 1 ):
res * = i
return res
num = 5
print ( "Factorial of" , num, "is" ,
factorial(num))
|
Output
Factorial of 5 is 120
Time Complexity: O(n)
Auxiliary Space: O(1)
Find the Factorial of a Number Using One line Solution (Using Ternary operator):
Python3
def factorial(n):
return 1 if (n = = 1 or n = = 0 ) else n * factorial(n - 1 )
num = 5
print ( "Factorial of" ,num, "is" ,
factorial(num))
|
Output:
Factorial of 5 is 120
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Factorial of a Number Using using In-built function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number.
Syntax: math.factorial(x)
Parameter:
x: This is a numeric expression.
Returns: factorial of desired number.
Python3
import math
def factorial(n):
return (math.factorial(n))
num = 5
print ( "Factorial of" , num, "is" ,
factorial(num))
|
Output:
Factorial of 5 is 120
Time complexity: O(1)
Auxiliary space: O(1)
Find the Factorial of a Number Using numpy.prod
Python3
import numpy
n = 5
x = numpy.prod([i for i in range ( 1 ,n + 1 )])
print (x)
|
Output
120
Time Complexity: O(n)
Auxiliary Space: O(1)
Prime Factorization Method to find Factorial
- Initialize the factorial variable to 1.
- For each number i from 2 to n, do the following:
a. Find the prime factorization of i.
b. For each prime factor p and its corresponding power k in the factorization of i, multiply the factorial variable by p raised to the power of k.
- Return the factorial variable.
Python3
def primeFactors(n):
factors = {}
i = 2
while i * i < = n:
while n % i = = 0 :
if i not in factors:
factors[i] = 0
factors[i] + = 1
n / / = i
i + = 1
if n > 1 :
if n not in factors:
factors[n] = 0
factors[n] + = 1
return factors
def factorial(n):
result = 1
for i in range ( 2 , n + 1 ):
factors = primeFactors(i)
for p in factors:
result * = p * * factors[p]
return result
num = 5
print ( "Factorial of" , num, "is" , factorial(num))
|
Output
Factorial of 5 is 120
Time Complexity: O(sqrt(n))
Auxiliary Space: O(sqrt(n))
Please refer complete article on Program for factorial of a number for more details!
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jun, 2023
Like Article
Save Article