CalcTune
📐
Math · Arithmetic

Factorial Calculator

Calculate the factorial of any number from 0 to 170. See the result, digit count, computation steps, double factorial, and subfactorial (derangements).

Example values — enter yours above
Factorial (n!)
120
Computation Steps
5! = 5 × 4 × 3 × 2 × 1 = 120
3 digits
Number of Digits
15
Double Factorial (n!!)
44
Subfactorial (!n)
n!! = Double Factorial (n!!) | !n = Subfactorial (!n) (44 derangements)

Understanding Factorials: A Complete Guide to n!, Double Factorials, and Subfactorials

The factorial is one of the most fundamental operations in mathematics. Written as n! (read "n factorial"), it represents the product of all positive integers from 1 up to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. Despite its simple definition, the factorial function appears in an enormous range of mathematical disciplines—from basic combinatorics and probability to advanced topics in analysis, algebra, and number theory. Understanding factorials is essential for anyone studying mathematics, computer science, statistics, or physics.

This guide covers the definition and properties of factorials, related functions such as the double factorial and subfactorial, practical applications, and computational considerations when working with very large values.

Definition and Basic Properties

The factorial of a non-negative integer n is defined recursively as: n! = n × (n-1)! with the base case 0! = 1. This base case is not arbitrary—it follows from the convention that an empty product (a product of zero factors) equals 1, which maintains consistency in combinatorial formulas. For instance, the number of ways to arrange 0 objects is exactly 1: do nothing.

Factorials grow at a super-exponential rate. While 10! is about 3.6 million, 20! exceeds 2.4 quintillion (2.4 × 10^18), and 100! is a number with 158 digits. This rapid growth means that even modest inputs produce astronomically large results. In JavaScript, the largest factorial that fits in a standard 64-bit floating-point Number without loss of precision is 170! (which has 307 digits). Beyond that, BigInt or arbitrary-precision libraries are required.

Stirling's Approximation

Because factorials grow so quickly, it is often useful to have an approximation. Stirling's approximation states that for large n, n! is approximately equal to the square root of 2 times pi times n, multiplied by (n/e) raised to the nth power. In formula form: n! ≈ sqrt(2*pi*n) × (n/e)^n. This approximation becomes increasingly accurate as n grows and is widely used in statistical mechanics, information theory, and asymptotic analysis.

For practical purposes, Stirling's formula lets you estimate the number of digits in n! without computing the full value. The number of digits is approximately n × log10(n/e) + 0.5 × log10(2*pi*n). This is useful when you need to know the order of magnitude of a factorial without performing the full multiplication.

Double Factorial (n!!)

The double factorial of a non-negative integer n, written n!!, is the product of all positive integers up to n that have the same parity (odd or even) as n. For even n: n!! = n × (n-2) × (n-4) × ... × 4 × 2. For odd n: n!! = n × (n-2) × (n-4) × ... × 3 × 1. By convention, 0!! = 1 and 1!! = 1.

Double factorials appear in various mathematical formulas, particularly in combinatorics and in the evaluation of certain integrals. For example, the Wallis product for pi involves double factorials, and they arise naturally when computing volumes of n-dimensional spheres. The relationship between the double factorial and the ordinary factorial is: for even n = 2k, (2k)!! = 2^k × k!; for odd n = 2k+1, (2k+1)!! = (2k+1)! / (2^k × k!).

Subfactorial and Derangements (!n)

The subfactorial of n, written !n, counts the number of derangements of n elements—that is, the number of permutations where no element appears in its original position. For example, !3 = 2: the derangements of {1, 2, 3} are {2, 3, 1} and {3, 1, 2}.

The subfactorial can be computed using the recurrence relation: !0 = 1, !1 = 0, and !n = (n-1) × (!(n-1) + !(n-2)) for n >= 2. Alternatively, it can be expressed as !n = n! × sum from k=0 to n of (-1)^k / k!. As n grows large, the ratio !n / n! approaches 1/e ≈ 0.3679. This means that roughly 36.79% of all permutations of a large set are derangements.

