taocp

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

Volume P2 · Concrete mathematics

The perturbation method and recurrences

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.

Contents
  1. Sums and recurrences
  2. The perturbation method
  3. Linear recurrences with constant coefficients

Sums and recurrences

Statement

A sum and a recurrence are two views of one object. The partial sums of a sequence satisfy a recurrence of the first order, and a recurrence of the first order often has a sum as its solution. This section makes the link precise and gives two methods to solve a recurrence: the repertoire method, for a recurrence with parameters, and the summation factor, for a linear recurrence of the first order.

Definition P2.2.1 (recurrence). A recurrence for a sequence x0,x1,x2,…x_0, x_1, x_2, \dots gives some first values x0,…,xd−1x_0, \dots, x_{d-1}, the initial values, and a rule that gives xnx_n for n≥dn \ge d from nn and the earlier values x0,…,xn−1x_0, \dots, x_{n-1}. A solution is a sequence that satisfies the initial values and the rule. A closed form of the solution is a formula for xnx_n that does not refer to earlier values, such as a polynomial in nn or a sum of powers cnc^n.

Lean: Mathlib defines sequences by recursion on ℕ with Nat.rec or with pattern matching; LinearRecurrence is the structure for linear recurrences with constant coefficients, and Section 2.3 uses it.

Example P2.2.2. The partial sums Sn=∑k=0nakS_n = \sum_{k=0}^{n} a_k satisfy the recurrence S0=a0S_0 = a_0 and Sn=Sn−1+anS_n = S_{n-1} + a_n for n≥1n \ge 1. This is the recursive definition of the sum with limits of Section 1.1, read as a recurrence.

Example P2.2.3. The recurrence x0=1x_0 = 1, xn=2xn−1x_n = 2 x_{n-1} has the solution xn=2nx_n = 2^n. The recurrence y0=0y_0 = 0, y1=1y_1 = 1, yn=yn−1+yn−2y_n = y_{n-1} + y_{n-2} for n≥2n \ge 2 has two initial values; its solution is the sequence of Fibonacci numbers 0,1,1,2,3,5,8,…0, 1, 1, 2, 3, 5, 8, \dots, and Section 2.3 finds a closed form.

Theorem P2.2.4 (one solution). A recurrence with initial values x0,…,xd−1x_0, \dots, x_{d-1} and a rule xn=Gn(x0,…,xn−1)x_n = G_n(x_0, \dots, x_{n-1}) for n≥dn \ge d, where each GnG_n is a function defined for all arguments, has exactly one solution.

Lean: in Mathlib a definition by recursion on ℕ gives the existence, and the uniqueness is a proof by Nat.strong_induction_on; for linear recurrences it is LinearRecurrence.eq_mk_of_is_sol_of_eq_init (proved in Mathlib).

Proof. Existence: define x0,…,xd−1x_0, \dots, x_{d-1} as given, and for n≥dn \ge d define xnx_n by the rule, from the values already defined. This is a definition by recursion, which P1 justifies. Uniqueness: let xx and yy be two solutions. Use strong induction on nn to prove xn=ynx_n = y_n. For n<dn < d both are the given initial value. For n≥dn \ge d, the induction hypothesis gives xk=ykx_k = y_k for all k<nk < n, so xn=Gn(x0,…,xn−1)=Gn(y0,…,yn−1)=ynx_n = G_n(x_0, \dots, x_{n-1}) = G_n(y_0, \dots, y_{n-1}) = y_n. □\square

Definition P2.2.5 (factorial and termial). For n≥0n \ge 0, the factorial is n!=∏k=1nkn! = \prod_{k=1}^{n} k and the termial is n?=∑k=1nkn? = \sum_{k=1}^{n} k. By the definitions of the product and the sum with limits, they satisfy the recurrences 0!=10! = 1, n!=(n−1)! nn! = (n-1)!\, n and 0?=00? = 0, n?=(n−1)?+nn? = (n-1)? + n for n≥1n \ge 1.

Lean: Mathlib Nat.factorial (notation n !), with Nat.factorial_zero, Nat.factorial_succ and Finset.prod_range_add_one_eq_factorial (proved in Mathlib); the termial has no own name and is ∑ i ∈ Finset.range (n + 1), i.

Example P2.2.6. The first factorials are 1,1,2,6,24,120,7201, 1, 2, 6, 24, 120, 720 for n=0,…,6n = 0, \dots, 6, and 10!=362880010! = 3628800. The factorial n!n! counts the orderings of nn objects: there are nn choices for the first object, and (n−1)!(n-1)! orderings of the rest. This is the recurrence n!=n⋅(n−1)!n! = n \cdot (n-1)! in words.

Example P2.2.7. The first termials are 0,1,3,6,10,15,210, 1, 3, 6, 10, 15, 21 for n=0,…,6n = 0, \dots, 6. The termial n?n? counts the pairs (i,j)(i, j) with 1≤i≤j≤n1 \le i \le j \le n, since for each jj there are jj choices of ii. The example on pairs below the diagonal in Section 1.3 counted these pairs as n(n+1)/2n(n+1)/2, so n?=n(n+1)/2n? = n(n+1)/2; the theorem on the termial below gives a second proof.

Proposition P2.2.8 (sums solve first order recurrences). A sequence satisfies xn=xn−1+anx_n = x_{n-1} + a_n for all n≥1n \ge 1 if and only if xn=x0+∑k=1nakx_n = x_0 + \sum_{k=1}^{n} a_k for all n≥0n \ge 0.

Lean: Mathlib Finset.sum_range_induction (proved in Mathlib), which proves a closed form for a sum from its recurrence, and Finset.eq_sum_range_sub, which writes xnx_n as x0x_0 plus the sum of the differences; Finset.sum_range_succ gives the other direction.

Proof. Let xn=x0+∑k=1nakx_n = x_0 + \sum_{k=1}^{n} a_k. For n≥1n \ge 1 the recursive definition of the sum gives xn=x0+∑k=1n−1ak+an=xn−1+anx_n = x_0 + \sum_{k=1}^{n-1} a_k + a_n = x_{n-1} + a_n. Conversely, if xn=xn−1+anx_n = x_{n-1} + a_n for n≥1n \ge 1, then xk−xk−1=akx_k - x_{k-1} = a_k, and the telescoping sum of Section 1.2 gives ∑k=1nak=xn−x0\sum_{k=1}^{n} a_k = x_n - x_0. □\square

Definition P2.2.9 (repertoire method). Consider a recurrence with parameters α,β,γ\alpha, \beta, \gamma of the form x0=αx_0 = \alpha and xn=cnxn−1+βf(n)+γg(n)x_n = c_n x_{n-1} + \beta f(n) + \gamma g(n) for n≥1n \ge 1, where cnc_n, ff and gg are given. The repertoire method solves it as follows. Choose simple sequences xnx_n, the repertoire, such as 11, nn, n2n^2 or 2n2^n. For each, find the values of α,β,γ\alpha, \beta, \gamma for which it is a solution. Each choice gives one linear equation for the functions AA, BB, CC of the theorem below. Enough equations determine AA, BB and CC.

Lean: not a Lean notion; each closed form that it finds is a statement proved by induction, for example with Finset.sum_range_induction.

Example P2.2.10. For x0=αx_0 = \alpha, xn=xn−1+β+γnx_n = x_{n-1} + \beta + \gamma n, the sequence xn=1x_n = 1 is a solution for (α,β,γ)=(1,0,0)(\alpha, \beta, \gamma) = (1, 0, 0). The sequence xn=nx_n = n is a solution when α=0\alpha = 0 and n=(n−1)+β+γnn = (n - 1) + \beta + \gamma n for all nn, that is, for (0,1,0)(0, 1, 0). The sequence xn=n2x_n = n^2 needs α=0\alpha = 0 and n2=(n−1)2+β+γnn^2 = (n-1)^2 + \beta + \gamma n, that is, 2n−1=β+γn2n - 1 = \beta + \gamma n, so (α,β,γ)=(0,−1,2)(\alpha, \beta, \gamma) = (0, -1, 2). These three facts give A(n)=1A(n) = 1, B(n)=nB(n) = n and −B(n)+2C(n)=n2-B(n) + 2C(n) = n^2, so C(n)=(n2+n)/2C(n) = (n^2 + n)/2.

