Factorial Calculator

n!

Number of Digits

Expansion

What Is a Factorial? Definition and Formula

A factorial, denoted n!, is the product of all positive integers from 1 to n. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120. The exclamation mark notation was introduced by French mathematician Christian Kramp in 1808. By convention, 0! is defined as 1, which is not arbitrary -- it ensures consistency in combinatorial formulas and maintains the recursive relationship n! = n x (n-1)! since 1! = 1 x 0! requires 0! = 1. This calculator handles factorials for any non-negative integer up to 170 and displays both the exact result and the full multiplication chain.

Factorials grow at a staggering rate, faster than exponential functions. While 5! = 120, 10! = 3,628,800, 15! = 1,307,674,368,000, and 20! = 2,432,902,008,176,640,000 (over 2.4 quintillion). By 100!, the result has 158 digits, and 170! has 307 digits. This explosive growth means that 171! exceeds the maximum value representable by standard 64-bit floating-point numbers (approximately 1.8 x 10^308), which is why most calculators cap at 170!. For exact computations of larger factorials, arbitrary-precision arithmetic libraries like Python's built-in integers or Java's BigInteger class are required.

The Recursive Definition

The factorial function has an elegant recursive definition: n! = n x (n-1)! with the base case 0! = 1. This means you can compute any factorial by building on the previous one: 1! = 1, 2! = 2 x 1 = 2, 3! = 3 x 2 = 6, 4! = 4 x 6 = 24, and so on. This recursive structure makes the factorial a classic introductory example in computer science courses when teaching recursive functions, though iterative computation is more efficient in practice because it avoids the overhead of function call stacks.

The recursive definition also reveals useful relationships. The ratio of consecutive factorials is simply n!/((n-1)!) = n, which makes many algebraic simplifications possible. When dividing factorials in permutation and combination formulas, most terms cancel. For instance, 10! / 7! = 10 x 9 x 8 = 720, because the factors from 7 down to 1 appear in both numerator and denominator. This cancellation is crucial for practical computation, since computing full factorials and then dividing would overflow numerical limits far sooner than computing the simplified product directly.

Permutations: Ordered Arrangements

The number of ways to arrange n distinct objects in a specific order is exactly n!. If you have 6 books to place on a shelf, there are 6! = 720 possible arrangements. The first position can be filled by any of the 6 books, the second by any of the remaining 5, the third by any of the remaining 4, and so on, giving 6 x 5 x 4 x 3 x 2 x 1 = 720. This is the fundamental counting principle applied sequentially.

When you want to arrange only r objects out of n (an r-permutation), the formula is P(n, r) = n! / (n-r)!. For example, the number of ways to award gold, silver, and bronze medals to 3 of 10 runners is P(10, 3) = 10! / 7! = 10 x 9 x 8 = 720. For permutations with repetition -- such as the number of distinct arrangements of the letters in "MISSISSIPPI" -- the formula is n! divided by the product of the factorials of the frequencies of each repeated element: 11! / (4! x 4! x 2!) = 34,650.

Combinations: Unordered Selections

The binomial coefficient, written as C(n, k) or "n choose k," counts the number of ways to select k items from n without regard to order: C(n, k) = n! / (k! x (n-k)!). For example, the number of ways to choose a committee of 3 people from a group of 10 is C(10, 3) = 10! / (3! x 7!) = 120. Unlike permutations, the order does not matter -- choosing Alice, Bob, and Carol is the same committee as choosing Carol, Alice, and Bob.

Binomial coefficients appear as the entries in Pascal's triangle, where each entry is the sum of the two entries above it. They are the coefficients in the binomial expansion: (a + b)^n = sum of C(n, k) x a^(n-k) x b^k for k = 0 to n. This connection between combinatorics and algebra is one of the most beautiful results in mathematics. Binomial coefficients also count the number of paths on a grid, the number of subsets of a given size, and the coefficients in polynomial multiplication.

Factorials in Probability and Statistics

Many probability distributions rely on factorials. The binomial distribution uses C(n, k) = n! / (k!(n-k)!) to compute the probability of getting exactly k successes in n independent trials. The Poisson distribution, which models rare events, uses k! in its probability formula: P(X = k) = (lambda^k x e^(-lambda)) / k!. The multinomial distribution, which generalizes the binomial to multiple categories, involves a ratio of factorials: n! / (n1! x n2! x ... x nk!), where the ni values are the counts in each category.

In Bayesian statistics, the beta function B(a, b) = (a-1)! x (b-1)! / (a+b-1)! (for integer arguments) is used as the normalizing constant for the beta distribution. In information theory, the entropy of a discrete uniform distribution over n outcomes is log(n!)/n for the multinomial model. The factorial moment of a random variable, defined as E[X(X-1)(X-2)...(X-k+1)], simplifies many calculations for discrete distributions and is closely related to the regular factorial function.

Stirling's Approximation for Large Factorials

For very large values of n, computing n! exactly is impractical, and Stirling's approximation provides an excellent estimate: n! is approximately sqrt(2 x pi x n) x (n/e)^n. The relative error decreases as n increases, falling below 1% for n > 10 and below 0.1% for n > 100. A more precise version adds correction terms: n! is approximately sqrt(2*pi*n) x (n/e)^n x (1 + 1/(12n) + 1/(288n^2) - 139/(51840n^3) + ...). Each additional term improves accuracy.

