taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume C1 · Basic concepts · TAOCP Section 1.2.5

Permutations and factorials

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read it next to Section 1.2.5 of the book. The nodes and the exercises of the section.

Contents
  1. Permutations and factorials

Permutations and factorials

Overview

Section 1.2.5 of TAOCP counts the ways to put nn distinct objects in a row, and it names the answer n!n!. It builds the permutations of nn objects from those of n−1n - 1 objects in two ways, and it states the first facts about n!n!: how large it is (Stirling's formula), which primes divide it and how often (Legendre's rule), and what n!n! could mean when nn is not an integer. It uses the sum 1+2+⋯+n1 + 2 + \cdots + n as a model of a function that has an easy extension. It then looks at two series of Stirling that fail or may succeed as definitions of n!n!, and at the limit of Euler that succeeds. From Euler's limit it takes the gamma function, the reflection formula and the contour integral of Hankel. It ends with the falling and rising powers xk‾x^{\underline{k}} and xk‾x^{\overline{k}}, which the rest of the book uses all the time. The section uses the product and sum notation of Section 1.2.3 and the floor function of Section 1.2.4. It uses the exponential function and the number ee of Section 1.2.2. Its proofs of Stirling's formula and of the series (11) and (12) need the binomial coefficients of Section 1.2.6, and this chapter says where.

The ideas

To count orders, choose the places one at a time. There are nn choices for the first place, n−1n - 1 for the second, and so on. The count does not depend on which objects stand in the earlier places, only on how many are left. This is why the count is a product n(n−1)(n−2)⋯n(n-1)(n-2)\cdots, and why it is 00 when you try to fill more places than you have objects.

A construction by induction is a proof of a count. If each order of n−1n - 1 objects gives exactly nn orders of nn objects, and each order of nn objects comes from exactly one order of n−1n - 1 objects, then the count of nn objects is nn times the count of n−1n - 1. The section gives two such constructions: insert the largest object in one of nn places, or add a new last element and rename. They give the same count but different lists, and different programs use different lists.

Rapid growth is the main property of n!n!. The number 10!=3 628 80010! = 3\,628\,800 is about where a search over all orders stops being practical. Stirling's formula says how fast the growth is: n!n! is close to 2πn (n/e)n\sqrt{2\pi n}\,(n/e)^n, and the relative error is about 1/(12n)1/(12n).

The prime factors of n!n! come from counting multiples. A prime pp divides n!n! once for each multiple of pp in {1,…,n}\{1, \ldots, n\}, one more time for each multiple of p2p^2, and so on. This is Legendre's rule, eq. (8).

An easy rule can fail to extend. The value (1/2)!(1/2)! has no meaning from the product (4). The sum n?=1+2+⋯+nn? = 1 + 2 + \cdots + n shows the problem on a small case: the formula n(n+1)/2n(n+1)/2 extends the sum to every real nn, but the extension is a choice, because many functions agree with a sum on the integers. For n!n! the choice must keep the rule n!=n⋅(n−1)!n! = n \cdot (n-1)! and must be smooth in a sense that the section makes precise only by examples. The extension that works is Euler's limit (13), which defines the gamma function.

Falling and rising powers are the products that counting gives. The count n(n−1)⋯(n−k+1)n(n-1)\cdots(n-k+1) of eq. (2) is a falling power, and it is a polynomial in nn for each fixed kk. Its twin with plus signs is the rising power. Section 1.2.6 and later sections write sums and series in these powers, because they behave under differences as ordinary powers behave under derivatives.

C1 does some things in a different order from the book. It proves the count of kk-permutations first by the product rule, then by induction, and it proves Stirling's formula in full with two monotone sequences and Wallis's product, a route that differs from the proof of Section 1.2.11.2. It proves Stirling's series (11) by counting permutations that move exactly kk elements, which the book defers to Section 1.2.6. It proves the reflection formula (16) and Hankel's integral (17) from the product (15), so that the reader sees one thread from (13) to (17).

Definitions, results and proofs

Definition (a permutation and a kk-permutation, TAOCP Section 1.2.5). Put nn distinct objects in a row, each of them once: the row is a permutation of the nn objects. For 0≤k≤n0 \le k \le n, a kk-permutation of the nn objects is an arrangement in a row of kk of them, chosen from the nn. A permutation of nn objects is an nn-permutation.

Node: TASWXB1M5H · Lean: Equiv.Perm, from Mathlib · status: not stated yet

Explanation. Order matters and no object is used twice. The objects can be letters, numbers or anything else, and only their number counts. Two arrangements are the same if each place holds the same object. In Lean, a permutation of a type α is an element of Equiv.Perm α, a bijection of α with itself, and a list of the objects in a row corresponds to the values of this bijection on 1,…,n1, \ldots, n. A kk-permutation corresponds to an injective function from {1,…,k}\{1, \ldots, k\} to the set of objects, a term of Fin k ↪ α, from Mathlib. The two views are the same, since the row a1a2⋯ana_1 a_2 \cdots a_n is the function i↦aii \mapsto a_i.

Example. The set {a,b,c,d}\{a, b, c, d\} has 2424 permutations. Its 22-permutations are ab,ac,ad,ba,bc,bd,ca,cb,cd,da,db,dcab, ac, ad, ba, bc, bd, ca, cb, cd, da, db, dc, which are 1212 in all. The set {1,2,3}\{1, 2, 3\} has one 00-permutation, the empty row. The set {a,b}\{a, b\} has no 33-permutation, since a row of three distinct objects needs three objects.

Example (the six orders of three letters, TAOCP eq. 1.2.5(1)). The permutations of {a,b,c}\{a, b, c\} are abc,acb,bac,bca,cab,cbaabc, acb, bac, bca, cab, cba.

Node: TADVJN5M00 · Lean: TAOCP.S1_2_5.eq1 · status: proved

Explanation. The list is the whole set, in the order of the alphabet. It has six terms. A reader can see why: aa, bb or cc leads, and the other two letters can follow in two orders. The list is the smallest case where the structure of the count shows. For n=2n = 2 the two orders abab and baba do not tell much, and for n=4n = 4 the list has 2424 terms and it is easy to lose one.

Example. The six rows, sorted as in the alphabet, are the six outputs of the Python call itertools.permutations('abc'). The same call on four letters gives 2424 rows, of which six start with aa, six with bb, six with cc and six with dd. This is the first sign of the count n⋅(n−1)!n \cdot (n-1)!.

Theorem (the number of kk-permutations, TAOCP eq. 1.2.5(2)). Let pnkp_{nk} be the number of kk-permutations of nn objects. For each k≥0k \ge 0 the count is the product below. For k=nk = n it counts the permutations of nn objects, and for k>nk > n the product is 00, which is also the right count: pnk=n(n−1)⋯(n−k+1)=∏j=0k−1(n−j).p_{nk} = n(n-1)\cdots(n-k+1) = \prod_{j=0}^{k-1}(n - j).

Node: TA5084B8HM · Lean: TAOCP.S1_2_5.eq2, TAOCP.S1_2_5.card_perm · status: proved

Explanation. A kk-permutation is made place by place. The first place gets any of the nn objects. The second place gets any object that is not in the first, so n−1n - 1 are left. After jj places are full, n−jn - j objects are left for the next place. The count of the choices is the product of these numbers. The formula is a polynomial of degree kk in nn, and Section 1.2.5 gives this polynomial a name in eq. (18). In Lean, Nat.descFactorial n k is this product, and for k > n it is 00 since a factor n−jn - j is 00 for j=nj = n. Mathlib states the count of injective functions as Fintype.card_embedding_eq, and the count of permutations as Fintype.card_perm.

Example. A club has 55 members and wants a chair, a secretary and a treasurer, all different. The number of ways is p5,3=5⋅4⋅3=60p_{5,3} = 5 \cdot 4 \cdot 3 = 60. A computer check with math.perm(5, 3) gives 6060. For all five members in a row, p5,5=120p_{5,5} = 120. For k=7>n=5k = 7 > n = 5 the product has the factor 5−5=05 - 5 = 0 and the count is 00.

Proof. The proof is by induction on kk, for fixed nn. For k=0k = 0 there is exactly one 00-permutation, the empty row, and the product with no factor is 11. Suppose that pn,k=∏j=0k−1(n−j)p_{n,k} = \prod_{j=0}^{k-1}(n - j) for some kk with k<nk < n. Each (k+1)(k+1)-permutation is a kk-permutation (its first kk places) followed by one more object that is not among those kk. For each kk-permutation there are exactly n−kn - k such objects. So the map that cuts off the last place sends the set of (k+1)(k+1)-permutations onto the set of kk-permutations, and each kk-permutation has exactly n−kn - k preimages. Then pn,k+1=(n−k) pn,k=∏j=0k(n−j)p_{n,k+1} = (n - k)\,p_{n,k} = \prod_{j=0}^{k}(n - j). For k≥nk \ge n the same argument holds if the number of objects that are left is read as max⁡(n−k,0)\max(n - k, 0), and the product has the factor 00 from j=nj = n on. □\square

Example (the six orders of three numbers, and two ways to go up one size, TAOCP eq. 1.2.5(3)). The permutations of {1,2,3}\{1, 2, 3\} are 123,132,213,231,312,321123, 132, 213, 231, 312, 321. There are two methods that build the permutations of {1,…,n}\{1, \ldots, n\} from those of {1,…,n−1}\{1, \ldots, n-1\}.

Node: TA2BNR00CJ · Lean: TAOCP.S1_2_5.eq3, TAOCP.S1_2_5.card_perm_succ · status: proved

Explanation. The relabeling to numbers is only to make the constructions work with order. The two methods make nn new permutations from each old one.

Method 1 (insert the largest). From a1⋯an−1a_1 \cdots a_{n-1}, insert the number nn in each of the nn places: before a1a_1, between two neighbors, or after an−1a_{n-1}. Method 2 (add a last element and rename). From a1⋯an−1a_1 \cdots a_{n-1} and a number kk with 1≤k≤n1 \le k \le n, add 11 to each aja_j that is at least kk, and put kk at the right end. The first step makes a permutation of {1,…,n}∖{k}\{1, \ldots, n\} \setminus \{k\} and the last step fills the gap. The pseudocode of the two methods is as follows.

