Factorial (denoted n!) is the product of all positive integers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. By convention, 0! = 1 (the empty product). Factorials grow extremely rapidly: 10! = 3,628,800, 20! = 2,432,902,008,176,640,000. This explosive growth makes factorials impractical to compute for large n without specialized algorithms or arbitrary-precision arithmetic. Factorials appear in combinatorics (counting permutations), probability (calculating probabilities of arrangements), and calculus (Taylor series expansions).
The factorial function has a simple recursive definition: n! = n × (n-1)!, with base case 0! = 1. This translates to iterative computation: start with result = 1, multiply by each integer from 1 to n. For example, 4! computes as 1 × 1 = 1, 1 × 2 = 2, 2 × 3 = 6, 6 × 4 = 24. While elegant, this naive approach fails for large n due to integer overflow. For n > 20, standard 64-bit integers overflow; BigInt or arbitrary-precision libraries are required to compute factorials accurately.
Factorials have mathematical properties useful for optimization. The ratio (n+1)!/n! = n+1 enables incremental computation. Stirling's approximation provides estimates for large factorials: n! ≈ √(2πn) × (n/e)^n. For combinatorial calculations, ratios like n!/(n-k)! simplify to n × (n-1) × ... × (n-k+1), avoiding full factorial computation. Understanding these properties enables efficient algorithms for permutations, combinations, and probability calculations without computing massive factorial values directly.