What is Factorial?
A factorial, denoted by n!, is the product of all positive integers from 1 to n. For example, 5! (read as "five factorial") equals 5 × 4 × 3 × 2 × 1 = 120. The factorial function is one of the most fundamental concepts in mathematics, particularly in combinatorics and probability theory.
By mathematical convention, 0! is defined to equal 1. This might seem counterintuitive, but it's essential for many formulas to work correctly, including the binomial coefficient and various counting formulas.
Factorial Formula
n! = n × (n-1) × (n-2) × ... × 3 × 2 × 1
Or recursively: n! = n × (n-1)! where 0! = 1
Examples
3! = 3 × 2 × 1 = 6
5! = 5 × 4 × 3 × 2 × 1 = 120
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5,040
10! = 3,628,800
Common Uses of Factorial
- Permutations: The number of ways to arrange n distinct objects is n!
- Combinations: C(n,r) = n! / (r! × (n-r)!) calculates how many ways to choose r items from n items
- Probability: Many probability calculations involve factorials, especially in binomial distributions
- Taylor Series: Factorials appear in the denominators of Taylor series expansions (e.g., e^x, sin(x))
- Computer Science: Used in algorithm analysis, counting problems, and recursive function design
Properties of Factorial
- Factorial is only defined for non-negative integers
- Factorials grow extremely fast - faster than exponential functions
- n! = n × (n-1)! (recursive property)
- For large n, Stirling's approximation can be used: n! ≈ √(2πn) × (n/e)^n
How Fast Factorials Grow, and Why This Tool Stops at 170
Put n! next to squaring and doubling and the gap becomes obvious. At n = 5 the three are close, but by n = 20 the factorial is already about 2.3 trillion times larger than 2²⁰. The table below tracks all three side by side.
| n | n! (factorial) | 2ⁿ (doubling) | n² (squaring) |
|---|---|---|---|
| 1 | 1 | 2 | 1 |
| 5 | 120 | 32 | 25 |
| 10 | 3,628,800 | 1,024 | 100 |
| 15 | 1,307,674,368,000 | 32,768 | 225 |
| 20 | 2,432,902,008,176,640,000 | 1,048,576 | 400 |
That growth explains the 170 limit. This calculator holds results in BigInt, so every digit up to 170! is exact, but a raw 64-bit signed integer tops out at 2⁶³ − 1 = 9,223,372,036,854,775,807. Since 20! = 2,432,902,008,176,640,000 still fits and 21! ≈ 5.1 × 10¹⁹ does not, 21! is the first factorial to overflow a 64-bit int. A double-precision float reaches roughly 1.8 × 10³⁰⁸, and 170! ≈ 7.3 × 10³⁰⁶ squeaks under that ceiling while 171! ≈ 1.2 × 10³⁰⁹ does not. 170 is simply the last factorial a standard float can represent.
Worked Counting Examples
Permutations count ordered outcomes. To find how many ways 8 runners can take the top three medals, use P(8, 3) = 8! / 5! = 8 × 7 × 6 = 336. Combinations ignore order. The number of distinct 5-card poker hands from a 52-card deck is C(52, 5) = 52! / (5! × 47!) = 2,598,960. Notice the 47! cancels most of the 52!, so you never actually compute the full factorials, which is why these stay tractable long after n! itself becomes astronomical.
Stirling's Approximation in Practice
When you only need a ballpark, Stirling's formula n! ≈ √(2πn) × (n/e)ⁿ is fast and improves as n rises. The relative error drops from under 2% at n = 5 to well below half a percent by n = 20.
| n | Exact n! | Stirling estimate | Relative error |
|---|---|---|---|
| 5 | 120 | ≈ 118.0 | 1.65% |
| 10 | 3,628,800 | ≈ 3,598,696 | 0.83% |
| 20 | 2.43 × 10¹⁸ | ≈ 2.42 × 10¹⁸ | 0.42% |
Trailing Zeros of n!
A trailing zero appears each time a factor of 10 = 2 × 5 enters the product, and 5s are always the scarce ingredient. Legendre's formula counts them by summing ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + …. For 100!, that is 20 + 4 + 0 = 24 trailing zeros, so 100! ends in exactly twenty-four zeros without computing the 158-digit number at all.
Computing Factorials in Code
A recursive version reads cleanly but adds a call-stack frame per step and can overflow the stack for large n. An iterative loop uses constant space and is what this tool runs under the hood, paired with BigInt so the digits never round off.
function factorial(n) {
let result = 1n; // BigInt keeps every digit exact
for (let i = 2n; i <= BigInt(n); i++) {
result *= i;
}
return result; // native Number would lose precision past 2^53
}