Example P2.2.11. For x0=αx_0 = \alpha, xn=2xn−1+β+γnx_n = 2 x_{n-1} + \beta + \gamma n, the repertoire 2n2^n, 11 and nn gives three equations. The sequence 2n2^n solves the recurrence for (1,0,0)(1, 0, 0), so A(n)=2nA(n) = 2^n. The sequence 11 needs 1=2+β1 = 2 + \beta, so it solves it for (1,−1,0)(1, -1, 0), and A(n)−B(n)=1A(n) - B(n) = 1. The sequence nn needs n=2(n−1)+β+γnn = 2(n-1) + \beta + \gamma n, so it solves it for (0,2,−1)(0, 2, -1), and 2B(n)−C(n)=n2B(n) - C(n) = n. So B(n)=2n−1B(n) = 2^n - 1 and C(n)=2n+1−n−2C(n) = 2^{n+1} - n - 2. For α=β=0\alpha = \beta = 0 and γ=1\gamma = 1 the solution is xn=2n+1−n−2x_n = 2^{n+1} - n - 2, and the first values 0,1,4,11,260, 1, 4, 11, 26 agree with the recurrence.

Theorem P2.2.12 (linearity in the parameters). The solution of x0=αx_0 = \alpha, xn=cnxn−1+βf(n)+γg(n)x_n = c_n x_{n-1} + \beta f(n) + \gamma g(n) is xn=αA(n)+βB(n)+γC(n)x_n = \alpha A(n) + \beta B(n) + \gamma C(n), where AA, BB and CC are the solutions for (α,β,γ)(\alpha, \beta, \gamma) equal to (1,0,0)(1, 0, 0), (0,1,0)(0, 1, 0) and (0,0,1)(0, 0, 1). If a sequence yy solves the recurrence for some (α,β,γ)(\alpha, \beta, \gamma), then yn=αA(n)+βB(n)+γC(n)y_n = \alpha A(n) + \beta B(n) + \gamma C(n) for those values.

Lean: not in Mathlib in this form; Mathlib has the linear structure of the solutions of a homogeneous recurrence as LinearRecurrence.solSpace.

Proof. Let yn=αA(n)+βB(n)+γC(n)y_n = \alpha A(n) + \beta B(n) + \gamma C(n). Then y0=α⋅1+β⋅0+γ⋅0=αy_0 = \alpha \cdot 1 + \beta \cdot 0 + \gamma \cdot 0 = \alpha. For n≥1n \ge 1, cnyn−1+βf(n)+γg(n)=α cnA(n−1)+β(cnB(n−1)+f(n))+γ(cnC(n−1)+g(n))=αA(n)+βB(n)+γC(n),c_n y_{n-1} + \beta f(n) + \gamma g(n) = \alpha\, c_n A(n-1) + \beta \bigl(c_n B(n-1) + f(n)\bigr) + \gamma \bigl(c_n C(n-1) + g(n)\bigr) = \alpha A(n) + \beta B(n) + \gamma C(n), since AA, BB and CC satisfy their recurrences. So yy is a solution, and by the theorem on one solution it is the solution. The second claim is the first one, applied to the given values of the parameters. □\square

Theorem P2.2.13 (termial). For n≥0n \ge 0, n?=∑k=1nk=n(n+1)2n? = \sum_{k=1}^{n} k = \dfrac{n(n+1)}{2}.

Lean: Mathlib Finset.sum_range_id_mul_two and Finset.sum_range_id (proved in Mathlib), in the form ∑i=0n−1i=n(n−1)/2\sum_{i=0}^{n-1} i = n(n-1)/2.

Proof. The termial is the solution of x0=0x_0 = 0, xn=xn−1+nx_n = x_{n-1} + n, which is the recurrence of the first example of the repertoire method with (α,β,γ)=(0,0,1)(\alpha, \beta, \gamma) = (0, 0, 1). By the theorem on linearity in the parameters, n?=C(n)=n(n+1)/2n? = C(n) = n(n+1)/2. The values found there by the repertoire are correct, since each repertoire sequence was checked to be a solution, and the theorem says that every solution has the form αA+βB+γC\alpha A + \beta B + \gamma C. □\square

Theorem P2.2.14 (summation factor). Let r1,r2,…r_1, r_2, \dots be nonzero numbers and d1,d2,…d_1, d_2, \dots numbers, and let TT satisfy Tn=rnTn−1+dnT_n = r_n T_{n-1} + d_n for n≥1n \ge 1. Let Pn=∏k=1nrkP_n = \prod_{k=1}^{n} r_k, so P0=1P_0 = 1. Then Tn=Pn(T0+∑k=1ndkPk).T_n = P_n \Bigl(T_0 + \sum_{k=1}^{n} \frac{d_k}{P_k}\Bigr). A recurrence anTn=bnTn−1+cna_n T_n = b_n T_{n-1} + c_n with an,bn≠0a_n, b_n \ne 0 has this form with rn=bn/anr_n = b_n / a_n and dn=cn/and_n = c_n / a_n. The number 1/Pn1/P_n is the summation factor.

Lean: not in Mathlib in this form; the closed form for a given recurrence is proved by induction, for example with Finset.prod_range_succ and Finset.sum_range_succ.

Proof. Each PnP_n is a product of nonzero numbers, so it is nonzero. Divide the recurrence by Pn=Pn−1rnP_n = P_{n-1} r_n: TnPn=Tn−1Pn−1+dnPn\frac{T_n}{P_n} = \frac{T_{n-1}}{P_{n-1}} + \frac{d_n}{P_n}. So Un=Tn/PnU_n = T_n / P_n satisfies Un=Un−1+dn/PnU_n = U_{n-1} + d_n / P_n for n≥1n \ge 1, and U0=T0U_0 = T_0. The proposition on sums that solve first order recurrences gives Un=T0+∑k=1ndk/PkU_n = T_0 + \sum_{k=1}^{n} d_k / P_k. Multiply by PnP_n. □\square

Proof

The theorem on one solution is the base of everything in this chapter. It turns every claim "the sequence yy is the solution" into a check that yy has the right initial values and satisfies the rule. Both methods of the section use it in this way. The repertoire method guesses many solutions of related recurrences and combines them; the theorem on linearity says that the combination is the solution. The summation factor changes the unknown sequence so that the rule becomes the rule of a sum.

The repertoire method needs no guess of the final answer, only of simple sequences. The method gives the right answer whenever the chosen repertoire produces as many independent equations as there are unknown functions. If a choice gives an equation that repeats an earlier one, add another simple sequence to the repertoire.

Examples

Example P2.2.15 (a factorial recurrence). Let T0=1T_0 = 1 and Tn=nTn−1+n!T_n = n T_{n-1} + n! for n≥1n \ge 1. Here rn=nr_n = n and Pn=n!P_n = n!, and dk/Pk=k!/k!=1d_k / P_k = k!/k! = 1. The summation factor gives Tn=n!(1+n)=(n+1)!T_n = n! (1 + n) = (n+1)!. The first values are 1,2,6,241, 2, 6, 24, and indeed T2=2⋅2+2=6T_2 = 2 \cdot 2 + 2 = 6 and T3=3⋅6+6=24T_3 = 3 \cdot 6 + 6 = 24.

Example P2.2.16 (random descent). A counter starts at nn. While its value mm is positive, a step replaces mm by an integer chosen at random from 0,1,…,m−10, 1, \dots, m - 1, each with probability 1/m1/m. Let TnT_n be the expected number of steps to reach 00. Then T0=0T_0 = 0, and for n≥1n \ge 1 one step leads to each value k<nk < n with probability 1/n1/n, so Tn=1+1n∑k=0n−1Tk.T_n = 1 + \frac1n \sum_{k=0}^{n-1} T_k. This rule uses all earlier values. Multiply by nn: nTn=n+∑k=0n−1Tkn T_n = n + \sum_{k=0}^{n-1} T_k. For n≥2n \ge 2 the same equation for n−1n - 1 is (n−1)Tn−1=n−1+∑k=0n−2Tk(n-1) T_{n-1} = n - 1 + \sum_{k=0}^{n-2} T_k. Subtract: nTn−(n−1)Tn−1=1+Tn−1n T_n - (n-1) T_{n-1} = 1 + T_{n-1}, so nTn=nTn−1+1n T_n = n T_{n-1} + 1 and Tn=Tn−1+1/nT_n = T_{n-1} + 1/n. The case n=1n = 1 gives T1=1+T0=1T_1 = 1 + T_0 = 1, which fits the same rule. By the proposition on sums, Tn=∑k=1n1/kT_n = \sum_{k=1}^{n} 1/k, the harmonic number HnH_n of Chapter 9. For n=3n = 3 the value is 1+12+13=1161 + \frac12 + \frac13 = \frac{11}{6}, and the rule gives T2=1+12(0+1)=32T_2 = 1 + \frac12 (0 + 1) = \frac32 and T3=1+13(0+1+32)=116T_3 = 1 + \frac13 (0 + 1 + \frac32) = \frac{11}{6}.