Method1(a, n):                          -- a is a permutation of 1..n-1
  for i := 0 to n-1:
    yield a[1..i] ++ [n] ++ a[i+1..n-1]

Method2(a, n):
  for k := 1 to n:
    b := [ (x >= k ? x+1 : x) for x in a ]
    yield b ++ [k]

A worked run. Take a=3 1 2a = 3\,1\,2 and n=4n = 4.

ii or kk Method 1 result Method 2 result
0 or 1 4 3 1 24\,3\,1\,2 4 2 3 14\,2\,3\,1
1 or 2 3 4 1 23\,4\,1\,2 4 1 3 24\,1\,3\,2
2 or 3 3 1 4 23\,1\,4\,2 4 1 2 34\,1\,2\,3
3 or 4 3 1 2 43\,1\,2\,4 3 1 2 43\,1\,2\,4

Each method gives four permutations of {1,2,3,4}\{1, 2, 3, 4\}. The list of Method 2 for k=2k = 2: the numbers 3,1,23, 1, 2 with x≥2x \ge 2 raised by 11 become 4,1,34, 1, 3, and then 22 is added at the end: 4 1 3 24\,1\,3\,2.

Why each method gives every permutation once. Take a permutation bb of {1,…,n}\{1, \ldots, n\}. In Method 1, bb comes from the permutation that is bb with the entry nn removed, and from no other. The place of nn in bb tells which of the nn insertions made it. In Method 2, bb comes from the permutation made of b1⋯bn−1b_1 \cdots b_{n-1} with each entry larger than bnb_n lowered by 11, together with k=bnk = b_n. Both inverse maps are well defined, so each method is a bijection from {permutations of n−1n - 1} ×\times {nn choices} onto {permutations of nn}. Hence the count satisfies pn=n pn−1p_n = n\,p_{n-1}. □\square

Example. With n=3n = 3 and the single permutation 1 21\,2 of two numbers, Method 1 gives 3 1 2, 1 3 2, 1 2 33\,1\,2,\ 1\,3\,2,\ 1\,2\,3 and Method 2 gives 2 3 1, 1 3 2, 1 2 32\,3\,1,\ 1\,3\,2,\ 1\,2\,3. Do the same for 2 12\,1 and the six rows of the list above appear, with no repetition.

Definition (the factorial, TAOCP eq. 1.2.5(4)). For a positive integer nn, n!=1⋅2⋯n=∏k=1nk.n! = 1 \cdot 2 \cdots n = \prod_{k=1}^{n} k.

Node: TAE960YGFQ · Lean: TAOCP.S1_2_5.eq4 · status: proved

Explanation. The number n!n!, read "nn factorial", counts the orders of nn objects, so it equals pnnp_{nn}. The two products n(n−1)⋯1n(n-1)\cdots 1 and 1⋅2⋯n1 \cdot 2 \cdots n are the same by the commutative law of multiplication. In Lean, Nat.factorial n, written n !, is defined by 0! = 1 and (n+1)! = (n+1) * n!. The product form of eq. (4) is a theorem there (Finset.prod_range_add_one_eq_factorial).

Example. The first values are 1!=11! = 1, 2!=22! = 2, 3!=63! = 6, 4!=244! = 24, 5!=1205! = 120, 6!=7206! = 720, 7!=50407! = 5040, 8!=403208! = 40320, 9!=3628809! = 362880, 10!=3 628 80010! = 3\,628\,800. The number 100!100! has 158158 decimal digits and begins with the digits 93329332.

Proof of the digit count. The number of decimal digits of N≥1N \ge 1 is ⌊log⁡10N⌋+1\lfloor \log_{10} N \rfloor + 1. A sum of logarithms gives log⁡10100!=∑k=1100log⁡10k=157.9700…\log_{10} 100! = \sum_{k=1}^{100}\log_{10} k = 157.9700\ldots, so the number 100!100! has 157+1=158157 + 1 = 158 digits. The leading digits come from the fractional part: 100.9700…=9.3326…10^{0.9700\ldots} = 9.3326\ldots. □\square

Definition (the factorial of zero, TAOCP eq. 1.2.5(5)). 0!=10! = 1.

Node: TA2HWE9VM8 · Lean: TAOCP.S1_2_5.eq5 · status: proved

Explanation. The product of zero factors is 11 by the convention for empty products (Section 1.2.3). The convention is also the right count: a set with no objects has exactly one arrangement, the empty row. The value 0!=10! = 1 keeps the formula n!=(n−1)! nn! = (n-1)!\,n true for n=1n = 1, and the formula of eq. (21), xk‾=x!/(x−k)!x^{\underline{k}} = x!/(x-k)!, true for k=xk = x. The same convention gives (n0)=1\binom{n}{0} = 1 in Section 1.2.6.

Example. For n=3n = 3 and k=3k = 3, p3,3=3!/(3−3)!=6/0!=6p_{3,3} = 3!/(3 - 3)! = 6/0! = 6. If 0!0! were 00 the formula would break, and if it were any number other than 11 the count would be wrong.

Theorem (the recursion of the factorial, TAOCP eq. 1.2.5(6)). For each positive integer nn, n!=(n−1)! n.n! = (n-1)!\,n.

Node: TA2MWKDW86 · Lean: TAOCP.S1_2_5.eq6 · status: proved

Explanation. This is the rule behind both methods of building permutations: each permutation of n−1n - 1 objects makes nn permutations of nn objects. It is also the single rule that, with 0!=10! = 1, fixes the factorials of all nonnegative integers. Later in the section the same rule, Γ(x+1)=x Γ(x)\Gamma(x+1) = x\,\Gamma(x) of eq. (14), is the one that the extension to real numbers must keep. In Lean it is Nat.factorial_succ n : (n + 1)! = (n + 1) * n !, with the index shifted by one.

Example. 6!=5!⋅6=120⋅6=7206! = 5! \cdot 6 = 120 \cdot 6 = 720 and 10!=9!⋅10=362 880⋅10=3 628 80010! = 9! \cdot 10 = 362\,880 \cdot 10 = 3\,628\,800.

Proof. For n=1n = 1 the claim is 1!=0!⋅11! = 0! \cdot 1, which is 1=11 = 1 by eq. (5). For n≥2n \ge 2, split off the last factor of the product (4): n!=(∏k=1n−1k)⋅n=(n−1)! nn! = \left(\prod_{k=1}^{n-1} k\right) \cdot n = (n-1)!\,n. □\square

