Skip to main content
🟢 Beginner

Factorial Calculator

Calculate the factorial of any non-negative integer. n! = n × (n-1) × … × 2 × 1. This free online math calculator gives you instant step-by-step results.

★★★★★ 4.8/5 · 📊 0 calculations · 🔒 Private & free

Understanding Factorials

The factorial of a non-negative integer n, written n!, is the product of all positive integers from 1 to n. The definition: n! = n × (n−1) × (n−2) × ... × 2 × 1. Special case: 0! = 1 by definition (not calculation) — this is required for combinatorial formulas to work consistently.

Factorials grow extraordinarily fast — faster than any polynomial or even most exponential functions. 5! = 120; 10! = 3,628,800; 15! = 1,307,674,368,000; 20! ≈ 2.43 × 10^18; 100! ≈ 9.33 × 10^157. The number 170! is approximately 7.26 × 10^306, which is the largest factorial representable as a 64-bit floating-point number (double precision). Our calculator uses BigInt arithmetic for exact integer results up to 170!

The recursive definition of factorial is: n! = n × (n−1)! for n > 0, with 0! = 1 as the base case. This recursive structure makes factorial a classic introductory example in computer science for teaching recursion, dynamic programming, and memoization. Factorial computation via iteration is also standard: initialize result = 1, then multiply by each integer from 2 to n.

The notation "n!" was introduced by Christian Kramp in 1808 as a convenient shorthand for the frequently occurring product 1 × 2 × 3 × ... × n. Before this, various other notations were used. Today n! is universally recognized across all mathematical traditions.

Factorials in Combinatorics and Probability

Factorial is the cornerstone of combinatorics — the branch of mathematics dealing with counting, arrangements, and selections. Virtually every counting problem in probability and statistics ultimately involves factorials.

Permutations (ordered arrangements): The number of ways to arrange n distinct objects in a row is n! — called n-factorial permutations. With 4 books on a shelf: 4! = 24 arrangements. With 10 runners in a race, the number of possible orderings for 1st, 2nd, and 3rd place is P(10,3) = 10!/(10−3)! = 10!/7! = 720.

The partial permutation formula: P(n,r) = n!/(n−r)! counts ordered selections of r items from n. The total permutations formula n! is the special case r = n.

Combinations (unordered selections): C(n,r) = n!/(r!(n−r)!), also written ⁿCᵣ or "n choose r" or as the binomial coefficient. This counts the number of ways to select r items from n where order doesn't matter. From 52 cards, the number of 5-card hands = C(52,5) = 52!/(5!×47!) = 2,598,960. The probability of being dealt a royal flush = 4/2,598,960 ≈ 0.000154%.

Multinomial coefficients: n!/(n₁! × n₂! × ... × nₖ!) counts arrangements of n items where n₁ are of type 1, n₂ of type 2, etc. Arranging the letters in MISSISSIPPI: 11!/(1!×4!×4!×2!) = 34,650 distinct arrangements.

FormulaExpressionExample
n! (all arrangements)n × (n−1) × ... × 15! = 120
P(n,r) permutationsn! / (n−r)!P(10,3) = 720
C(n,r) combinationsn! / (r!(n−r)!)C(10,3) = 120
Multinomialn! / (n₁! n₂! ... nₖ!)MISS: 4!/(1!3!) = 4

The Factorial Table: n! for n = 0 to 20

Here is the complete factorial table for small values of n. Memorizing the first 10 factorials is useful for quick mental combinatorics calculations.

nn!Approximate
011
111
222
366
42424
5120120
6720720
75,0405 thousand
840,32040 thousand
9362,880363 thousand
103,628,8003.6 million
12479,001,600479 million
151,307,674,368,0001.3 trillion
202,432,902,008,176,640,0002.4 × 10^18

The explosive growth is striking: from 10! = 3.6 million to 20! = 2.4 quintillion in just 10 steps. This rapid growth is why factorial appears in the denominators of Taylor series (ensuring convergence) and in normalization factors of probability distributions.

Stirling's Approximation and Large Factorials

For large n, computing exact factorials is impractical — 100! has 158 digits. Stirling's approximation provides an excellent estimate: n! ≈ √(2πn) × (n/e)^n, where e ≈ 2.71828 is Euler's number.

Accuracy of Stirling's approximation: for n=10, exact = 3,628,800; Stirling gives ≈ 3,598,696, an error of less than 1%. For n=100, the relative error is under 0.1%. The larger n, the more accurate the approximation becomes — the approximation's relative error is O(1/n).

The log-factorial ln(n!) = Σ ln(k) for k=1 to n (sum of logarithms) is computationally important. In statistics and machine learning, log-probabilities are used instead of raw probabilities to avoid numerical underflow (multiplying many small numbers together rapidly underflows to 0 in floating-point arithmetic). The log-gamma function extends this to non-integer arguments.

The Gamma function Γ(n) is the continuous extension of factorial to all complex numbers except non-positive integers: Γ(n) = (n−1)! for positive integers. This appears in probability distributions (Gamma distribution, Chi-squared distribution, Beta distribution) and many physics formulas. Some calculators can compute Γ(1.5) = √π/2 ≈ 0.886 — the "factorial" of 0.5.

