Factorial Calculator | n!, n!! & Derangements
Calculate standard factorial (n!), double factorial (n!!), and subfactorial (!n / derangements) with exact BigInt arithmetic, scientific notation, digit count, trailing zeros, Stirling approximation, and step-by-step working.
Exact BigInt for n ≤ 5,000. For larger n, Stirling approximation is shown.
Press Enter to calculate · Esc to reset
What Is the Factorial Calculator?
The factorial of a non-negative integer n, written n!, is the product of every positive integer from 1 up to n. It answers the question: "In how many ways can n distinct items be arranged in a sequence?", which is the number of permutations. Factorials grow extraordinarily fast: 10! = 3,628,800 and 20! ≈ 2.43 × 10¹⁸ already exceeds the number of nanoseconds since the Big Bang.
This calculator uses JavaScript BigInt for exact integer arithmetic, no floating-point rounding for n up to 5,000. Beyond that, Stirling's approximation gives a result accurate to several significant figures. The exact value can be copied to the clipboard as a full string of digits.
The double factorial n!! multiplies every other integer: 7!! = 7 × 5 × 3 × 1 = 105. It appears in Wallis's formula for π, in the normalisation of half-integer Gamma function values, and in physics integrals over odd powers of trigonometric functions.
The subfactorial !n counts derangements, permutations of n elements where no element occupies its original position. The classic formulation is the hat-check problem: if n people leave n hats and each person randomly takes one hat, the probability that nobody gets their own hat is !n/n!, which approaches 1/e ≈ 36.79% as n grows. This calculator uses the exact recurrence !n = (n−1)(!(n−1) + !(n−2)) with BigInt arithmetic.
Factorial Calculator Formula and Method
| Symbol | Name | Description |
|---|---|---|
| n | Input integer | Non-negative integer; the number whose factorial is computed |
| n! | Standard factorial | Product of all integers from 1 to n; 0! = 1 by definition |
| n!! | Double factorial | Product of every other integer from n down to 1 or 2 |
| !n | Subfactorial | Number of derangements, permutations with no fixed point |
| e | Euler's number | 2.71828…, appears in subfactorial approximation !n ≈ n!/e |
| ⌊x⌋ | Floor function | Largest integer ≤ x; used in Legendre's trailing-zero formula |
| P(n,r) | Permutations | n! / (n−r)!, number of ordered selections of r from n items |
| C(n,r) | Combinations (nCr) | n! / (r!(n−r)!), number of unordered selections of r from n |
How to Use
- 1Choose a mode: Select "n!" for standard factorial, "n!!" for double factorial, or "!n" for subfactorial (derangements) using the mode tabs at the top.
- 2Load a preset: Click a preset button (n = 0, 5, 10, 20, 52, 100, 1000…) to auto-fill a well-known value. The preset tooltips describe what each factorial represents.
- 3Enter n: Type any non-negative integer. For standard factorial: n ≤ 5,000 gives exact BigInt results; n up to 10,000 uses Stirling's approximation. Decimals and negatives are flagged as invalid.
- 4Press Calculate or Enter: Click "Calculate" or press Enter. The result appears instantly, exact for small n, approximated for very large n.
- 5Read the results: The primary card shows the exact value (truncated if > 60 digits) and scientific notation. Stat cards show digit count, trailing zeros (standard mode), and Stirling's approximation.
- 6Check nearby values: The "Nearby values" table compares n−2, n−1, n, n+1, n+2 so you can see the ratio between consecutive factorials (each is just multiplied by the next integer).
- 7Expand step trace: Click "Calculation steps" to see the derivation, formula applied, key intermediate results, trailing-zero formula, and Stirling approximation side by side.
- 8Copy exact value: Click "Copy exact value" to copy the full untruncated integer string to your clipboard, useful for pasting into programming environments or verifying against other tools.
Factorial Calculator Example
Example 1: 10!, a manageable factorial
10! is often used as a first non-trivial factorial example in combinatorics courses.
Example 2: 52!, ordering a standard deck of cards
The number of ways to arrange a standard 52-card deck. Nearly every shuffle in history is unique.
Example 3: !5, subfactorial and derangements
Five letters are placed randomly into five addressed envelopes. How many ways result in no letter going to its correct address?
Understanding Factorial
How Fast Do Factorials Grow?
Factorials grow faster than any exponential function, a fact captured by Stirling's approximation: n! ≈ √(2πn)(n/e)ⁿ. The digit count of n! grows roughly as n log₁₀(n/e). Here are some reference points:
| n | n! | Digits |
|---|---|---|
| 0 | 1 | 1 |
| 5 | 120 | 3 |
| 10 | 3,628,800 | 7 |
| 20 | 2,432,902,008,176,640,000 | 19 |
| 52 | 8.07 × 10⁶⁷ | 68 |
| 100 | 9.33 × 10¹⁵⁷ | 158 |
| 170 | 7.26 × 10³⁰⁶ | 307 |
| 1000 | 4.02 × 10²⁵⁶⁷ | 2,568 |
| 5000 | ≈ 10¹⁶,³²⁶ | 16,326 |
Trailing Zeros, Legendre's Formula
The number of trailing zeros in n! equals the number of times 10 divides n!, which equals the number of times 5 divides n! (since there are always more factors of 2 than 5). Legendre's formula gives:
This formula appears in number theory and is a classic interview problem. The calculator shows it for every standard factorial.
Factorials in Combinatorics
- ›Permutations P(n, r) = n! / (n−r)!: The number of ordered arrangements of r items chosen from n. Computed directly using the factorial. See also the permutation calculator.
- ›Combinations C(n, r) = n! / (r!(n−r)!): Unordered selections (binomial coefficients). The denominator r! removes the overcounting of identical selections in different orders.
- ›Multinomial coefficient n! / (k₁!k₂!…kₘ!): Generalization for partitioning n items into groups of sizes k₁, k₂, …, kₘ.
- ›Taylor series: The coefficient of xⁿ in the Maclaurin series of eˣ is 1/n!. The series eˣ = Σ xⁿ/n! is why e appears in the subfactorial approximation.
- ›Gamma function: Γ(n) = (n−1)! extends factorial to non-integers. Γ(1/2) = √π, connecting factorial growth to the normal distribution.
Double Factorial Applications
The double factorial n!! appears in several important contexts:
- ›Wallis product: π/2 = (2/1)(2/3)(4/3)(4/5)… = (2·2·4·4·6·6…)/(1·3·3·5·5·7…) = (2ⁿ n!!)²/((2n−1)!! × (2n+1)!!)
- ›Physics integrals: ∫₀^(π/2) sinⁿ(x)dx = π/2 × (n−1)!! / n!! for even n, and (n−1)!!/n!! for odd n.
- ›Quantum mechanics: Spherical harmonic normalisation constants involve double factorials of (2l+1)!!
- ›Combinatorics: (2n−1)!! = (2n)! / (2ⁿ n!) counts the number of ways to partition 2n elements into n pairs.
The Derangement Problem
A derangement is a permutation where no element appears in its original position. The subfactorial !n counts all such arrangements of n elements, and the probability that a random permutation is a derangement converges rapidly to 1/e ≈ 36.79%, regardless of how large n gets:
Frequently Asked Questions
What is a factorial and what does n! mean?
The factorial of n, written n!, is the product of all positive integers from 1 up to n.
- • 5! = 5 × 4 × 3 × 2 × 1 = 120
- • 10! = 10 × 9 × 8 × … × 1 = 3,628,800
- • The recurrence relation is n! = n × (n−1)!, each factorial is just the previous one multiplied by n.
Factorials count ordered arrangements. 3! = 6 because there are 6 ways to arrange the letters A, B, C: ABC, ACB, BAC, BCA, CAB, CBA.
Why is 0! equal to 1?
By convention, 0! = 1, and there are two complementary reasons this makes sense:
- • Empty product: A product over zero factors is defined as 1 (the multiplicative identity), just as a sum over zero terms is 0 (the additive identity). So 0! = 1 by the same logic.
- • Consistency with the recurrence: n! = n × (n−1)! requires that 1! = 1 × 0!, so 0! must equal 1! / 1 = 1.
- • Combinatorics: The number of ways to arrange 0 items is 1 (the single empty arrangement). This ensures C(n, 0) = 1 and C(n, n) = 1 for all n.
How many digits does n! have?
The number of digits in n! is ⌊log₁₀(n!)⌋ + 1, which can be approximated using Stirling's formula:
- • log₁₀(n!) ≈ (n·log₁₀(n/e) + ½·log₁₀(2πn)) for large n
- • 100! has 158 digits, 1000! has 2,568 digits, 10000! has about 35,660 digits.
- • This calculator shows the exact digit count (from the BigInt string length) for n ≤ 5,000.
The digit count grows roughly linearly with n because log₁₀(n!) ≈ n·log₁₀(n/e), which is O(n·log n).
How many trailing zeros does n! have?
Trailing zeros come from factors of 10 = 2 × 5 in n!. Since factors of 2 always outnumber factors of 5, the count equals the number of times 5 divides n!.
Use Legendre's formula: ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + …
- • 10!: ⌊10/5⌋ = 2 trailing zeros → 10! = 3,628,800 ✓
- • 25!: ⌊25/5⌋ + ⌊25/25⌋ = 5 + 1 = 6 trailing zeros
- • 100!: ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24 trailing zeros
Note that 25 contributes two factors of 5 (25 = 5²), which is why ⌊n/25⌋ appears as a separate term.
What is the double factorial (n!!)?
The double factorial n!! multiplies every other integer from n downward, not the factorial of a factorial.
- • Odd n: 7!! = 7 × 5 × 3 × 1 = 105
- • Even n: 8!! = 8 × 6 × 4 × 2 = 384
- • 0!! = 1 and 1!! = 1 by convention
The relationship to standard factorial is: (2n)!! = 2ⁿ · n! and (2n−1)!! = (2n)! / (2ⁿ · n!). Double factorials appear in Wallis's product for π and in physics integrals over odd powers of sine and cosine.
What is a subfactorial (!n) and what does it count?
The subfactorial !n counts derangements, permutations of n elements where no element appears in its original position.
- • !1 = 0: impossible to derange one element
- • !2 = 1: only one derangement of (A, B), swap them: (B, A)
- • !3 = 2: (B,C,A) and (C,A,B)
- • !10 = 1,334,961 out of 10! = 3,628,800 total permutations
A classic application: if n people drop their hats and each randomly picks one up, !n/n! ≈ 1/e ≈ 36.79% is the probability nobody gets their own hat back, and this probability barely changes as n grows, converging to exactly 1/e.
Can factorials be computed for negative or decimal numbers?
Standard factorials (n!) are only defined for non-negative integers. However, there are extensions:
- • Gamma function Γ(n): Extends factorial to all real (and complex) numbers except non-positive integers. Γ(n) = (n−1)! for positive integers.
- • Half-integer values: Γ(1/2) = √π, so (1/2)! = Γ(3/2) = √π/2 ≈ 0.886.
- • Negative integers: The Gamma function has poles at 0, −1, −2, …, factorial is undefined at these points (diverges to ±∞).
This calculator restricts to non-negative integers and flags decimal inputs with an explanatory error, the Gamma function extension is beyond the scope of a factorial calculator.
Why do factorials appear in probability and statistics?
Factorials quantify counting, and counting underlies all of probability theory.
- • Permutations: Arranging n items in order, n! ways. Choosing r from n in order, P(n,r) = n!/(n−r)! ways.
- • Combinations: Choosing r from n regardless of order, C(n,r) = n!/(r!(n−r)!) ways. These are the binomial coefficients in (a+b)ⁿ.
- • Binomial distribution: P(X=k) = C(n,k) · pᵏ · (1−p)^(n−k), the C(n,k) factor uses factorial.
- • Poisson distribution: P(X=k) = e⁻λ · λᵏ / k!, k! appears directly in the denominator.
- • Normal distribution (central limit theorem): The proof uses Stirling's approximation to n! in the limit argument.
You Might Also Like
Explore 360+ Free Calculators
From math and science to finance and everyday life — all free, no account needed.