Factorial of a Number – Python
The factorial of a number is the product of all positive integers less than or equal to that number. For example, the factorial of 5 (denoted as 5!) is 5 × 4 × 3 × 2 × 1 = 120. In Python, we can calculate the factorial of a number using various methods, such as loops, recursion, built-in functions, and other approaches.
Example: Simple Python program to find the factorial of a number
n = 6
# Initialize the factorial variable to 1
fact = 1
# Calculate the factorial using a for loop
for i in range(1, n + 1):
fact *= i
print(fact)
Output
720
Explanation: This code calculates the factorial of a number n (which is 6 in this case) using a for loop. It initializes the variable fact to 1. Then, it multiplies fact by each integer from 1 to n (inclusive) in the loop, updating the value of fact with each iteration. Finally, it prints the result, which is the factorial of 6 (720).
Table of Content
Using a 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.
def fact(n):
# single line to find factorial
return 1 if (n==1 or n==0) else n * fact(n - 1)
# Driver Code
num = 5
print(fact(num))
Output
120
Explanation: This approach calculates the factorial using recursion. The base case is when n is 0 or 1, in which case the factorial is 1. For any other number, the function calls itself, reducing the number until it reaches the base case.
Using math.factorial()
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.
import math
def factorial(n):
return(math.factorial(n))
# Driver Code
num = 5
print(factorial(num))
Explanation: This code defines a function factorial(n) that uses Python’s built-in math.factorial() function to calculate the factorial of a given number n. In the driver code, it calls this function with num = 5 and prints the result, which is the factorial of 5 (120).
Using numpy.prod()
This Python code calculates the factorial of n using NumPy. It creates a list of numbers from 1 to n, computes their product with numpy.prod(), and prints the result.
import numpy
n=5
x=numpy.prod([i for i in range(1,n+1)])
print(x)
Output
120
Explanation: Here, the numpy.prod() function is used to compute the factorial. The list comprehension generates a list of numbers from 1 to n, and numpy.prod() calculates the product of all the elements in the list, which is equivalent to the factorial of the number.
Using Prime Factorization
This code calculates the factorial of a number by first finding the prime factors of each number from 2 to n, and then multiplying them together to compute the factorial. The prime factors are stored along with their powers for efficient multiplication.
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
# Function to find factorial of a number
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
# Driver Code
num = 5
print(factorial(num))
Output
120
Explanation: This approach uses prime factorization to calculate the factorial. The primeFactors() function finds the prime factors of each number from 2 to n, and the factorial() function multiplies the factors accordingly.