{| class="wikitable" style="margin:0 0 0 1em; text-align:right; float:right;"
|+ Selected factorials; values in scientific notation are rounded
|-
! <math>n</math>
! <math>n!</math>
|-
| 0 || 1
|-
| 1 || 1
|-
| 2 || 2
|-
| 3 || 6
|-
| 4 || 24
|-
| 5 || 120
|-
| 6 || 720
|-
| 7 ||
|-
| 8 ||
|-
| 9 ||
|-
| 10 ||
|-
| 11 ||
|-
| 12 ||
|-
| 13 ||
|-
| 14 ||
|-
| 15 ||
|-
| 16 ||
|-
| 17 ||
|-
| 18 ||
|-
| 19 ||
|-
| 20 ||
|-
| 25
| style="text-align:left" |
|-
| 50
| style="text-align:left" |
|-
| 52
| style="text-align:left" |
|-
| 70
| style="text-align:left" |
|-
| 100
| style="text-align:left" |
|-
| 450
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
|
| style="text-align:left" |
|-
| <br />=
| style="text-align:right" | <br/>≈ 10<sup></sup>
|-
| || 10<sup></sup>
|-
| || 10<sup></sup>
|-
| || 10<sup></sup>
|-
| googol| || 10<sup></sup>
|-
| || 10<sup></sup>
|}
In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of <math>n</math> with the next smaller factorial:
<math display="block">
\begin{align}
n! &= n \times (n-1) \times (n-2) \times (n-3) \times \cdots \times 3 \times 2 \times 1 \\
&= \begin{cases}
1, & \text{if } n = 0 \\
n \times (n-1)!, & \text{if } n \ge 1.
\end{cases}\\
\end{align}</math>
For example,
<math display="block">5! = 5\times 4! = 5 \times 4 \times 3 \times 2 \times 1 = 120.</math>
The value of 0! is 1, according to the convention for an empty product.
Factorials have been discovered in several ancient cultures, notably in Indian mathematics in the canonical works of Jain literature, and by Jewish mystics in the Talmudic book Sefer Yetzirah. The factorial operation is encountered in many areas of mathematics, notably in combinatorics, where its most basic use counts the possible distinct sequences – the permutations – of <math>n</math> distinct objects: there In mathematical analysis, factorials are used in power series for the exponential function and other functions, and they also have applications in algebra, number theory, probability theory, and computer science.
Much of the mathematics of the factorial function was developed beginning in the late 18th and early 19th centuries.
Stirling's approximation provides an accurate approximation to the factorial of large numbers, showing that it grows more quickly than exponential growth. Legendre's formula describes the exponents of the prime numbers in a prime factorization of the factorials, and can be used to count the trailing zeros of the factorials. Daniel Bernoulli and Leonhard Euler interpolated the factorial function to a continuous function of complex numbers, except at the negative integers, the (offset) gamma function.
Many other notable functions and number sequences are closely related to the factorials, including the binomial coefficients, double factorials, falling factorials, primorials, and subfactorials. Implementations of the factorial function are commonly used as an example of different computer programming styles, and are included in scientific calculators and scientific computing software libraries. Although directly computing large factorials using the product formula or recurrence is not efficient, faster algorithms are known, matching to within a constant factor the time for fast multiplication algorithms for numbers with the same number of digits.
History
The concept of factorials has arisen independently in many cultures:
- In Indian mathematics, one of the earliest known descriptions of factorials comes from the Anuyogadvāra-sūtra, It separates out the sorted and reversed order of a set of items from the other ("mixed") orders, evaluating the number of mixed orders by subtracting two from the usual product formula for the factorial. The product rule for permutations was also described by 6th-century CE Jain monk Jinabhadra. Hindu scholars have been using factorial formulas since at least 1150, when Bhāskara II mentioned factorials in his work Līlāvatī, in connection with a problem of how many ways Vishnu could hold his four characteristic objects (a conch shell, discus, mace, and lotus flower) in his four hands, and a similar problem for a ten-handed god.
- In the mathematics of the Middle East, the Hebrew mystic book of creation Sefer Yetzirah, from the Talmudic period (200 to 500 CE), lists factorials up to 7! as part of an investigation into the number of words that can be formed from the Hebrew alphabet. Factorials were also studied for similar reasons by 8th-century Arab grammarian Al-Khalil ibn Ahmad al-Farahidi.
- In Europe, although Greek mathematics included some combinatorics, and Plato famously used 5,040 (a factorial) as the population of an ideal community, in part because of its divisibility properties, there is no direct evidence of ancient Greek study of factorials. Instead, the first work on factorials in Europe was by Jewish scholars such as Shabbethai Donnolo, explicating the Sefer Yetzirah passage. In 1677, British author Fabian Stedman described the application of factorials to change ringing, a musical art involving the ringing of several tuned bells.
From the late 15th century onward, factorials became the subject of study by Western mathematicians. In a 1494 treatise, Italian mathematician Luca Pacioli calculated factorials up to 11!, in connection with a problem of dining table arrangements. Christopher Clavius discussed factorials in a 1603 commentary on the work of Johannes de Sacrobosco, and in the 1640s, French polymath Marin Mersenne published large (but not entirely correct) tables of factorials, up to 64!, based on the work of Clavius. The power series for the exponential function, with the reciprocals of factorials for its coefficients, was first formulated in 1676 by Isaac Newton in a letter to Gottfried Wilhelm Leibniz. Other important works of early European mathematics on factorials include extensive coverage in a 1685 treatise by John Wallis, a study of their approximate values for large values of <math>n</math> by Abraham de Moivre in 1721, a 1729 letter from James Stirling to de Moivre stating what became known as Stirling's approximation, and work at the same time by Daniel Bernoulli and Leonhard Euler formulating the continuous extension of the factorial function to the gamma function. Adrien-Marie Legendre included Legendre's formula, describing the exponents in the factorization of factorials into prime powers, in an 1808 text on number theory.
The notation <math>n!</math> for factorials was introduced by the French mathematician Christian Kramp in 1808. The word "factorial" (originally French: factorielle) was first used in 1800 by Louis François Antoine Arbogast, in the first work on Faà di Bruno's formula, but referring to a more general concept of products of arithmetic progressions. The "factors" that this name refers to are the terms of the product formula for the factorial.
Definition
The factorial function of a positive integer <math>n</math> is defined by the product of all positive integers not greater than <math>n</math>
- There is exactly one permutation of zero objects: with nothing to permute, the only rearrangement is to do nothing.
- This convention makes many identities in combinatorics valid for all valid choices of their parameters. For instance, the number of ways to choose all <math>n</math> elements from a set of <math>n</math> is <math display=inline>\tbinom{n}{n} = \tfrac{n!}{n!0!} = 1,</math> a binomial coefficient identity that would only be valid
- With the recurrence relation for the factorial remains valid Therefore, with this convention, a recursive computation of the factorial needs to have only the value for zero as a base case, simplifying the computation and avoiding the need for additional special cases.
- Setting <math>0!=1</math> allows for the compact expression of many formulae, such as the exponential function, as a power series: This usage of factorials in power series connects back to analytic combinatorics through the exponential generating function, which for a combinatorial class with <math>n_i</math> elements of is defined as the power series <math display=block>\sum_{k=0}^{\infty} \frac{x^k n_k}{k!}.</math>
In number theory, the most salient property of factorials is the divisibility of <math>n!</math> by all positive integers up 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
<math>n!\pm 1</math>, leading to a proof of Euclid's theorem that the number of primes is infinite. When <math>n!\pm 1</math> 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 In contrast, the numbers <math>n!+2,n!+3,\dots n!+n</math> 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 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 <math>\log_2 n!=n\log_2n-O(n)</math> on the number of comparisons needed to comparison sort a set of <math>n</math> items, 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.
Properties
thumb|upright=1.6|Comparison of the factorial, Stirling's approximation, and the simpler approximation on a doubly logarithmic scale
thumb|upright=1.6|[[Relative error in a truncated Stirling series vs. number of terms]]
Growth and approximation
As a function the factorial has faster than exponential growth, but grows more slowly than a double exponential function. Its growth rate is similar but slower by an exponential factor. One way of approaching this result is by taking the natural logarithm of the factorial, which turns its product formula into a sum, and then estimating the sum by an integral:
<math display="block">\ln n! = \sum_{x=1}^n \ln x \approx \int_1^n\ln x\, dx=n\ln n-n+1.</math>
Exponentiating the result (and ignoring the negligible <math>+1</math> term) approximates <math>n!</math> as
More carefully bounding the sum both above and below by an integral, using the trapezoid rule, shows that this estimate needs a correction factor proportional The constant of proportionality for this correction can be found from the Wallis product, which expresses <math>\pi</math> as a limiting ratio of factorials and powers of two. The result of these corrections is Stirling's approximation:
<math display="block">n!\sim\sqrt{2\pi n}\left(\frac{n}{e}\right)^n\,.</math>
Here, the <math>\sim</math> symbol means that, as <math>n</math> goes to infinity, the ratio between the left and right sides approaches <math>1</math> in the limit.
Stirling's formula provides the first term in an asymptotic series that becomes even more accurate when taken to greater numbers of terms:
<math display="block">
n! \sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^n \left(1 +\frac{1}{12n}+\frac{1}{288n^2} - \frac{139}{51840n^3} -\frac{571}{2488320n^4}+ \cdots \right).</math>
An alternative version (the approximation derived directly from the Euler–Maclaurin formula) converges faster because it only requires odd exponents in the correction terms:
<math display=block>\log_2 n! = n\log_2 n- n \log_2 e + \frac12\log_2 n + O(1).</math>
Divisibility and digits
The product formula for the factorial implies that <math>n!</math> is divisible by all prime numbers that are at and by no larger prime numbers. More precise information about its divisibility is given by Legendre's formula, which gives the exponent of each prime <math>p</math> in the prime factorization of <math>n!</math> as
<math display=block>\sum_{i=1}^\infty \left \lfloor \frac n {p^i} \right \rfloor=\frac{n - s_p(n)}{p - 1}.</math>
Here <math>s_p(n)</math> denotes the sum of the digits The exponent given by this formula can more technically be called the -adic valuation of the factorial. Grouping the prime factors of the factorial into prime powers in different ways produces the multiplicative partitions of factorials.
The special case of Legendre's formula for <math>p=5</math> gives the number of trailing zeros in the decimal representation of the factorials. Legendre's formula implies that the exponent of the prime <math>p=2</math> is always larger than the exponent for so each factor of five can be paired with a factor of two to produce one of these trailing zeros. The leading digits of the factorials are distributed according to Benford's law. Every sequence of digits, in any base, is the sequence of initial digits of some factorial number in that base.
Another result on divisibility of factorials, Wilson's theorem, states that <math>(n-1)!+1</math> is divisible by <math>n</math> if and only if <math>n</math> is a prime number. For almost all numbers (all but a subset of exceptions with asymptotic density zero), it coincides with the largest prime factor
The product of two factorials, always evenly divides It would follow from the conjecture that there are only finitely many nontrivial examples.
The greatest common divisor of the values of a primitive polynomial of degree <math>d</math> over the integers evenly divides
Continuous interpolation and non-integer generalization
thumb|upright=1.65|The gamma function (shifted one unit left to match the facto­rials) continuously interpolates the factorial to non-integer values
thumb|Absolute values of the complex gamma function, showing poles at non-positive integers
There are infinitely many ways to extend the factorials to a continuous function.
The same integral converges more generally for any complex number <math>z</math> whose real part is positive. It can be extended to the non-integer points in the rest of the complex plane by solving for Euler's reflection formula
<math display=block>\Gamma(z)\Gamma(1-z)=\frac{\pi}{\sin\pi z}.</math>
However, this formula cannot be used at integers because, for them, the <math>\sin\pi z</math> term would produce a division by zero. The result of this extension process is an analytic function (more specifically a meromorphic function), the analytic continuation of the integral formula for the gamma function. It has a nonzero value at all complex numbers, except for the non-positive integers where it has simple poles. Correspondingly, this provides a definition for the factorial at all complex numbers other than the negative integers.
One property of the gamma function, distinguishing it from other continuous interpolations of the factorials, is given by the Bohr–Mollerup theorem, which states that the gamma function (offset by one) is the only log-convex function on the positive real numbers that interpolates the factorials and obeys the same functional equation. A related uniqueness theorem of Helmut Wielandt states that the complex gamma function and its scalar multiples are the only holomorphic functions on the positive complex half-plane that obey the functional equation and remain bounded for complex numbers with real part between 1 and 2.
Other complex functions that interpolate the factorial values include Hadamard's gamma function, which is an entire function over all the complex numbers, including the non-positive integers. In the -adic numbers, it is not possible to continuously interpolate the factorial function directly, because the factorials of large integers (a dense subset of the -adic integers) converge to zero according to Legendre's formula, forcing any continuous function that is close to their values to be zero everywhere. Instead, the -adic gamma function provides a continuous interpolation of a modified form of the factorial, omitting the factors in the factorial that are divisible by .
The digamma function is the logarithmic derivative of the gamma function. Just as the gamma function provides a continuous interpolation of the factorials, offset by one, the digamma function provides a continuous interpolation of the harmonic numbers, offset by the Euler–Mascheroni constant.
Computation
thumb|[[TI SR-50|TI SR-50A, a 1975 calculator with a factorial key (third row, center right)]]
The factorial function is a common feature in scientific calculators. It is also included in scientific programming libraries such as the Python mathematical functions module and the Boost C++ library.
If efficiency is not a concern, computing factorials is trivial: just successively multiply a variable initialized by the integers up The simplicity of this computation makes it a common example in the use of different computer programming styles and methods. The computation of <math>n!</math> can be expressed in pseudocode using iteration as
define factorial(n):
f := 1
for i := 1, 2, 3, ..., n:
f := f * i
return f
or using recursion based on its recurrence relation as
define factorial(n):
if (n = 0) return 1
return n * factorial(n − 1)
Other methods suitable for its computation include memoization, dynamic programming, and functional programming. The computational complexity of these algorithms may be analyzed using the unit-cost random-access machine model of computation, in which each arithmetic operation takes constant time and each number uses a constant amount of storage space. In this model, these methods can compute <math>n!</math> in time and the iterative version uses space Unless optimized for tail recursion, the recursive version takes linear space to store its call stack. However, this model of computation is only suitable when <math>n</math> is small enough to allow <math>n!</math> to fit into a machine word. The values 12! and 20! are the largest factorials that can be stored in, respectively, the 32-bit Floating point can represent larger factorials, but approximately rather than exactly, and will still overflow for factorials larger than
The exact computation of larger factorials involves arbitrary-precision arithmetic, because of fast growth and integer overflow. Time of computation can be analyzed as a function of the number of digits or bits in the result. However, computing the factorial involves repeated products, rather than a single multiplication, so these time bounds do not apply directly. In this setting, computing <math>n!</math> by multiplying the numbers from 1 in sequence is inefficient, because it involves <math>n</math> multiplications, a constant fraction of which take time <math>O(n\log^2 n)</math> each, giving total time A better approach is to perform the multiplications as a divide-and-conquer algorithm that multiplies a sequence of <math>i</math> numbers by splitting it into two subsequences of <math>i/2</math> numbers, multiplies each subsequence, and combines the results with one last multiplication. This approach to the factorial takes total time one logarithm comes from the number of bits in the factorial, a second comes from the multiplication algorithm, and a third comes from the divide and conquer.
Even better efficiency is obtained by computing from its prime factorization, based on the principle that exponentiation by squaring is faster than expanding an exponent into a product. An algorithm for this by Arnold Schönhage begins by finding the list of the primes up for instance using the sieve of Eratosthenes, and uses Legendre's formula to compute the exponent for each prime. Then it computes the product of the prime powers with these exponents, using a recursive algorithm, as follows:
- Use divide and conquer to compute the product of the primes whose exponents are odd
- Divide all of the exponents by two (rounding down to an integer), recursively compute the product of the prime powers with these smaller exponents, and square the result
- Multiply together the results of the two previous steps
The product of all primes up to <math>n</math> is an <math>O(n)</math>-bit number, by the prime number theorem, so the time for the first step is <math>O(n\log^2 n)</math>, with one logarithm coming from the divide and conquer and another coming from the multiplication algorithm. In the recursive calls to the algorithm, the prime number theorem can again be invoked to prove that the numbers of bits in the corresponding products decrease by a constant factor at each level of recursion, so the total time for these steps at all levels of recursion adds in a geometric series The time for the squaring in the second step and the multiplication in the third step are again because each is a single multiplication of a number with <math>O(n\log n)</math> bits. Again, at each level of recursion the numbers involved have a constant fraction as many bits (because otherwise repeatedly squaring them would produce too large a final result) so again the amounts of time for these steps in the recursive calls add in a geometric series Consequentially, the whole algorithm takes proportional to a single multiplication with the same number of bits in its result.
;Bhargava factorial
:The Bhargava factorials are a family of integer sequences defined by Manjul Bhargava with similar number-theoretic properties to the factorials, including the factorials themselves as a special case. That is, <math display=block>(2k-1)!! = \prod_{i=1}^k (2i-1) = \frac{(2k)!}{2^k k!}.</math> For example, . Double factorials are used in trigonometric integrals, in expressions for the gamma function at half-integers and the volumes of hyperspheres, and in counting binary trees and perfect matchings.
;Exponential factorial
:Just as triangular numbers sum the numbers from <math>1</math> and factorials take their product, the exponential factorial exponentiates. The exponential factorial is defined recursively </math>. For example, the exponential factorial of 4 is <math display=block>4^{3^{2^{1}=262144.</math> These numbers grow much more quickly than regular factorials.
;Falling factorial
:The notations <math>(x)_{n}</math> or <math>x^{\underline n}</math> are sometimes used to represent the product of the greatest <math>n</math> integers counting up to and equal to This is also known as a falling factorial or backward factorial, and the <math>(x)_{n}</math> notation is a Pochhammer symbol. Falling factorials count the number of different sequences of <math>n</math> distinct items that can be drawn from a universe of <math>x</math> items. They occur as coefficients in the higher derivatives of polynomials, and in the factorial moments of random variables.
;Hyperfactorials
:The hyperfactorial of <math>n</math> is the product <math>1^1\cdot 2^2\cdots n^n</math>. These numbers form the discriminants of Hermite polynomials. They can be continuously interpolated by the K-function, and obey analogues to Stirling's formula and Wilson's theorem.
;Jordan–Pólya numbers
:The Jordan–Pólya numbers are the products of factorials, allowing repetitions. Every tree has a symmetry group whose number of symmetries is a Jordan–Pólya number, and every Jordan–Pólya number counts the symmetries of some tree.
;Primorial
:The primorial <math>n\#</math> is the product of prime numbers less than or equal this construction gives them some similar divisibility properties to factorials, As with the factorial primes researchers have studied primorial primes
