Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find factorial of a large number
Before going to the solution let's understand about factorial. The factorial is a product of all the integers smaller than or equal to n. The mathematical representation is n! = n × (n-1) × (n-2) × ... × 1. For example, 4! = 4 × 3 × 2 × 1 = 24.
In this article, let's explore different ways to find the factorial of a large number in Python.
Various Methods
Following are multiple ways to find the factorial of a large number in Python ?
- Using 'math' Module
- Using 'for' loop
- Using 'array'
Using the 'math' Module
The math module is a built-in Python module used for performing mathematical operations. It provides the factorial() function which can handle very large numbers efficiently.
Syntax
import math result = math.factorial(n)
Example
Following is an example of finding the factorial of a large number using the math module ?
import math
def solve(n):
return math.factorial(n)
n = 50
print("Factorial of", n, ":", solve(n))
Factorial of 50 : 30414093201713378043612608166064768844377641568960512000000000000
Using 'for' Loop
The for loop provides a straightforward approach to calculate factorial by iterating through numbers and multiplying them sequentially.
Example
In the following example we calculate the factorial using a for loop ?
def factorial(n):
fact = 1
for i in range(2, n + 1):
fact = fact * i
return fact
print("Factorial of 50:", factorial(50))
Factorial of 50: 30414093201713378043612608166064768844377641568960512000000000000
Using Array for Large Numbers
For extremely large factorials, we can use an array to store individual digits. This approach manually handles multiplication and carry operations digit by digit.
Algorithm
The algorithm works as follows ?
- Create an array to store digits of the result
- Initialize array with 1 and size as 1
- Multiply each number from 2 to n with the stored result
- Handle carry operations manually for each multiplication
Example
In the following example we calculate factorial using an array approach ?
def factorial(n):
# Array to store result digits
result = [0] * 500
result[0] = 1
result_size = 1
# Multiply numbers from 2 to n
for x in range(2, n + 1):
result_size = multiply(x, result, result_size)
# Print result in reverse order
print("Factorial of", n, "is:")
for i in range(result_size - 1, -1, -1):
print(result[i], end="")
print()
def multiply(x, result, result_size):
carry = 0
# Multiply each digit with x
for i in range(result_size):
prod = result[i] * x + carry
result[i] = prod % 10
carry = prod // 10
# Store remaining carry digits
while carry:
result[result_size] = carry % 10
carry = carry // 10
result_size += 1
return result_size
# Calculate factorial of 50
factorial(50)
Factorial of 50 is: 30414093201713378043612608166064768844377641568960512000000000000
Comparison
| Method | Complexity | Best For |
|---|---|---|
| math.factorial() | Simple | Most use cases |
| for loop | Medium | Learning purposes |
| Array method | Complex | Understanding algorithms |
Conclusion
For practical applications, use math.factorial() as it's optimized and handles large numbers efficiently. The manual approaches help understand the underlying mathematics and are useful for educational purposes.
