TExcelTAD
Home › Linear Algebra & Matrix Theory › Application: Solving Recurrences

Application: Solving Recurrences

A linear recurrence such as the Fibonacci rule Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1} defines each term from the previous few. Packing the last few terms into a vector turns the rule into one matrix multiplication per step. Eigenvalues then give a closed formula for the nn-th term, and fast matrix powers give an exact O(log⁡n)O(\log n) algorithm.

A recurrence is a matrix power

Keep the two most recent Fibonacci numbers in a vector. One step of the rule is a fixed matrix QQ:

[Fn+1Fn]=[1110][FnFn−1]so[Fn+1Fn]=Qn⋅[F1F0]=Qn[10]\begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} F_n \\ F_{n-1} \end{bmatrix} \qquad \text{so} \qquad \begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = Q^n \cdot \begin{bmatrix} F_1 \\ F_0 \end{bmatrix} = Q^n \begin{bmatrix} 1 \\ 0 \end{bmatrix}

Taking nn steps is the matrix power QnQ^n, and in fact Qn=[Fn+1FnFnFn−1]Q^n = \begin{bmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{bmatrix}. Everything from Application: Matrix Powers now applies.

Binet's formula from the eigenvalues

The characteristic polynomial of QQ is λ2−λ−1\lambda^2 - \lambda - 1 (trace 1, determinant −1), with roots

φ=1+52≈1.618034ψ=1−52≈−0.618034φ−ψ=5\varphi = \frac{1 + \sqrt{5}}{2} \approx 1.618034 \qquad \psi = \frac{1 - \sqrt{5}}{2} \approx -0.618034 \qquad \varphi - \psi = \sqrt{5}

Each eigenvalue λ\lambda has eigenvector (λ,1)(\lambda, 1), since Q(λ,1)=(λ+1,λ)Q(\lambda, 1) = (\lambda + 1, \lambda) and λ+1=λ2\lambda + 1 = \lambda^2 for these roots. The starting vector splits as

(1,0)=15⋅[(φ,1)−(ψ,1)](Fn+1,Fn)=Qn(1,0)=15⋅[φn(φ,1)−ψn(ψ,1)]Fn=φn−ψn5(Binet’s formula)\begin{aligned} (1, 0) &= \tfrac{1}{\sqrt{5}} \cdot [(\varphi, 1) - (\psi, 1)] \\ (F_{n+1}, F_n) &= Q^n(1, 0) = \tfrac{1}{\sqrt{5}} \cdot [\varphi^n(\varphi, 1) - \psi^n(\psi, 1)] \\ F_n &= \frac{\varphi^n - \psi^n}{\sqrt{5}} && \text{(Binet's formula)} \end{aligned}

Since ∣ψ∣<1\lvert\psi\rvert < 1, the ψn\psi^n term shrinks to nothing, so FnF_n is the integer closest to φn/5\varphi^n/\sqrt{5}, and the ratio Fn+1/FnF_{n+1}/F_n tends to φ\varphi, the golden ratio. SymPy confirms the algebra exactly:

python · try it yourself
from sympy import Matrix, sqrt, simplify, expand, fibonacci

Q = Matrix([[1, 1], [1, 0]])
print("eigenvalues:", list(Q.eigenvals()))
phi, psi = (1 + sqrt(5)) / 2, (1 - sqrt(5)) / 2
binet = [simplify(expand((phi**n - psi**n) / sqrt(5))) for n in range(11)]
print("Binet n=0..10:", binet)
print("matches fibonacci():", binet == [fibonacci(n) for n in range(11)])
print("Q^10 =", (Q**10).tolist())
eigenvalues: [1/2 - sqrt(5)/2, 1/2 + sqrt(5)/2]
Binet n=0..10: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
matches fibonacci(): True
Q^10 = [[89, 55], [55, 34]]

Two programs: exact powers vs. floating point

Binet's formula looks like an O(1)O(1) algorithm, but it needs φn\varphi^n to full precision. With 64-bit floats it breaks once FnF_n has more digits than a double can hold. Raising QQ to the nn-th power by repeated squaring needs only about 2⋅log⁡2n2 \cdot \log_2 n small matrix products and Python's exact integers, so it works for any nn. This is the same "square and multiply" idea as fast modular exponentiation, and an alternative to the O(n)O(n) loop or memoised recursion of Dynamic Programming.

python · try it yourself
import math

def mat_mult(X, Y):
    return [[X[0][0] * Y[0][0] + X[0][1] * Y[1][0], X[0][0] * Y[0][1] + X[0][1] * Y[1][1]],
            [X[1][0] * Y[0][0] + X[1][1] * Y[1][0], X[1][0] * Y[0][1] + X[1][1] * Y[1][1]]]

def fib(n):                          # Q^n by repeated squaring: O(log n) products
    result, base = [[1, 0], [0, 1]], [[1, 1], [1, 0]]
    while n:
        if n & 1:
            result = mat_mult(result, base)
        base = mat_mult(base, base)
        n >>= 1
    return result[0][1]              # Q^n = [[F(n+1), F(n)], [F(n), F(n-1)]]

phi = (1 + math.sqrt(5)) / 2
first_bad = next(n for n in range(200) if round(phi**n / math.sqrt(5)) != fib(n))
print("float Binet first wrong at n =", first_bad)
print("F(71) exact:", fib(71), " float:", round(phi**71 / math.sqrt(5)))
print("F(300) has", len(str(fib(300))), "digits")
float Binet first wrong at n = 71
F(71) exact: 308061521170129  float: 308061521170130
F(300) has 63 digits

General two-term recurrences

The same method solves an=c1an−1+c2an−2a_n = c_1a_{n-1} + c_2a_{n-2} for any constants. The step matrix is the companion matrix [c1c210]\begin{bmatrix} c_1 & c_2 \\ 1 & 0 \end{bmatrix}, whose characteristic polynomial is λ2−c1λ−c2\lambda^2 - c_1\lambda - c_2. So:

Roots of λ2=c1λ+c2\lambda^2 = c_1\lambda + c_2 General solution
two distinct roots r,sr, s an=α⋅rn+β⋅sna_n = \alpha \cdot r^n + \beta \cdot s^n
one repeated root rr an=(α+β⋅n)⋅rna_n = (\alpha + \beta \cdot n) \cdot r^n

The constants α,β\alpha, \beta come from a0a_0 and a1a_1. The repeated-root case is exactly when the companion matrix is not diagonalizable, which is where the extra factor nn comes from.

Example: an=5an−1−6an−2a_n = 5a_{n-1} - 6a_{n-2} with a0=1a_0 = 1, a1=4a_1 = 4. The roots of λ2−5λ+6\lambda^2 - 5\lambda + 6 are 2 and 3, so an=α⋅2n+β⋅3na_n = \alpha \cdot 2^n + \beta \cdot 3^n with α+β=1\alpha + \beta = 1 and 2α+3β=42\alpha + 3\beta = 4, giving β=2\beta = 2, α=−1\alpha = -1:

an=2⋅3n−2ncheck: a2=5⋅4−6⋅1=14=18−4 ✓a_n = 2 \cdot 3^n - 2^n \qquad \text{check: } a_2 = 5 \cdot 4 - 6 \cdot 1 = 14 = 18 - 4 \ \checkmark

SymPy's rsolve does the same automatically:

python · try it yourself
from sympy import Function, rsolve, symbols

n = symbols("n", integer=True)
a = Function("a")
sol = rsolve(a(n) - 5 * a(n - 1) + 6 * a(n - 2), a(n), {a(0): 1, a(1): 4})
print("a(n) =", sol)
print([sol.subs(n, i) for i in range(6)])
a(n) = -2**n + 2*3**n
[1, 4, 14, 46, 146, 454]

In software

  • Algorithm analysis: the running time of divide-and-conquer or dynamic-programming code is often a linear recurrence; the largest root of its characteristic polynomial gives the growth rate.
  • Counting: the number of binary strings without two consecutive 1s, of tilings of a strip, or of paths in a state machine all satisfy linear recurrences, and matrix powers count them for huge nn (modulo a prime in competitive programming).
  • Signal processing: an IIR filter yn=a⋅yn−1+b⋅yn−2+xny_n = a \cdot y_{n-1} + b \cdot y_{n-2} + x_n is a linear recurrence; it is stable exactly when both roots satisfy ∣r∣<1\lvert r\rvert < 1.
  • Random number generators: linear congruential and xorshift generators are linear recurrences, the latter over GF(2)\mathrm{GF}(2) (Fields).

Exercises

Exercise 1: A Fibonacci cousin

Solve an=an−1+2an−2a_n = a_{n-1} + 2a_{n-2} with a0=0a_0 = 0, a1=1a_1 = 1.

Answer

an=(2n−(−1)n)/3a_n = (2^n - (-1)^n)/3, giving 0,1,1,3,5,11,21,…0, 1, 1, 3, 5, 11, 21, \ldots

Solution
λ2−λ−2=(λ−2)(λ+1) → r=2, s=−1an=α⋅2n+β⋅(−1)na0:α+β=0,a1:2α−β=1 → α=13, β=−13check a3=a2+2a1=1+2=3=(8+1)/3 ✓\begin{aligned} &\lambda^2 - \lambda - 2 = (\lambda - 2)(\lambda + 1) \ \to\ r = 2,\ s = -1 \\ &a_n = \alpha \cdot 2^n + \beta \cdot (-1)^n \\ &a_0: \alpha + \beta = 0, \quad a_1: 2\alpha - \beta = 1 \ \to\ \alpha = \tfrac{1}{3},\ \beta = -\tfrac{1}{3} \\ &\text{check } a_3 = a_2 + 2a_1 = 1 + 2 = 3 = (8 + 1)/3 \ \checkmark \end{aligned}

Exercise 2: Lucas numbers

The Lucas numbers follow the Fibonacci rule but start L0=2L_0 = 2, L1=1L_1 = 1. Show that Ln=φn+ψnL_n = \varphi^n + \psi^n.

Answer

Ln=φn+ψnL_n = \varphi^n + \psi^n (α=β=1\alpha = \beta = 1).

Solution
Ln=α⋅φn+β⋅ψnL0:α+β=2L1:αφ+βψ=1α=β=1 works: φ+ψ=1 (the trace of Q). The solution is unique since φ≠ψ.check: L2=φ2+ψ2=(φ+ψ)2−2φψ=1−2⋅(−1)=3=L1+L0 ✓\begin{aligned} &L_n = \alpha \cdot \varphi^n + \beta \cdot \psi^n \\ &L_0: \alpha + \beta = 2 \\ &L_1: \alpha\varphi + \beta\psi = 1 \\ &\alpha = \beta = 1 \text{ works: } \varphi + \psi = 1 \text{ (the trace of } Q\text{). The solution is unique since } \varphi \ne \psi. \\ &\text{check: } L_2 = \varphi^2 + \psi^2 = (\varphi + \psi)^2 - 2\varphi\psi = 1 - 2 \cdot (-1) = 3 = L_1 + L_0 \ \checkmark \end{aligned}

Exercise 3: A repeated root

Solve an=4an−1−4an−2a_n = 4a_{n-1} - 4a_{n-2} with a0=1a_0 = 1, a1=4a_1 = 4.

Answer

an=(n+1)⋅2na_n = (n + 1) \cdot 2^n.

Solution
λ2−4λ+4=(λ−2)2 → an=(α+βn)⋅2na0=α=1;a1=2(1+β)=4 → β=1check a2=4⋅4−4⋅1=12=3⋅4 ✓\begin{aligned} &\lambda^2 - 4\lambda + 4 = (\lambda - 2)^2 \ \to\ a_n = (\alpha + \beta n) \cdot 2^n \\ &a_0 = \alpha = 1; \quad a_1 = 2(1 + \beta) = 4 \ \to\ \beta = 1 \\ &\text{check } a_2 = 4 \cdot 4 - 4 \cdot 1 = 12 = 3 \cdot 4 \ \checkmark \end{aligned}

Exercise 4: Tribonacci by matrix power

The tribonacci numbers satisfy Tn+1=Tn+Tn−1+Tn−2T_{n+1} = T_n + T_{n-1} + T_{n-2} with T0=0T_0 = 0, T1=0T_1 = 0, T2=1T_2 = 1. Write the 3×33 \times 3 step matrix, compute T50T_{50} with a matrix power, and compare T50/T49T_{50}/T_{49} with the matrix's largest eigenvalue.

Answer

Step matrix [111100010]\begin{bmatrix} 1 & 1 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{bmatrix}; T50=3122171529233T_{50} = 3122171529233; both the ratio and the dominant eigenvalue are ≈1.839287\approx 1.839287.

Solution

The state vector is (Tn,Tn−1,Tn−2)(T_n, T_{n-1}, T_{n-2}), and the first row adds the three entries. Starting from (T2,T1,T0)=(1,0,0)(T_2, T_1, T_0) = (1, 0, 0), TnT_n is the first entry of Mn−2(1,0,0)M^{n-2}(1, 0, 0). SymPy matrices use exact integers:

python · try it yourself
import numpy as np
from sympy import Matrix

M = Matrix([[1, 1, 1], [1, 0, 0], [0, 1, 0]])
state = M**48 * Matrix([1, 0, 0])      # (T50, T49, T48)
T50, T49 = state[0], state[1]
print("T50 =", T50)
print("T50/T49 =", round(float(T50 / T49), 6))
lam = max(abs(np.linalg.eigvals(np.array(M.tolist(), dtype=float))))
print("largest eigenvalue =", round(float(lam), 6))
T50 = 3122171529233
T50/T49 = 1.839287
largest eigenvalue = 1.839287

Test yourself

Which matrix advances the Fibonacci state (Fn,Fn−1)(F_n, F_{n-1}) to (Fn+1,Fn)(F_{n+1}, F_n)?

  1. [1011]\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}
  2. [1110]\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}
  3. [0111]\begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}
  4. [1101]\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}

Why is FnF_n the integer nearest to φn/5\varphi^n/\sqrt{5}?

  1. Because φ\varphi is rational
  2. Because ∣ψ∣<1\lvert\psi\rvert < 1, so ψn/5\psi^n/\sqrt{5} is always smaller than 12\frac{1}{2} in size
  3. Because 5\sqrt{5} is an integer
  4. It is only true for even nn

How many matrix multiplications does repeated squaring need for QnQ^n?

  1. About nn
  2. About n2n^2
  3. About 2⋅log⁡2n2 \cdot \log_2 n
  4. Exactly 1