Factorials in Probability Distributions

Many of the most important probability distributions in statistics use factorials in their formulas, connecting pure combinatorics to real-world data analysis.

Binomial distribution: P(X = k) = C(n,k) × p^k × (1−p)^(n−k). Models the number of successes in n independent trials with probability p each. C(n,k) = n!/(k!(n−k)!) is the combinatorial coefficient. The sum over all k of these terms equals 1 (total probability).

Poisson distribution: P(X = k) = (λ^k × e^(−λ)) / k!. Models the number of rare events occurring in a fixed interval when the average rate is λ. The k! in the denominator normalizes the distribution. Used for: hospital arrivals per hour, insurance claims per day, mutations per genome replication.

Normal distribution and Stirling's approximation are deeply connected. The Central Limit Theorem — that sums of independent random variables approach a normal distribution — can be proved using Stirling's approximation of n!. This connection between the discrete (factorial) and continuous (normal distribution) worlds is one of the deepest results in probability theory.

Birthday problem: The probability that all 23 people in a room have different birthdays = 365!/(365−23)! ÷ 365^23 ≈ 49.3%. So there's a greater than 50% chance that at least two share a birthday — a famous counterintuitive result that uses partial permutations.

Factorial in Number Theory: Trailing Zeros and Wilson's Theorem

Factorial interacts richly with prime number theory, yielding elegant results about divisibility and prime detection.

Trailing zeros in n!: The number of trailing zeros in n! equals the number of factors of 10, which equals the number of factors of 5 (since factors of 2 are always more abundant). The formula: count = ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + ... (sum while power of 5 ≤ n). For 100!: ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24 trailing zeros.

Legendre's formula: The highest power of prime p dividing n! is ⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + ... This tells you the exact prime factorization of n!, which is essential in number theory and combinatorics.

Wilson's theorem: An integer p > 1 is prime if and only if (p−1)! ≡ −1 (mod p). For p=5: 4! = 24 ≡ 4 ≡ −1 (mod 5) ✓. For p=6 (composite): 5! = 120 ≡ 0 (mod 6) ✗. While Wilson's theorem is beautiful theoretically, it's computationally impractical for large numbers since computing (p−1)! is exponentially expensive.

Factorial primes: A factorial prime is a prime of the form n! + 1 or n! − 1. Examples: 2! − 1 = 1 (not prime), 3! − 1 = 5 (prime), 3! + 1 = 7 (prime), 4! − 1 = 23 (prime). Finding large factorial primes is an active research area in recreational and computational number theory.

How to Use This Factorial Calculator

Enter a non-negative integer n (from 0 to 170) and click Calculate. The calculator returns the exact factorial value as a full integer using JavaScript's BigInt for large values, avoiding floating-point imprecision that would corrupt results for n ≥ 19.

Notes:

Frequently Asked Questions

Why is 0! = 1?

By definition and mathematical convention: 0! = 1 ensures combinatorial formulas work consistently. C(n,0) = n!/(0! × n!) = 1, meaning there's exactly 1 way to choose 0 items (do nothing). Without this definition, every formula using C(n,0) would need a special case. The empty product convention (product of zero terms = 1) provides the same rationale.

What is the factorial of a negative number?

Factorial is undefined for negative integers. The recursive relation n! = n × (n−1)! would give 0! = 1/(−1)! = 0! at n=0, but 0! = 1, and (−1)! would be 1/(0!) = 1, and (−2)! = 1/((−1)×(−1)!) = undefined (division by zero at n=0). The Gamma function extends factorial to positive real numbers but has poles (undefined singularities) at all non-positive integers.

How many zeros are at the end of 100!?

