Skip to content
uencoder

Factorial

The factorial n! = n×(n-1)×...×1 is the basis of permutations and combinations; it grows extremely fast, with 20! already exceeding the 64-bit integer range.

Purpose

To count permutations of n elements, a foundation of combinatorics.

Mechanism

n! = n×(n-1)!, with 0!=1. Stirling's approximation: n! ≈ √(2πn)(n/e)^n. Large n requires BigInteger. The Γ function extends the factorial to non-integers. Log-factorials use lgamma to avoid overflow.

Use cases

  • Permutation and combination counting
  • Probability and statistics (Poisson distribution)
  • Taylor series coefficients
  • Gamma function

Examples

  • Input
    5=5×4×3×2×1=120
    Output
    120

    Number of permutations of 5 elements

Common errors

Factorial overflow for large n
Use BigInteger or log-factorials

Related terms

Related tools

References