A counting problem usually asks for a whole sequence of answers, one for each size, and it would help to handle the sequence as a single object that can be added, multiplied and solved for.
The previous lesson solved every linear recurrence with constant coefficients by the characteristic equation: substitute , find the roots, fit the initial conditions. That method says nothing about a count built from independent choices, such as the ways to pay 50p in coins, and fails outright when earlier terms are multiplied together. This lesson packs a sequence into a power series, its generating function, so that operations on counts become algebra. It recovers Binet's formula without a guess, and finds the Catalan numbers, which no characteristic equation can reach.
A sequence as a power series
Given a sequence , its generating function is
The power of is a label: the coefficient of is the th term, and nothing else is stored. The row of Pascal's triangle becomes , the binomial theorem read backwards. The notation means the coefficient of in , so .
Two series are added term by term and multiplied as polynomials are, so the coefficient of in is . That is a finite sum: each coefficient of a sum or product needs only finitely many terms of the inputs, so every operation is ordinary arithmetic.
That is why convergence does not matter here. A series in this sense is a formal power series: an infinite list of coefficients written in a convenient notation, with never replaced by a number. The series , whose coefficients are the factorials , converges for no other than , yet it is a perfectly good formal series and can be added and multiplied like any other. An identity between formal series means only that the two coefficient lists agree, place by place; nothing is ever summed.
Division needs one condition. If has a nonzero constant term, there is exactly one series with , found one coefficient at a time: the constant term of the product fixes , the coefficient of then fixes , and so on. So fractions like below are genuine series, not symbols awaiting a value of .
The geometric series and its relatives
The most important series has every coefficient equal to . Multiply it by :
Every coefficient after the constant cancels, and each cancellation involves only two terms, so this is a formal identity with no condition on . Therefore
The same cancellation works with in place of , for any constant , so generates the powers . With it generates ; with it generates . Putting in place of gives , which has a at every even power and a at every odd one, and in general marks the multiples of .
Multiplying by shifts a sequence one place to the right, so is . A finite sequence is a polynomial: equals , the finite geometric sum.
These pieces are enough to read off many coefficients. For example, is the coefficient of in , which is , plus , giving . Each problem below turns a count into a product or quotient of such pieces, then reads the coefficients back.
Multiplying counts combined choices
Multiplying series has a meaning in counting. Suppose there are ways to make a choice of size from one source and ways from a second, independent source. A combined choice of total size takes some size from the first and the remaining from the second, in ways by the product rule. Adding over by the sum rule gives
which is exactly the coefficient of in . This sum is the convolution of the two sequences, and it is the whole reason generating functions count: exponents add, so from one factor meets from the other on .
One die generates , the exponent recording the score. In its square the coefficient of collects up to , the six ways to throw with two dice, and the cube gives the ways to throw with three.
Now take copies of , each standing for an unlimited supply of one kind of object, with the exponent counting how many are taken. The coefficient of in counts the ways to write with every , which is a selection of objects from kinds with repetition. Stars and bars, from an earlier lesson (arrange stars and dividing bars in a row), counts these as . So
and in particular generates the numbers .
Example. A shop sells lemon, cherry and mint sweets. How many bags of sweets contain an even number of cherry and at most one mint?
Each flavour becomes a factor recording the allowed amounts. Lemon may be any number, ; cherry must be even, ; mint is or , . The bags are counted by the product
because and the factor cancels. So there are bags of sweets, and bags of . A direct check agrees: with no mint the cherries number , six ways, and with one mint they number out of nine, five ways.
Now you. A basket of fruit holds an even number of apples, a multiple of bananas, at most oranges and at most pear. How many baskets of fruit are there?
Answer
The factors are , , for the oranges (the finite sum ) and . The cancels, and , so the product is again and there are baskets.
Making change
In how many ways can 50p be paid using 1p, 2p, 5p, 10p and 20p coins, with any number of each? Order does not matter: two 20p and a 10p is one way, whichever coin is handed over first. Each coin type is an unlimited supply whose contribution comes in multiples of its value, so the 5p coins contribute , and the answer is the coefficient of in
Its coefficients have no tidy closed form, but they are fast to compute. Suppose counts the ways using some coins, and adds a coin worth . Then , and comparing coefficients of gives , that is,
with for . In words, a payment either uses no coin worth or uses one that can be removed. So each new coin is one pass along a row, adding the entry places back.
Start with 1p coins alone, which give one way for every amount. Adding 2p coins gives for amounts to , the value from choosing how many 2p coins to use. Adding 5p coins then gives . Carried on to 50p, the rows end at with 1p and 2p only, then with 5p, with 10p, and finally
A brute force count by computer agrees. Allowing a 50p coin as well adds exactly one way, the coin itself, for .
Example. In how many ways can 10p be made from 1p, 2p and 5p coins?
The last entry of the 1p, 2p and 5p row is , from the step : six ways with no 5p coin, and four ways to make the rest once one 5p coin is set aside. Listing confirms it: with two 5p coins there is one way, with one 5p coin the remaining 5p takes , or of the 2p coins, three ways, and with none the 10p takes to of the 2p coins, six ways, and .
Now you. In how many ways can 20p be made from 1p, 2p, 5p and 10p coins?
Answer
Extend the 1p and 2p row to , where it ends at , then add 5p coins to get . Adding 10p coins changes each entry from on by adding the new entry ten places back: and . There are ways.
Fibonacci by partial fractions
A recurrence becomes an equation for its generating function. Let for the Fibonacci numbers, with , and . Shifting by one and two places gives and , whose coefficients of are and . So in the coefficient of is for every , and only the first two terms survive: the constant and . Hence
The whole sequence is now one fraction. To read its coefficients, factor the denominator as . Expanding, this needs and , so and are the roots of : the golden ratio and , with . Then split the fraction into partial fractions, a sum of the simple kind whose coefficients are known:
Dividing by gives . Each piece is a geometric series, generating , so
which is Binet's formula from the previous lesson. There it came from guessing that solutions look like ; here nothing was guessed, and the powers appear because each factor of the denominator is a geometric series. A forcing term, which needed a separate particular solution before, is just another series on the right.
Example. Solve the Tower of Hanoi recurrence , , with a generating function.
For the coefficient of in is , and the constant term is . So , and
where the split checks because . Reading coefficients, , as unrolling found.
Now you. Solve with and by a generating function, and find .
Answer
, and . Writing needs and , so and . So , and , which the recurrence confirms: .
The Catalan numbers
How many ways can pairs of brackets be written so that they match? With three pairs there are five: , , , and . Call the number , with for the empty string. A nonempty matched string opens with a bracket, and that bracket closes somewhere, so the string is where and are matched strings. If the whole has pairs and has , then has , and
This recurrence multiplies earlier terms together, so no characteristic equation applies. But the sum is a convolution, the coefficient of in . Multiplying by to shift it into place and adding back gives
a quadratic in . The quadratic formula gives , and the sign is forced: with the plus sign the numerator has constant term , so dividing by would leave a term . Taking the minus sign, the formal square root is the one series with constant term whose square is , and Newton's generalisation of the binomial theorem to the power gives it:
Subtracting from and dividing by gives , matching the count of five for three pairs. The general coefficient of in the root, for , is . Its numerator is times over , and the odd product equals . Multiplying by and dividing by leaves . Put , negate and halve, and
So and . The same numbers count the ways to cut a convex polygon with sides into triangles, which Euler worked out in 1751; they are named after Eugène Catalan, who met them counting bracketings in 1838, and a later lesson meets them again counting binary trees.
The end of counting
Generating functions close the counting half of the course because they absorb what came before. The binomial theorem is the generating function ; stars and bars is the coefficient of ; a linear recurrence is a rational function taken apart by partial fractions; and a count built from independent pieces, like a pile of coins, is a product. The limits are honest ones: many generating functions, for coins among them, have no closed formula for their coefficients, and then the method becomes an efficient way to compute rather than a formula.
Everything so far has asked how many. The second half of the course asks what the integers themselves are made of, beginning with the most basic relation between two of them: whether one divides the other. The next lesson proves that division always leaves a well defined remainder, and turns that fact into Euclid's algorithm for the greatest common divisor, one of the oldest algorithms still in use.