Stirling's approximation is widely used in statistical mechanics, where the logarithm of factorial -- log(n!) -- appears in entropy calculations. Using Stirling's approximation, log(n!) is approximately n*log(n) - n + 0.5*log(2*pi*n). This simplification transforms products into sums and makes analytical derivations tractable. It is essential for deriving the Maxwell-Boltzmann distribution, computing partition functions, and analyzing the thermodynamic limit in physical systems. In algorithm analysis, Stirling's approximation proves that log(n!) = O(n log n), which establishes the lower bound for comparison-based sorting algorithms.

The Gamma Function: Factorial for Non-Integers

The gamma function extends the factorial to non-integer and even complex arguments. For positive integers, Gamma(n) = (n-1)!, so Gamma(6) = 5! = 120. For non-integers, the gamma function is defined by the integral Gamma(x) = integral from 0 to infinity of t^(x-1) x e^(-t) dt. This gives meaningful results like Gamma(1/2) = sqrt(pi), which means (1/2)! = sqrt(pi)/2 in the generalized sense. The gamma function is defined for all complex numbers except non-positive integers, where it has poles.

The gamma function appears throughout advanced mathematics and physics. It is used in the probability density functions of the gamma, chi-square, beta, and Student's t distributions. In quantum field theory, gamma function evaluations arise in dimensional regularization. In number theory, the gamma function appears in the functional equation of the Riemann zeta function, connecting it to deep questions about the distribution of prime numbers. The digamma function (the logarithmic derivative of the gamma function) and the polygamma functions are also important in many applications.

Factorials in Computer Science

In algorithm analysis, factorials quantify the running time of brute-force algorithms. The traveling salesman problem, which seeks the shortest route visiting n cities, has (n-1)!/2 possible routes for undirected graphs, making brute-force search impractical for even moderate n. Algorithms with O(n!) time complexity are classified as factorial time and are generally considered intractable. This is why researchers develop approximation algorithms, heuristics, and dynamic programming solutions for problems whose naive solutions involve factorial time.

Factorials also appear in the analysis of sorting algorithms. The number of possible orderings of n elements is n!, and since a comparison-based sorting algorithm must distinguish among all n! orderings, it needs at least log2(n!) comparisons in the worst case. By Stirling's approximation, this lower bound is approximately n*log2(n) - n*log2(e), proving that no comparison-based sorting algorithm can do better than O(n log n) in the worst case. Merge sort and heapsort achieve this bound, making them asymptotically optimal.

Common Factorial Values Reference Table

For quick reference, here are the first twenty factorial values: 0! = 1, 1! = 1, 2! = 2, 3! = 6, 4! = 24, 5! = 120, 6! = 720, 7! = 5,040, 8! = 40,320, 9! = 362,880, 10! = 3,628,800, 11! = 39,916,800, 12! = 479,001,600, 13! = 6,227,020,800, 14! = 87,178,291,200, 15! = 1,307,674,368,000, 16! = 20,922,789,888,000, 17! = 355,687,428,096,000, 18! = 6,402,373,705,728,000, 19! = 121,645,100,408,832,000, and 20! = 2,432,902,008,176,640,000. Notice that 13! is the first factorial to exceed the maximum value of a 32-bit signed integer (about 2.1 billion), and 21! exceeds the maximum of a 64-bit signed integer (about 9.2 x 10^18).

Frequently Asked Questions

Why is 0 factorial equal to 1?

0! = 1 by definition, and this is not arbitrary. It ensures consistency in combinatorial formulas: there is exactly 1 way to arrange zero objects (do nothing). It also maintains the recursive relationship n! = n x (n-1)!, since 1! = 1 x 0! requires 0! = 1.

What is the largest factorial a calculator can compute?

Standard 64-bit floating-point numbers can represent factorials up to 170! (approximately 7.26 x 10^306). 171! exceeds the maximum representable value and returns infinity. For exact results with larger factorials, arbitrary-precision arithmetic libraries are required.

What is the difference between factorial and permutation?

A factorial n! counts the total number of ways to arrange all n distinct objects. A permutation P(n,r) = n!/(n-r)! counts the number of ways to arrange r objects chosen from n. When r = n, the permutation equals n! because you are arranging all objects.

Can you take the factorial of a non-integer?

Yes, the gamma function extends the factorial to non-integers: for any positive real number x, Gamma(x) = (x-1)!. This gives meaningful values like (1/2)! = sqrt(pi)/2. The gamma function is defined for all complex numbers except non-positive integers.

How is Stirling's approximation used for large factorials?

Stirling's approximation estimates n! as sqrt(2 * pi * n) * (n/e)^n. The relative error drops below 1% for n > 10 and below 0.1% for n > 100. It is widely used in statistical mechanics for entropy calculations, in algorithm analysis to prove the O(n log n) lower bound for comparison-based sorting, and whenever exact computation of very large factorials is impractical. Taking the logarithm gives the even more useful form: log(n!) is approximately n*log(n) - n.

What are some real-world applications of factorials?

Factorials appear in many practical contexts. In probability, the binomial coefficient C(n,k) = n!/(k!(n-k)!) calculates lottery odds and card-game probabilities. In computer science, n! represents the number of permutations of n items, giving the brute-force complexity of problems like the traveling salesman (which has (n-1)!/2 routes). In Taylor series, factorials appear in denominators: e^x = sum of x^n/n!. Even in everyday life, the number of ways to arrange 10 books on a shelf is 10! = 3,628,800, and the number of possible shuffles of a 52-card deck is 52! -- a number with 68 digits, larger than the estimated number of atoms in the Milky Way galaxy.

Related Calculators