structural:reformatting
Applications

The earliest uses of the factorial function involve counting permutations: there are n ! {\displaystyle n!} different ways of arranging n {\displaystyle n} distinct objects into a sequence. Factorials appear more broadly in many formulas in combinatorics, to account for different orderings of objects. For instance the binomial coefficients (n k) {\displaystyle {\tbinom {n}{k}}} count the k {\displaystyle k} -element combinations (subsets of k {\displaystyle k} elements) from a set with n {\displaystyle n} elements, and can be computed from factorials using the formula (n k) = n ! k ! (n − k) !. {\displaystyle {\binom {n}{k}}={\frac {n!}{k!(n-k)!}}.} The Stirling numbers of the first kind sum to the factorials, and count the permutations of n {\displaystyle n} grouped into subsets with the same numbers of cycles. Another combinatorial application is in counting derangements, permutations that do not leave any element in its original position; the number of derangements of n {\displaystyle n} items is the nearest integer to n ! / e {\displaystyle n!/e}.

In algebra, the factorials arise through the binomial theorem, which uses binomial coefficients to expand powers of sums. They also occur in the coefficients used to relate certain families of polynomials to each other, for instance in Newton's identities for symmetric polynomials. Their use in counting permutations can also be restated algebraically: the factorials are the orders of finite symmetric groups. In calculus, factorials occur in Faà di Bruno's formula for chaining higher derivatives. In mathematical analysis, factorials frequently appear in the denominators of power series, most notably in the series for the exponential function, e x = 1 + x 1 + x 2 2 + x 3 6 + ⋯ = ∑ i = 0 ∞ x i i !, {\displaystyle e^{x}=1+{\frac {x}{1}}+{\frac {x^{2}}{2}}+{\frac {x^{3}}{6}}+\cdots =\sum _{i=0}^{\infty }{\frac {x^{i}}{i!}},} and in the coefficients of other Taylor series (in particular those of the trigonometric and hyperbolic functions), where they cancel factors of n ! {\displaystyle n!} coming from the n {\displaystyle n} th derivative of x n {\displaystyle x^{n}}. This usage of factorials in power series connects back to analytic combinatorics through the exponential generating function, which for a combinatorial class with n i {\displaystyle n_{i}} elements of size i {\displaystyle i} is defined as the power series ∑ i = 0 ∞ x i n i i !. {\displaystyle \sum _{i=0}^{\infty }{\frac {x^{i}n_{i}}{i!}}.}

In number theory, the most salient property of factorials is the divisibility of n ! {\displaystyle n!} by all positive integers up to n {\displaystyle n}, described more precisely for prime factors by Legendre's formula. It follows that arbitrarily large prime numbers can be found as the prime factors of the numbers n ! ± 1 {\displaystyle n!\pm 1}, leading to a proof of Euclid's theorem that the number of primes is infinite. When n ! ± 1 {\displaystyle n!\pm 1} is itself prime it is called a factorial prime; relatedly, Brocard's problem, also posed by Srinivasa Ramanujan, concerns the existence of square numbers of the form n ! + 1 {\displaystyle n!+1}. In contrast, the numbers n ! + 2, n ! + 3, … n ! + n {\displaystyle n!+2,n!+3,\dots n!+n} must all be composite, proving the existence of arbitrarily large prime gaps. An elementary proof of Bertrand's postulate on the existence of a prime in any interval of the form {\displaystyle }, one of the first results of Paul Erdős, was based on the divisibility properties of factorials. The factorial number system is a mixed radix notation for numbers in which the place values of each digit are factorials.

Factorials are used extensively in probability theory, for instance in the Poisson distribution and in the probabilities of random permutations. In computer science, beyond appearing in the analysis of brute-force searches over permutations, factorials arise in the lower bound of log 2 n ! = n log 2 n − O (n) {\displaystyle \log _{2}n!=n\log _{2}n-O(n)} on the number of comparisons needed to comparison sort a set of n {\displaystyle n} items, and in the analysis of chained hash tables, where the distribution of keys per cell can be accurately approximated by a Poisson distribution. Moreover, factorials naturally appear in formulae from quantum and statistical physics, where one often considers all the possible permutations of a set of particles. In statistical mechanics, calculations of entropy such as Boltzmann's entropy formula or the Sackur–Tetrode equation must correct the count of microstates by dividing by the factorials of the numbers of each type of indistinguishable particle to avoid the Gibbs paradox. Quantum physics provides the underlying reason for why these corrections are necessary.