The Standard Engine.
MathematicsFinanceHealthPhysicsEngineeringBrowse all

Mathematics · Algebra & Calculus

Factorial Calculator

Calculate the factorial of any non-negative integer n, defined as the product of all positive integers from 1 to n.

Calculator

Advertisement

Formula

n is the non-negative integer for which the factorial is computed. The factorial is the product of all positive integers up to and including n. By convention, 0! is defined as 1.

Source: NIST Digital Library of Mathematical Functions, §26.1 — Combinatorial Analysis: https://dlmf.nist.gov/26.1

How it works

The factorial function is defined recursively: n! = n × (n−1)! with the base case 0! = 1. Expanding this recurrence gives the explicit product n! = n × (n−1) × (n−2) × ⋯ × 2 × 1. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. The convention that 0! = 1 is not arbitrary — it is required for combinatorial formulas such as the binomial coefficient to remain consistent when choosing zero elements from a set, and it arises naturally from the Gamma function identity Γ(n+1) = n!.

Factorials grow extremely rapidly — faster than any exponential function with a fixed base. By n = 20, the factorial already exceeds 2.4 × 10¹⁸, and by n = 100, it surpasses 9.3 × 10¹⁵⁷. For very large n, the exact integer value becomes unwieldy, so mathematicians and scientists often work with log₁₀(n!), which allows comparisons and order-of-magnitude estimates. The log-factorial also equals the sum of log₁₀(k) for k from 1 to n, a numerically stable way to compute it. This calculator displays log₁₀(n!) alongside the exact result for practical reference.

Stirling's approximation provides a smooth, continuous estimate of n! for large n: n! ≈ √(2πn) × (n/e)ⁿ. This remarkable formula, derived from the asymptotic expansion of the Gamma function, is accurate to within 1% for n ≥ 10 and becomes proportionally more precise as n increases. It is widely used in statistical mechanics, information theory, and asymptotic analysis where exact integer factorials are unnecessary. The approximation is especially central to deriving entropy formulas in thermodynamics via Boltzmann's equation.

Worked example

Suppose you want to calculate 10!.

Step 1 — Write out the product:
10! = 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1

Step 2 — Multiply sequentially:
10 × 9 = 90
90 × 8 = 720
720 × 7 = 5,040
5,040 × 6 = 30,240
30,240 × 5 = 151,200
151,200 × 4 = 604,800
604,800 × 3 = 1,814,400
1,814,400 × 2 = 3,628,800
3,628,800 × 1 = 3,628,800

Step 3 — Verify with Stirling's approximation:
10! ≈ √(2π × 10) × (10/e)¹⁰ = √(62.832) × (3.6788)¹⁰ ≈ 7.926 × 457,852 ≈ 3,628,684
This is within 0.003% of the exact value, demonstrating the accuracy of the approximation.

Step 4 — Compute log₁₀(10!):
log₁₀(3,628,800) = 6.5598, confirming the result has 7 digits.

Limitations & notes

Due to JavaScript's 64-bit floating-point number representation (IEEE 754 double precision), exact integer results are guaranteed only up to n = 18 (where 18! = 6,402,373,705,728,000, still within the safe integer range of 2⁵³ − 1 = 9,007,199,254,740,991). For n between 19 and approximately 170, the result is displayed but may lose precision in the last several significant digits due to floating-point rounding. For n ≥ 171, the exact factorial exceeds JavaScript's maximum finite number (~1.8 × 10³⁰⁸) and returns Infinity; in these cases, log₁₀(n!) and Stirling's approximation remain valid and useful. This calculator does not support negative integers or non-integer inputs, as the factorial function in its standard form is defined only on non-negative integers; the Gamma function generalizes factorials to real and complex numbers, but that is a separate computation.

Frequently asked questions

Why is 0! equal to 1?

The definition 0! = 1 is a convention that preserves the consistency of mathematical formulas. The most compelling justification is combinatorial: there is exactly one way to arrange zero objects (the empty arrangement), so the number of permutations of zero elements must be 1. It also ensures that the binomial coefficient formula C(n, 0) = n! / (0! × n!) = 1 remains valid, and it is consistent with the recursive definition (n+1)! = (n+1) × n! evaluated at n = 0.

What is the largest factorial this calculator can display exactly?

JavaScript's safe integer limit (Number.MAX_SAFE_INTEGER = 2⁵³ − 1 ≈ 9.007 × 10¹⁵) means exact integer representation is guaranteed only up to 18! = 6,402,373,705,728,000. For n from 19 to about 170, the calculator returns a floating-point approximation that may differ in its least significant digits. For n ≥ 171, the value overflows to Infinity, but the log₁₀(n!) output remains numerically accurate for any reasonable n.

How are factorials used in probability and combinatorics?

Factorials are foundational in counting problems. The number of ways to arrange n distinct objects in a sequence (permutations) is exactly n!. The binomial coefficient C(n, k) = n! / (k! × (n−k)!) counts the number of ways to choose k items from n without regard to order, and this formula underlies the binomial theorem, Pascal's triangle, and probability distributions such as the binomial and hypergeometric distributions. Multinomial coefficients generalize this to partitioning n objects into multiple groups.

What is Stirling's approximation and when should I use it?

Stirling's approximation states that n! ≈ √(2πn) × (n/e)ⁿ, and it is primarily used when working with very large factorials where exact computation is impractical. It is especially valuable in statistical mechanics (e.g., deriving the Boltzmann entropy formula), information theory (computing binomial entropy), and asymptotic analysis. The relative error of the approximation is approximately 1/(12n), so it is already within 0.8% at n = 10 and improves steadily for larger n.

Can factorials be computed for non-integer or negative values?

Yes, but not with the standard factorial function — instead, the Gamma function Γ(z) generalizes factorials to real and complex numbers via the identity n! = Γ(n+1). For example, (1/2)! = Γ(3/2) = √π/2 ≈ 0.8862. However, the Gamma function has poles (undefined values) at all non-positive integers, so factorials of negative integers remain undefined. This calculator handles only non-negative integers as inputs, consistent with the classical combinatorial definition.

Last updated: 2025-01-15 · Formula verified against primary sources.