Solved.tools — Free Online Calculators & Tools

We use cookies for analytics and advertising. Learn more about our cookie policy

Factorial Calculator

Last updated: 19 August 2026

Reviewed by Gavin · Research and drafting assisted by AI

Compute n! for any non-negative integer up to 5,000 with exact integer arithmetic (BigInt, every digit preserved), plus the double factorial n!! on demand and a Stirling approximation for quick sanity checks.

Standard factorial uses BigInt by default to preserve every digit at every n.
10! (exact, BigInt)3,628,8007 digits · 2 trailing zeros · log₁₀ = 6.5561
Stirling approximation & sanity checks (n > 20)

Stirling approximation: n! ≈ √(2π n) · (n/e)^n = 3.598696 × 10^6

Tight Stirling (log₁₀): 6.5561

Stirling relative error vs exact: 0.8296 %

Working & Formulas

Single factorial n! = 1 · 2 · 3 · … · n (with 0! = 1).

Double factorial n!! = n · (n − 2) · (n − 4) · … down to 1 or 2.

Stirling n! ≈ √(2π n) · (n / e)^n.

Permutation preview P(n, k) = n! / (n − k)! → P(10, 3) = 720.

Combination preview C(n, k) = n! / (k! · (n − k)!) → C(10, 3) = 120.

Quick reference. 0! = 1 by convention. 1! = 1. n! = n · (n−1) · … · 2 · 1. For n ≥ 21 we use BigInt arithmetic — IEEE-754 doubles lose integer precision beyond 2^53 ≈ 9 · 10^15 (20! = 2.4329 · 10^18 already exceeds that). Stirling's approximation n! ≈ √(2π n) · (n/e)^n is the standard shortcut for very large n. The number of trailing zeros in n! equals the number of factors of 5 in 1 · 2 · … · n (Legendre's formula).
Was this helpful?


Factorial Calculator, n! and n!! (BigInt)

What this tool does

This page computes the factorial n! for any non-negative integer n, from 0 up to 170, with exact big-integer precision (no floating-point rounding). It also computes the double factorial n!!, which multiplies numbers by step 2 (n × (n-2) × (n-4) × …). For large n (greater than ~20), the result also shows Stirling's approximation as a sanity check.