Example P2.2.17 (a loop). The program fragment "for ii from 11 to nn, for jj from 11 to ii, do one step" does ∑i=1ni=n?\sum_{i=1}^{n} i = n? steps. Let LnL_n be this number. The outer loop for nn is the loop for n−1n - 1 followed by nn steps, so L0=0L_0 = 0 and Ln=Ln−1+nL_n = L_{n-1} + n, and Ln=n(n+1)/2L_n = n(n+1)/2 by the theorem on the termial. For n=1000n = 1000 the fragment does 500500500500 steps.

Example P2.2.18 (a summation factor that is not a power). Let T0=1T_0 = 1 and (n+1)Tn=2n Tn−1(n + 1) T_n = 2n\, T_{n-1} for n≥1n \ge 1. Here rn=2n/(n+1)r_n = 2n / (n+1), dn=0d_n = 0 and Pn=2nn!/(n+1)!=2n/(n+1)P_n = 2^n n! / (n+1)! = 2^n / (n+1). So Tn=2n/(n+1)T_n = 2^n / (n+1). For n=1n = 1 the rule gives 2T1=22 T_1 = 2, so T1=1=2/2T_1 = 1 = 2/2, and for n=2n = 2 it gives 3T2=43 T_2 = 4, so T2=4/3T_2 = 4/3.

Exercises

  1. [M15] Solve R0=1R_0 = 1, Rn=Rn−1+2n+1R_n = R_{n-1} + 2n + 1 with the repertoire functions AA, BB, CC of the first example of the repertoire method.
  2. [M20] Use the repertoire method to solve R0=αR_0 = \alpha, Rn=3Rn−1+β+γnR_n = 3 R_{n-1} + \beta + \gamma n for n≥1n \ge 1.
  3. [M20] Solve T0=5T_0 = 5, Tn=3Tn−1+2T_n = 3 T_{n-1} + 2 with a summation factor.
  4. [M22] Solve T0=0T_0 = 0, nTn=(n+1)Tn−1+nn T_n = (n+1) T_{n-1} + n for n≥1n \ge 1, in terms of the sums Hm=∑k=1m1/kH_m = \sum_{k=1}^{m} 1/k.
  5. [M20] Let D0=1D_0 = 1 and Dn=(2n−1)Dn−1D_n = (2n - 1) D_{n-1} for n≥1n \ge 1. Prove that Dn=(2n)!2n n!D_n = \frac{(2n)!}{2^n\, n!}.
  6. [M18] Prove that 8⋅n?+1=(2n+1)28 \cdot n? + 1 = (2n+1)^2 and that n?+(n−1)?=n2n? + (n-1)? = n^2 for n≥1n \ge 1.
  7. [M25] Let T0=0T_0 = 0 and Tn=n+1n∑k=0n−1TkT_n = n + \frac1n \sum_{k=0}^{n-1} T_k for n≥1n \ge 1. Find a closed form in terms of Hn=∑k=1n1/kH_n = \sum_{k=1}^{n} 1/k.
  8. [M24] Solve T0=0T_0 = 0, Tn=2Tn−1+nT_n = 2 T_{n-1} + n with a summation factor, and compare the answer with the second example of the repertoire method.
  9. [M25] Prove that n!≤(n+12)nn! \le \bigl(\frac{n+1}{2}\bigr)^n for n≥1n \ge 1, by pairing the factor kk with the factor n+1−kn + 1 - k.
  10. [M25] Solve R0=αR_0 = \alpha, Rn=Rn−1+β+γn+δn2R_n = R_{n-1} + \beta + \gamma n + \delta n^2 with the repertoire 11, nn, n2n^2, n3n^3, and deduce a closed form for ∑k=1nk2\sum_{k=1}^{n} k^2.

Lean

Mathlib has the factorial as Nat.factorial, with Nat.factorial_zero for 0!=10! = 1 and Nat.factorial_succ for (n+1)!=(n+1) n!(n+1)! = (n+1)\, n!. The product form is Finset.prod_range_add_one_eq_factorial. The termial has no name in Mathlib; its closed form is Finset.sum_range_id_mul_two, which states 2∑i=0n−1i=n(n−1)2 \sum_{i=0}^{n-1} i = n(n-1) in the natural numbers, and Finset.sum_range_id with a division.

A closed form for a sum is proved in Lean from the recurrence with Finset.sum_range_induction: if s0=0s_0 = 0 and sn+1=sn+f(n)s_{n+1} = s_n + f(n), then ∑i<nf(i)=sn\sum_{i < n} f(i) = s_n. This is the proposition on sums that solve first order recurrences, and most of the closed forms of this chapter follow from it with a short computation by ring or field_simp. The harmonic numbers are Mathlib harmonic, a rational number, and TAOCP/Notation (tamnd/taocp-lean main) has TAOCP.harmonicPow with TAOCP.harmonicPow_one for the bridge to harmonic.

The repertoire method and the summation factor are methods, not theorems, and they have no Lean names. Their results are statements that Lean checks one at a time.

Notes and history

TAOCP Section 1.2.5 defines the factorial as the product of eq. 1.2.5(4), with 0!=10! = 1 in eq. 1.2.5(5) and the recurrence of eq. 1.2.5(6). The termial and its closed form are eqs. 1.2.5(9) and 1.2.5(10), where the closed form also defines the termial for numbers that are not integers. Section 1.2.3 proves the arithmetic sum by a manipulation of sums, and Section 2.2 of this chapter does the same.

The repertoire method is due to the spine text GKP94, which uses it in Chapter 1 for recurrences with several parameters. The summation factor is the discrete form of the integrating factor of a linear differential equation of the first order. The random descent of the examples is a model of several algorithms: the mean number of records in a random permutation of nn elements satisfies the same recurrence, so it is also HnH_n, and harmonic numbers also give the mean depth of a node in a random search tree, as TAOCP Sections 1.2.10 and 6.2.2 show.

The perturbation method

Statement

The perturbation method finds a closed form for a sum Sn=∑k=0nakS_n = \sum_{k=0}^{n} a_k from one identity. Write Sn+1S_{n+1} in two ways: split off the last term, and split off the first term. The second way gives a sum of the shifted terms ak+1a_{k+1}. If ak+1a_{k+1} can be written with aka_k, the identity becomes an equation for SnS_n. This section applies the method to the geometric sum, the arithmetic sum, the sum ∑kxk\sum k x^k and the sums of powers.

In this section n≥0n \ge 0 is an integer, xx is a real or complex number, and 00=10^0 = 1.

Proposition P2.2.19 (perturbation identity). For every sequence a0,a1,…a_0, a_1, \dots and every n≥0n \ge 0, with Sn=∑k=0nakS_n = \sum_{k=0}^{n} a_k, Sn+an+1=a0+∑k=0nak+1.S_n + a_{n+1} = a_0 + \sum_{k=0}^{n} a_{k+1}.

Lean: Mathlib Finset.sum_range_succ and Finset.sum_range_succ' (proved in Mathlib), the two ways to split off a term of ∑k<n+1\sum_{k < n+1}.

Proof. Both sides equal Sn+1S_{n+1}. The left side is the recursive definition of the sum: the last term is split off. For the right side, split the range 0≤k≤n+10 \le k \le n + 1 into k=0k = 0 and 1≤k≤n+11 \le k \le n + 1, and shift the index of the second part with the corollary on shift and reflection of Section 1.2: ∑k=1n+1ak=∑k=0nak+1\sum_{k=1}^{n+1} a_k = \sum_{k=0}^{n} a_{k+1}. □\square

Theorem P2.2.20 (geometric sum). For x≠1x \ne 1 and every number aa, ∑k=0naxk=a 1−xn+11−x.\sum_{k=0}^{n} a x^k = a\,\frac{1 - x^{n+1}}{1 - x}. For x=1x = 1 the sum is a(n+1)a(n+1).

Lean: Mathlib geom_sum_eq (proved in Mathlib), in the form ∑i<nxi=(xn−1)/(x−1)\sum_{i < n} x^i = (x^n - 1)/(x - 1) for x≠1x \ne 1, and geom_sum_mul for (∑i<nxi)(x−1)=xn−1(\sum_{i<n} x^i)(x - 1) = x^n - 1 in every ring.