Theorem (Stirling's approximation, TAOCP eq. 1.2.5(7)). For each n≥1n \ge 1, 1<n!2πn (n/e)n<e1/(12n).1 < \frac{n!}{\sqrt{2\pi n}\,(n/e)^n} < e^{1/(12n)}.

Node: TAX5X8WQPJ · Lean: TAOCP.S1_2_5.eq7 (partial) · status: proved

Explanation. The bound says that the ratio of n!n! to 2πn (n/e)n\sqrt{2\pi n}\,(n/e)^n tends to 11 as n→∞n \to \infty. This limit is the meaning of the sign ≈\approx in the formula n!≈2πn (n/e)nn! \approx \sqrt{2\pi n}\,(n/e)^n of eq. (7). The formula says that n!n! is about 2πn\sqrt{2\pi n} times (n/e)n(n/e)^n. The factor (n/e)n(n/e)^n is the main growth. The factor 2πn\sqrt{2\pi n} is a correction of lower order. The sharper bound shows that the ratio exceeds 11 by less than e1/(12n)−1e^{1/(12n)} - 1, which is a little more than 1/(12n)1/(12n): the bound is 8.7%8.7\% for n=1n = 1 and 0.42%0.42\% for n=20n = 20, and the true excess is 8.4%8.4\% and 0.42%0.42\%. In practice the formula is used in the form ln⁡n!=nln⁡n−n+12ln⁡(2πn)+O(1/n)\ln n! = n \ln n - n + \tfrac12 \ln(2\pi n) + O(1/n), which turns a product into a sum that can be added. The book proves (7) in Section 1.2.11.2 and gives a cruder bound in exercise 24. In Lean, Mathlib has the statement as a limit: Stirling.stirlingSeq n is n!/(2n (n/e)n)n!/(\sqrt{2n}\,(n/e)^n) and Stirling.tendsto_stirlingSeq_sqrt_pi says that it tends to π\sqrt\pi, which is the same claim, since 2nπ=2πn\sqrt{2n}\sqrt\pi = \sqrt{2\pi n}.

Example. The table compares n!n! with the formula.

nn n!n! 2πn(n/e)n\sqrt{2\pi n}(n/e)^n ratio n!/formulan!/\text{formula} 1+1/(12n)1 + 1/(12n)
5 120 118.019 1.01678 1.01667
6 720 710.078 1.01397 1.01389
10 3628800 3598695.619 1.00837 1.00833
20 2432902008176640000 2.42279×10182.42279 \times 10^{18} 1.00418 1.00417

For n=6n = 6 the formula gives 710.08710.08 against 6!=7206! = 720, so it is low by about 1.4%1.4\%. The last two columns show that the ratio is close to 1+1/(12n)1 + 1/(12n), and the proof below gives the bound.

Proof. The plan has three parts: the sequence sn=n! en/nn+1/2s_n = n!\,e^n / n^{n+1/2} has a limit C>0C > 0 with C<sn<C e1/(12n)C < s_n < C\,e^{1/(12n)}, and C=2πC = \sqrt{2\pi} by Wallis's product. This proof is not the proof of Section 1.2.11.2.

Part 1: the sequence decreases. From sn=n! en/nn+1/2s_n = n!\,e^n/n^{n+1/2}, snsn+1=n! en(n+1)n+3/2(n+1)! en+1 nn+1/2=1e(n+1n)n+1/2.\frac{s_n}{s_{n+1}} = \frac{n!\,e^n (n+1)^{n+3/2}}{(n+1)!\,e^{n+1}\,n^{n+1/2}} = \frac{1}{e}\left(\frac{n+1}{n}\right)^{n+1/2}. So ln⁡(sn/sn+1)=(n+12)ln⁡n+1n−1\ln(s_n/s_{n+1}) = (n + \tfrac12)\ln\frac{n+1}{n} - 1. Put t=1/(2n+1)t = 1/(2n+1). Then n+1n=1+t1−t\frac{n+1}{n} = \frac{1+t}{1-t} and n+12=12tn + \tfrac12 = \frac{1}{2t}. The series ln⁡1+t1−t=2(t+t33+t55+⋯ )\ln\frac{1+t}{1-t} = 2\left(t + \frac{t^3}{3} + \frac{t^5}{5} + \cdots\right) holds for ∣t∣<1|t| < 1, so ln⁡snsn+1=t23+t45+t67+⋯ .\ln\frac{s_n}{s_{n+1}} = \frac{t^2}{3} + \frac{t^4}{5} + \frac{t^6}{7} + \cdots. All terms are positive, so sn>sn+1s_n > s_{n+1}. Also each term is at most the term of 13(t2+t4+⋯ )\frac13(t^2 + t^4 + \cdots), so 0<ln⁡snsn+1<t23(1−t2)=13((2n+1)2−1)=112 n(n+1)=112(1n−1n+1).0 < \ln\frac{s_n}{s_{n+1}} < \frac{t^2}{3(1 - t^2)} = \frac{1}{3((2n+1)^2 - 1)} = \frac{1}{12\,n(n+1)} = \frac{1}{12}\left(\frac1n - \frac1{n+1}\right).

Part 2: the limit exists. The sequence sns_n decreases. The sequence un=sne−1/(12n)u_n = s_n e^{-1/(12n)} increases, since ln⁡(un+1/un)=ln⁡(sn+1/sn)+112n−112(n+1)>0\ln(u_{n+1}/u_n) = \ln(s_{n+1}/s_n) + \frac1{12n} - \frac1{12(n+1)} > 0 by the bound of Part 1. Then u1≤un≤sn≤s1=eu_1 \le u_n \le s_n \le s_1 = e, so sns_n is bounded below by u1=e11/12>0u_1 = e^{11/12} > 0. A decreasing sequence that is bounded below has a limit C≥e11/12>0C \ge e^{11/12} > 0. The limit of unu_n is also CC, because e−1/(12n)→1e^{-1/(12n)} \to 1. Since sns_n decreases strictly to CC, each sn>Cs_n > C. Since unu_n increases strictly to CC, each un<Cu_n < C, that is sn<C e1/(12n)s_n < C\,e^{1/(12n)}.

Part 3: the value of CC. Let Im=∫0π/2sin⁡mx dxI_m = \int_0^{\pi/2} \sin^m x\,dx. Integration by parts gives Im+2=m+1m+2ImI_{m+2} = \frac{m+1}{m+2} I_m, with I0=π/2I_0 = \pi/2 and I1=1I_1 = 1. So I2n=π2⋅(2n−1)(2n−3)⋯12n(2n−2)⋯2=π2(2nn)4−nI_{2n} = \frac{\pi}{2}\cdot\frac{(2n-1)(2n-3)\cdots 1}{2n(2n-2)\cdots 2} = \frac{\pi}{2}\binom{2n}{n}4^{-n} and I2n+1=2n(2n−2)⋯2(2n+1)(2n−1)⋯3=4n(2n+1)(2nn)I_{2n+1} = \frac{2n(2n-2)\cdots 2}{(2n+1)(2n-1)\cdots 3} = \frac{4^n}{(2n+1)\binom{2n}{n}}. Since 0≤sin⁡x≤10 \le \sin x \le 1, the sequence ImI_m decreases, so I2n+1≤I2n≤I2n−1=2n+12nI2n+1I_{2n+1} \le I_{2n} \le I_{2n-1} = \frac{2n+1}{2n} I_{2n+1}. Thus I2n/I2n+1→1I_{2n}/I_{2n+1} \to 1, which says π2 (2n+1)(2nn)216n→1,so(2nn)∼4nπn.\frac{\pi}{2}\,\frac{(2n+1)\binom{2n}{n}^2}{16^n} \to 1, \qquad\text{so}\qquad \binom{2n}{n} \sim \frac{4^n}{\sqrt{\pi n}}. Now (2nn)=(2n)!(n!)2\binom{2n}{n} = \frac{(2n)!}{(n!)^2}, and in terms of sns_n this is s2nsn2⋅22n+1/2n\frac{s_{2n}}{s_n^2}\cdot\frac{2^{2n+1/2}}{\sqrt n} (because (2n)2n+1/2=22n+1/2n2n+1/2(2n)^{2n+1/2} = 2^{2n+1/2} n^{2n+1/2} and (nn+1/2)2=n2n+1(n^{n+1/2})^2 = n^{2n+1}, and the powers of ee cancel). Let n→∞n \to \infty: s2n/sn2→1/Cs_{2n}/s_n^2 \to 1/C, so (2nn)n/4n→2/C\binom{2n}{n}\sqrt n/4^n \to \sqrt2/C. Comparing with the first limit, 2/C=1/π\sqrt2/C = 1/\sqrt\pi, so C=2πC = \sqrt{2\pi}. □\square

The strict form. With C=2πC = \sqrt{2\pi}, the bound C<sn<Ce1/(12n)C < s_n < Ce^{1/(12n)} is the claim 1<n!/(2πn(n/e)n)<e1/(12n)1 < n!/(\sqrt{2\pi n}(n/e)^n) < e^{1/(12n)}. The ratio to the formula is always greater than 11, as the table shows.

Theorem (the exponent of a prime in n!n!, Legendre's rule, TAOCP eq. 1.2.5(8)). Let pp be a prime and n≥0n \ge 0. The largest μ\mu with pμ∣n!p^\mu \mid n! is μ=⌊np⌋+⌊np2⌋+⌊np3⌋+⋯=∑k≥1⌊npk⌋.\mu = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \cdots = \sum_{k \ge 1}\left\lfloor \frac{n}{p^k} \right\rfloor.

Node: TABMAWKDEM · Lean: TAOCP.S1_2_5.eq8, TAOCP.S1_2_5.eq8_padic · status: proved

Explanation. The sum is written as an infinite sum, but it has finitely many nonzero terms, since ⌊n/pk⌋=0\lfloor n/p^k \rfloor = 0 as soon as pk>np^k > n. Each term can come from the one before it by an integer division, since ⌊n/pk+1⌋=⌊⌊n/pk⌋/p⌋\lfloor n/p^{k+1} \rfloor = \lfloor \lfloor n/p^k \rfloor / p \rfloor (Section 1.2.4). This gives a fast way to find μ\mu: divide nn by pp, throw away the remainder, add the quotient to a sum, and repeat with the quotient. Mathlib states the rule as Nat.Prime.emultiplicity_factorial, with the sum on a finite range Finset.Ico 1 b for any bb larger than log⁡pn\log_p n.

Example. For n=100n = 100 and p=5p = 5: ⌊100/5⌋=20\lfloor 100/5 \rfloor = 20, ⌊20/5⌋=4\lfloor 20/5 \rfloor = 4, and ⌊4/5⌋=0\lfloor 4/5 \rfloor = 0, so μ=24\mu = 24. That is why 100!100! ends in exactly 2424 zeros: μ2=50+25+12+6+3+1=97≥24\mu_2 = 50 + 25 + 12 + 6 + 3 + 1 = 97 \ge 24, so there are 2424 pairs 2⋅52 \cdot 5. For n=25n = 25 all the exponents are

pp 2 3 5 7 11 13 17 19 23
μ\mu 22 10 6 3 2 1 1 1 1

so 25!=222 310 56 73 112 13⋅17⋅19⋅2325! = 2^{22}\,3^{10}\,5^6\,7^3\,11^2\,13\cdot 17\cdot 19\cdot 23. A run of a short program confirms the product.

Proof. Fix k≥1k \ge 1. The number of m∈{1,…,n}m \in \{1, \ldots, n\} that are multiples of pkp^k is ⌊n/pk⌋\lfloor n/p^k \rfloor. Let v(m)v(m) be the exponent of pp in mm, that is the largest jj with pj∣mp^j \mid m. Then mm is a multiple of pkp^k exactly when k≤v(m)k \le v(m), so mm is counted in the term kk of the sum for each kk from 11 to v(m)v(m), and in no other term. Hence mm is counted v(m)v(m) times in all, and ∑k≥1⌊npk⌋=∑m=1nv(m).\sum_{k \ge 1}\left\lfloor \frac{n}{p^k}\right\rfloor = \sum_{m=1}^{n} v(m). The exponent of pp in the product n!=∏m=1nmn! = \prod_{m=1}^n m is the sum of the exponents of pp in the factors, which is ∑m=1nv(m)\sum_{m=1}^n v(m). For the shortcut, let x=n/pkx = n/p^k. For an integer rr, r≤x/pr \le x/p is equivalent to rp≤xrp \le x, and since rprp is an integer this is equivalent to rp≤⌊x⌋rp \le \lfloor x \rfloor, that is r≤⌊x⌋/pr \le \lfloor x \rfloor/p. So ⌊x/p⌋=⌊⌊x⌋/p⌋\lfloor x/p \rfloor = \lfloor \lfloor x \rfloor / p \rfloor, which is the claim. □\square

A closed form with the digits of nn. If n=akpk+⋯+a1p+a0n = a_k p^k + \cdots + a_1 p + a_0 with 0≤ai<p0 \le a_i < p, then ⌊n/pj⌋=∑i≥jaipi−j\lfloor n/p^j \rfloor = \sum_{i \ge j} a_i p^{i-j}, and summing over j≥1j \ge 1 gives μ=∑iai (pi−1)/(p−1)=(n−s)/(p−1)\mu = \sum_i a_i\,(p^i - 1)/(p - 1) = (n - s)/(p - 1), where s=a0+⋯+aks = a_0 + \cdots + a_k is the sum of the digits. For n=100n = 100 and p=5p = 5 the digits are 4,0,04, 0, 0 and μ=(100−4)/4=24\mu = (100 - 4)/4 = 24, as before.

Definition (the termial, TAOCP eq. 1.2.5(9)). For a positive integer nn the termial of nn is n?=1+2+⋯+n=∑k=1nk.n? = 1 + 2 + \cdots + n = \sum_{k=1}^{n} k.

Node: TAV952E1A7 · Lean: TAOCP.S1_2_5.termial · status: not stated yet

Explanation. The name is the book's own, and it is a game: the termial is to the sum what the factorial is to the product. It is useful as a laboratory. The sum has a closed form, which extends to all real nn at once, and the question of what n!n! should mean for non-integers is easier to see with this model first. Mathlib does not name the termial, but Finset.sum_range_id_mul_two n : (∑ i ∈ range n, i) * 2 = n * (n - 1) is its closed form, with the sum over 0,…,n−10, \ldots, n-1 (use n+1n + 1 for nn).

Example. 1?=11? = 1, 2?=32? = 3, 3?=63? = 6, 4?=104? = 10, 10?=5510? = 55, and each is n?=(n−1)?+nn? = (n-1)? + n.

Theorem (the closed form of the termial, TAOCP eq. 1.2.5(10)). For each positive integer nn, n?=12 n(n+1).n? = \tfrac12\,n(n+1). The right side defines x?x? for each real xx, and then x?=x+(x−1)?x? = x + (x-1)? for all real xx.

Node: TA3MNK0ZC3 · Lean: TAOCP.S1_2_5.eq10, TAOCP.S1_2_5.termialReal (partial) · status: proved

Explanation. The formula is the arithmetic series of Section 1.2.3 (that section's eq. (15)). The book uses it to show how an extension of a function from the integers to the reals can be found: pick a formula that holds for integers and that makes sense for real arguments. For the termial this is easy and the extension is polynomial. The identity x?=x+(x−1)?x? = x + (x-1)? is the analogue of n!=n (n−1)!n! = n\,(n-1)!, and it is the rule that the extension keeps.

Example. For x=52x = \tfrac52: (52)?=12⋅52⋅72=358(\tfrac52)? = \tfrac12\cdot\tfrac52\cdot\tfrac72 = \tfrac{35}8. The recursion checks: 52+(32)?=52+12⋅32⋅52=208+158=358\tfrac52 + (\tfrac32)? = \tfrac52 + \tfrac12\cdot\tfrac32\cdot\tfrac52 = \tfrac{20}8 + \tfrac{15}8 = \tfrac{35}8. For x=10x = 10 the formula gives 12⋅10⋅11=55\tfrac12 \cdot 10 \cdot 11 = 55.

Proof. Write S=1+2+⋯+nS = 1 + 2 + \cdots + n and S=n+(n−1)+⋯+1S = n + (n-1) + \cdots + 1. Add the two rows place by place: each of the nn places sums to n+1n + 1, so 2S=n(n+1)2S = n(n+1). For the recursion, for real xx, x+(x−1)?=x+12(x−1)x=12x(2+x−1)=12x(x+1)=x?x + (x-1)? = x + \tfrac12(x-1)x = \tfrac12 x(2 + x - 1) = \tfrac12 x(x+1) = x?. □\square

The extension of a function from {1,2,…}\{1, 2, \ldots\} to the reals is not unique: the function x?+sin⁡(2πx)x? + \sin(2\pi x) also satisfies the recursion and agrees with n?n? at the integers. For the termial the polynomial is the natural choice. For n!n! the book looks for the analogous natural choice, and the next four equations show what fails and what works.

Theorem (a series for n!n! that fails as a definition, Stirling's series, TAOCP eq. 1.2.5(11)). Let ck=∑j=0k(−1)j/j!c_k = \sum_{j=0}^{k} (-1)^j/j!. For each nonnegative integer nn the sum below has finitely many nonzero terms and the equation holds. For each nn that is not a nonnegative integer, the series does not converge: n!=1+(1−11!)n+(1−11!+12!)n(n−1)+⋯=∑k≥0ck nk‾.n! = 1 + \Bigl(1 - \tfrac1{1!}\Bigr) n + \Bigl(1 - \tfrac1{1!} + \tfrac1{2!}\Bigr) n(n-1) + \cdots = \sum_{k \ge 0} c_k\, n^{\underline{k}}.

Node: TATD2K1CQE · status: not stated yet

Explanation. Each term is a falling power times a constant, so for an integer nn the term nk‾n^{\underline{k}} is 00 when k>nk > n, and the sum has n+1n + 1 terms. This looks like a good formula for n!n! that makes sense for other nn, but it is not: at a non-integer nn no falling power is 00, and the terms do not tend to 00. The coefficient ckc_k is the partial sum of the series of e−1e^{-1}, so ck→e−1c_k \to e^{-1}, and k! ck=dkk!\,c_k = d_k counts the permutations of kk objects that leave no object in its place (the derangements). The book states (11) and defers the proof to Section 1.2.6; the proof below is by counting and does not need that section.

Example. For n=4n = 4 the coefficients are c0=1c_0 = 1, c1=0c_1 = 0, c2=12c_2 = \tfrac12, c3=13c_3 = \tfrac13, c4=38c_4 = \tfrac38, and 4k‾=1,4,12,24,244^{\underline k} = 1, 4, 12, 24, 24. The terms are 1,0,6,8,91, 0, 6, 8, 9 and their sum is 24=4!24 = 4!. For n=5n = 5 the terms are 1,0,10,20,45,441, 0, 10, 20, 45, 44 and the sum is 120=5!120 = 5!. For n=5/2n = 5/2 the terms are 1, 0, 1.875, 0.625, −0.3516, 0.5156,…1,\ 0,\ 1.875,\ 0.625,\ -0.3516,\ 0.5156, \ldots, and the term for k=20k = 20 is −3.3×1013-3.3 \times 10^{13}: they grow and the sum does not exist.

Proof of the formula for an integer nn. Let dk=k! ckd_k = k!\,c_k. By induction on kk, d0=1d_0 = 1 and dk=k dk−1+(−1)kd_k = k\,d_{k-1} + (-1)^k, because ck=ck−1+(−1)k/k!c_k = c_{k-1} + (-1)^k/k!. The first values are d0,…,d6=1,0,1,2,9,44,265d_0, \ldots, d_6 = 1, 0, 1, 2, 9, 44, 265. Next, dkd_k counts the permutations of {1,…,k}\{1, \ldots, k\} that fix no point. By the rule of inclusion and exclusion, the number of permutations that fix none of the kk points is ∑j=0k(−1)j(kj)(k−j)!=k!∑j=0k(−1)j/j!=dk\sum_{j=0}^{k}(-1)^j\binom{k}{j}(k-j)! = k!\sum_{j=0}^k (-1)^j/j! = d_k, because the sum over a set JJ of jj points that are fixed counts (k−j)!(k-j)! permutations. Now count all n!n! permutations of {1,…,n}\{1, \ldots, n\} by the set MM of the points that they move (the points that are not fixed). A set MM with ∣M∣=k|M| = k occurs for exactly dkd_k permutations, since the permutation is the identity outside MM and a permutation of MM without a fixed point inside MM. There are (nk)\binom{n}{k} sets MM of size kk, so n!=∑k=0n(nk)dk=∑k=0nnk‾k! k! ck=∑k=0nck nk‾.n! = \sum_{k=0}^{n}\binom{n}{k} d_k = \sum_{k=0}^n \frac{n^{\underline k}}{k!}\,k!\,c_k = \sum_{k=0}^n c_k\,n^{\underline k}. Here (nk)\binom nk is the number of subsets of size kk (Section 1.2.6) and (nk) k!=nk‾\binom nk\,k! = n^{\underline k} is the count of eq. (2) for the ordered choices of kk points. The proof uses no result that depends on eq. (11).

Proof of the divergence. Suppose that nn is not a nonnegative integer. Then n−j≠0n - j \ne 0 for each j≥0j \ge 0, so nk‾≠0n^{\underline k} \ne 0 for all kk, and ∣nk+1‾∣=∣n−k∣ ∣nk‾∣|n^{\underline{k+1}}| = |n - k|\,|n^{\underline k}|. Since ck→e−1≠0c_k \to e^{-1} \ne 0, the terms tk=cknk‾t_k = c_k n^{\underline k} satisfy ∣tk+1/tk∣=∣n−k∣ ∣ck+1/ck∣→∞|t_{k+1}/t_k| = |n-k|\,|c_{k+1}/c_k| \to \infty. A series whose terms do not tend to 00 does not converge. Here ∣tk∣|t_k| is eventually increasing, so it does not tend to 00. □\square

Theorem (a series for ln⁡n!\ln n!, TAOCP eq. 1.2.5(12)). There is exactly one sequence a1,a2,…a_1, a_2, \ldots of real numbers for which the equation below holds for each nonnegative integer nn. It is ak=1k!∑j=0k(−1)k−j(kj)ln⁡j!a_k = \frac{1}{k!}\sum_{j=0}^{k}(-1)^{k-j}\binom kj \ln j!. ln⁡n!=a1n+a2 n(n−1)+⋯=∑k≥0ak+1∏0≤j≤k(n−j).\ln n! = a_1 n + a_2\,n(n-1) + \cdots = \sum_{k \ge 0} a_{k+1}\prod_{0 \le j \le k}(n - j).

Node: TAKJDAFN4G · status: not stated yet

Explanation. This is Stirling's second attempt. The series is the expansion of f(n)=ln⁡n!f(n) = \ln n! in falling powers, in the form that Newton found for any function: each term has a constant ak=Δkf(0)/k!a_k = \Delta^k f(0)/k! made from the differences of ff. At an integer the sum is finite, so it equals ln⁡n!\ln n! for trivial reasons. The hard part is not in the theorem above: it is the claim that the series also converges at non-integers and gives ln⁡\ln of the right value. Stirling could not prove this. Hermite proved it in 1900 (the book cites this). C1 does not prove Hermite's theorem, and the book does not either. What C1 proves is that the coefficients are forced, so the question has no choice in it. The first coefficients are a1=0a_1 = 0, a2=12ln⁡2≈0.34657a_2 = \tfrac12\ln 2 \approx 0.34657, a3=16ln⁡34≈−0.04795a_3 = \tfrac16\ln\tfrac34 \approx -0.04795, a4≈0.00708a_4 \approx 0.00708 and a5≈−0.00097a_5 \approx -0.00097.

Example. For n=5n = 5: 5k‾=5,20,60,120,1205^{\underline k} = 5, 20, 60, 120, 120 for k=1,…,5k = 1, \ldots, 5 and 00 after. The sum 0⋅5+0.34657⋅20−0.04795⋅60+0.00708⋅120−0.00097⋅1200 \cdot 5 + 0.34657 \cdot 20 - 0.04795 \cdot 60 + 0.00708 \cdot 120 - 0.00097 \cdot 120 is about 4.78764.7876, and ln⁡120=4.78749\ln 120 = 4.78749 (the exact coefficients give the same digits). For n=2n = 2: 2a1+2a2=ln⁡22a_1 + 2a_2 = \ln 2 gives a2=12ln⁡2a_2 = \tfrac12 \ln 2 as above.

Proof. Let f(n)=ln⁡n!f(n) = \ln n! for n≥0n \ge 0 and let Δf(n)=f(n+1)−f(n)\Delta f(n) = f(n+1) - f(n). Let EE be the shift, Ef(n)=f(n+1)Ef(n) = f(n+1), so Δ=E−1\Delta = E - 1. The operators EE and 11 commute, so the binomial theorem holds for E=1+ΔE = 1 + \Delta: En=∑k=0n(nk)ΔkE^n = \sum_{k=0}^n\binom nk\Delta^k. Apply this to ff at 00: f(n)=∑k=0n(nk)Δkf(0)f(n) = \sum_{k=0}^n\binom nk\Delta^k f(0). Likewise Δk=(E−1)k=∑j(−1)k−j(kj)Ej\Delta^k = (E-1)^k = \sum_j(-1)^{k-j}\binom kj E^j, so Δkf(0)=∑j(−1)k−j(kj)f(j)\Delta^kf(0) = \sum_j (-1)^{k-j}\binom kj f(j). Since (nk)=nk‾/k!\binom nk = n^{\underline k}/k! and Δ0f(0)=f(0)=ln⁡0!=0\Delta^0 f(0) = f(0) = \ln 0! = 0, the formula holds with ak=Δkf(0)/k!a_k = \Delta^k f(0)/k!, which is the stated value. For uniqueness, suppose that ∑kbknk‾\sum_k b_k n^{\underline k} equals f(n)f(n) for each integer n≥0n \ge 0. At n=1n = 1 only b1b_1 is nonzero in the sum, so b1=f(1)b_1 = f(1). At each next nn, the term bnnn‾b_n n^{\underline n} is the only one that is new, and nn‾=n!≠0n^{\underline n} = n! \ne 0, so bnb_n is fixed by f(n)f(n) and the b1,…,bn−1b_1, \ldots, b_{n-1}. □\square

Definition (the factorial as Euler's limit, TAOCP eq. 1.2.5(13)). For each real or complex number nn that is not a negative integer, the limit below exists, and it is the value of n!n!. For a negative integer nn the denominator contains the factor 00 for large mm, and the value is taken to be infinite. n!=lim⁡m→∞mn m!(n+1)(n+2)⋯(n+m).n! = \lim_{m\to\infty}\frac{m^n\,m!}{(n+1)(n+2)\cdots(n+m)}.

Node: TAXD28BRRQ · Lean: TAOCP.S1_2_5.eq13 (partial) · status: proved

Explanation. This is the extension that works. It is built from one idea: the ratio (n+m)!/m!(n+m)!/m! is a product of nn factors that are each about mm when mm is large, so (n+m)!/m!≈mn(n+m)!/m! \approx m^n for integer nn. Solving for n!n! gives the form of the limit. For an integer n≥0n \ge 0 the product (n+1)⋯(n+m)(n+1)\cdots(n+m) is (n+m)!/n!(n+m)!/n! and the limit reduces to n!n! by an exact calculation. For a non-integer nn the same formula is used as a definition, and the convergence has to be proved. Mathlib states the limit in the form of eq. (15): Real.GammaSeq s m is the quotient of (15) with x=sx = s, and Real.GammaSeq_tendsto_Gamma says that it tends to Real.Gamma s. At s=n+1s = n + 1 that quotient is the quotient of (13) times m/(n+m+1)m/(n+m+1), a factor that tends to 11. Exercises 8 and 22 of the book explain why (13) is a reasonable choice.

Example. For n=3n = 3 the quotient is 6 m3(m+1)(m+2)(m+3)\frac{6\,m^3}{(m+1)(m+2)(m+3)}, which is 0.5828⋅60.5828\cdot 6 for m=10m = 10, 0.9424⋅60.9424\cdot 6 for m=100m = 100 and 0.9940⋅60.9940\cdot 6 for m=1000m = 1000, so it tends to 6=3!6 = 3!. For n=12n = \tfrac12 the quotient for m=105m = 10^5 is 0.8862240.886224, and π/2=0.886227\sqrt\pi/2 = 0.886227.

Proof that the limit is n!n! for integer n≥0n \ge 0. The product (n+1)(n+2)⋯(n+m)(n+1)(n+2)\cdots(n+m) is (n+m)!/n!(n+m)!/n!, so the quotient equals n!⋅mn m!(n+m)!=n!⋅mn(m+1)(m+2)⋯(m+n)=n!∏j=1nmm+j.n!\cdot\frac{m^n\,m!}{(n+m)!} = n!\cdot\frac{m^n}{(m+1)(m+2)\cdots(m+n)} = n!\prod_{j=1}^{n}\frac{m}{m+j}. Each of the nn factors m/(m+j)m/(m+j) tends to 11, so the product tends to 11 and the limit is n!n!. □\square

Proof that the limit exists for each real nn that is not a negative integer. The same proof works for complex nn with the principal values of the powers. Write x=nx = n. Since mx=∏k=1m−1(1+1/k)xm^x = \prod_{k=1}^{m-1}(1+1/k)^x (the product telescopes to mxm^x), and m!/((x+1)⋯(x+m))=∏k=1mkx+km!/((x+1)\cdots(x+m)) = \prod_{k=1}^{m}\frac{k}{x+k}, mxm!(x+1)⋯(x+m)=mx(m+1)x⋅(m+1)x m!(x+1)⋯(x+m)=(mm+1)x∏k=1m(1+1/k)x1+x/k.\frac{m^x m!}{(x+1)\cdots(x+m)} = \frac{m^x}{(m+1)^x}\cdot\frac{(m+1)^x\,m!}{(x+1)\cdots(x+m)} = \left(\frac{m}{m+1}\right)^{x}\prod_{k=1}^{m}\frac{(1 + 1/k)^x}{1 + x/k}. The first factor tends to 11. The factors of the product are 1+εk1 + \varepsilon_k with ln⁡(1+εk)=xln⁡(1+1k)−ln⁡(1+xk)\ln(1+\varepsilon_k) = x\ln(1 + \tfrac1k) - \ln(1 + \tfrac xk). For k>2∣x∣k > 2|x|, Taylor expansion of the two logarithms gives ln⁡(1+1/k)=1k+O(k−2)\ln(1+1/k) = \frac1k + O(k^{-2}) and ln⁡(1+x/k)=xk+O(k−2)\ln(1 + x/k) = \frac xk + O(k^{-2}), so xln⁡(1+1k)−ln⁡(1+xk)=O(k−2)x\ln(1+\frac1k) - \ln(1+\frac xk) = O(k^{-2}) with a constant that depends on xx only. The series ∑kk−2\sum_k k^{-2} converges, so the series of the logarithms converges absolutely, and the partial products converge to a nonzero limit (no factor is zero since xx is not a negative integer). □\square

Theorem (the gamma function and the factorial, TAOCP eq. 1.2.5(14)). For each xx that is not a negative integer, x!=Γ(x+1),andΓ(x+1)=x Γ(x)  for each x∉{0,−1,−2,…}.x! = \Gamma(x+1), \qquad\text{and}\qquad \Gamma(x+1) = x\,\Gamma(x)\ \text{ for each } x \notin \{0, -1, -2, \ldots\}.

Node: TAT6P5YAK2 · Lean: TAOCP.S1_2_5.eq14, TAOCP.S1_2_5.eq14_factorial (partial) · status: proved

Explanation. This is a change of notation due to Legendre. The function Γ(x)=(x−1)!\Gamma(x) = (x-1)! is the factorial moved one step to the left. It is neater in many formulas, for instance in those with Γ(x)Γ(1−x)\Gamma(x)\Gamma(1-x) and in the integral ∫0∞e−ttx−1 dt\int_0^\infty e^{-t}t^{x-1}\,dt. The identity Γ(x+1)=xΓ(x)\Gamma(x+1) = x\Gamma(x) is the rule n!=n (n−1)!n! = n\,(n-1)! for real xx. In Mathlib, Real.Gamma_add_one says Real.Gamma (s + 1) = s * Real.Gamma s for s ≠ 0, and Real.Gamma_nat_eq_factorial says Real.Gamma (n + 1) = n !.

Example. Γ(6)=5!=120\Gamma(6) = 5! = 120, Γ(3.5)=2.5 Γ(2.5)=2.5⋅1.5 Γ(1.5)=3.3234\Gamma(3.5) = 2.5\,\Gamma(2.5) = 2.5 \cdot 1.5\,\Gamma(1.5) = 3.3234 and Γ(1.5)=12Γ(12)=12π=0.88623\Gamma(1.5) = \tfrac12\Gamma(\tfrac12) = \tfrac12\sqrt\pi = 0.88623. So (12)!=Γ(32)=12π(\tfrac12)! = \Gamma(\tfrac32) = \tfrac12\sqrt\pi, the value that Stirling found.

Proof. Let gm(y)=mym!y(y+1)⋯(y+m)g_m(y) = \frac{m^y m!}{y(y+1)\cdots(y+m)} be the quantity of eq. (15), so Γ(y)=lim⁡gm(y)\Gamma(y) = \lim g_m(y). Then gm(x+1)=mx+1 m!(x+1)(x+2)⋯(x+m+1)=mx m!(x+1)⋯(x+m)⋅mx+m+1g_m(x+1) = \frac{m^{x+1}\,m!}{(x+1)(x+2)\cdots(x+m+1)} = \frac{m^x\,m!}{(x+1)\cdots(x+m)}\cdot\frac{m}{x+m+1}. The first factor tends to x!x! by (13) and the second tends to 11, so Γ(x+1)=x!\Gamma(x+1) = x!. For the recursion, gm(x+1)=x gm(x)⋅mx+m+1g_m(x+1) = x\,g_m(x)\cdot\frac{m}{x+m+1}, since x gm(x)=mx m!(x+1)⋯(x+m)x\,g_m(x) = \frac{m^x\,m!}{(x+1)\cdots(x+m)}. Let m→∞m \to \infty to get Γ(x+1)=x Γ(x)\Gamma(x+1) = x\,\Gamma(x). □\square

Definition (the gamma function as a limit, TAOCP eq. 1.2.5(15)). For xx not zero and not a negative integer, Γ(x)=x!x=lim⁡m→∞mx m!x(x+1)(x+2)⋯(x+m).\Gamma(x) = \frac{x!}{x} = \lim_{m\to\infty}\frac{m^x\,m!}{x(x+1)(x+2)\cdots(x+m)}.

Node: TAN3FDZEVK · Lean: TAOCP.S1_2_5.eq15 (partial) · status: proved

Explanation. This is (13) with a different bookkeeping: the factor xx moves from the numerator to the denominator, so that the quotient has a pole at x=0x = 0 and at each negative integer, where one denominator factor is 00. The graph of Γ\Gamma on the real line has a pole at each of 0,−1,−2,…0, -1, -2, \ldots and a single minimum for x>0x > 0, at x≈1.46163x \approx 1.46163 where Γ≈0.88560\Gamma \approx 0.88560 (the book's Fig. 7). In Mathlib, Real.Gamma is defined by the integral for x>0x > 0 and by the functional equation for the rest, and it agrees with the limit by Real.GammaSeq_tendsto_Gamma.

Example. Γ(12)=lim⁡m1/2 m!/(12⋅32⋯(m+12))\Gamma(\tfrac12) = \lim m^{1/2}\,m!/(\tfrac12\cdot\tfrac32\cdots(m+\tfrac12)). For m=10m = 10 the quantity is 1.70931.7093, for m=1000m = 1000 it is 1.77181.7718 and for m=105m = 10^5 it is 1.772451.77245, which tends to π=1.772454\sqrt\pi = 1.772454.

Proof. Divide the formula (13) by xx. Since Γ(x)=x!/x\Gamma(x) = x!/x and x!=lim⁡mxm!/((x+1)⋯(x+m))x! = \lim m^x m!/((x+1)\cdots(x+m)), the limit of 1x⋅mx m!(x+1)⋯(x+m)\frac{1}{x}\cdot\frac{m^x\,m!}{(x+1)\cdots(x+m)} is Γ(x)\Gamma(x). The quantity under the limit is gm(x)g_m(x). □\square

Remark (an integral form). For x>0x > 0, gm(x)=mx∫01(1−t)mtx−1 dtg_m(x) = m^x\int_0^1(1-t)^m t^{x-1}\,dt. Indeed, by mm integrations by parts, ∫01(1−t)mtx−1dt=m!x(x+1)⋯(x+m)\int_0^1(1-t)^m t^{x-1}dt = \frac{m!}{x(x+1)\cdots(x+m)}. The substitution t↦t/mt \mapsto t/m gives gm(x)=∫0m(1−t/m)mtx−1 dtg_m(x) = \int_0^m(1 - t/m)^m t^{x-1}\,dt. Since (1−t/m)m↑e−t(1 - t/m)^m \uparrow e^{-t} and 0≤(1−t/m)m≤e−t0 \le (1-t/m)^m \le e^{-t}, the theorem of dominated convergence gives Γ(x)=∫0∞e−ttx−1 dt\Gamma(x) = \int_0^\infty e^{-t}t^{x-1}\,dt for x>0x > 0 (exercises 19 and 20 of the book). This integral is the form in which Mathlib defines Γ\Gamma, and the next two proofs use it.

Theorem (the reflection formula, TAOCP eq. 1.2.5(16)). For each zz that is not an integer, (−z)! Γ(z)=πsin⁡πz.(-z)!\,\Gamma(z) = \frac{\pi}{\sin\pi z}.

Node: TAAPFX3WDA · Lean: TAOCP.S1_2_5.eq16 (partial) · status: proved

Explanation. Since (−z)!=Γ(1−z)(-z)! = \Gamma(1-z), the formula says Γ(z)Γ(1−z)=π/sin⁡πz\Gamma(z)\Gamma(1-z) = \pi/\sin\pi z. It links the values at zz and at 1−z1 - z. It shows that Γ\Gamma never takes the value 00 (the right side is never 00), and it gives the value Γ(12)=π\Gamma(\tfrac12) = \sqrt\pi at once. It is the tool that makes 1/Γ(z)1/\Gamma(z) an entire function (exercise 1.2.7-24 of the book). In Mathlib it is Complex.Gamma_mul_Gamma_one_sub (z : ℂ) : Gamma z * Gamma (1 - z) = π / sin (π * z).

Example. For z=12z = \tfrac12: Γ(12)2=π/sin⁡(π/2)=π\Gamma(\tfrac12)^2 = \pi/\sin(\pi/2) = \pi, so Γ(12)=π\Gamma(\tfrac12) = \sqrt\pi (the root is positive since Γ(x)>0\Gamma(x) > 0 for x>0x > 0 by the integral). Then Γ(−12)=Γ(12)/(−12)=−2π=−3.5449\Gamma(-\tfrac12) = \Gamma(\tfrac12)/(-\tfrac12) = -2\sqrt\pi = -3.5449. For z=13z = \tfrac13: Γ(13)Γ(23)=π/sin⁡(π/3)=2π/3=3.6276\Gamma(\tfrac13)\Gamma(\tfrac23) = \pi/\sin(\pi/3) = 2\pi/\sqrt3 = 3.6276, and a numerical check gives Γ(13)=2.67894\Gamma(\tfrac13) = 2.67894, Γ(23)=1.35412\Gamma(\tfrac23) = 1.35412, whose product is 3.627603.62760.

Proof. The proof uses the product formula of Euler for the sine, sin⁡πz=πz∏k≥1(1−z2/k2)\sin\pi z = \pi z\prod_{k\ge1}(1 - z^2/k^2) (the book gives it in exercise 23 as a known fact, and C1 takes it as the input). By (15) with x=zx = z and by (13) with n=−zn = -z, the product of the two limits is the limit of the product: gm(z)⋅m−z m!(1−z)(2−z)⋯(m−z)=m!2z (z+1)⋯(z+m) (1−z)⋯(m−z).g_m(z)\cdot\frac{m^{-z}\,m!}{(1-z)(2-z)\cdots(m-z)} = \frac{m!^2}{z\,(z+1)\cdots(z+m)\,(1-z)\cdots(m-z)}. The powers mzm^z and m−zm^{-z} cancel. The denominator is z∏k=1m(k+z)(k−z)=z∏k=1m(k2−z2)z\prod_{k=1}^{m}(k+z)(k-z) = z\prod_{k=1}^m(k^2 - z^2). Divide numerator and denominator by m!2=∏k2m!^2 = \prod k^2: the quotient is [z∏k=1m(1−z2/k2)]−1\Bigl[z\prod_{k=1}^{m}(1 - z^2/k^2)\Bigr]^{-1}. As m→∞m \to \infty this tends to [z∏k≥1(1−z2/k2)]−1=π/sin⁡πz\bigl[z\prod_{k\ge1}(1 - z^2/k^2)\bigr]^{-1} = \pi/\sin\pi z. □\square

Theorem (Hankel's contour integral for 1/Γ1/\Gamma, TAOCP eq. 1.2.5(17)). Let the path of integration come from −∞-\infty below the negative real axis, go counterclockwise around the origin, and return to −∞-\infty above the negative real axis, and let t−zt^{-z} have its principal value. Then for each complex zz, 1Γ(z)=12πi∫et dttz.\frac{1}{\Gamma(z)} = \frac{1}{2\pi i}\int \frac{e^t\,dt}{t^z}.

Node: TAZDRQAGCW · status: not stated yet

Explanation. The formula holds at each zz, also where Γ(z)\Gamma(z) is infinite: there 1/Γ(z)=01/\Gamma(z) = 0. The integral converges for each zz because ∣et∣|e^t| decays like eRe⁡te^{\operatorname{Re} t} on the two branches to −∞-\infty, and it is an analytic function of zz with no poles. So 1/Γ1/\Gamma is an entire function. Hankel's formula is used when a proof needs 1/Γ1/\Gamma itself, without the poles of Γ\Gamma.

Example. For z=3z = 3 the integrand et/t3e^t/t^3 has no branch cut, so the two infinite branches cancel and the path is a closed circle around 00. By the residue theorem the integral is 2πi2\pi i times the coefficient of t2t^2 in ete^t, which is 12\tfrac12. So the right side is 12=1/Γ(3)=1/2!\tfrac12 = 1/\Gamma(3) = 1/2!. For z=0z = 0 and each negative integer, ett−ze^t t^{-z} is entire, the closed path gives 00, and 1/Γ(z)=01/\Gamma(z) = 0, as it must.

Proof. Take Re⁡z<1\operatorname{Re} z < 1 first. Shrink the path to the two sides of the negative real axis and a small circle of radius ρ\rho. On the circle ∣ett−z∣≤Cρ−Re⁡z|e^t t^{-z}| \le C\rho^{-\operatorname{Re}z} and its length is 2πρ2\pi\rho, so its contribution is O(ρ1−Re⁡z)→0O(\rho^{1 - \operatorname{Re}z}) \to 0. On the lower side t=re−iπt = re^{-i\pi} with rr from ∞\infty to 00, so et=e−re^t = e^{-r}, t−z=r−zeiπzt^{-z} = r^{-z}e^{i\pi z} and dt=−drdt = -dr. This part gives eiπz∫0∞e−rr−z dr=eiπz Γ(1−z)e^{i\pi z}\int_0^\infty e^{-r}r^{-z}\,dr = e^{i\pi z}\,\Gamma(1-z). On the upper side t=reiπt = re^{i\pi} with rr from 00 to ∞\infty, which gives −e−iπz Γ(1−z)-e^{-i\pi z}\,\Gamma(1-z). The sum is (eiπz−e−iπz)Γ(1−z)=2isin⁡(πz) Γ(1−z)(e^{i\pi z} - e^{-i\pi z})\Gamma(1-z) = 2i\sin(\pi z)\,\Gamma(1-z). Divide by 2πi2\pi i and use (16): sin⁡πzπΓ(1−z)=1Γ(z)\frac{\sin\pi z}{\pi}\Gamma(1-z) = \frac{1}{\Gamma(z)}. Both sides are entire functions of zz (the integral is entire since the integrand is entire in zz and the convergence is uniform on compact sets), and they agree on the half plane Re⁡z<1\operatorname{Re} z < 1, so they agree everywhere by the identity theorem. □\square

Definition (the falling power, TAOCP eq. 1.2.5(18)). For a positive integer kk and any xx, the falling power xk‾x^{\underline{k}}, read "xx to the kk falling", is the product below. By the convention for empty products x0‾=1x^{\underline 0} = 1. xk‾=x(x−1)⋯(x−k+1)=∏j=0k−1(x−j).x^{\underline{k}} = x(x-1)\cdots(x-k+1) = \prod_{j=0}^{k-1}(x - j).

Node: TAYMSEK4R4 · Lean: TAOCP.fallingPow, TAOCP.S1_2_5.eq18 · status: proved

Explanation. The falling power is the product of kk factors that start at xx and fall by 11 each time. It is a polynomial in xx of degree kk. The number pnkp_{nk} of eq. (2) is nk‾n^{\underline k}, and nn‾=n!n^{\underline n} = n!. In Lean, TAOCP.fallingPow x k is the value of descPochhammer R k at xx, for xx in any ring. For natural numbers it agrees with Nat.descFactorial n k of Mathlib, by TAOCP.fallingPow_natCast.

Example. 73‾=7⋅6⋅5=2107^{\underline 3} = 7 \cdot 6 \cdot 5 = 210. For a non-integer: (12)2‾=12⋅(−12)=−14(\tfrac12)^{\underline 2} = \tfrac12\cdot(-\tfrac12) = -\tfrac14 and (52)3‾=52⋅32⋅12=158(\tfrac52)^{\underline 3} = \tfrac52\cdot\tfrac32\cdot\tfrac12 = \tfrac{15}8. For k>nk > n with nn a natural number, nk‾=0n^{\underline k} = 0.

Definition (the rising power, TAOCP eq. 1.2.5(19)). For a positive integer kk and any xx, the rising power xk‾x^{\overline{k}}, read "xx to the kk rising", is the product below, and x0‾=1x^{\overline 0} = 1. xk‾=x(x+1)⋯(x+k−1)=∏j=0k−1(x+j).x^{\overline{k}} = x(x+1)\cdots(x+k-1) = \prod_{j=0}^{k-1}(x + j).

Node: TAHQZSJ0KG · Lean: TAOCP.risingPow, TAOCP.S1_2_5.eq19 · status: proved

Explanation. The rising power has the same kk factors but they grow. Like the falling power, it is a polynomial of degree kk in xx. It also counts: for a positive integer xx, the number xk‾/k!x^{\overline k}/k! is the number of multisets of size kk whose elements come from xx kinds. In Lean, TAOCP.risingPow x k is the value of ascPochhammer R k at xx. In Mathlib, Nat.ascFactorial n k equals n(n+1)⋯(n+k−1)n(n+1)\cdots(n+k-1) in the current convention (earlier versions started at n+1n+1), and TAOCP.risingPow_natCast links the two.

Example. 73‾=7⋅8⋅9=5047^{\overline 3} = 7\cdot 8\cdot 9 = 504, (12)2‾=12⋅32=34(\tfrac12)^{\overline 2} = \tfrac12\cdot\tfrac32 = \tfrac34 and (52)3‾=52⋅72⋅92=3158(\tfrac52)^{\overline 3} = \tfrac52\cdot\tfrac72\cdot\tfrac92 = \tfrac{315}8.

Theorem (falling and rising powers are related, TAOCP eq. 1.2.5(20)). For each integer k≥0k \ge 0 and each xx, xk‾=(x+k−1)k‾=(−1)k (−x)k‾.x^{\overline k} = (x + k - 1)^{\underline k} = (-1)^k\,(-x)^{\underline k}.

Node: TAEYMGYQSX · Lean: TAOCP.fallingPow_eq_risingPow, TAOCP.S1_2_5.eq20_left, TAOCP.S1_2_5.eq20_right · status: not stated yet

Explanation. Two reductions follow. A rising power is a falling power that starts at the other end, and it is a falling power of −x-x with a sign. So each result about one kind gives a result about the other. In Lean, TAOCP.fallingPow_eq_risingPow x k : fallingPow x k = risingPow (x - k + 1) k is the first equality with xx replaced by x−k+1x - k + 1, and TAOCP.risingPow_neg x k : risingPow (-x) k = (-1) ^ k * fallingPow x k is the second with xx replaced by −x-x. For natural numbers Mathlib has Nat.add_descFactorial_eq_ascFactorial' (n k : ℕ) : (n + k - 1).descFactorial k = n.ascFactorial k.

Example. With x=7x = 7, k=3k = 3: 73‾=5047^{\overline 3} = 504, (7+2)3‾=9⋅8⋅7=504(7+2)^{\underline 3} = 9\cdot8\cdot7 = 504 and (−1)3(−7)3‾=−(−7)(−8)(−9)=504(-1)^3(-7)^{\underline 3} = -(-7)(-8)(-9) = 504. With x=52x = \tfrac52, k=3k = 3 all three expressions equal 3158\tfrac{315}8.

Proof. The factors of xk‾x^{\overline k} are x,x+1,…,x+k−1x, x+1, \ldots, x+k-1. List them from the last to the first: (x+k−1),(x+k−2),…,(x+k−1)−(k−1)=x(x+k-1), (x+k-2), \ldots, (x+k-1) - (k-1) = x. This is the product ∏j=0k−1(x+k−1−j)\prod_{j=0}^{k-1}(x + k - 1 - j), which is (x+k−1)k‾(x+k-1)^{\underline k}. For the second equality, the factor x+jx + j equals −(−x−j)-(-x - j), so ∏j=0k−1(x+j)=(−1)k∏j=0k−1(−x−j)=(−1)k(−x)k‾\prod_{j=0}^{k-1}(x + j) = (-1)^k\prod_{j=0}^{k-1}(-x - j) = (-1)^k(-x)^{\underline k}. □\square

Theorem (falling and rising powers by factorials and gamma, TAOCP eq. 1.2.5(21)). Whenever the quotients are defined, xk‾=x!(x−k)!=Γ(x+1)Γ(x−k+1),xk‾=Γ(x+k)Γ(x).x^{\underline{k}} = \frac{x!}{(x-k)!} = \frac{\Gamma(x+1)}{\Gamma(x-k+1)}, \qquad x^{\overline{k}} = \frac{\Gamma(x+k)}{\Gamma(x)}.

Node: TA2Z3K9STM · Lean: TAOCP.risingPow_eq_Gamma_div, TAOCP.S1_2_5.eq21_falling, TAOCP.S1_2_5.eq21_rising · status: not stated yet

Explanation. The right sides make sense for each kk for which the two gamma values exist, so they define xk‾x^{\underline k} and xk‾x^{\overline k} for non-integer kk too. This is the use that the book suggests. The conditions are that x+1x + 1 and x−k+1x - k + 1 are not poles (are not nonpositive integers) for the first formula, and the same for xx and x+kx + k in the second. In Lean, TAOCP.risingPow_eq_Gamma_div states the second formula for real xx that is not 00 or a negative integer. For natural numbers, Nat.descFactorial_eq_div (h : k ≤ n) : n.descFactorial k = n ! / (n - k)! is the first formula, and Nat.ascFactorial_eq_div (n k : ℕ) : (n + 1).ascFactorial k = (n + k)! / n ! is the second with x=n+1x = n + 1.

Example. For x=5x = 5, k=3k = 3: 5!/2!=60=5⋅4⋅35!/2! = 60 = 5\cdot4\cdot3. For x=52x = \tfrac52, k=3k = 3: Γ(72)/Γ(12)=158π/π=158\Gamma(\tfrac72)/\Gamma(\tfrac12) = \tfrac{15}{8}\sqrt\pi/\sqrt\pi = \tfrac{15}8, which is 52⋅32⋅12\tfrac52\cdot\tfrac32\cdot\tfrac12. For the rising power with x=12x = \tfrac12, k=2k = 2: Γ(52)/Γ(12)=34π/π=34\Gamma(\tfrac52)/\Gamma(\tfrac12) = \tfrac{3}{4}\sqrt\pi/\sqrt\pi = \tfrac34.

Proof. Apply the rule Γ(y+1)=y Γ(y)\Gamma(y + 1) = y\,\Gamma(y) of (14) kk times: Γ(x+1)=x Γ(x)=x(x−1)Γ(x−1)=⋯=x(x−1)⋯(x−k+1) Γ(x−k+1)\Gamma(x+1) = x\,\Gamma(x) = x(x-1)\Gamma(x-1) = \cdots = x(x-1)\cdots(x-k+1)\,\Gamma(x-k+1). Dividing by Γ(x−k+1)\Gamma(x - k + 1) gives the first formula, and Γ(x+1)/Γ(x−k+1)=x!/(x−k)!\Gamma(x+1)/\Gamma(x-k+1) = x!/(x-k)! by (14). Likewise Γ(x+k)=(x+k−1)(x+k−2)⋯x Γ(x)\Gamma(x+k) = (x+k-1)(x+k-2)\cdots x\,\Gamma(x), and dividing by Γ(x)\Gamma(x) gives xk‾x^{\overline k}. The use of the rule stops short of a pole because the quotients are defined. □\square

A law of exponents for the falling powers. For nonnegative integers mm and nn, xm+n‾=xm‾ (x−m)n‾x^{\underline{m+n}} = x^{\underline m}\,(x - m)^{\underline n}. The factors of xm+n‾x^{\underline{m+n}} are x,…,x−m−n+1x, \ldots, x - m - n + 1; the first mm are xm‾x^{\underline m} and the last nn start at x−mx - m. For x=73x = \tfrac73, m=2m = 2, n=3n = 3 both sides are 280243\tfrac{280}{243}. This is not the law xm+n=xmxnx^{m+n} = x^m x^n: the second factor is shifted. For the rising powers the law is xm+n‾=xm‾ (x+m)n‾x^{\overline{m+n}} = x^{\overline m}\,(x+m)^{\overline n}.

Algorithms and programs

The section has no algorithm and no program. Its two constructions of permutations (Method 1 and Method 2) and its procedure for the exponent of a prime are given as pseudocode with worked runs in the blocks of eq. (3) and eq. (8).

The exercises

The table has the 25 exercises of Section 1.2.5. The kind is the class of spec 06.3 that C1 proposes. The marker ▶ is the mark of the book for a recommended exercise.

Exercise Rating Markers Kind Lean Solution
1.2.5-1 00 K3 not yet stated X1, 1.2.5-1
1.2.5-2 10 K1 not yet stated X1, 1.2.5-2
1.2.5-3 10 K3 not yet stated X1, 1.2.5-3
1.2.5-4 13 ▶ K3 not yet stated X1, 1.2.5-4
1.2.5-5 15 K3 not yet stated X1, 1.2.5-5
1.2.5-6 17 ▶ K3 not yet stated X1, 1.2.5-6
1.2.5-7 10 M K1 not yet stated X1, 1.2.5-7
1.2.5-8 15 HM K1 not yet stated X1, 1.2.5-8
1.2.5-9 10 M K3 not yet stated X1, 1.2.5-9
1.2.5-10 20 ▶ HM K2 not yet stated X1, 1.2.5-10
1.2.5-11 15 M K1 not yet stated X1, 1.2.5-11
1.2.5-12 22 ▶ M K3 not yet stated X1, 1.2.5-12
1.2.5-13 23 M K1 not yet stated X1, 1.2.5-13
1.2.5-14 28 ▶ M K1 not yet stated X1, 1.2.5-14
1.2.5-15 15 HM K3 not yet stated X1, 1.2.5-15
1.2.5-16 15 HM K1 not yet stated X1, 1.2.5-16
1.2.5-17 20 HM K1 not yet stated X1, 1.2.5-17
1.2.5-18 20 M K1 not yet stated X1, 1.2.5-18
1.2.5-19 22 HM K1 not yet stated X1, 1.2.5-19
1.2.5-20 21 HM K1 not yet stated X1, 1.2.5-20
1.2.5-21 25 HM K1 not yet stated X1, 1.2.5-21
1.2.5-22 20 ▶ HM informal not yet stated X1, 1.2.5-22
1.2.5-23 20 HM K1 not yet stated X1, 1.2.5-23
1.2.5-24 21 ▶ HM K1 not yet stated X1, 1.2.5-24
1.2.5-25 20 M K2 not yet stated X1, 1.2.5-25

The order to do them: start with 1.2.5-1, 2 and 3, which fix the counts of eq. (2) and the two methods of eq. (3). Then do 1.2.5-4, 5 and 6, which are three computations with eqs. (4), (7) and (8). Exercises 1.2.5-7, 9 and 10 go with the termial and the first values of Γ\Gamma, and 1.2.5-10 asks at which real xx the rule Γ(x+1)=x Γ(x)\Gamma(x+1) = x\,\Gamma(x) holds. Exercises 1.2.5-11 and 12 are Legendre's rule in the digits of nn (the closed form after eq. (8) in this chapter is the answer of 12), and 1.2.5-13 and 14 take the factorial modulo a prime, with Wilson's theorem as the first case. Exercise 1.2.5-15 is a small step on permanents, and 1.2.5-21 is the chain rule for higher derivatives. The group 1.2.5-8, 16, 17, 18, 19, 20, 22 and 23 is the analysis of eqs. (11), (13), (15) and (16): 8 and 22 justify Euler's limit, 16 is the divergence proved in this chapter, 19 and 20 give the integral for Γ\Gamma that the remark after eq. (15) uses, and 23 is eq. (16). Exercise 1.2.5-24 is a simple bound on n!n! that needs only 1+x≤ex1 + x \le e^x, and 1.2.5-25 asks for the law of exponents of the falling powers, which this chapter states and proves after eq. (21).

Lean

The Lean code of tamnd/taocp-lean at main has the file TAOCP/V1/S1_2_5.lean, in the namespace TAOCP.S1_2_5. It states eqs. (1) to (8), (10) and (13) to (16) and (18) to (21). The falling and rising powers are defined in TAOCP/Notation/Pochhammer.lean as TAOCP.fallingPow and TAOCP.risingPow. The file TAOCP/Solutions/S1_2_5.lean imports no exercise yet. The Node lines name the declarations. Where the Lean statement is weaker, the Node line says "partial". The block on the permutations as a definition names Equiv.Perm of Mathlib.

Not yet stated: the series of eqs. (11) and (12) and Hankel's integral of eq. (17).

The points where Lean differs from the book are these.

  • Eqs. (1) and (3) list the permutations of ['a', 'b', 'c'] and of [1, 2, 3] as a set. TAOCP.S1_2_5.card_perm and TAOCP.S1_2_5.card_perm_succ give the count n!n! and the recurrence of the two construction methods.
  • Eq. (2) counts the injections of Fin k into Fin n, and the result is the falling power.
  • Eq. (7) states Stirling's approximation as an asymptotic equivalence (the ratio tends to 1). The two-sided bound 1<n!/(2πn(n/e)n)<e1/(12n)1 < n!/(\sqrt{2\pi n}(n/e)^n) < e^{1/(12n)} of the handbook is not stated.
  • Eq. (8) is stated with a bound bb above log⁡pn\log_p n on the sum, and TAOCP.S1_2_5.eq8_padic states it with padicValNat. Division on ℕ rounds down, so n / p ^ k is ⌊n/pk⌋\lfloor n/p^k \rfloor.
  • Eq. (10) is stated for natural nn. The termial of a real number is TAOCP.S1_2_5.termialReal, and the rule x?=x+(x−1)?x? = x + (x-1)? for real xx is not stated.
  • Eqs. (13), (15) and (16) are stated for real numbers, not for complex numbers. Mathlib gives Γ(x)=0\Gamma(x) = 0 at the poles, so TAOCP.S1_2_5.eq15 and TAOCP.S1_2_5.eq16 hold there but say nothing. TAOCP.S1_2_5.eq13 asks that xx is not a negative integer.
  • Eq. (14): TAOCP.S1_2_5.eq14 is the recursion Γ(x+1)=x Γ(x)\Gamma(x + 1) = x\,\Gamma(x) for real x≠0x \ne 0, and TAOCP.S1_2_5.eq14_factorial is n!=Γ(n+1)n! = \Gamma(n + 1) for natural nn only. The statement x!=Γ(x+1)x! = \Gamma(x + 1) for real xx is the definition by eq. (13), and it is not a separate declaration.
  • Eq. (21): the Gamma forms are stated for real xx with the hypothesis that the Gamma arguments are not poles.

The Mathlib names that the statements use are these. The factorial is Nat.factorial. The falling and rising powers have the bridges Nat.descFactorial and Nat.ascFactorial, and Nat.ascFactorial n k is n(n+1)⋯(n+k−1)n(n+1)\cdots(n+k-1). The gamma function is Real.Gamma and Complex.Gamma, with Real.Gamma_nat_eq_factorial, Real.GammaSeq_tendsto_Gamma for the limit of eq. (13), Complex.Gamma_mul_Gamma_one_sub and Real.Gamma_mul_Gamma_one_sub for the reflection formula, and Real.eq_Gamma_of_log_convex (Bohr and Mollerup) for the uniqueness of the extension. Stirling's formula is Stirling.tendsto_stirlingSeq_sqrt_pi. Derangements, for the proof of eq. (11), are numDerangements and card_derangements_eq_numDerangements.

Notes and history

The count of arrangements is old: Indian texts of the 12th century (Bhaskara II) give the rule. The sign n!n! is due to Christian Kramp (1808). Before him Euler used [n][n] and Gauss wrote Πn\Pi n. Legendre's sign Γ(x)\Gamma(x) stands for (x−1)!(x-1)!.

James Stirling published his approximation in the Methodus Differentialis (1730). Abraham de Moivre found the form nn+1/2e−nn^{n+1/2}e^{-n} with an unknown constant, and Stirling found that the constant is 2π\sqrt{2\pi}. The proof above finds the constant CC with Wallis's product (1655). The improvement n!=2πn(n/e)n(1+112n+1288n2−⋯ )n! = \sqrt{2\pi n}(n/e)^n(1 + \frac{1}{12n} + \frac{1}{288n^2} - \cdots) is an asymptotic series, which does not converge, as the book says. The two-sided bound of the proof above is of the kind that H. Robbins gave in 1955, with 1/(12n+1)1/(12n+1) and 1/(12n)1/(12n) in the exponents.

Euler gave the limit (13) in a letter to Goldbach of October 13, 1729. Euler also found an integral for n!n! in 1730. The change of variable t=−ln⁡ut = -\ln u turns his integral ∫01(−ln⁡u)x du\int_0^1(-\ln u)^x\,du into the form ∫0∞e−ttx dt=Γ(x+1)\int_0^\infty e^{-t}t^{x}\,dt = \Gamma(x+1). The reflection formula is Euler's, and the contour integral is due to Hermann Hankel (1864). Hermite's proof that Stirling's series (12) defines the factorial for non-integers is from 1900. H. Bohr and J. Mollerup proved in 1922 that the gamma function is the only function on x>0x > 0 that is positive, log-convex, equal to 11 at 11 and that obeys f(x+1)=xf(x)f(x+1) = x f(x). This explains why the choice in eq. (13) is not arbitrary. Wilson's theorem and Legendre's rule are old: Leibniz knew the first and Legendre stated the second in 1808.

The derangement numbers that the proof of eq. (11) uses were counted by Pierre Rémond de Montmort in 1708, in the problem of the matches. The word "permanent" of exercise 1.2.5-15 goes back to Cauchy (1812), and Thomas Muir gave it its present meaning.

The project has no errata of Section 1.2.5 to report.

Section 1.2.5 of TAOCP counts the orders of nn objects and gives the number n!n! its first properties: its size, its prime factors and its extension to non-integer arguments. The section is short, but it holds the gamma function, the falling and rising powers of the later sections, and the first look at Stirling's formula. This chapter of C1 explains each of the 21 numbered equations and the definition of a permutation, proves each claim in full, and shows the reader how to check every number by hand or by a short program.

The source of this page. Something is wrong? Report it.