Inmathematics, thefactorial of a non-negativeinteger, denotedby, is theproduct of all positive integers less than or equalto. The factorialof also equals the product of with the next smaller factorial:For example,The value of 0! is 1, according to the convention for anempty product.[1]
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 thanexponential growth.Legendre's formula describes the exponents of the prime numbers in aprime factorization of the factorials, and can be used to count the trailing zeros of the factorials.Daniel Bernoulli andLeonhard Eulerinterpolated the factorial function to a continuous function ofcomplex numbers, except at the negative integers, the (offset)gamma function.
Many other notable functions and number sequences are closely related to the factorials, including thebinomial coefficients,double factorials,falling factorials,primorials, andsubfactorials. Implementations of the factorial function are commonly used as an example of differentcomputer programming styles, and are included inscientific 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 fastmultiplication algorithms for numbers with the same number of digits.
The concept of factorials has arisen independently in many cultures:
InIndian mathematics, one of the earliest known descriptions of factorials comes from the Anuyogadvāra-sūtra,[2] one of the canonical works ofJain literature, which has been assigned dates varying from 300 BCE to 400 CE.[3] 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 monkJinabhadra.[2] Hindu scholars have been using factorial formulas since at least 1150, whenBhāskara II mentioned factorials in his workLīlāvatī, in connection with a problem of how many waysVishnu could hold his four characteristic objects (aconch shell,discus,mace, andlotus flower) in his four hands, and a similar problem for a ten-handed god.[4]
In the mathematics of the Middle East, the Hebrew mystic book of creationSefer Yetzirah, from theTalmudic 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 theHebrew alphabet.[5][6] Factorials were also studied for similar reasons by 8th-century Arab grammarianAl-Khalil ibn Ahmad al-Farahidi.[5] Arab mathematicianIbn al-Haytham (also known as Alhazen, c. 965 – c. 1040) was the first to formulateWilson's theorem connecting the factorials with theprime numbers.[7]
In Europe, althoughGreek mathematics included some combinatorics, andPlato famously used 5,040 (a factorial) as the population of an ideal community, in part because of its divisibility properties,[8] there is no direct evidence of ancient Greek study of factorials. Instead, the first work on factorials in Europe was by Jewish scholars such asShabbethai Donnolo, explicating the Sefer Yetzirah passage.[9] In 1677, British authorFabian Stedman described the application of factorials tochange ringing, a musical art involving the ringing of several tuned bells.[10][11]
The notation for factorials was introduced by the French mathematicianChristian Kramp in 1808.[17] 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.[17] The word "factorial" (originally French:factorielle) was first used in 1800 byLouis François Antoine Arbogast,[18] in the first work onFaà di Bruno's formula,[19] but referring to a more general concept of products ofarithmetic progressions. The "factors" that this name refers to are the terms of the product formula for the factorial.[20]
The factorial function of a positive integer is defined by the product of all positive integers not greater than[1]This may be written more concisely inproduct notation as[1]
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 arecurrence relation, according to which each value of the factorial function can be obtained by multiplying the previous valueby:[21]For example,.
The factorialofis, 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 theempty product, a product of no factors, is equal to the multiplicative identity.[22]
There is exactly one permutation of zero objects: with nothing to permute, the only rearrangement is to do nothing.[21]
This convention makes many identities incombinatorics valid for all valid choices of their parameters. For instance, the number of ways to choose all elements from a set of is abinomial coefficient identity that would only be validwith.[23]
With, the recurrence relation for the factorial remains validat. Therefore, with this convention, arecursive computation of the factorial needs to have only the value for zero as abase case, simplifying the computation and avoiding the need for additional special cases.[24]
The earliest uses of the factorial function involve countingpermutations: there are different ways of arranging distinct objects into a sequence.[26] Factorials appear more broadly in many formulas incombinatorics, to account for different orderings of objects. For instance thebinomial coefficients count the-elementcombinations (subsets of elements) from a set with elements, and can be computed from factorials using the formula[27] TheStirling numbers of the first kind sum to the factorials, and count the permutationsof grouped into subsets with the same numbers of cycles.[28] Another combinatorial application is in countingderangements, permutations that do not leave any element in its original position; the number of derangements of items is thenearest integerto.[29]
Comparison of the factorial, Stirling's approximation, and the simpler approximation, on a doubly logarithmic scaleRelative error in a truncated Stirling series vs. number of terms
As a functionof, the factorial has faster thanexponential growth, but grows more slowly than adouble exponential function.[48] Its growth rate is similarto, but slower by an exponential factor. One way of approaching this result is by taking thenatural 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.[49]More carefully bounding the sum both above and below by an integral, using thetrapezoid rule, shows that this estimate needs a correction factor proportionalto. The constant of proportionality for this correction can be found from theWallis product, which expresses as a limiting ratio of factorials and powers of two. The result of these corrections isStirling's approximation:[50]Here, the symbol means that, as goes to infinity, the ratio between the left and right sides approaches one in thelimit.Stirling's formula provides the first term in anasymptotic series that becomes even more accurate when taken to greater numbers of terms:[51]An alternative version uses only odd exponents in the correction terms:[51]Many other variations of these formulas have also been developed, bySrinivasa Ramanujan,Bill Gosper, and others.[51]
The product formula for the factorial implies that isdivisible by allprime numbers that are atmost, and by no larger prime numbers.[52] More precise information about its divisibility is given byLegendre's formula, which gives the exponent of each prime in the prime factorization of as[53][54]Here denotes the sum of thebase- digitsof, and the exponent given by this formula can also be interpreted in advanced mathematics as thep-adic valuation of the factorial.[54] Applying Legendre's formula to the product formula forbinomial coefficients producesKummer's theorem, a similar result on the exponent of each prime in the factorization of a binomial coefficient.[55] Grouping the prime factors of the factorial intoprime powers in different ways produces themultiplicative partitions of factorials.[56]
The special case of Legendre's formula for gives the number oftrailing zeros in the decimal representation of the factorials.[57] 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.[58] 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.[57] The leading digits of the factorials are distributed according toBenford's law.[59] Every sequence of digits, in any base, is the sequence of initial digits of some factorial number in that base.[60]
Another result on divisibility of factorials,Wilson's theorem, states that is divisible by if and only if is aprime number.[52] For any giveninteger, theKempner function of is given by the smallest for which divides.[61] For almost all numbers (all but a subset of exceptions withasymptotic density zero), it coincides with the largest prime factorof.[62]
The product of two factorials,, always evenly divides.[63] 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.[64] It would follow from theabc conjecture that there are only finitely many nontrivial examples.[65]
The gamma function (shifted one unit left to match the factorials) continuously interpolates the factorial to non-integer valuesAbsolute values of the complex gamma function, showing poles at non-positive integers
There are infinitely many ways to extend the factorials to acontinuous function.[66] The most widely used of these[67] uses thegamma function, which can be defined for positive real numbers as theintegralThe resulting function is related to the factorial of a non-negative integer by the equationwhich can be used as a definition of the factorial for non-integer arguments.At all values for which both and are defined, the gamma function obeys thefunctional equationgeneralizing therecurrence relation for the factorials.[66]
The same integral converges more generally for anycomplex number whose real part is positive. It can be extended to the non-integer points in the rest of thecomplex plane by solving for Euler'sreflection formulaHowever, this formula cannot be used at integers because, for them, the term would produce adivision by zero. The result of this extension process is ananalytic function, theanalytic 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 hassimple poles. Correspondingly, this provides a definition for the factorial at all complex numbers other than the negative integers.[67]One property of the gamma function, distinguishing it from other continuous interpolations of the factorials, is given by theBohr–Mollerup theorem, which states that the gamma function (offset by one) is the onlylog-convex function on the positive real numbers that interpolates the factorials and obeys the same functional equation. A related uniqueness theorem ofHelmut Wielandt states that the complex gamma function and its scalar multiples are the onlyholomorphic 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.[68]
Other complex functions that interpolate the factorial values includeHadamard's gamma function, which is anentire function over all the complex numbers, including the non-positive integers.[69][70] In thep-adic numbers, it is not possible to continuously interpolate the factorial function directly, because the factorials of large integers (a dense subset of thep-adics) converge to zero according to Legendre's formula, forcing any continuous function that is close to their values to be zero everywhere. Instead, thep-adic gamma function provides a continuous interpolation of a modified form of the factorial, omitting the factors in the factorial that are divisible byp.[71]
TI SR-50A, a 1975 calculator with a factorial key (third row, center right)
The factorial function is a common feature inscientific calculators.[73] It is also included in scientific programming libraries such as thePython mathematical functions module[74] and theBoost C++ library.[75] If efficiency is not a concern, computing factorials is trivial: just successively multiply a variable initializedto by the integers upto. The simplicity of this computation makes it a common example in the use of different computer programming styles and methods.[76]
Other methods suitable for its computation includememoization,[79]dynamic programming,[80] andfunctional programming.[81] Thecomputational complexity of these algorithms may be analyzed using the unit-costrandom-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 in time, and the iterative version uses space. Unless optimized fortail recursion, the recursive version takes linear space to store itscall stack.[82] However, this model of computation is only suitable when is small enough to allow to fit into amachine word.[83] The values 12! and 20! are the largest factorials that can be stored in, respectively, the32-bit[84] and64-bitintegers.[85]Floating point can represent larger factorials, but approximately rather than exactly, and will still overflow for factorials larger than.[84]
The exact computation of larger factorials involvesarbitrary-precision arithmetic, because offast growth andinteger overflow. Time of computation can be analyzed as a function of the number of digits or bits in the result.[85] By Stirling's formula, has bits.[86] TheSchönhage–Strassen algorithm can produce a-bit product in time, and fastermultiplication algorithms taking time are known.[87] 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 1to 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 adivide-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.[88]
Even better efficiency is obtained by computingn! from its prime factorization, based on the principle thatexponentiation by squaring is faster than expanding an exponent into a product.[86][89] An algorithm for this byArnold Schönhage begins by finding the list of the primes upto, for instance using thesieve 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 theprime 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 ageometric 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 seriesto. Consequentially, the whole algorithm takestime, proportional to a single multiplication with the same number of bits in its result.[89]
Several other integer sequences are similar to or related to the factorials:
Alternating factorial
Thealternating factorial is the absolute value of thealternating 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.[90]
Bhargava factorial
TheBhargava factorials are a family of integer sequences defined byManjul Bhargava with similar number-theoretic properties to the factorials, including the factorials themselves as a special case.[63]
Just astriangular numbers sum the numbers fromto, and factorials take their product, theexponential factorial exponentiates. The exponential factorial is defined recursivelyas. For example, the exponential factorial of 4 is These numbers grow much more quickly than regular factorials.[95]
Falling factorial
The notations or are sometimes used to represent the product of the greatest integers counting up to andincluding, equal to. This is also known as afalling factorial or backward factorial, and the notation is a Pochhammer symbol.[96] Falling factorials count the number of different sequences of distinct items that can be drawn from a universe of items.[97] They occur as coefficients in thehigher derivatives of polynomials,[98] and in thefactorial moments ofrandom variables.[99]
TheJordan–Pólya numbers are the products of factorials, allowing repetitions. Everytree has asymmetry group whose number of symmetries is a Jordan–Pólya number, and every Jordan–Pólya number counts the symmetries of some tree.[104]
^abDatta, Bibhutibhusan; Singh, Awadhesh Narayan (2019). "Use of permutations and combinations in India". In Kolachana, Aditya; Mahesh, K.; Ramasubramanian, K. (eds.).Studies in Indian Mathematics and Astronomy: Selected Articles of Kripa Shankar Shukla. Sources and Studies in the History of Mathematics and Physical Sciences. Springer Singapore. pp. 356–376.doi:10.1007/978-981-13-7326-8_18.ISBN978-981-13-7325-1.S2CID191141516.. Revised by K. S. Shukla from a paper inIndian Journal of History of Science 27 (3): 231–249, 1992,MR1189487. See p. 363.
^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 theSociety 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.
^Randić, Milan (1987). "On the evaluation of the characteristic polynomial via symmetric function theory".Journal of Mathematical Chemistry.1 (1):145–152.doi:10.1007/BF01205340.MR0895533.S2CID121752631.
^Cameron, Peter J. (1994). "2.4: Orders of magnitude".Combinatorics: Topics, Techniques, Algorithms. Cambridge University Press. pp. 12–14.ISBN978-0-521-45133-8.
^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.
^Alzer, Horst (2009). "A superadditive property of Hadamard's gamma function".Abhandlungen aus dem Mathematischen Seminar der Universität Hamburg.79 (1):11–23.doi:10.1007/s12188-008-0009-5.MR2541340.S2CID123691692.
^Robert 2000. "7.1: The gamma function". pp. 366–385.
^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 particularp. 34.
^Chaudhuri, Ranjan (June 2003). "Do the arithmetic operations really execute in constant time?".ACM SIGCSE Bulletin.35 (2). Association for Computing Machinery:43–44.doi:10.1145/782941.782977.S2CID13629142.