Proof. Let Sn=∑k=0naxkS_n = \sum_{k=0}^{n} a x^k. Here ak+1=x aka_{k+1} = x\, a_k. The perturbation identity and the distributive law give Sn+axn+1=a+∑k=0naxk+1=a+xSn.S_n + a x^{n+1} = a + \sum_{k=0}^{n} a x^{k+1} = a + x S_n. So (1−x)Sn=a−axn+1(1 - x) S_n = a - a x^{n+1}. Divide by 1−x≠01 - x \ne 0. For x=1x = 1 all n+1n + 1 terms are aa. □\square

Corollary P2.2.21 (infinite geometric sum). For a real number xx with ∣x∣<1|x| < 1, the infinite sum ∑k≥0xk\sum_{k \ge 0} x^k exists and equals 11−x\frac{1}{1 - x}.

Lean: Mathlib tsum_geometric_of_lt_one (proved in Mathlib) for 0≤x<10 \le x < 1, and tsum_geometric_of_abs_lt_one for ∣x∣<1|x| < 1.

Proof. By the theorem, ∑k=0nxk=11−x−xn+11−x\sum_{k=0}^{n} x^k = \frac{1}{1 - x} - \frac{x^{n+1}}{1 - x}. Since ∣x∣<1|x| < 1, xn+1→0x^{n+1} \to 0 as n→∞n \to \infty, so the partial sums tend to 11−x\frac{1}{1-x}. For the definition of an infinite sum by two limits in Section 1.1 the sum has only nonnegative indices, so this one limit is the whole condition. P5 treats the convergence of such sums in general. □\square

Theorem P2.2.22 (arithmetic sum). For all numbers aa and bb, ∑k=0n(a+bk)=(n+1)(a+bn2)=a(n+1)+b n(n+1)2.\sum_{k=0}^{n} (a + b k) = (n+1)\Bigl(a + \frac{b n}{2}\Bigr) = a(n+1) + \frac{b\, n(n+1)}{2}.

Lean: Mathlib Finset.sum_range_id_mul_two (proved in Mathlib) for the case a=0a = 0, b=1b = 1; the general case follows with Finset.sum_add_distrib and Finset.mul_sum.

Proof. Let SS be the sum. The reflection k↦n−kk \mapsto n - k of Section 1.2 gives S=∑k=0n(a+b(n−k))S = \sum_{k=0}^{n} (a + b(n - k)). Add the two forms term by term, with the law for the sum of two families: 2S=∑k=0n(2a+bn)=(n+1)(2a+bn)2S = \sum_{k=0}^{n} (2a + b n) = (n+1)(2a + bn). Divide by 22. □\square

The perturbation identity for ∑k=0nk\sum_{k=0}^{n} k alone gives Sn+(n+1)=0+Sn+(n+1)S_n + (n + 1) = 0 + S_n + (n + 1), which holds for every SnS_n and says nothing. The method fails on this sum. The way out is to perturb the sum of the next power. With Qn=∑k=0nk2Q_n = \sum_{k=0}^{n} k^2 the identity gives Qn+(n+1)2=∑k=0n(k2+2k+1)=Qn+2∑k=0nk+(n+1)Q_n + (n+1)^2 = \sum_{k=0}^{n} (k^2 + 2k + 1) = Q_n + 2 \sum_{k=0}^{n} k + (n + 1). Now QnQ_n cancels, and ∑k=0nk=(n+1)2−(n+1)2=n(n+1)2\sum_{k=0}^{n} k = \frac{(n+1)^2 - (n+1)}{2} = \frac{n(n+1)}{2}, the case a=0a = 0, b=1b = 1 of the theorem. The proposition on power sums below makes this trick general.

Theorem P2.2.23 (the sum ∑kxk\sum k x^k). For x≠1x \ne 1, ∑k=0nkxk=x−(n+1)xn+1+nxn+2(1−x)2.\sum_{k=0}^{n} k x^k = \frac{x - (n+1) x^{n+1} + n x^{n+2}}{(1 - x)^2}. For x=1x = 1 the sum is n(n+1)/2n(n+1)/2.

Lean: not in Mathlib in this finite form; the infinite form ∑k≥0kxk=x/(1−x)2\sum_{k \ge 0} k x^k = x/(1-x)^2 for ∣x∣<1|x| < 1 is tsum_coe_mul_geometric_of_norm_lt_one (proved in Mathlib).

Proof. Let Sn=∑k=0nkxkS_n = \sum_{k=0}^{n} k x^k. Here ak+1=(k+1)xk+1=x⋅kxk+x⋅xka_{k+1} = (k + 1) x^{k+1} = x \cdot k x^k + x \cdot x^k. The perturbation identity, the sum of two families and the geometric sum give Sn+(n+1)xn+1=0+xSn+x∑k=0nxk=xSn+x(1−xn+1)1−x.S_n + (n+1) x^{n+1} = 0 + x S_n + x \sum_{k=0}^{n} x^k = x S_n + \frac{x (1 - x^{n+1})}{1 - x}. So (1−x)Sn=x−xn+21−x−(n+1)xn+1(1 - x) S_n = \frac{x - x^{n+2}}{1 - x} - (n+1) x^{n+1}. Put the right side over the denominator 1−x1 - x: the numerator is x−xn+2−(n+1)xn+1+(n+1)xn+2=x−(n+1)xn+1+nxn+2x - x^{n+2} - (n+1) x^{n+1} + (n+1) x^{n+2} = x - (n+1) x^{n+1} + n x^{n+2}. Divide by 1−x1 - x once more. The case x=1x = 1 is the arithmetic sum. □\square

Theorem P2.2.24 (sum of squares). For n≥0n \ge 0, ∑k=0nk2=n(n+1)(2n+1)6.\sum_{k=0}^{n} k^2 = \frac{n(n+1)(2n+1)}{6}.

Lean: Mathlib sum_range_pow (proved in Mathlib) gives every power sum through the Bernoulli numbers; the case of squares then needs a computation with bernoulli at 00, 11, 22.

Proof. Perturb Cn=∑k=0nk3C_n = \sum_{k=0}^{n} k^3. Since (k+1)3=k3+3k2+3k+1(k+1)^3 = k^3 + 3k^2 + 3k + 1, the perturbation identity gives Cn+(n+1)3=0+Cn+3∑k=0nk2+3∑k=0nk+(n+1).C_n + (n+1)^3 = 0 + C_n + 3 \sum_{k=0}^{n} k^2 + 3 \sum_{k=0}^{n} k + (n+1). Cancel CnC_n and use the arithmetic sum: 3∑k=0nk2=(n+1)3−3n(n+1)2−(n+1)3 \sum_{k=0}^{n} k^2 = (n+1)^3 - \frac{3n(n+1)}{2} - (n+1). The right side is (n+1)(n2+2n+1−3n2−1)=(n+1)2n2+n2=n(n+1)(2n+1)2(n+1)\bigl(n^2 + 2n + 1 - \frac{3n}{2} - 1\bigr) = (n+1) \frac{2n^2 + n}{2} = \frac{n(n+1)(2n+1)}{2}. Divide by 33. □\square

Theorem P2.2.25 (sum of cubes). For n≥0n \ge 0, ∑k=0nk3=(n(n+1)2)2=(n?)2.\sum_{k=0}^{n} k^3 = \Bigl(\frac{n(n+1)}{2}\Bigr)^2 = (n?)^2.

Lean: Mathlib sum_range_pow (proved in Mathlib) for the general power sum; the identity for cubes is not stated in Mathlib under its own name.

Proof. Perturb ∑k=0nk4\sum_{k=0}^{n} k^4. Since (k+1)4=k4+4k3+6k2+4k+1(k+1)^4 = k^4 + 4k^3 + 6k^2 + 4k + 1, the fourth powers cancel as in the last proof, and (n+1)4=4∑k=0nk3+6∑k=0nk2+4∑k=0nk+(n+1).(n+1)^4 = 4 \sum_{k=0}^{n} k^3 + 6 \sum_{k=0}^{n} k^2 + 4 \sum_{k=0}^{n} k + (n + 1). By the two theorems above, 6∑k2+4∑k+(n+1)=n(n+1)(2n+1)+2n(n+1)+(n+1)=(n+1)(2n2+3n+1)=(n+1)2(2n+1)6 \sum k^2 + 4 \sum k + (n+1) = n(n+1)(2n+1) + 2n(n+1) + (n+1) = (n+1)(2n^2 + 3n + 1) = (n+1)^2 (2n + 1). So 4∑k=0nk3=(n+1)2((n+1)2−(2n+1))=(n+1)2n24 \sum_{k=0}^{n} k^3 = (n+1)^2 \bigl((n+1)^2 - (2n+1)\bigr) = (n+1)^2 n^2. Divide by 44. □\square

