Fast algorithms for computing the factorial
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Computing n! (n factorial) is straightforward for small values but becomes a big-integer problem rapidly — 100! has 158 digits. The naive loop multiplying 1 through n is O(n) multiplications, but each multiplication grows more expensive as the product gets larger. Faster approaches include divide-and-conquer (splitting the range to balance operand sizes), prime factorization (binary splitting on prime powers), and Stirling's approximation for floating-point estimates. For most applications, language built-ins (math.factorial in Python) already use optimized algorithms internally.
Iterative Method
The iterative approach performs n - 1 multiplications. For large n, the intermediate result grows to thousands of digits, making later multiplications significantly slower than earlier ones (big-integer multiplication is not O(1)).
Recursive Method
Recursion has the same time complexity as iteration but adds call stack overhead. For large n, it risks RecursionError in Python or stack overflow in C/Java.
Divide and Conquer
Splitting the range produces balanced operands for each multiplication. Two 500-digit numbers multiplied is faster than one 1000-digit number multiplied by a small number, because sub-quadratic multiplication algorithms (Karatsuba, Toom-Cook) are more efficient on equal-sized operands.
Prime Factorization (Binary Splitting)
Legendre's formula counts the exact power of each prime p in n!: sum of floor(n / p^k) for k = 1, 2, .... This avoids multiplying by non-prime factors entirely and can be combined with binary splitting for the final product.
Stirling's Approximation (Floating Point)
Stirling's approximation is not exact but is useful for estimating the magnitude of factorials, computing log-factorials for statistical applications, and cases where floating-point precision is sufficient.
Language Built-Ins
Python's math.factorial uses an optimized binary splitting algorithm in C, making it faster than any pure Python implementation. Always prefer language built-ins when available.
Memoization for Repeated Calls
If your application calls factorial repeatedly with different values, memoization avoids recomputation. Each call to factorial_memo(k) caches all values from 1 to k.
Common Pitfalls
- Integer overflow in typed languages:
intandlongoverflow for n > 12 (32-bit) or n > 20 (64-bit). UseBigInteger(Java),boost::multiprecision(C++), or arbitrary-precision types. - Recursion stack overflow: Recursive factorial hits the call stack limit at ~1000 in Python and ~10,000 in Java. Use iteration or increase the stack size.
- Unbalanced multiplication in naive loop: Multiplying a huge number by a small number is slower per digit than multiplying two medium numbers. Divide-and-conquer balances operand sizes for better performance.
- Using Stirling's for exact results: Stirling's approximation has relative error ~1/(12n). For combinatorics requiring exact integer results, use the iterative or prime factorization method.
- Not using language built-ins: Custom implementations in Python are 10-100x slower than
math.factorial, which is implemented in optimized C with binary splitting.
Summary
- Iterative multiplication is the simplest: O(n) multiplications, but each grows expensive for large n
- Divide-and-conquer balances operand sizes, improving big-integer multiplication performance
- Prime factorization computes the exact prime power decomposition of n!
- Stirling's approximation gives fast floating-point estimates with ~0.08% error at n=10
- Use
math.factorial(Python),BigInteger(Java), or equivalent built-ins for production code - Memoize when computing factorials repeatedly with different arguments

