Factorial Algorithms in different languages
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The factorial of a non-negative integer n (written n!) is the product of all positive integers from 1 to n. Factorials appear in permutations, combinations, probability, and series expansions. Every language can compute factorials iteratively, recursively, or with built-in library functions. The key differences are how each language handles large integers, stack depth, and idiomatic style.
Python
Python's math.factorial() is the best choice. It uses an optimized C implementation with divide-and-conquer multiplication for large numbers.
JavaScript
JavaScript's Number type loses precision beyond 2^53. Use BigInt for factorials above 20.
Java
long overflows at 21!. Use BigInteger for values above 20.
C++
C++ has no built-in big integer. For n > 20, use a library like Boost.Multiprecision or implement your own.
Go
Rust
Rust panics on overflow in debug mode and wraps in release mode. Use the num crate for arbitrary precision.
Haskell
Haskell's lazy evaluation and arbitrary-precision Integer make factorial implementations concise and correct for any input size.
Performance Comparison
| Language | Max with native int | Arbitrary precision | Notes |
| Python | Unlimited (int) | Built-in | math.factorial is C-optimized |
| JavaScript | 20! (Number) | BigInt | BigInt slower than Number |
| Java | 20! (long) | BigInteger | Verbose but capable |
| C++ | 20! (uint64) | External library | Compile-time possible |
| Go | 20! (uint64) | math/big | Clean API |
| Rust | 34! (u128) | num crate | Overflow detection in debug |
| Haskell | Unlimited (Integer) | Built-in | Most concise |
Common Pitfalls
- Integer overflow: 21! exceeds 64-bit integers. In C/C++/Java, overflow silently produces wrong results. Always check bounds or use big integer types.
- Stack overflow with recursion: Recursive factorial hits the call stack limit around n=1000 in Python, n=10000 in Java. Use iterative versions for large n or enable tail-call optimization where supported.
- Floating-point factorial: Using
doubleloses precision after 18!.170!is the largest representable as a double (beyond that, it returns infinity). Never use floats for exact factorial values. - Negative input: n! is undefined for negative integers. Guard against negative input. The gamma function extends factorials to non-integers but that is a different computation.
- Performance for large n: Naive iteration is O(n) multiplications, but each multiplication grows with the size of the result. For very large n, divide-and-conquer multiplication (used by Python's
math.factorial) is significantly faster.
Summary
- Every language supports factorial via iteration or recursion
- 64-bit integers overflow at 21!, so use arbitrary-precision types for larger values
- Python and Haskell handle big integers natively; other languages need special types
math.factorial()in Python is the fastest single-language option (C-optimized)- Prefer iterative implementations to avoid stack overflow with large inputs
- C++ offers unique compile-time factorial computation via
constexprand templates
Related reading
- Factorial of a large number in python
- Factoring a number into roughly equal factors
- Factoring a quantum state
- Failed to get convolution algorithm. This is probably because cuDNN failed to initialize,
- Fair division of a kingdom
- Fast accurate atan/arctan approximation algorithm
- False Sharing in Hogwild Algorithms
- Family Tree Algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.