Power series that encode a sequence of numbers as coefficients, turning combinatorics into algebra.
IntuitionWhat is a generating function?
Imagine a clothesline with infinitely many pegs, numbered 0,1,2,3,…. To describe a sequence of numbers a0,a1,a2,…, you pin the number an at peg n. A generating function is this clothesline written as a single algebraic object: the number an becomes the coefficient of xn in a power series. The variable x is never plugged in with a number — it just keeps every term in its own slot, so that adding, multiplying or shifting sequences becomes ordinary algebra on polynomials.
Plot of the cubic $1+x+x^2+x^3$ approximating the generating function $\frac{1}{1-x}$.
The curve is the cubic 1+x+x2+x3, the first four terms of 1−x1=∑n=0∞xn, the generating function of the constant sequence an=1. Each coefficient (drag a,b,c,d) is one peg of the sequence packed into the curve.
SchoolPacking a sequence into a power series
Definition: Ordinary generating function
The ordinary generating function (OGF) of a sequence a0,a1,a2,… is the formal power series G(x)=∑n=0∞anxn. "Formal" means we treat this sum as an algebraic expression, not a function to be evaluated numerically; questions of convergence for particular values of x are irrelevant to combinatorics.
G(x)=n=0∑∞anxn
The simplest building block is the sequence an=1 for every n≥0, whose OGF is the geometric series 1−x1=∑n=0∞xn. This identity is the engine behind most generating-function arguments: it says that 1−x1 encodes "choose n items from an unlimited supply, order irrelevant, one way each".
(1−x)21=n=0∑∞(n+1)xn
Common sequences and their ordinary generating functions
Let F0=0,F1=1 and Fn=Fn−1+Fn−2 for n≥2. Then the ordinary generating function F(x)=∑n=0∞Fnxn equals F(x)=1−x−x2x.
Why is it true?
A linear recurrence with constant coefficients always turns into a simple algebraic (in fact, rational) equation once you multiply through by xn and sum over all n — the recurrence becomes multiplication by a polynomial in x.
Proof
Multiply the recurrence Fn=Fn−1+Fn−2 (valid for n≥2) by xn and sum over n≥2: ∑n≥2Fnxn=∑n≥2Fn−1xn+∑n≥2Fn−2xn.
The left side is F(x)−F0−F1x=F(x)−x. The first sum on the right is x∑n≥2Fn−1xn−1=x∑m≥1Fmxm=x(F(x)−F0)=xF(x). The second sum is x2∑n≥2Fn−2xn−2=x2∑k≥0Fkxk=x2F(x).
So F(x)−x=xF(x)+x2F(x), i.e. (1−x−x2)F(x)=x.
Solving for F(x) gives F(x)(1−x−x2)=x, hence F(x)=1−x−x2x, a single rational function that packs the entire infinite Fibonacci sequence.
With φ=21+5 and ψ=21−5, the roots of 1−x−x2 give the closed form Fn=51(φn−ψn) for every n≥0.
Why is it true?
Every rational generating function with distinct linear factors in its denominator splits into a sum of simple geometric series, and each geometric series is read off directly as an explicit formula for the coefficient.
Proof
Factor the denominator: since the roots of 1−x−x2=0 are x=1/φ and x=1/ψ, we have 1−x−x2=(1−φx)(1−ψx).
Write 1−x−x2x=1−φxA+1−ψxB for constants A,B. Clearing denominators and matching the constant term and the coefficient of x gives a linear system whose solution is A=51, B=−51, so 1−x−x2x=51(1−φx1−1−ψx1).
Expand each term as a geometric series: 1−φx1=∑n≥0φnxn and 1−ψx1=∑n≥0ψnxn.
Reading off the coefficient of xn on both sides gives Fn=51(φn−ψn), known as Binet's formula.
UndergraduateReal-world applications and worked examples
Generating functions are a working tool, not just a theoretical curiosity: computer scientists use them to find exact and asymptotic running times of recursive algorithms, physicists use closely related partition functions in statistical mechanics, and probabilists encode entire probability distributions as generating functions to compute moments by differentiating instead of summing.
Example: Change-making with two coin types
In how many ways can an amount of 4 be made using an unlimited supply of 1-unit and 2-unit coins, if the order of the coins does not matter?
Solution
Each way to make an amount is a choice of how many 1-coins and how many 2-coins to use, so the generating function is the product of the OGF for 1-coins, 1−x1, and the OGF for 2-coins, 1−x21: the combined generating function is (1−x)(1−x2)1.
Expand the two factors as geometric series and multiply: (∑i≥0xi)(∑j≥0x2j). The coefficient of x4 counts pairs (i,j) with i+2j=4 and i,j≥0.
The valid pairs are (4,0), (2,1), (0,2), so there are 3 ways: four 1-coins; two 1-coins and one 2-coin; two 2-coins.
Example: Binet's formula from the generating function
Use the closed form F(x)=1−x−x2x and Binet's formula Fn=51(φn−ψn) to compute F5 directly, without listing F0,…,F4 one by one.
Solution
By the theorem above, φ=21+5 and ψ=21−5 are the roots feeding Binet's formula Fn=51(φn−ψn).
Numerically, φ≈1.618 and ψ≈−0.618, so φ5≈11.09 and ψ5≈−0.09.
Then F5=51(φ5−ψ5)≈51(11.09−(−0.09))=511.18≈5.
Indeed F5=5, matching the value obtained by iterating the recurrence 0,1,1,2,3,5 — but Binet's formula lets us jump directly to any single Fn without computing the ones before it.
What is the ordinary generating function for the constant sequence an=1 for every n≥0?
What is the coefficient of x3 in (1−x)21?
Which of the following problems is most naturally solved using generating functions?
The generating function of the Fibonacci sequence F(x)=∑Fnxn satisfies which closed form?