What Is a Factorial (n!)? Formula, Growth & Why 0! = 1
In mathematics, the factorial of a non-negative integer n, written n! (pronounced "n factorial"), equals the product of every positive whole number from n down to 1. First symbolized with the exclamation mark by French mathematician Christian Kramp in 1808, factorials lie at the heart of permutations, combinations, Maclaurin power series, statistical distributions, and algorithm complexity bounds.
Stirling's Asymptotic Formula: n! ≈ √(2πn) × (n ÷ e)^n
Legendre Trailing Zeros: Z(n!) = floor(n/5) + floor(n/25) + floor(n/125) + ...
Continuous Gamma Extension: x! = Γ(x + 1) = Integral_0^Inf (t^x · e^(−t)) dt (for x > −1)
Why does 0! strictly equal 1 rather than zero? Three separate proofs force 0! = 1: first, dividing down the recurrence (n − 1)! = n! ÷ n at n = 1 gives 0! = 1! ÷ 1 = 1; second, there is exactly 1 way to arrange an empty set of zero items; and third, setting 0! = 1 keeps combinatorial formulas like C(n, n) = n! ÷ (n! · 0!) = 1 and Taylor coefficients x⁰ ÷ 0! = 1 free of division by zero.
Super-Exponential Growth & Stirling's Asymptotic Approximation
Because every increment of n multiplies the entire previous product by a larger integer rather than a fixed base, n! overtakes any constant exponential c^n cleanly once n grows past e × c. While 2¹² = 4,096 takes four digits, 12! = 479,001,600 already reaches nine digits, and shuffling a standard 52-card deck spans 52! ≈ 8.0658 × 10⁶⁷ (68 decimal digits)!
Because standard IEEE-754 floating-point numbers overflow to Infinity once n >= 171, our Factorial Calculator evaluates integer inputs all the way to n = 5,000 using exact BigInt multiplication while simultaneously showing James Stirling's famous asymptotic estimate √(2πn) · (n / e)^n. Stirling's relative error shrinks inversely proportional to 1 / (12n): from -1.65% at n = 5 to -0.69% at n = 12, and less than 0.083% by n = 100.
n |
Exact Factorial n! |
Digits | Trailing Zeros | Stirling Error |
|---|---|---|---|---|
| 0! and 1! | 1 | 1 | 0 | -7.79% (at n=1) |
| 5! | 120 | 3 | 1 | -1.651% |
| 12! | 479,001,600 | 9 | 2 | -0.692% |
| 20! | 2,432,902,008,176,640,000 | 19 | 4 | -0.416% |
| 52! (Cards) | 8.065818 × 10^67 | 68 | 12 | -0.160% |
| 100! | 9.332622 × 10^157 | 158 | 24 | -0.083% |
Legendre's Formula: Counting Trailing Zeros in Any Factorial n!
How many zeros sit at the right-hand end of 100! without computing all 158 digits by hand? Every trailing zero represents one multiplication by 10 = 2 × 5. Because prime factors of 2 occur far more frequently than factors of 5 in sequential integers (100! contains 97 factors of 2), the bottleneck is strictly the number of prime factors of 5 inside n!.
Adrien-Marie Legendre proved that the exact multiplicity of any prime p inside n! equals the integer floor series sum_{k >= 1} floor(n / p^k). For p = 5 and n = 100, single multiples of 5 contribute floor(100 / 5) = 20 fives, while squares 25, 50, 75, 100 each hide a bonus second factor of 5 (floor(100 / 25) = 4), producing 20 + 4 = 24 trailing zeros instantly!
Double Factorials (n!!), Derangements (!n) & Continuous Gamma (x! = Γ(x + 1))
Beyond ordinary factorials, three classic generalizations appear regularly across calculus, physics, and probability:
- Double Factorial (
n!!): Skips every second factor rather than multiplying consecutive integers. For odd integers,9!! = 9 × 7 × 5 × 3 × 1 = 945; for even integers,8!! = 8 × 6 × 4 × 2 = 384 = 2⁴ · 4!. Notice carefully that(n!)!(taking factorial twice) differs completely from double factorialn!!! - Subfactorial / Derangements (
!n): Counts permutations ofnitems where zero objects remain in their original position (for instance, drawing Secret Santa names where nobody pulls their own name). Recurrence!n = (n − 1)(!(n−1) + !(n−2))gives!4 = 9(out of 24) and!9 = 133,496, and the fraction!n ÷ n!rapidly converges to1/e ≈ 36.7879%. - Continuous Gamma Extension (
x! = Γ(x + 1)): Leonhard Euler extended factorials smoothly to real and decimal arguments via the Gamma integralΓ(z). Using Lanczos coefficients in Mode 4, fractional half-integers connect directly toπ:(-0.5)! = Γ(0.5) = √π = 1.772454,0.5! = √π ÷ 2 = 0.886227, and4.5! = (945 / 32)√π = 52.342778!
2 Worked Examples
Example 1: Computing 100! Trailing Zeros, 12! Stirling Error & Ratio 12! ÷ 8!
Part A (Exact 12! & Factorial Ratio in Modes 1 & 3): Evaluate 12!, check Stirling's estimate, and simplify the division 12! ÷ 8! without full expansion:
- Evaluate
12!: Multiplying12 × 11 × ... × 1yields479,001,600(9 decimal digits, ending in2trailing zeros from factors5and10). - Compare Stirling estimate:
√(24π) · (12 / e)¹² ≈ 8.6832 × 54,782,360 = 475,687,486.47, within-0.692%of exact12!. - Cancel
12! ÷ 8!via Mode 3: Rather than dividing479,001,600 ÷ 40,320, cancel8!directly:12 × 11 × 10 × 9 = 11,880!
Part B (Trailing Zeros of 100!): Apply Legendre's formula cleanly: floor(100/5) + floor(100/25) + floor(100/125) = 20 + 4 + 0 = 24 trailing zeros at the end of all 158 digits of 100!.
Example 2: Odd/Even Double Factorials, Secret Santa Derangements & Decimal Gamma 4.5!
Part A (Double & Subfactorial in Mode 2 with n = 9): Nine coworkers draw names from a hat. Find 9!!, the exact number of ways nobody draws their own name (!9), and the probability of zero self-draws:
- Odd double factorial
9!!: Multiply odd steps only:9 × 7 × 5 × 3 × 1 = 945(whereas even8!! = 8 × 6 × 4 × 2 = 384). - Compute derangements
!9: Total orders equal9! = 362,880. Dividing362,880 ÷ e ≈ 133,496.09gives exact integer!9 = 133,496! Dividing133,496 ÷ 362,880gives a36.7879%chance of a clean gift exchange with zero self-assignments.
Part B (Decimal Gamma 4.5! in Mode 4): Compute 4.5! = Γ(5.5) by repeatedly applying the recurrence Γ(z + 1) = z · Γ(z) down to Γ(0.5) = √π ≈ 1.77245385: 4.5 × 3.5 × 2.5 × 1.5 × 0.5 × √π = (945 ÷ 32) × 1.77245385 = 52.342778!
Where Factorials Appear in Calculus, Statistics & Computer Science
Beyond counting arrangements, factorials serve as the foundational scaling denominator in continuous calculus and algorithm analysis. Every smooth function's Maclaurin and Taylor polynomial expansion scales each higher derivative term by 1 ÷ k! so infinite sums converge rapidly across all real inputs: Euler's constant equals e = 1/0! + 1/1! + 1/2! + 1/3! + ..., while sin(x) and cos(x) alternate powers over odd and even factorials (x³ ÷ 3!, x² ÷ 2!).
In probability theory, factorials power the discrete Poisson distribution (e^(−λ) · λ^k ÷ k!) for rare event arrivals, the Binomial and Multinomial PMFs, and hypergeometric likelihood tests. In computer science, algorithms with O(n!) factorial time complexity, such as brute-force Traveling Salesman route enumeration, become computationally intractable past n = 15 cities because 15! already exceeds 1.3 trillion paths, motivating dynamic programming and heuristic approximations instead.
Factorial Formulas in Excel, Google Sheets & Python
Evaluate integer factorials, double factorials, and continuous Gamma extensions in spreadsheets or Python:
Excel / Sheets Double Factorial n!!: =FACTDOUBLE(n)
Excel / Sheets Gamma Extension x!: =GAMMA(x + 1) [or =EXP(GAMMALN(x + 1))]
Python Standard Exact Integer n!: math.factorial(n)
Python Continuous Decimal x!: math.gamma(x + 1)
5 Common Factorial Mistakes to Avoid
- Confusing Double Factorial
n!!With Nested Factorial(n!)!: Forn = 4, double factorial skips by 2 (4!! = 4 × 2 = 8), whereas applying factorial twice gives(4!)! = 24! ≈ 6.2 × 10²³! - Thinking Factorials Are Undefined at Zero: Setting
0! = 1is algebraically required byn! = n × (n − 1)!and preserves every combinatorial and calculus identity. - Forgetting Multiples of 25, 125, and 625 in Trailing Zeros: Stopping at
floor(n / 5)undercounts zeros whenevern >= 25because25carries two fives and125carries three fives. - Trying Negative Integers Inside Gamma: While decimals like
-0.5! = √πare completely finite, negative integers (-1, -2, -3...) hit vertical poles whereΓ(0),Γ(-1), etc. diverge to infinity. - Computing
12! ÷ 8!by Expanding Both Numbers First: Even modest ratios like200! ÷ 197! = 200 × 199 × 198 = 7,880,400overflow regular 64-bit floats if not canceled algebraically beforehand.
Related Combinatorics & Probability Tools
Use factorials as building blocks inside our other discrete math and statistics solvers in the Math & Statistics hub:
- Permutation Calculator (nPr): compute falling factorial arrangements
n! ÷ (n − r)!, word anagrams, and circular rings. - Combination Calculator (nCr): evaluate binomial subsets
n! ÷ [r!(n − r)!]and Stars & Bars repetitions. - Probability Calculator: run Binomial distribution PMFs using combinations and trial powers.
- Sample Size Calculator & Confidence Interval Calculator: design statistical samples and interval bounds.
Verify how Staticnexa tests BigInt multiplication and Lanczos Gamma accuracy in our Editorial & Mathematical Accuracy Policy.
Frequently Asked Questions
Why does 0 factorial (0!) equal 1?
Using the backward recurrence (n - 1)! = n! / n at n = 1 forces 0! = 1! / 1 = 1. It also matches the empty product rule (multiplying zero numbers yields the multiplicative identity 1) and keeps C(n, 0) = 1 valid.
How do you find the number of trailing zeros in n!?
Use Legendre's formula for factors of 5: floor(n/5) + floor(n/25) + floor(n/125) + ... stopping once 5^k > n. For 100!, floor(100/5) + floor(100/25) = 20 + 4 = 24 trailing zeros.
What is the difference between n!! (double factorial) and (n!)!?
Double factorial n!! steps down by 2 (e.g., 8!! = 8*6*4*2 = 384 and 9!! = 9*7*5*3*1 = 945), whereas (n!)! computes the factorial of n! itself (e.g., (4!)! = 24!).
Can you take the factorial of a decimal or fraction like 0.5! or 4.5!?
Yes! The continuous Gamma function extends factorials via x! = Gamma(x + 1) for any real number x > -1. In particular, (-0.5)! = sqrt(pi) = 1.77245, 0.5! = sqrt(pi)/2 = 0.88623, and 4.5! = 52.34278.
What is a subfactorial (!n) or derangement?
Subfactorial !n counts permutations of n items where NO item ends up in its original position. For n >= 1, !n equals the nearest integer to n! / e (~36.79% of all n! permutations).
What is the largest factorial a standard 64-bit calculator can hold?
Standard IEEE-754 double-precision floating-point numbers overflow to Infinity at 171! (~1.24e309). Because this calculator uses arbitrary-precision BigInt math, it computes exact digits cleanly up to 5,000!.
Mathematical References & Standards
- Wikipedia: Factorial: Recursive Definition, 0! Identity & Applications (Core definition, Legendre trailing zero formula, and combinatorial applications)
- Wikipedia: Stirling's Approximation: Asymptotic Bounds & Relative Error (Derivation of n! ~ sqrt(2*pi*n)*(n/e)^n for large-scale factorial estimation)
- Wikipedia: Double Factorial: Odd vs. Even Parity Products n!! (Identities connecting even 2n!! = 2^n * n! and odd half-integer Gamma values)
- Wikipedia: Derangement: Subfactorial !n Recurrence & 1/e Limit (Fixed-point-free permutation counting and round(n!/e) identity)
- Wikipedia: Gamma Function: Analytic Extension of Factorials to Real & Complex Numbers (Euler integral defining x! = Gamma(x + 1) and half-integer sqrt(pi) bridge)