The factorial is one of the most fundamental functions in combinatorics: it counts the number of ways to arrange n distinct objects in a sequence (permutations of n items), and it appears as a multiplicative factor in the binomial coefficient, the Gamma function, Taylor series expansions, the birthday problem, and many physics formulas (Stirling's approximation for the entropy of an ideal gas, for instance, uses n! throughout). The double factorial appears in the formulas for the number of perfect matchings in a complete graph, in counting the ways to pair up 2n items, and in the recurrence for π via the Wallis product.

How to use this calculator

  1. Enter a non-negative integer n in the input box. The calculator accepts 0 ≤ n ≤ 170 (170! is the largest factorial that fits in JavaScript BigInt precision without truncation).
  2. Choose the factorial type with the toggle: "Factorial (n!)" or "Double factorial (n!!)".
  3. Click Calculate (or press Enter). The result appears immediately.
  4. Read the result panel: the exact value (digit-grouped), digit count, and, for n > 20, Stirling's approximation alongside it.
  5. Try the example buttons for common values (5!, 10!, 20!, 50!, 100!) without typing.
  6. Copy the result with the copy button to paste into spreadsheets, documents, or code.

Algorithm, Factorial

The factorial is defined recursively:

0! = 1
n! = n × (n-1)! for n ≥ 1

Equivalently, iteratively:

n! = 1 × 2 × 3 × ... × n

The empty product equals 1 by convention, which is why 0! = 1. This convention is essential to make many combinatorial identities work cleanly: the binomial coefficient C(n,0) = n!/0!/n! = 1, the number of ways to arrange zero items is 1, and the recurrence C(n,k) = C(n-1,k-1) + C(n-1,k) holds for n = 0.

BigInt precision

JavaScript's Number type can exactly represent integers only up to 2^53 - 1 ≈ 9.0 × 10^15, which is smaller than 21! ≈ 5.1 × 10^19. Beyond that, floating-point rounding corrupts the result. This calculator switches to BigInt arithmetic for n ≥ 21, which can represent arbitrarily large integers exactly. BigInt operations are slower than Number, so the calculator uses Number for small n (faster) and BigInt for large n (correct).

Stirling's approximation

For large n, the factorial grows so fast that the exact digit sequence is unwieldy. Stirling's approximation gives a closed-form estimate:

n! ≈ √(2πn) × (n/e)^n

This is the basis of the leading-order term. With the next-order correction:

n! ≈ √(2πn) × (n/e)^n × (1 + 1/(12n) + 1/(288n²) - ...)

For n = 100, Stirling gives 9.33 × 10^157, while the exact value is 9.33 × 10^157. The relative error is well under 1%. For n = 1000, Stirling's error is approximately 0.08% (about 8 parts in 10,000), which is precise enough for statistical mechanics work but not for cryptographic applications.

Algorithm, Double factorial

The double factorial is:

n!! = n × (n-2) × (n-4) × ... × (last positive term)

For even n, n!! ends at 2. For odd n, n!! ends at 1. Special cases: (-1)!! = 1 by convention, 0!! = 1 by convention. The double factorial relates to the regular factorial by:

(2k)!! = 2^k × k!
(2k-1)!! = (2k)! / (2^k × k!)

The double factorial appears in:

  • Number of perfect matchings in a complete graph K(2n): (2n-1)!!
  • Wallis product for π: π/2 = ∏ (2k)(2k) / ((2k-1)(2k+1)) for k = 1, 2, ...
  • Beta and Gamma function identities
  • Counting involutions in combinatorics

Worked examples

1. 0! = 1

The empty product. Important for binomial identities and for the recursive definition of factorial.

2. 5! = 120

5 × 4 × 3 × 2 × 1 = 120. The number of ways to arrange 5 distinct items in a row (e.g., 5 people in a queue).

3. 10! = 3,628,800

10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 3,628,800. The number of ways to seat 10 guests at a dinner table. BigInt not yet needed, Number precision holds.

4. 20! = 2,432,902,008,176,640,000

20 × 19 × ... × 1 = 2.43 × 10^18. This is the largest factorial that fits in JavaScript Number precision (the 53-bit mantissa of IEEE 754 floats is exhausted just below 21!). The calculator switches to BigInt at n = 21 to maintain exact integer output for all larger values.

5. 25! = 15,511,210,043,330,985,984,000,000

This is the first factorial that requires BigInt. JavaScript Number would round it incorrectly; the BigInt result is exact to all 24 digits.

6. 8!! = 384

The even double factorial. 8 × 6 × 4 × 2 = 384. Also: 8!! = 2^4 × 4! = 16 × 24 = 384.

7. 9!! = 945

The odd double factorial. 9 × 7 × 5 × 3 × 1 = 945. Also: 9!! = 10! / (2^5 × 5!) = 3,628,800 / (32 × 120) = 3,628,800 / 3,840 = 945.

8. Negative input rejected

The factorial is defined only for non-negative integers. Negative inputs produce an error message.

9. Stirling's approximation at n = 100

Exact: 100! ≈ 9.33262154439441 × 10^157. Stirling: √(2π × 100) × (100/e)^100 ≈ 9.32485 × 10^157. Relative error: 0.083%.

Where the factorial shows up

  • Permutation counting, n! counts the arrangements of n items in a row; P(n,r) = n! / (n-r)! counts the arrangements of r items chosen from n.
  • Binomial coefficients, C(n,r) = n! / (r! × (n-r)!) is the foundation of binomial distributions, Pascal's triangle, and many combinatorial identities.
  • Probability and statistics, the birthday problem uses 365! / (365-k)! in its formula; the Poisson distribution uses k! in the denominator; hypergeometric distributions involve factorials in numerator and denominator.
  • Taylor series expansions, sin(x) = Σ (-1)^k × x^(2k+1) / (2k+1)!; e^x = Σ x^k / k!; cos(x), ln(1+x), and many other elementary functions are defined by factorial-weighted polynomials.
  • Statistical mechanics, Stirling's approximation of ln(n!) appears in the entropy formula S = k × ln(W), where W (the number of microstates) is a multinomial coefficient involving factorials.
  • Combinatorics of matchings, the number of ways to pair 2n items (perfect matchings in a complete graph) is (2n-1)!!.
  • Cryptography, RSA key generation requires finding two large primes, and the security rests partly on the difficulty of factoring N without knowledge of (p-1)! × (q-1)! structure.
  • Numerical analysis, the gamma function Γ(z) extends the factorial to non-integer and complex arguments; Γ(n+1) = n! for non-negative integer n.
  • Physics, quantum mechanical partition functions, occupancy statistics, Bose-Einstein and Fermi-Dirac distributions involve factorials of large numbers.

Common mistakes

  • Thinking 0! = 0. It is 1 by convention. The factorial is the count of arrangements; there is exactly one way to arrange zero items.
  • Trying to compute n! for large n with Number. JavaScript Number corrupts values above 2^53 ≈ 9 × 10^15, which is below 21!. Always use BigInt for n ≥ 21.
  • Confusing factorial with power. 2^5 = 32 (two multiplied by itself five times); 5! = 120 (the product of all integers from 1 to 5). These are very different.
  • Forgetting that factorial grows faster than exponential. n! grows roughly like n^n × e^(-n), which is faster than any exponential a^n for fixed a. Beyond n = 20, the factorial is already astronomical.
  • Mixing up factorial and double factorial. n! = 1 × 2 × ... × n; n!! = n × (n-2) × (n-4) × ... The double factorial is half as many terms but the same starting point.
  • Computing factorial in a loop in the wrong direction. Both forward (1 × 2 × ... × n) and backward (n × (n-1) × ... × 1) work; some languages are faster one way than the other. JavaScript handles both well.
  • Assuming the calculator can handle n > 170. BigInt has no theoretical upper limit, but n = 170 produces a number of about 10^308 digits, which is near the practical limit for browser display and exact integer arithmetic. Beyond n = 1000, Stirling's approximation is more useful than the exact value.
  • Forgetting the empty product. When computing 0! or (-1)!!, the answer is 1, not 0 or undefined.

Frequently Asked Questions

Why is 0! equal to 1 instead of 0? The factorial counts the number of ways to arrange n items in a row. There is exactly one way to arrange zero items: do nothing. The convention 0! = 1 is also essential to make the binomial theorem C(n,0) = 1 work cleanly (since C(n,0) = n! / 0! / n! = 1 implies 0! = 1). The convention also makes the recursive formula n! = n × (n-1)! work for n = 1, since 1! = 1 × 0! = 1 × 1 = 1.

What is the largest n for which n! can be computed exactly? The calculator goes up to n = 170. The value of 170! has about 309 digits (a 309-digit number). JavaScript BigInt can represent this exactly. Beyond n ≈ 170, the number of digits grows linearly with n, and very large factorials become slow to display even though they are still computable. Stirling's approximation becomes useful for n > 100.

What is the difference between n! and Γ(n+1)? Γ is the Gamma function, a continuous extension of the factorial to non-integer and complex arguments. For non-negative integers n, Γ(n+1) = n!. But Γ is defined for all complex z except non-positive integers: Γ(z) = ∫₀^∞ t^(z-1) × e^(-t) dt. So Γ(0.5) = √π (about 1.772), Γ(-0.5) = -2√π, and Γ(1) = 1 = 0!. The factorial is just the integer case.

How does the calculator handle large inputs? For n ≤ 20, the calculator uses JavaScript Number (fast, exact up to 2^53). For n ≥ 21, it switches to BigInt (slower but exact for any size). The BigInt result is converted to a decimal string with comma-grouped digits for readability. For n > 100, Stirling's approximation is also displayed alongside.

What is the connection between n! and the birthday problem? The birthday problem asks: in a room of k people, what is the probability that at least two share a birthday? The answer involves 365! / (365-k)!, which counts the number of ways to assign k distinct birthdays without replacement. The probability of all-different birthdays is 365! / ((365-k)! × 365^k). The "birthday paradox", that this probability drops below 50% at just k = 23, comes from the factorial growing very fast with k.

Why does factorial grow so fast? Each step multiplies the running product by an integer that itself grows. The doubling at each step means that even a modest n quickly exceeds any fixed a^n. By n = 20, n! is already past the IEEE 754 integer precision limit (about 2.4 × 10^18). This growth rate is exactly why combinatorics problems that seem innocuous (like the traveling salesman problem with 20 cities) become computationally intractable, the search space is 20! ≈ 2.4 × 10^18.

Can factorial be computed for negative integers? No, factorial is undefined for negative integers in the standard definition. The Gamma function Γ(z) extends to non-integer negative values (where Γ(z) is defined via analytic continuation), but Γ(z) has poles at z = 0, -1, -2, ... so the Gamma function itself is not defined at negative integers.

What is subfactorial (derangement)? A derangement !n (also called subfactorial) counts the number of permutations of n items where no item is in its original position. The formula is !n = n! × Σ (-1)^k / k! for k = 0 to n, which simplifies to !n = round(n! / e). For n = 4: 4! = 24, derangements = 9 (all permutations except identity). The probability that a random permutation of n items is a derangement approaches 1/e ≈ 0.368 as n → ∞.

What is the most common use of factorial in everyday life? The most common applications are lottery odds (P(49,6) for "6/49" lotteries), poker hand probabilities (C(52,5) = 2,598,960 for 5-card poker hands), and password-entropy calculations (length × log2(charset), though this is permutations in a different sense, not factorial). Counting permutations and combinations comes up any time you ask "how many ways" a process can occur.

References

  • Concrete Mathematics: A Foundation for Computer Science. Graham, R. L., Knuth, D. E., Patashnik, O. (1994). Addison-Wesley. 2nd edition. ISBN 978-0201558029. Standard reference covering factorial, binomial coefficients, Stirling numbers, and the Gamma function with deep rigor.
  • NIST Digital Library of Mathematical Functions (DLMF). Olver, F. W. J., et al. (eds). §5.4 (Gamma function), §5.11 (Stirling numbers), §26.8 (Lagrange inversion). Free online at dlmf.nist.gov.
  • Concrete Factorials (combinatorics primer). Cameron, P. J. (1994). Introduction to Combinatorics. Chapter 1 covers factorial and binomial coefficient identities.
  • Stirling's approximation: original derivation in Stirling, J. (1730). Methodus Differentialis. London. The leading-order term and the next two corrections.
  • Gamma function: original definition in Euler, L. (1729). Letter to Goldbach. The integral form Γ(z) = ∫₀^∞ t^(z-1) e^(-t) dt was first written by Euler.
  • Knuth, D. E. (1997). The Art of Computer Programming, Volume 1: Fundamental Algorithms. 3rd edition. §1.2.5 covers factorial and binomial coefficients with full precision considerations.
  • Wall, H. S. (1948). Analytic Theory of Continued Fractions. Van Nostrand. Chapter 11 covers Wallis-type products, including the connection to double factorials in the Wallis product for π.