Derangements have practical applications in probability theory (the hat-check problem), cryptography, and combinatorial analysis. The classic hat-check problem asks: if n people each place their hat in a pile and then each randomly selects a hat, what is the probability that no one gets their own hat? The answer is !n / n!, which approaches 1/e.

Applications of Factorials

Factorials are ubiquitous in mathematics. In combinatorics, the number of ways to arrange n distinct objects is n! (permutations). The binomial coefficient, C(n, k) = n! / (k! × (n-k)!), counts the number of ways to choose k items from n, and is fundamental to probability theory and the binomial theorem. Factorials also appear in the Taylor series expansions of exponential, trigonometric, and other functions—for example, e^x = sum of x^n / n! for n from 0 to infinity.

In computer science, factorials arise in the analysis of algorithms. The number of possible orderings of n items is n!, which defines the theoretical lower bound for comparison-based sorting algorithms. Problems with factorial time complexity—O(n!)—are computationally intractable for even moderately large n, which is why efficient algorithms and heuristics are so important in optimization and operations research.

In probability and statistics, factorials underpin the calculation of permutations, combinations, and probability distributions such as the Poisson distribution and the multinomial distribution. They are also central to Bayesian inference and the computation of likelihoods.

Gamma Function: Extending Factorials

The factorial function is defined only for non-negative integers, but the gamma function extends it to real and complex numbers. The gamma function satisfies Gamma(n+1) = n! for non-negative integers n, and is defined for all complex numbers except non-positive integers. This extension is important in advanced mathematics, physics, and engineering, where factorial-like computations arise for non-integer values.

For example, Gamma(1/2) = sqrt(pi), which means that (−1/2)! = sqrt(pi) in a generalized sense. The gamma function appears in the formulas for the surface area and volume of n-dimensional spheres, in probability distributions (the gamma and beta distributions), and in many areas of theoretical physics.

Computational Considerations

Computing factorials for small values is straightforward, but for large values, the numbers become enormous. This calculator supports values from 0 to 170 and uses BigInt for exact integer arithmetic, displaying the full result as a string. Beyond n = 170, the result exceeds the range of standard 64-bit floating-point numbers, though BigInt can handle arbitrarily large integers.

For applications that require very large factorials—such as combinatorial computations involving millions of elements—logarithmic representations (log(n!)) or Stirling's approximation are preferred over computing the exact value. Many programming languages and mathematical libraries provide efficient implementations of the log-gamma function for this purpose.

Frequently Asked Questions

What is a factorial?

A factorial, written as n!, is the product of all positive integers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. By convention, 0! = 1. Factorials are fundamental in combinatorics, probability, and many areas of mathematics.

Why is 0 factorial equal to 1?

0! = 1 by convention, based on the mathematical concept of the empty product—a product of zero factors equals 1. This convention ensures consistency in formulas like the binomial coefficient C(n, 0) = n! / (0! × n!) = 1, which correctly states there is exactly one way to choose zero items from n items.

What is the largest factorial this calculator can compute?

This calculator supports factorials from 0! to 170!. The value 170! has 307 digits and is the largest factorial that can be represented as a standard JavaScript Number. The calculator uses BigInt to display the exact result. Beyond 170, the values exceed standard floating-point representation.

What is a double factorial (n!!)?

The double factorial n!! is the product of all positive integers up to n that share the same parity. For odd n: n!! = n × (n-2) × ... × 3 × 1. For even n: n!! = n × (n-2) × ... × 4 × 2. For example, 7!! = 7 × 5 × 3 × 1 = 105, and 6!! = 6 × 4 × 2 = 48.

What is a subfactorial (!n) and what are derangements?

The subfactorial !n counts the number of derangements of n elements—permutations where no element remains in its original position. For example, !3 = 2 because {2,3,1} and {3,1,2} are the only derangements of {1,2,3}. As n grows, approximately 36.8% of all permutations are derangements (approaching 1/e).