Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Linear recurrences

Fibonacci's recurrence Fn=Fn-1+Fn-2 is easy to run and hard to solve: unrolling it doubles the work at every step, and its values suggest no formula to guess.

The previous lesson set up recurrences, rules that give each term of a sequence from earlier ones, and solved the simple ones by unrolling, or by guessing a formula and proving it by induction. Both methods stalled on the Fibonacci numbers 0,1,1,2,3,5,8,13,…, which also count the bit strings with no two consecutive 1s, and left one clue: the ratio of consecutive terms settles near 1.618, as if the sequence grew like a power. This lesson follows the clue to a method that solves every recurrence of that shape, then extends it to recurrences with an extra term, the Tower of Hanoi's among them.

Trying a power

A recurrence is linear with constant coefficients if each term is a fixed combination of the k terms before it, an=c1an-1+⋯+ckan-k, where the numbers ci do not depend on n. The number k is the order, and it is also how many initial conditions are needed. It is homogeneous, meaning nothing is added beyond the combination; a later section adds something.

Geometric sequences reproduce themselves under a shift: rn shifted back by one is rn-1, the same sequence divided by r. So try an=rn with r≠0 in the order 2 recurrence an=c1an-1+c2an-2. It needs rn=c1rn-1+c2rn-2 for every n, and dividing by rn-2 removes n altogether:

r2=c1r+c2

This is the characteristic equation. For order k it reads rk=c1rk-1+⋯+ck, and each root gives a solution rn, though not yet one that meets the initial conditions.

For an=5an-1-6an-2 the equation is r2-5r+6=(r-2)(r-3)=0, so 2n and 3n both satisfy the recurrence. Check one: 5×3n-1-6×3n-2=3n-2(15-6)=3n.

Two roots, two constants

The recurrence is linear, and that supplies the second ingredient. If xn and yn both satisfy it, so does Axn+Byn for any constants A and B: multiply the equation for xn by A, the one for yn by B, and add. So when the characteristic equation has distinct roots r1 and r2, every sequence

an=Ar1n+Br2n

satisfies the recurrence, and the constants are free to meet the initial conditions. Setting n=0 and n=1 gives A+B=a0 and Ar1+Br2=a1. Subtracting r1 times the first from the second leaves B(r2-r1)=a1-r1a0, solvable precisely because r1≠r2, and then A=a0-B.

That settles every such recurrence, not just some. The fitted formula and the true sequence agree at n=0 and n=1, and both compute each term from the two before it by the same rule, so by induction they agree forever. The formula is the solution, and the characteristic equation has missed nothing. Always check a fitted formula against the first few terms, since that catches almost every slip in the fit.

Example. A path 1 unit wide and n units long is paved with square tiles in 2 colours and dominoes, 1 by 2, in 3 colours. Find a formula for the number of pavings, and the number for length 6.

Sort the pavings by the last tile. A square, in 2 colours, leaves any paving of length n-1; a domino, in 3 colours, leaves any paving of length n-2. So an=2an-1+3an-2, with a0=1 (the empty path has one paving) and a1=2. The characteristic equation r2-2r-3=(r-3)(r+1)=0 has roots 3 and -1, so an=A×3n+B(-1)n. The conditions A+B=1 and 3A-B=2 give A=34 and B=14:

an=3n+1+(-1)n4

Check: the recurrence gives a2=4+3=7 and a3=14+6=20, and the formula gives 27+14=7 and 81-14=20. So a6=2187+14=547. Fractional constants are normal: the recurrence guarantees integers.

Now you. Solve an=an-1+6an-2 with a0=3 and a1=4, check against a2 and a3, and find a10.

Answer

The equation r2-r-6=(r-3)(r+2)=0 has roots 3 and -2. Then A+B=3 and 3A-2B=4 give A=2, B=1, so an=2×3n+(-2)n. The recurrence gives a2=22 and a3=46; the formula gives 18+4 and 54-8. So a10=2×59049+1024=119122.

Binet's formula

For Fn=Fn-1+Fn-2 the characteristic equation is r2=r+1, with roots

φ=1+52≈1.618,ψ=1-52≈-0.618

The first is the golden ratio, the 1.618 the ratios were approaching (the letter has nothing to do with the totient of an earlier lesson). Their difference is φ-ψ=5.

The roots are distinct, so Fn=Aφn+Bψn. The condition F0=0 gives B=-A, and then F1=1 gives A(φ-ψ)=1, so A=15. Therefore

Fn=φn-ψn5

This is Binet's formula, published by Jacques Binet in 1843, though Abraham de Moivre and Daniel Bernoulli had it more than a century before. A formula full of 5 that returns an integer for every n looks miraculous, but expanding (1+5)n and (1-5)n by the binomial theorem shows why: the terms with even powers of 5 cancel in the difference, and each survivor carries exactly one 5 to cancel the denominator.

The formula also finishes a count from the previous lesson. Bit strings of length n with no two consecutive 1s number Fn+2, so length 20 has F22=17711 of them among 1048576 strings, and each extra bit multiplies the count by about 1.618, where unrestricted strings double.

The nearest integer

Since |ψ|<1, the powers ψn shrink towards zero, alternating in sign: ψ10≈0.008. So the second term of Binet's formula, ψn5, has size at most 15≈0.447 for every n≥0, which is less than 12. The number φn5 is therefore within a half of the integer Fn:

Fnis the nearest integer toφn5

