Calculate factorial (n!) of any non-negative integer with high precision. Uses BigNumber.js for accurate large number calculations.
The factorial of a non‑negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. Formally: n! = n × (n−1) × ... × 2 × 1, with the special case 0! = 1 (empty product). Factorials are fundamental in combinatorics, probability theory, calculus (Taylor series), and algorithm analysis (complexity of permutations).
The factorial notation was introduced by Christian Kramp in 1808, though the concept dates back to ancient Indian mathematics (permutations) and later to Euler’s work on the gamma function. Factorials govern the number of ways to arrange n distinct objects (n! permutations), and they appear in binomial coefficients: \(\binom{n}{k} = \frac{n!}{k!(n-k)!}\). The gamma function \(\Gamma(z)\) extends factorial to complex numbers: \(\Gamma(n+1) = n!\) for positive integers. This extension is critical in advanced calculus, physics, and statistics (e.g., chi-squared distribution).
This calculator employs JavaScript's BigInt primitive, which supports arbitrary‑precision integers. The algorithm performs a simple iterative multiplication from 1 to n, ensuring no loss of precision for extremely large values (up to 2000!). For n = 2000, the result contains about 5736 digits, computed locally without floating‑point errors. The scientific approximation uses high‑precision logarithms to determine magnitude and the first few significant digits.
Additional metrics: digit count via log10(n!) formula (Stirling's approximation for verification), trailing zeros count using the Legendre formula: \(\lfloor n/5 \rfloor + \lfloor n/25 \rfloor + \lfloor n/125 \rfloor + ... \).
.toString().length.
The number of trailing zeros in n! is determined by the exponent of 5 in its prime factorization (since 2s are always abundant). Legendre's formula gives: \( \text{zeros} = \sum_{i=1}^{\infty} \left\lfloor \frac{n}{5^i} \right\rfloor \). For n = 100, trailing zeros = 20 + 4 = 24. This concept is widely used in competitive programming and combinatorial number theory.
The digit length of n! grows super‑exponentially. Using Stirling’s approximation: \(\log_{10}(n!) \approx n \log_{10}(n/e) + \frac{1}{2}\log_{10}(2\pi n)\). Our calculator provides exact digit length for any n up to 2000.
| n | n! (exact) | Digit count | Trailing zeros |
|---|---|---|---|
| 0 | 1 | 1 | 0 |
| 5 | 120 | 3 | 1 |
| 10 | 3,628,800 | 7 | 2 |
| 20 | 2,432,902,008,176,640,000 | 19 | 4 |
| 50 | ≈ 3.0414×10⁶⁴ | 65 | 12 |
| 100 | ≈ 9.3326×10¹⁵⁷ | 158 | 24 |
The factorial function lies at the heart of the birthday paradox: the probability that no two people share a birthday in a group of k is \(\frac{365!}{(365-k)! \cdot 365^k}\). Our calculator can compute intermediate factorial ratios for k up to about 50. For k=23, the probability is ~0.493, demonstrating the surprising nature of collisions. This illustrates how factorials enable precise probabilistic modeling.
This tool is optimized for n up to 2000 (result length ≈ 5736 digits). For n > 2000, the string length exceeds 10,000 digits and may cause performance issues or UI slowdown. However, for educational and typical combinatorial usage, n rarely exceeds a few hundred. If you require larger factorials, consider using specialized libraries or scientific approximation.