This article is about products of consecutive integers. For statistical experiments over all combinations of values, see factorial experiment. For data representation by independent components, see factorial code.
Selected factorials; values in scientific notation are rounded
In mathematics, the factorial of a non-negative integer, denoted by , is the product of all positive integers less than or equal to . The factorial of also equals the product of with the next smaller factorial:
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, one of the canonical works of Jain literature, which has been assigned dates varying from 300 BCE to 400 CE. 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 Europe, although Greek mathematics included some combinatorics, and Plato famously used 5040 (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.
The notation for factorials was introduced by the French mathematician Christian Kramp in 1808. Many other notations have also been used. Another later notation, in which the argument of the factorial was half-enclosed by the left and bottom sides of a box, was popular for some time in Britain and America but fell out of use, perhaps because it is difficult to typeset. 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.
The factorial function of a positive integer is defined by the product of all positive integers not greater than 
If this product formula is changed to keep all but the last term, it would define a product of the same form, for a smaller factorial. This leads to a recurrence relation, according to which each value of the factorial function can be obtained by multiplying the previous value by :
For example, .
Factorial of zero
The factorial of is , or in symbols, . There are several motivations for this definition:
For , the definition of as a product involves the product of no numbers at all, and so is an example of the broader convention that the empty product, a product of no factors, is equal to the multiplicative identity.
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 elements from a set of is a binomial coefficient identity that would only be valid with .
With , the recurrence relation for the factorial remains valid at . 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.
The earliest uses of the factorial function involve counting permutations: there are different ways of arranging 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 count the -elementcombinations (subsets of elements) from a set with elements, and can be computed from factorials using the formula
The Stirling numbers of the first kind sum to the factorials, and count the permutations of 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 items is the nearest integerto .
As a function of , the factorial has faster than exponential growth, but grows more slowly than a double exponential function. Its growth rate is similar to , 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:
Exponentiating the result (and ignoring the negligible term) approximates as .
More carefully bounding the sum both above and below by an integral, using the trapezoid rule, shows that this estimate needs a correction term proportional to . The constant of proportionality for this correction can be found from the Wallis product, which expresses as a limiting ratio of factorials and powers of two. The result of these corrections is Stirling's approximation:
Here, the symbol means that, as goes to infinity, the ratio between the left and right sides approaches one 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:
An alternative version uses only odd exponents in the correction terms:
The product formula for the factorial implies that is divisible by all prime numbers that are at most , 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 in the prime factorization of as
The special case of Legendre's formula for gives the number of trailing zeros in the decimal representation of the factorials. According to this formula, the number of zeros can be obtained by subtracting the base-5 digits of from , and dividing the result by four. Legendre's formula implies that the exponent of the prime 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.
The product of two factorials, , always evenly divides . There are infinitely many factorials that equal the product of other factorials: if is itself any product of factorials, then equals that same product multiplied by one more factorial, . The only known examples of factorials that are products of other factorials but are not of this "trivial" form are ,, and . It would follow from the abc conjecture that there are only finitely many nontrivial examples.
However, this formula cannot be used at integers because, for them, the term would produce a division by zero. The result of this extension process is an analytic 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 p-adic numbers, it is not possible to continuously interpolate the factorial function directly, because the factorials of large integers (a dense subset of the p-adics) converge to zero according to Legendre's formula, forcing any continuous function that is close to their values to be zero everywhere. Instead, the p-adic gamma function provides a continuous interpolation of a modified form of the factorial, omitting the factors in the factorial that are divisible by p.
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 to by the integers up to . The simplicity of this computation makes it a common example in the use of different computer programming styles and methods.
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. By Stirling's formula, has bits. The Schönhage–Strassen algorithm can produce a -bit product in time , and faster multiplication algorithms taking time are known. However, computing the factorial involves repeated products, rather than a single multiplication, so these time bounds do not apply directly. In this setting, computing by multiplying the numbers from 1 to in sequence is inefficient, because it involves multiplications, a constant fraction of which take time each, giving total time . A better approach is to perform the multiplications as a divide-and-conquer algorithm that multiplies a sequence of numbers by splitting it into two subsequences of 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 n! 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 to , 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 is an -bit number, by the prime number theorem, so the time for the first step is , 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 seriesto . 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 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 to . Consequentially, the whole algorithm takes time , proportional to a single multiplication with the same number of bits in its result.
Several other integer sequences are similar to or related to the factorials:
The alternating factorial is the absolute value of the alternating sum of the first factorials, . These have mainly been studied in connection with their primality; only finitely many of them can be prime, but a complete list of primes of this form is not known.
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.
The product of all the odd integers up to some odd positive integer is called the double factorialof , and denoted by . That is,
Just as triangular numbers sum the numbers from to , and factorials take their product, the exponential factorial exponentiates. The exponential factorial is defined recursively as . For example, the exponential factorial of 4 is
These numbers grow much more quickly than regular factorials.
The notations or are sometimes used to represent the product of the integers counting up to and including , equal to . This is also known as a falling factorial or backward factorial, and the notation is a Pochhammer symbol. Falling factorials count the number of different sequences of distinct items that can be drawn from a universe of items. They occur as coefficients in the higher derivatives of polynomials, and in the factorial moments of random variables.
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.
^Jadhav, Dipak (August 2021). "Jaina Thoughts on Unity Not Being a Number". History of Science in South Asia. University of Alberta Libraries. 9: 209–231. doi:10.18732/hssa67. S2CID238656716.. See discussion of dating on p. 211.
^Stedman, Fabian (1677). Campanalogia. London. pp. 6–9. The publisher is given as "W.S." who may have been William Smith, possibly acting as agent for the Society of College Youths, to which society the "Dedicatory" is addressed.
^Haberman, Bruria; Averbuch, Haim (2002). "The case of base cases: Why are they so difficult to recognize? Student difficulties with recursion". In Caspersen, Michael E.; Joyce, Daniel T.; Goelman, Don; Utting, Ian (eds.). Proceedings of the 7th Annual SIGCSE Conference on Innovation and Technology in Computer Science Education, ITiCSE 2002, Aarhus, Denmark, June 24-28, 2002. Association for Computing Machinery. pp. 84–88. doi:10.1145/544414.544441.
^Palmer, Edgar M. (1985). "Appendix II: Stirling's formula". Graphical Evolution: An introduction to the theory of random graphs. Wiley-Interscience Series in Discrete Mathematics. Chichester: John Wiley & Sons. pp. 127–128. ISBN0-471-81577-2. MR0795795.
^Sussman, Gerald Jay (1982). "LISP, programming, and implementation". Functional Programming and Its Applications: An Advanced Course. CREST Advanced Courses. Cambridge University Press. pp. 29–72. ISBN978-0-521-24503-6. See in particular p. 34.
^Chaudhuri, Ranjan (June 2003). "Do the arithmetic operations really execute in constant time?". ACM SIGCSE Bulletin. Association for Computing Machinery. 35 (2): 43–44. doi:10.1145/782941.782977. S2CID13629142.
^ abWinkler, Jürgen F. H.; Kauer, Stefan (March 1997). "Proving assertions is also useful". ACM SIGPLAN Notices. Association for Computing Machinery. 32 (3): 38–41. doi:10.1145/251634.251638. S2CID17347501.