24 trailing zeros. Count factors of 5: ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24. (There's no ⌊100/125⌋ term since 125 > 100.) Since factors of 2 always outnumber factors of 5, the number of trailing zeros equals the count of 5s in the prime factorization of n!.

What is the largest factorial a calculator can compute?

Standard floating-point calculators max out around 170! (≈ 7.26 × 10^306, just within IEEE 754 double-precision range). Beyond 170!, floating-point gives Infinity. Our calculator uses JavaScript BigInt for exact integer computation up to 170!, displaying the full digit string without any approximation.

How are factorials used in probability?

Factorials underlie permutations P(n,r) = n!/(n−r)! and combinations C(n,r) = n!/(r!(n−r)!). These count the number of ways to arrange or select items, forming the basis of probability calculations. They appear in the binomial distribution, Poisson distribution, and many other statistical formulas.

What is Stirling's approximation?

Stirling's approximation: n! ≈ √(2πn) × (n/e)^n. For n=10: exact = 3,628,800; Stirling gives ≈ 3,598,696 (error <1%). For n=100: error <0.1%. The log form: ln(n!) ≈ n×ln(n) − n + ½×ln(2πn) is invaluable in statistics for working with log-probabilities without computing enormous factorials.

What is the connection between factorial and the Gamma function?

The Gamma function Γ(n) satisfies Γ(n) = (n−1)! for positive integers. This extends factorial to all complex numbers (except non-positive integers). Γ(1/2) = √π ≈ 1.7725, so we can say (−1/2)! = √π by convention. The Gamma function appears in probability distributions (Gamma, Beta, Chi-squared), signal processing, and quantum mechanics.

How does factorial relate to Pascal's triangle?

Every entry in Pascal's triangle is a binomial coefficient C(n,r) = n!/(r!(n−r)!). Row n of Pascal's triangle contains C(n,0), C(n,1), ..., C(n,n). Each entry is the sum of the two entries above it (Pascal's rule: C(n,r) = C(n−1,r−1) + C(n−1,r)), which can be verified from the factorial formula. Pascal's triangle encodes combinatorial counting using factorials.

What is Wilson's theorem?

Wilson's theorem: p is prime if and only if (p−1)! ≡ −1 (mod p). For p=7: 6! = 720 = 102×7 + 6 ≡ 6 ≡ −1 (mod 7) ✓. For p=8 (composite): 7! = 5040 = 630×8 + 0 ≡ 0 ≢ −1 (mod 8) ✓. Beautiful theoretically, but impractical for primality testing since computing (p−1)! for large p is computationally prohibitive.

What does n! represent in the number of arrangements?

n! is the number of distinct ways to arrange n unique items in a line (a permutation). With 3 items {A, B, C}: 3! = 6 arrangements: ABC, ACB, BAC, BCA, CAB, CBA. With 10 items: 10! = 3,628,800 arrangements — over 3 million orderings of just 10 things. With 52 cards: 52! ≈ 8 × 10^67, an astronomically large number demonstrating why shuffled card decks are essentially never in the same order twice in history.

Factorials in Computer Science: Algorithms and Complexity

Factorial is intimately connected to computational complexity theory — the study of how difficult problems are to solve algorithmically. Understanding factorial helps explain why certain problems are "hard" in a precise mathematical sense.

The Traveling Salesman Problem (TSP) asks: given n cities and distances between each pair, find the shortest route visiting all cities exactly once. A naive brute-force approach checks all possible orderings: (n−1)!/2 routes (dividing by 2 for symmetry, fixing the starting city). For n=20 cities: 19!/2 ≈ 6 × 10^16 routes. Even at 1 trillion routes/second, this would take 60,000+ years. This factorial explosion is why TSP is "NP-hard" and why heuristic algorithms (instead of exact solutions) are used in practice for large instances.

The sorting problem has factorial connections too: n! is the number of possible orderings of n elements. An optimal comparison-based sorting algorithm must distinguish between all n! cases, requiring at least log₂(n!) comparisons. By Stirling's approximation, log₂(n!) ≈ n×log₂(n), which is why the theoretical minimum comparisons for sorting is O(n log n) — achieved by merge sort and heap sort.

In dynamic programming, factorial sub-problems can be memoized: once you compute k!, you can get (k+1)! = (k+1) × k! without recomputing from scratch. This reduces the cost of computing all factorials from 1 to n from O(n²) to O(n), a key optimization in applications requiring many factorial values such as probability table generation.

},{"@type":"Question","name":"What is the factorial of a negative number?","acceptedAnswer":{"@type":"Answer","text":"Factorial is not defined for negative integers. The gamma function extends factorial to positive real numbers but has singularities at negative integers."}},{"@type":"Question","name":"How many zeros are at the end of 100!?","acceptedAnswer":{"@type":"Answer","text":"24 trailing zeros. Count factors of 5: ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24."}},{"@type":"Question","name":"What is the largest factorial a calculator can compute?","acceptedAnswer":{"@type":"Answer","text":"Standard floating-point maxes around 170!. Our calculator uses BigInt for exact results up to 170!."}},{"@type":"Question","name":"How are factorials used in probability?","acceptedAnswer":{"@type":"Answer","text":"Factorials underlie permutations P(n,r) = n!/(n-r)! and combinations C(n,r) = n!/(r!(n-r)!), foundational to all counting problems in probability."}},{"@type":"Question","name":"What is Stirling's approximation?","acceptedAnswer":{"@type":"Answer","text":"n! ≈ √(2πn) × (n/e)^n. Accurate to < 1% for n ≥ 10, invaluable for large factorial estimation."}},{"@type":"Question","name":"What is the Gamma function?","acceptedAnswer":{"@type":"Answer","text":"Γ(n) = (n-1)! for positive integers. It extends factorial to all complex numbers except non-positive integers."}},{"@type":"Question","name":"How does factorial relate to Pascal's triangle?","acceptedAnswer":{"@type":"Answer","text":"Every entry in Pascal's triangle is C(n,r) = n!/(r!(n-r)!). Pascal's triangle encodes combinatorial counting via factorials."}},{"@type":"Question","name":"What is Wilson's theorem?","acceptedAnswer":{"@type":"Answer","text":"p is prime iff (p-1)! ≡ -1 (mod p). Beautiful theoretically but impractical for large primality testing."}},{"@type":"Question","name":"What does n! represent in arrangements?","acceptedAnswer":{"@type":"Answer","text":"n! is the number of distinct ways to arrange n unique items in a line. 52! ≈ 8 × 10^67 — why shuffled decks are never repeated in history."}}]}