This holds even at n=0, where 0.447 rounds to 0, and the approximation tightens fast: φ105≈55.0036 against F10=55, and φ305≈832040.0000002 against F30=832040. It is the same shape as the derangement count of an earlier lesson, an exact count equal to a simple expression rounded, and for the same reason: an error term known to be below a half.

The fading term also explains the ratios. In Fn+1/Fn the ψ terms vanish in the limit, so the ratio tends to φ, overshooting and undershooting in turn as ψn changes sign: 8955≈1.61818, then 14489≈1.61798. In any order 2 recurrence with distinct real roots, the root of larger size sets the growth and the other only decides the rounding.

A repeated root

The fit needed r1≠r2. The recurrence an=6an-1-9an-2 has characteristic equation (r-3)2=0 and the single root 3, so A×3n is the only family on offer, and one constant cannot meet two initial conditions. A second solution is missing.

Nearby recurrences show where it hides. With distinct roots r and s, linearity makes sn-rns-r a solution. As s approaches r the recurrence approaches the one with a double root, and this solution approaches the derivative of xn at r, which is nrn-1. Dividing by the constant r gives the candidate nrn: the old solution with an extra factor n.

The limit only suggests it, so check. A double root means x2-c1x-c2=(x-r)2, so c1=2r and c2=-r2, and then

c1(n-1)rn-1+c2(n-2)rn-2=rn(2n-2-n+2)=nrn

So the general solution is an=(A+Bn)rn, fitted by A=a0 and (A+B)r=a1, and the induction argument shows it is every solution. At higher orders a root of multiplicity m contributes rn,nrn,…,nm-1rn.

Example. Solve an=6an-1-9an-2 with a0=1 and a1=6.

The double root is 3, so an=(A+Bn)3n. From a0=1, A=1; from a1=6, 3(1+B)=6, so B=1 and an=(n+1)3n. The recurrence gives a2=36-9=27 and a3=162-54=108, and the formula gives 3×9=27 and 4×27=108.

Now you. Solve an=10an-1-25an-2 with a0=2 and a1=15, and find a6.

Answer

The equation r2-10r+25=(r-5)2 has the double root 5, so an=(A+Bn)5n with A=2 and 5(2+B)=15, so B=1 and an=(n+2)5n. Check: the recurrence gives a2=150-50=100=4×25. So a6=8×15625=125000.

Adding a forcing term

The Tower of Hanoi recurrence Tn=2Tn-1+1, the fewest moves for n discs, is not homogeneous: the +1 is a forcing term, added whatever the earlier terms are. In general an=c1an-1+⋯+ckan-k+f(n), and linearity still does the work. If pn is one solution, a particular solution, and an is any other, subtracting their recurrences cancels f(n), so an-pn solves the homogeneous recurrence. Every solution is therefore pn plus the general homogeneous solution, with the constants fitted last, to the whole formula.

A particular solution is guessed from the shape of the forcing term, with unknown coefficients: a constant for a constant, a polynomial of degree d for a polynomial of degree d. For Hanoi, p=2p+1 gives p=-1, the homogeneous part has root 2, so Tn=A×2n-1, and T0=0 gives A=1. That is Tn=2n-1, as unrolling found, with no series to sum.

Example. A procedure given an input of size n does n steps of work and then calls itself twice on size n-1; size 0 costs nothing. Find the total work an.

The recurrence is an=2an-1+n with a0=0. The forcing term has degree 1, so try pn=αn+β. Substituting, αn+β=2α(n-1)+2β+n, and matching coefficients gives α=2α+1 and β=2β-2α, so α=-1 and β=-2. Then an=A×2n-n-2, and a0=0 gives A=2:

an=2n+1-n-2

Check: the recurrence gives 1,4,11 for n=1,2,3, and the formula gives 4-3, 8-4 and 16-5. At size 10 the work is 2036 steps.

Now you. Solve an=an-1+2an-2+4 with a0=a1=1, and find a10.

Answer

A constant p needs p=3p+4, so p=-2. The roots of r2-r-2=0 are 2 and -1, so an=A×2n+B(-1)n-2. The conditions give A+B=3 and 2A-B=3, so A=2, B=1 and an=2n+1+(-1)n-2. Check: the recurrence gives a2=7 and a3=13, as does the formula. So a10=2048+1-2=2047.

The guess fails in one case, for the reason the double root did. The regions cut by n lines in general position satisfy Rn=Rn-1+n, whose homogeneous part has root 1, so constants already solve it and αn+β cannot produce the +n. Multiply the guess by n, as with a repeated root: pn=n(αn+β) gives 2αn-α+β=n, so α=β=12 and Rn=1+n(n+1)2, the formula the previous lesson reached by guessing.

How far the method reaches

At any order the steps are the same: find the k roots of the characteristic equation, write one term per root (with extra factors of n for a repeated one), add a particular solution if there is a forcing term, and fit k initial conditions. Bit strings with no three consecutive 1s obey cn=cn-1+cn-2+cn-3, whose cubic has one real root near 1.839 and two complex ones of size below 1, so those counts grow like 1.839n. Complex roots need no new idea; the answer comes out real.

The method needs both halves of its name. The derangement recurrence Dn=(n-1)(Dn-1+Dn-2) has a coefficient that grows with n, and trying rn leaves n in the equation. The Catalan numbers, which count the ways to bracket a product, obey a recurrence in which terms multiply each other, so superposition fails. A tool that turned the counting argument itself into algebra would reach further, and the next lesson builds one: the generating function.