Recurrence Relations and Generating Functions
From Fibonacci to Catalan: solving linear recurrences in closed form, and turning a whole sequence into a single analytic object you can manipulate.
Leads to: 19.7 evaluates recurrences by DP; 19.13 accelerates them with matrix powers; 19.11 uses generating functions for partitions. The Euler Lab’s Combinatorics path is full of ‘count the ways’ problems that yield to a recurrence.
Learning Objectives
Click a status chip to cycle: Not started → In progress → Studied → Practiced → Needs review → Mastered.
- Set up a recurrence from a combinatorial decomposition and verify it against brute force.
- Solve a linear homogeneous recurrence with constant coefficients via the characteristic polynomial, including repeated roots.
- Derive Binet’s formula for Fibonacci and explain why you should still not compute \(F_n\) with it in floating point.
- Define the ordinary generating function of a sequence and use it to solve a recurrence and to extract coefficients.
- Derive the Catalan numbers from a quadratic generating-function equation and interpret them combinatorially.
Key Vocabulary
- Linear recurrence
- \(a_n=c_1a_{n-1}+\cdots+c_ka_{n-k}\) with constant \(c_i\); determined by \(k\) initial values.
- Characteristic polynomial
- \(x^k-c_1x^{k-1}-\cdots-c_k=0\); its roots \(r_i\) give the basis solutions \(r_i^n\) (times \(n^j\) for a root of multiplicity \(j+1\)).
- Ordinary generating function (OGF)
- \(A(x)=\sum_{n\ge0}a_nx^n\): a formal power series that encodes the whole sequence as one object.
- Convolution
- \([x^n]A(x)B(x)=\sum_{k}a_kb_{n-k}\) - the algebraic shadow of ‘split the structure into two independent parts’.
- Catalan number
- \(C_n=\frac1{n+1}\binom{2n}{n}\): counts balanced bracketings, binary trees, monotone lattice paths below the diagonal, triangulations.
- Golden ratio
- \(\varphi=\frac{1+\sqrt5}{2}\approx1.618\): the dominant characteristic root of Fibonacci, so \(F_n\sim\varphi^n/\sqrt5\).
Intuition & Motivation
Setting up and solving linear recurrences
Fibonacci: \(F_n=F_{n-1}+F_{n-2}\), \(F_0=0,\ F_1=1\) - the number of ways to tile a \(1\times n\) strip with monominoes and dominoes, decomposed by what covers the last cell. That decomposition is the recurrence.
Generating functions
The mechanical method: multiply the recurrence by \(x^n\), sum over all \(n\) where it is valid, recognise shifted copies of \(A(x)\), and solve for \(A(x)\).
Catalan numbers: a nonlinear recurrence solved by a quadratic
Let \(C_n\) count binary trees with \(n\) internal nodes (equivalently, balanced bracket strings of length \(2n\)). Splitting a tree at its root into a left subtree with \(k\) nodes and a right subtree with \(n-1-k\) gives the convolution recurrence
Solving the quadratic and taking the branch with \(C(0)=1\): \(C(x)=\frac{1-\sqrt{1-4x}}{2x}\). Expanding \(\sqrt{1-4x}\) with the generalised binomial theorem yields
Note how the product \(C(x)^2\) encoded ‘a left subtree AND a right subtree’. That correspondence - disjoint union → sum, independent concatenation → product - is the whole reason generating functions work.
Coding workbench
Whenever a Euler Lab problem in the Combinatorics path says ‘in how many ways can …’ and the objects are built recursively (paths, trees, tilings, bracketings, coin decompositions), the intended route is: find the decomposition, write the recurrence, then either evaluate it by DP (19.7) or solve it in closed form here.
- Forgetting the initial conditions when writing the OGF equation - the \(-F_0-F_1x\) boundary terms are where most algebra errors live.
- Using Binet in floating point for large \(n\) and trusting the digits. Beyond \(F_{78}\) a double cannot even hold the answer exactly (19.4).
- Choosing the wrong branch of the quadratic in (19.13): \(\frac{1+\sqrt{1-4x}}{2x}\) blows up at \(x=0\) and is not a power series.
- Assuming every recurrence has a nice closed form. Most do not; the recurrence itself, evaluated by DP, is often the final answer.
- Mixing up \(O\)-growth with exact values: \(F_n\sim\varphi^n/\sqrt5\) tells you the answer has \(\approx0.209n\) digits, not what those digits are.
- Always sanity-check a new recurrence by brute-force enumeration for \(n\le6\). A wrong recurrence run fast is still wrong.
- ‘Sum over a split point’ in a recurrence \(\Leftrightarrow\) ‘multiply the generating functions’. Recognising a convolution instantly is worth more than memorising the Catalan formula.
- The dominant characteristic root gives the asymptotic growth for free, which is exactly what you need for the feasibility estimates of 19.14.
- If the recurrence has constant coefficients and \(n\) is huge (\(10^{18}\)), stop - that is a matrix-power problem (19.13), and it runs in \(O(k^3\log n)\).
Practice this in the Euler Lab
Computational problems that exercise exactly this technique. Each opens in the Euler Lab with a Python workbench, a progressive hint ladder, and answer checking. Tier A/B run at full scale in the browser.
Warm-up:
#2 Even Fibonacci Numbers (1%, tier A) #6 Sum Square Difference (1%, tier A) #42 Coded Triangle Numbers (2%, tier A) #12 Highly Divisible Triangular Number (3%, tier A)
Applied:
#313 Sliding Game (16%, tier B) #816 Shortest Distance Among Points (17%, tier B) #323 Bitwise-OR Operations on Random In (18%, tier B)
Challenge:
#208 Robot Walks (41%, tier C) #375 Minimum of Subsequences (41%, tier C)
234 Project Euler problems in total are mapped to this lesson. Open the Euler Lab to filter them all.
Knowledge Check
Practical Exercise
(a) Solve \(a_n=5a_{n-1}-6a_{n-2}\) with \(a_0=1,\ a_1=4\) in closed form. (b) Find the OGF for the number of ways \(t_n\) to tile a \(1\times n\) strip with tiles of length 1 and 2, and identify the sequence. (c) Prove the Catalan convolution (19.13) combinatorially and use the OGF to get (19.14)’s first five values.
(a) Characteristic: \(x^2-5x+6=0\Rightarrow x=2,3\) (distinct). So \(a_n=\alpha2^n+\beta3^n\). Initial conditions: \(\alpha+\beta=1\) and \(2\alpha+3\beta=4\), giving \(\beta=2,\ \alpha=-1\). Hence \(a_n=2\cdot3^n-2^n\). Check: \(a_0=2-1=1\) ✓, \(a_1=6-2=4\) ✓, \(a_2=18-4=14\) and \(5\cdot4-6\cdot1=14\) ✓.
(b) Decompose by the last tile: a length-1 tile leaves a strip of \(n-1\), a length-2 tile leaves \(n-2\). So \(t_n=t_{n-1}+t_{n-2}\) with \(t_0=1,\ t_1=1\). Summing against \(x^n\): \(T(x)-1-x=x(T(x)-1)+x^2T(x)\), so \(T(x)=\frac{1}{1-x-x^2}\). Comparing with (19.12), \(T(x)=F(x)/x\), i.e. \(t_n=F_{n+1}\) - the tilings ARE the Fibonacci numbers, shifted by one.
(c) A nonempty binary tree has a root; deleting it leaves an ordered pair (left subtree, right subtree) whose sizes sum to \(n-1\). Summing over the left size \(k\) gives \(C_n=\sum_{k=0}^{n-1}C_kC_{n-1-k}\), and the pair-of-independent-structures is exactly a product of OGFs, so \(C(x)=1+xC(x)^2\). Solving: \(xC^2-C+1=0\Rightarrow C=\frac{1\pm\sqrt{1-4x}}{2x}\); the \(+\) branch diverges as \(x\to0\), so take \(-\). Expanding: \(C(x)=1+x+2x^2+5x^3+14x^4+\cdots\), i.e. \(C_0..C_4=1,1,2,5,14\), matching \(\frac1{n+1}\binom{2n}{n}\) (\(\tfrac15\binom84=\tfrac{70}5=14\) ✓).
Lesson Summary
Retrieval Practice
Close the lesson and answer from memory before checking. This is deliberate, effortful recall - the single highest-yield study action.
A: It contributes the \(\mu\) independent solutions \(r^n, nr^n, \dots, n^{\mu-1}r^n\); the coefficients are then fixed by the initial conditions.
A: Convolution: choosing an A-structure and a B-structure whose sizes sum to \(n\) (\([x^n]AB=\sum_k a_kb_{n-k}\)). Disjoint union corresponds to addition.
Completion Checklist
- I can explain the core ideas in my own words
- I worked the derivations/examples by hand
- I completed the interactive workbench(es)
- I passed the knowledge check