Proposition P2.2.26 (power sums). For m≥0m \ge 0 let Sm(n)=∑k=0nkmS_m(n) = \sum_{k=0}^{n} k^m, with S0(n)=n+1S_0(n) = n + 1. Then (n+1)m+1=∑j=0m(m+1j)Sj(n).(n+1)^{m+1} = \sum_{j=0}^{m} \binom{m+1}{j} S_j(n). So Sm(n)S_m(n) is a polynomial in nn of degree m+1m + 1 with leading coefficient 1m+1\frac{1}{m+1}, with rational coefficients.

Lean: Mathlib sum_range_pow (proved in Mathlib), Faulhaber's formula for ∑k<nkp\sum_{k < n} k^p with the Bernoulli numbers bernoulli; the binomial theorem is add_pow.

Proof. Perturb Sm+1(n)S_{m+1}(n). By the binomial theorem of P1, (k+1)m+1=∑j=0m+1(m+1j)kj(k + 1)^{m+1} = \sum_{j=0}^{m+1} \binom{m+1}{j} k^j, so the perturbation identity and the interchange of two finite sums give Sm+1(n)+(n+1)m+1=0m+1+∑j=0m+1(m+1j)Sj(n).S_{m+1}(n) + (n+1)^{m+1} = 0^{m+1} + \sum_{j=0}^{m+1} \binom{m+1}{j} S_j(n). Here 0m+1=00^{m+1} = 0, and the term j=m+1j = m + 1 on the right is Sm+1(n)S_{m+1}(n), which cancels. This proves the identity. For the second claim use strong induction on mm. For m=0m = 0, S0(n)=n+1S_0(n) = n + 1 has degree 11 and leading coefficient 11. For m≥1m \ge 1, the identity gives (m+1)Sm(n)=(n+1)m+1−∑j=0m−1(m+1j)Sj(n)(m+1) S_m(n) = (n+1)^{m+1} - \sum_{j=0}^{m-1} \binom{m+1}{j} S_j(n). By the induction hypothesis each Sj(n)S_j(n) with j<mj < m has degree j+1≤mj + 1 \le m. So the right side has degree m+1m + 1 with leading coefficient 11, and Sm(n)S_m(n) has degree m+1m + 1 with leading coefficient 1m+1\frac{1}{m+1}. □\square

Proof

Every proof of this section has three steps. First, apply the perturbation identity to a sum. Second, write the shifted term ak+1a_{k+1} as a combination of terms whose sums are known, and of the sum itself. Third, solve the resulting linear equation for the unknown sum. The method works when the sum itself appears on the right with a coefficient other than 11, as in the geometric sum with the coefficient xx. When the coefficient is 11, the sum cancels; then the identity gives a lower sum instead, and the right move is to perturb a higher sum, as in the proofs for the squares, the cubes and the power sums.

The arithmetic sum has a second proof by reflection. Both proofs are short, and the reflection proof shows a fact that the perturbation hides: the terms a+bka + bk and a+b(n−k)a + b(n - k) have the same average for every kk, so the sum is the number of terms times the average of the first and the last term.

Examples

Example P2.2.27 (a sum with halves). Take x=12x = \frac12 in the theorem on ∑kxk\sum k x^k. Then (1−x)2=14(1 - x)^2 = \frac14, and ∑k=0nk2k=4(12−n+12n+1+n2n+2)=2−2(n+1)2n+n2n=2−n+22n.\sum_{k=0}^{n} \frac{k}{2^k} = 4 \Bigl(\frac12 - \frac{n+1}{2^{n+1}} + \frac{n}{2^{n+2}}\Bigr) = 2 - \frac{2(n+1)}{2^{n}} + \frac{n}{2^{n}} = 2 - \frac{n + 2}{2^n}. For n=4n = 4 the sum is 0+12+24+38+416=1380 + \frac12 + \frac24 + \frac38 + \frac4{16} = \frac{13}{8}, and 2−616=1382 - \frac{6}{16} = \frac{13}{8}. As n→∞n \to \infty the sum tends to 22. The limit 22 is the expected number of trials up to and including the first success, when each trial succeeds with probability 12\frac12, because that number is kk with probability 2−k2^{-k}.

Example P2.2.28 (alternating signs). Take x=−1x = -1 and a=1a = 1 in the geometric sum: ∑k=0n(−1)k=1−(−1)n+12\sum_{k=0}^{n} (-1)^k = \frac{1 - (-1)^{n+1}}{2}. This is 11 for even nn and 00 for odd nn, so the sum is the bracket [n even][n \text{ even}] of Section 1.1.

Example P2.2.29 (binary numbers). Take x=2x = 2: ∑k=0n2k=2n+1−1\sum_{k=0}^{n} 2^k = 2^{n+1} - 1. The binary number with n+1n + 1 ones is one less than the next power of 22. So a counter with n+1n + 1 bits holds at most 2n+1−12^{n+1} - 1, and a binary tree in which every level 00 to nn is full has 2n+1−12^{n+1} - 1 nodes.

Example P2.2.30 (odd numbers). The arithmetic sum with a=1a = 1 and b=2b = 2 gives ∑k=0n−1(2k+1)=n(1+(n−1))=n2\sum_{k=0}^{n-1} (2k + 1) = n(1 + (n-1)) = n^2. So the first nn odd numbers add up to n2n^2. The picture is a square of n2n^2 points, cut into nn L-shaped layers of 1,3,5,…1, 3, 5, \dots points.

Exercises

  1. [M10] Find ∑k=0n3k\sum_{k=0}^{n} 3^k in closed form.
  2. [M20] Prove that ∑k=0nk 3k=(2n−1) 3n+1+34\sum_{k=0}^{n} k\, 3^k = \frac{(2n - 1)\, 3^{n+1} + 3}{4}.
  3. [M20] Prove that ∑k=0n(−1)kk=(2n+1)(−1)n−14\sum_{k=0}^{n} (-1)^k k = \frac{(2n+1)(-1)^n - 1}{4}.
  4. [M25] Use the perturbation method on ∑k=0nk2xk\sum_{k=0}^{n} k^2 x^k to prove that for x≠1x \ne 1 it equals x(1+x−(n+1)2xn+(2n2+2n−1)xn+1−n2xn+2)(1−x)3\frac{x\bigl(1 + x - (n+1)^2 x^n + (2n^2 + 2n - 1) x^{n+1} - n^2 x^{n+2}\bigr)}{(1 - x)^3}.
  5. [M20] Prove that ∑k=1n(2k−1)2=n(2n−1)(2n+1)3\sum_{k=1}^{n} (2k - 1)^2 = \frac{n(2n - 1)(2n + 1)}{3}.
  6. [M25] Use the proposition on power sums with m=4m = 4 to prove that ∑k=0nk4=n(n+1)(2n+1)(3n2+3n−1)30\sum_{k=0}^{n} k^4 = \frac{n(n+1)(2n+1)(3n^2 + 3n - 1)}{30}.
  7. [M15] Prove the theorem on the sum of cubes by induction on nn, without perturbation.
  8. [HM22] Prove that ∑k≥0kxk=x(1−x)2\sum_{k \ge 0} k x^k = \frac{x}{(1-x)^2} for real xx with ∣x∣<1|x| < 1.
  9. [M22] Let Hk=∑j=1k1/jH_k = \sum_{j=1}^{k} 1/j, with H0=0H_0 = 0. Show that perturbation of ∑k=0nHk\sum_{k=0}^{n} H_k gives nothing, and that perturbation of ∑k=0nkHk\sum_{k=0}^{n} k H_k gives ∑k=0nHk=(n+1)Hn−n\sum_{k=0}^{n} H_k = (n+1) H_n - n.
  10. [M22] Let Mn=∑i=1n∑j=1nijM_n = \sum_{i=1}^{n} \sum_{j=1}^{n} i j. Prove that Mn=(n?)2M_n = (n?)^2. Then prove that the terms with max⁡(i,j)=k\max(i, j) = k add up to k3k^3, and deduce the theorem on the sum of cubes a second time.

Lean

