Generating Functions
Pack a whole sequence into one function the way a zip file packs folders: algebra on the package does counting for every entry at once.
Why Does This Exist?
Counting problems ask about whole sequences at once: how many binary strings of length avoid consecutive ones, for every ? Answering term by term means solving infinitely many problems. A generating function folds the entire sequence into a single object
and then ordinary algebra on answers questions about all the simultaneously. A recurrence between sequence terms becomes an equation for ; solving that equation and reading off coefficients solves the recurrence in closed form.
The same idea resurfaces wherever series do work: moment generating functions in probability package a distribution's moments the same way, and Taylor series are generating functions for a smooth function's derivatives. Learn the trick once here and both of those read as dialects.
The one idea you need first
A power series is a polynomial that never ends: coefficients attached to increasing powers of . Two series are equal exactly when all their coefficients match, so extracting information means reading coefficients. Adding series adds coefficients term by term; multiplying convolves them, which is why products of generating functions count combined choices.
Think of It Like This
A zip archive for sequences
A folder of a thousand text files is awkward to email, so you zip it into one archive. The archive is not the files, but every file is recoverable from it, and some operations (copying, checksumming) are easier on the single package.
A generating function is a zip archive for a sequence. The "files" are the terms and the "archive" is the function . Adding archives merges file lists term by term. Differentiating shifts and reweights the contents. And just as you would never edit a photo by hex-editing its zip, you manipulate the sequence through the function, then unzip by reading coefficients at the end.
The analogy stops at convergence: a zip always opens, but a power series only represents its sequence where it converges, and formal manipulation outside that range can lie.
How It Actually Works
Start from a recurrence and let the series do the bookkeeping. The Fibonacci numbers , , have generating function . Multiply the recurrence by and sum over :
The left side is (missing only the term ). The first sum on the right is and the second is . So , giving the closed package:
Worked example
Expand the package by long division and watch the sequence emerge. Write and match coefficients:
- constant:
- : , so
- : , so
- : , so
- : , so
- : , so
Out comes : Fibonacci, recovered from one rational function with no guessing. From here partial fractions lead to Binet's closed form, but the coefficient extraction above is already the whole method.
Code
The point is the check: rebuild the coefficients from the recurrence the algebra implies, and confirm they match.
def fib_from_package(n_terms: int) -> list[int]: a = [0] * n_terms for n in range(n_terms): if n == 1: a[n] = 1 # the lone x on the right hand side elif n >= 2: a[n] = a[n - 1] + a[n - 2] # coefficient matching rule return a
print(fib_from_package(6)) # -> [0, 1, 1, 2, 3, 5]Watch Out For
Mixing up ordinary and exponential generating functions
Ordinary generating functions attach to ; exponential ones attach to . The two packages answer different questions (unlabelled versus labelled structures) and their product rules differ. Using ordinary multiplication on exponential packages scrambles the counts. Check which convention your source uses before combining anything.
Manipulating where the series diverges
Formal algebra on is valid inside its radius of convergence; outside it, rearranging terms can produce any answer you like. When extracting asymptotics or evaluating at a point, confirm the point lies inside the disc first. The zip analogy breaks exactly here: every zip opens, but not every series converges.
The Quick Version
- A generating function packs a whole sequence into one power series: .
- Recurrences between terms become solvable equations for the package function.
- Fibonacci falls out of : matching coefficients gives 0, 1, 1, 2, 3, 5.
- Ordinary and exponential variants answer different counting questions; do not mix them.
- Series algebra is only trustworthy inside the radius of convergence.