Mathlib states the geometric sum as geom_sum_eq, over a division ring: for x≠1x \ne 1, ∑i∈range nxi=(xn−1)/(x−1)\sum_{i \in \mathrm{range}\, n} x^i = (x^n - 1)/(x - 1). The form without division, geom_sum_mul, holds in every ring and gives the identity (∑i<nxi)(x−1)=xn−1(\sum_{i<n} x^i)(x - 1) = x^n - 1, which is the second step of the perturbation proof. Partial sums over an interval m≤i<nm \le i < n are geom_sum_Ico. The infinite sums are tsum_geometric_of_lt_one and tsum_coe_mul_geometric_of_norm_lt_one.

The perturbation identity is the pair Finset.sum_range_succ and Finset.sum_range_succ': the first splits off the last term of ∑i<n+1f(i)\sum_{i < n+1} f(i), and the second splits off the first term and shifts the rest. A Lean proof of the theorem on ∑kxk\sum k x^k follows the text: rewrite the sum for n+1n + 1 in both ways, then close the linear equation with field_simp and ring.

For the power sums, Mathlib proves Faulhaber's formula sum_range_pow with the Bernoulli numbers bernoulli, a topic of Chapter 11. The proposition on power sums of this section is the recurrence behind it. The sum of squares and the sum of cubes have no own names in Mathlib, and a proof by Finset.sum_range_succ, induction and ring is three lines.

Notes and history

TAOCP states the geometric sum as eq. 1.2.3(14) and the arithmetic sum as eq. 1.2.3(15), and proves both by manipulation of sums. The sum of squares is eq. 1.2.6(12) in the form of a sum of binomial coefficients, and Chapter 3 gives a third proof with finite calculus. Section 1.2.3 of TAOCP and its exercises also treat ∑kxk\sum k x^k.

The name perturbation method is from the spine text GKP94. The sums of squares and of cubes are very old. Aryabhata stated both around the year 500, and al-Karaji and Ibn al-Haytham gave proofs for cubes and for fourth powers. Jacob Bernoulli gave a rule for all power sums in his book of 1713, and the coefficients of his rule are the numbers now named after him. The identity ∑k3=(∑k)2\sum k^3 = (\sum k)^2 is attributed to Nicomachus of Gerasa, who noted that the cubes are sums of consecutive odd numbers.

Linear recurrences with constant coefficients

Statement

Many recurrences in the analysis of algorithms give xnx_n as a fixed linear combination of the last few values, plus a known term. The Fibonacci numbers are the main example. This section solves such recurrences of the first and the second order with a closed form, and shows how to prove a guessed closed form by induction. In this section the numbers are complex numbers, so that every polynomial has its roots.

Definition P2.2.31 (linear recurrence with constant coefficients). Let d≥1d \ge 1 and let c1,…,cdc_1, \dots, c_d be numbers with cd≠0c_d \ne 0. A linear recurrence of order dd with constant coefficients is a rule xn=c1xn−1+c2xn−2+⋯+cdxn−d+f(n)(n≥d),x_n = c_1 x_{n-1} + c_2 x_{n-2} + \cdots + c_d x_{n-d} + f(n) \qquad (n \ge d), with dd initial values x0,…,xd−1x_0, \dots, x_{d-1} and a given sequence ff. It is homogeneous if f(n)=0f(n) = 0 for all nn. Its characteristic polynomial is χ(z)=zd−c1zd−1−c2zd−2−⋯−cd\chi(z) = z^d - c_1 z^{d-1} - c_2 z^{d-2} - \cdots - c_d.

Lean: Mathlib LinearRecurrence (a structure with order and coeffs), LinearRecurrence.IsSolution and LinearRecurrence.charPoly, for homogeneous recurrences; Mathlib indexes the coefficients in the order xn,…,xn+d−1x_n, \dots, x_{n+d-1}.

Example P2.2.32. The Fibonacci numbers are defined by F0=0F_0 = 0, F1=1F_1 = 1 and Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2} for n≥2n \ge 2. This is a homogeneous recurrence of order 22 with c1=c2=1c_1 = c_2 = 1 and χ(z)=z2−z−1\chi(z) = z^2 - z - 1. The first values are 0,1,1,2,3,5,8,13,21,34,55,890, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.

Example P2.2.33. The number of nodes of a full binary tree with levels 00 to nn satisfies N0=1N_0 = 1 and Nn=2Nn−1+1N_n = 2 N_{n-1} + 1: the tree is a root and two full trees with levels 00 to n−1n - 1. This is a recurrence of order 11 with c1=2c_1 = 2 and f(n)=1f(n) = 1, so it is not homogeneous. Its characteristic polynomial is z−2z - 2.

Proposition P2.2.34 (the solutions of a homogeneous recurrence). The sequences that satisfy a fixed homogeneous linear rule of order dd for all n≥dn \ge d form a vector space under termwise addition and multiplication by numbers. A solution is fixed by its dd initial values, and for every choice of initial values there is exactly one solution. So the space has dimension dd.

Lean: Mathlib LinearRecurrence.solSpace, LinearRecurrence.eq_mk_of_is_sol_of_eq_init and LinearRecurrence.solSpace_rank (proved in Mathlib).

Proof. If xx and yy satisfy the rule and λ\lambda, μ\mu are numbers, then λxn+μyn=∑i=1dci(λxn−i+μyn−i)\lambda x_n + \mu y_n = \sum_{i=1}^{d} c_i (\lambda x_{n-i} + \mu y_{n-i}) by the distributive law, so λx+μy\lambda x + \mu y satisfies the rule too. Existence and uniqueness for given initial values are the theorem on one solution of Section 2.1. So the map from a solution to its initial values (x0,…,xd−1)(x_0, \dots, x_{d-1}) is linear and bijective onto the space of dd-tuples, which has dimension dd. □\square

Proposition P2.2.35 (geometric solutions). Let r≠0r \ne 0. The sequence xn=rnx_n = r^n satisfies the homogeneous rule xn=∑i=1dcixn−ix_n = \sum_{i=1}^{d} c_i x_{n-i} for all n≥dn \ge d if and only if χ(r)=0\chi(r) = 0.

Lean: Mathlib LinearRecurrence.geom_sol_iff_root_charPoly (proved in Mathlib).

Proof. For n≥dn \ge d, rn−∑i=1dcirn−i=rn−dχ(r)r^n - \sum_{i=1}^{d} c_i r^{n-i} = r^{n-d} \chi(r). Since rn−d≠0r^{n-d} \ne 0, this is 00 exactly when χ(r)=0\chi(r) = 0. □\square

Theorem P2.2.36 (first order). Let xn=cxn−1+bx_n = c x_{n-1} + b for n≥1n \ge 1, with constants bb and c≠0c \ne 0. If c≠1c \ne 1, then with p=b/(1−c)p = b/(1 - c), xn=cn(x0−p)+p.x_n = c^n (x_0 - p) + p. If c=1c = 1, then xn=x0+nbx_n = x_0 + n b.

Lean: not in Mathlib in this form; it is the case d=1d = 1 of LinearRecurrence plus a constant, and a direct proof is an induction with pow_succ and ring.

Proof. The constant sequence pp satisfies the rule, since cp+b=cb+b(1−c)1−c=pc p + b = \frac{cb + b(1 - c)}{1 - c} = p. So yn=xn−py_n = x_n - p satisfies yn=c yn−1y_n = c\, y_{n-1}, and yn=cny0y_n = c^n y_0 by induction. For c=1c = 1 the rule is xn=xn−1+bx_n = x_{n-1} + b, and the proposition on sums of Section 2.1 gives xn=x0+nbx_n = x_0 + nb. □\square

Theorem P2.2.37 (second order, two distinct roots). Let xn=c1xn−1+c2xn−2x_n = c_1 x_{n-1} + c_2 x_{n-2} for n≥2n \ge 2, with c2≠0c_2 \ne 0, and let χ(z)=z2−c1z−c2\chi(z) = z^2 - c_1 z - c_2 have two distinct roots r≠sr \ne s. Then xn=Arn+Bsnx_n = A r^n + B s^n for all n≥0n \ge 0, with A=x1−sx0r−s,B=rx0−x1r−s.A = \frac{x_1 - s x_0}{r - s}, \qquad B = \frac{r x_0 - x_1}{r - s}.

Lean: not in Mathlib in this form; it follows from LinearRecurrence.geom_sol_iff_root_charPoly and LinearRecurrence.eq_mk_of_is_sol_of_eq_init.

Proof. The roots are not 00, since χ(0)=−c2≠0\chi(0) = -c_2 \ne 0. By the proposition on geometric solutions, rnr^n and sns^n are solutions, and by the proposition on the solution space, so is yn=Arn+Bsny_n = A r^n + B s^n. Its initial values are y0=A+B=(r−s)x0r−s=x0y_0 = A + B = \frac{(r - s) x_0}{r - s} = x_0 and y1=Ar+Bs=rx1−rsx0+rsx0−sx1r−s=x1y_1 = A r + B s = \frac{r x_1 - rs x_0 + rs x_0 - s x_1}{r - s} = x_1. So y=xy = x by uniqueness. □\square

Corollary P2.2.38 (Binet's formula). Let ϕ=1+52\phi = \frac{1 + \sqrt5}{2} and ϕ^=1−52=1−ϕ\hat\phi = \frac{1 - \sqrt5}{2} = 1 - \phi. Then for all n≥0n \ge 0, Fn=ϕn−ϕ^n5,F_n = \frac{\phi^n - \hat\phi^n}{\sqrt5}, and FnF_n is the integer nearest to ϕn/5\phi^n / \sqrt5.

Lean: Mathlib Real.coe_fib_eq (proved in Mathlib) states Fn=(ϕn−ψn)/5F_n = (\phi^n - \psi^n)/\sqrt5 with Real.goldenRatio and Real.goldenConj, and Real.one_sub_goldenConj relates the two roots; TAOCP/Notation (tamnd/taocp-lean main) has TAOCP.fibInt for FnF_n at all integers nn.

Proof. The roots of z2−z−1z^2 - z - 1 are ϕ\phi and ϕ^\hat\phi, and they are distinct, with ϕ−ϕ^=5\phi - \hat\phi = \sqrt5 and ϕ+ϕ^=1\phi + \hat\phi = 1. With x0=0x_0 = 0 and x1=1x_1 = 1 the theorem gives A=15A = \frac{1}{\sqrt5} and B=−15B = -\frac{1}{\sqrt5}. For the second claim, ∣ϕ^∣=5−12<1|\hat\phi| = \frac{\sqrt5 - 1}{2} < 1 since 5<3\sqrt5 < 3. So ∣Fn−ϕn/5∣=∣ϕ^∣n/5≤1/5<12\bigl|F_n - \phi^n/\sqrt5\bigr| = |\hat\phi|^n / \sqrt5 \le 1/\sqrt5 < \frac12. □\square

Theorem P2.2.39 (second order, one double root). Let xn=c1xn−1+c2xn−2x_n = c_1 x_{n-1} + c_2 x_{n-2} for n≥2n \ge 2, and let χ(z)=(z−r)2\chi(z) = (z - r)^2 with r≠0r \ne 0, that is, c1=2rc_1 = 2r and c2=−r2c_2 = -r^2. Then xn=(A+Bn) rn,A=x0,B=x1r−x0.x_n = (A + B n)\, r^n, \qquad A = x_0, \qquad B = \frac{x_1}{r} - x_0.

Lean: not in Mathlib in this form; the check that nrnn r^n is a solution is a computation with ring after pow_succ.

Proof. The sequence rnr^n is a solution by the proposition on geometric solutions. The sequence nrnn r^n is a solution too: for n≥2n \ge 2, 2r(n−1)rn−1−r2(n−2)rn−2=(2(n−1)−(n−2))rn=nrn.2r (n-1) r^{n-1} - r^2 (n-2) r^{n-2} = \bigl(2(n - 1) - (n - 2)\bigr) r^n = n r^n. So yn=(A+Bn)rny_n = (A + Bn) r^n is a solution, with y0=A=x0y_0 = A = x_0 and y1=(A+B)r=x1y_1 = (A + B) r = x_1. By uniqueness y=xy = x. □\square

Proposition P2.2.40 (particular and homogeneous parts). Let pp be one solution of the rule xn=∑i=1dcixn−i+f(n)x_n = \sum_{i=1}^{d} c_i x_{n-i} + f(n) for n≥dn \ge d. Then a sequence xx satisfies this rule if and only if x−px - p satisfies the homogeneous rule with f=0f = 0.

Lean: not in Mathlib in this form; it is the subtraction of two instances of the rule, a proof by sub_eq and ring.

Proof. Subtract the rule for pp from the rule for xx: xn−pn=∑i=1dci(xn−i−pn−i)x_n - p_n = \sum_{i=1}^{d} c_i (x_{n-i} - p_{n-i}), since f(n)f(n) cancels. Conversely, add the rule for pp to the homogeneous rule for x−px - p. □\square

Proposition P2.2.41 (calls of a recursive program). A program computes FnF_n by the rule: if n≤1n \le 1 return nn, else return the value for n−1n - 1 plus the value for n−2n - 2. Let CnC_n be the number of calls, the first call included. Then Cn=2Fn+1−1C_n = 2 F_{n+1} - 1.

Lean: not in Mathlib; with Nat.fib the statement is an induction with Nat.fib_add_two.

Proof. A call with n≤1n \le 1 makes no further call, so C0=C1=1C_0 = C_1 = 1. A call with n≥2n \ge 2 makes one call for n−1n - 1 and one for n−2n - 2, so Cn=1+Cn−1+Cn−2C_n = 1 + C_{n-1} + C_{n-2}. The first values are 1,1,3,5,9,151, 1, 3, 5, 9, 15, and Fn+1F_{n+1} is 1,1,2,3,5,81, 1, 2, 3, 5, 8, which suggests the guess Cn=2Fn+1−1C_n = 2 F_{n+1} - 1. Prove it by strong induction. For n=0n = 0 and n=1n = 1 both sides are 11. For n≥2n \ge 2, the induction hypothesis and the Fibonacci rule give Cn=1+(2Fn−1)+(2Fn−1−1)=2(Fn+Fn−1)−1=2Fn+1−1C_n = 1 + (2F_n - 1) + (2F_{n-1} - 1) = 2(F_n + F_{n-1}) - 1 = 2F_{n+1} - 1. □\square

Proof

The proofs of this section rest on two facts. The first is uniqueness: a solution of a recurrence of order dd is fixed by its dd initial values (Section 2.1). The second is linearity: sums and multiples of solutions of a homogeneous rule are solutions. Together they reduce the problem to finding dd solutions of a simple form and matching the initial values. The geometric sequences rnr^n with χ(r)=0\chi(r) = 0 give the simple solutions when the roots are distinct; a double root rr gives the second solution nrnn r^n.

A recurrence that is not homogeneous needs one more solution, a particular one. The proposition on particular and homogeneous parts reduces the rest to the homogeneous case. For a constant term f(n)=bf(n) = b try a constant pp; this works when 11 is not a root of χ\chi. For a term b tnb\, t^n try p tnp\, t^n when tt is not a root. The last proposition shows the method that works for every recurrence: compute the first values, guess a closed form, and prove it by induction.

Examples

Example P2.2.42 (two roots of opposite sign). Let x0=1x_0 = 1, x1=8x_1 = 8 and xn=xn−1+6xn−2x_n = x_{n-1} + 6 x_{n-2}. Then χ(z)=z2−z−6=(z−3)(z+2)\chi(z) = z^2 - z - 6 = (z - 3)(z + 2), with r=3r = 3 and s=−2s = -2. The theorem gives A=8+25=2A = \frac{8 + 2}{5} = 2 and B=3−85=−1B = \frac{3 - 8}{5} = -1, so xn=2⋅3n−(−2)nx_n = 2 \cdot 3^n - (-2)^n. The first values are 1,8,14,62,1461, 8, 14, 62, 146, and indeed 14=8+614 = 8 + 6 and 62=14+4862 = 14 + 48.

Example P2.2.43 (a double root). Let x0=1x_0 = 1, x1=6x_1 = 6 and xn=6xn−1−9xn−2x_n = 6 x_{n-1} - 9 x_{n-2}. Then χ(z)=(z−3)2\chi(z) = (z - 3)^2. The theorem gives A=1A = 1 and B=6/3−1=1B = 6/3 - 1 = 1, so xn=(n+1) 3nx_n = (n + 1)\, 3^n. The value x2=36−9=27=3⋅9x_2 = 36 - 9 = 27 = 3 \cdot 9 agrees.

Example P2.2.44 (strings without two adjacent ones). Let WnW_n be the number of strings of nn bits with no two adjacent 11s. Then W0=1W_0 = 1 (the empty string) and W1=2W_1 = 2. For n≥2n \ge 2, a good string ends in 00, after a good string of length n−1n - 1, or ends in 0101, after a good string of length n−2n - 2. So Wn=Wn−1+Wn−2W_n = W_{n-1} + W_{n-2}. Since F2=1F_2 = 1 and F3=2F_3 = 2, uniqueness gives Wn=Fn+2W_n = F_{n+2}. For n=4n = 4 there are F6=8F_6 = 8 such strings: 00000000, 00010001, 00100010, 01000100, 10001000, 01010101, 10101010, 10011001.

Example P2.2.45 (a full binary tree). The recurrence N0=1N_0 = 1, Nn=2Nn−1+1N_n = 2 N_{n-1} + 1 of the second example of the definition is the first order case with c=2c = 2 and b=1b = 1. So p=1/(1−2)=−1p = 1/(1 - 2) = -1, and Nn=2n(1+1)−1=2n+1−1N_n = 2^n (1 + 1) - 1 = 2^{n+1} - 1. This agrees with the geometric sum of Section 2.2, since the tree has 2k2^k nodes on level kk.

Exercises

  1. [M15] Solve x0=0x_0 = 0, x1=1x_1 = 1, xn=5xn−1−6xn−2x_n = 5 x_{n-1} - 6 x_{n-2}.
  2. [M15] Solve x0=1x_0 = 1, x1=4x_1 = 4, xn=4xn−1−4xn−2x_n = 4 x_{n-1} - 4 x_{n-2}.
  3. [M18] The Lucas numbers are L0=2L_0 = 2, L1=1L_1 = 1 and Ln=Ln−1+Ln−2L_n = L_{n-1} + L_{n-2}. Prove that Ln=ϕn+ϕ^nL_n = \phi^n + \hat\phi^n, and that Ln=Fn−1+Fn+1L_n = F_{n-1} + F_{n+1} for n≥1n \ge 1.
  4. [M20] Let A0=A1=0A_0 = A_1 = 0 and An=An−1+An−2+1A_n = A_{n-1} + A_{n-2} + 1 for n≥2n \ge 2. Prove that An=Fn+1−1A_n = F_{n+1} - 1, and explain the meaning of AnA_n for the program of the proposition on calls.
  5. [M15] Prove that ∑k=0nFk=Fn+2−1\sum_{k=0}^{n} F_k = F_{n+2} - 1.
  6. [M22] Solve x0=x1=0x_0 = x_1 = 0, xn=xn−1+2xn−2+3x_n = x_{n-1} + 2 x_{n-2} + 3 with a particular solution.
  7. [M20] Prove that ϕn=Fnϕ+Fn−1\phi^n = F_n \phi + F_{n-1} for n≥1n \ge 1, and the same identity with ϕ^\hat\phi in place of ϕ\phi. Deduce Binet's formula a second time, and compute ϕ10\phi^{10} to three decimals with the identity.
  8. [M25] Solve x0=0x_0 = 0, x1=1x_1 = 1, xn=2xn−1−2xn−2x_n = 2 x_{n-1} - 2 x_{n-2} with the complex roots of χ\chi, and prove that xn=2n/2sin⁡(nπ/4)x_n = 2^{n/2} \sin(n \pi / 4).
  9. [M25] Let y0=3y_0 = 3 and yk+1=yk2−2y_{k+1} = y_k^2 - 2. Prove that yk=L2k+1y_k = L_{2^{k+1}}, with the Lucas numbers of exercise 3.
  10. [M28] Let xn=pxn−1+qxn−2x_n = p x_{n-1} + q x_{n-2} for n≥2n \ge 2. Prove that xn+1xn−1−xn2=(−q)n−1(x2x0−x12)x_{n+1} x_{n-1} - x_n^2 = (-q)^{n-1} (x_2 x_0 - x_1^2) for n≥1n \ge 1, and deduce Cassini's identity Fn+1Fn−1−Fn2=(−1)nF_{n+1} F_{n-1} - F_n^2 = (-1)^n.

Lean

Mathlib has a general theory of homogeneous linear recurrences in LinearRecurrence. A recurrence E has an order and coeffs, the predicate E.IsSolution u states the rule for a sequence u : ℕ → R, and E.mkSol init is the solution with the initial values init. The lemma LinearRecurrence.eq_mk_of_is_sol_of_eq_init is the uniqueness, LinearRecurrence.solSpace is the vector space of solutions, LinearRecurrence.solSpace_rank says that its dimension is the order, and LinearRecurrence.geom_sol_iff_root_charPoly is the proposition on geometric solutions. Mathlib does not state the closed forms for two distinct roots or one double root in general.

The Fibonacci numbers are Nat.fib, with the rule Nat.fib_add_two, and Mathlib also has Int.fib for integer indices. Binet's formula is Real.coe_fib_eq, with ϕ\phi as Real.goldenRatio and ϕ^\hat\phi as Real.goldenConj; the facts ϕ+ϕ^=1\phi + \hat\phi = 1 and 1−ϕ=ϕ^1 - \phi = \hat\phi are Real.goldenRatio_add_goldenConj and Real.one_sub_goldenConj. TAOCP/Notation (tamnd/taocp-lean main) has TAOCP.fibInt, with TAOCP.fibInt_natCast and TAOCP.fibInt_neg_natCast for the values at nn and −n-n. The nearest integer form of Binet's formula and the proposition on calls are not yet stated.

Notes and history

TAOCP defines the Fibonacci numbers by eq. 1.2.8(2), names the second root in eq. 1.2.8(13) and states Binet's formula as eq. 1.2.8(14). Section 1.2.8 proves the formula with a generating function, a method of P3; this section proves it with the characteristic polynomial and uniqueness. Chapter 10 treats the Fibonacci numbers in full.

The formula for FnF_n is named after Jacques Binet, who published it in 1843, but de Moivre and Daniel Bernoulli knew it about a century before. The solution of linear recurrences with constant coefficients by the roots of a polynomial is due to de Moivre and Euler, in the eighteenth century. The method of the last proposition, to guess a formula from small cases and prove it by induction, is the most common way to solve a recurrence in practice.

The results of Chapter 2 and the equations of TAOCP Section 1.2 that they prove:

TAOCP 1.2 Result of P2
1.2.3 eq. 14 Theorem P2.2.20
1.2.3 eq. 15 Theorem P2.2.22
1.2.5 eqs. 4 to 6 Definition P2.2.5
1.2.5 eqs. 9, 10 Definition P2.2.5 and Theorem P2.2.13
1.2.6 eq. 12 Theorem P2.2.24
1.2.8 eq. 2 Definition P2.2.31 and its first example
1.2.8 eqs. 13, 14 Corollary P2.2.38

This chapter finds closed forms. A sum ∑k=0nak\sum_{k=0}^{n} a_k and a sequence defined by a recurrence are two forms of the same problem: the partial sums satisfy the recurrence Sn=Sn−1+anS_n = S_{n-1} + a_n, and the solution of a recurrence of the first order is often a sum. The chapter gives a small set of methods that turn such a problem into a formula in nn, and it proves the special sums that TAOCP Section 1.2 uses most: the geometric sum, the arithmetic sum and the sums of squares and cubes.

Section 2.1 defines recurrences, proves that a recurrence has exactly one solution, and introduces the factorial and the termial. It solves first order recurrences with two methods: the repertoire method, which combines simple solutions of related recurrences, and the summation factor, which turns a linear recurrence into a sum. Section 2.2 presents the perturbation method, which splits a sum in two ways and solves the resulting equation. It gives the geometric and the arithmetic sum, the sum ∑kxk\sum k x^k, and the sums of powers. Section 2.3 solves linear recurrences with constant coefficients of the first and the second order, with two distinct roots or a double root, and proves Binet's formula for the Fibonacci numbers.

The chapter proves eqs. 1.2.3(14) and 1.2.3(15) of TAOCP, the factorial and the termial of eqs. 1.2.5(4) to 1.2.5(6), 1.2.5(9) and 1.2.5(10), the sum of squares of eq. 1.2.6(12), and the Fibonacci recurrence and Binet's formula of eqs. 1.2.8(2), 1.2.8(13) and 1.2.8(14). The table at the end of Section 2.3 gives the result of this chapter for each equation. The proof of Binet's formula here uses the characteristic polynomial; TAOCP uses a generating function, which is the topic of P3.

The chapter assumes P1 and Chapter 1: the laws of sums, the change of variable and the telescoping sum. Section 2.3 allows complex roots of a polynomial, but the examples in its text have real roots. Chapter 3 gives a second set of methods for sums, the finite calculus, and Chapters 9 and 10 return to the harmonic and the Fibonacci numbers that appear here for the first time.

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