Generating functions: counting with algebra
Suppose we need to pay 2 units using coins worth 1 and 2 units. We can give two 1s or one 2. Our question records how many coins of each value we use; handing them over in a different order does not count as another payment. For this small amount, we can list every possibility. But if the amount changes, or we add another coin value, how can we combine the choices we already have without starting the list again?
Recording the actual payments gives us a table of amounts and numbers of ways. How could ordinary addition and multiplication help us organise this table? We will build a record called a generating function to answer that question. There is no need to memorise the name yet, and infinite series only enter in section 05. We begin by keeping track of each choice in the small example below.
First decide what counts as one way
First make the rules precise. We may use at most two one-unit coins and at most one two-unit coin, including none of either kind. Coins of the same value have no identifying labels, so we do not distinguish which particular coin was used. A separate rule says that a payment records the quantity of each value, not the order of handing them over. Thus “1 then 2” and “2 then 1” both leave the record “one 1 and one 2”. Below we can change what the record contains and inspect the difference.
What does our record keep?
Both trays below pay 3 units. Read each tray from left to right as the handing-over order. Register each tray, then change the question and register them again.
Our payment question keeps just the number of 1s and 2s. It deliberately leaves out the handing-over order.
Hand over 1, then 2
Hand over 2, then 1
Let name the number of one-unit coins and let name the number of two-unit coins. These letters name quantities: may be 0, 1 or 2, while may be 0 or 1. Each coin of the second kind contributes 2 units, so the amount is . For example, and give units.
For each a, we have the two choices and . There are therefore pairs of counts: (0,0), (0,1), (1,0), (1,1), (2,0), (2,1). We group them by a, and each group has exactly the two possible values of . This includes every pair once. Their totals are 0, 2, 1, 3, 2 and 4, respectively.
The numbers of ways to make totals 0, 1, 2, 3 and 4 are therefore 1, 1, 2, 1 and 1. There is one way to make zero: take neither kind. That is a definite choice, different from having no legal choice at all. Next we want to keep this table of totals and counts in a form that also helps us calculate.
Keep the complete list before shortening it
| Record | 1-coins | 2-coins | Amount |
|---|---|---|---|
| 1 | 0 | 0 | |
| 2 | 0 | 1 | |
| 3 | 1 | 0 | |
| 4 | 1 | 1 | |
| 5 | 2 | 0 | |
| 6 | 2 | 1 |
| Amount | Number of ways | Original records |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 1 | 3 |
| 2 | 2 | 2 and 5 |
| 3 | 1 | 4 |
| 4 | 1 | 6 |
Can the record help us calculate?
Our ordinary table contains two different pieces of information: the amount paid, and how many payments give that amount. “Amount 2” and “2 ways” both contain a 2, but mean different things. Two columns keep them apart. We also want the record to help combine choices: a contribution of 2 joined to a contribution of 3 must record 5. Can we find a notation whose multiplication adds the recorded amounts?
Use the letter as a marker. Two copies multiplied together are written , and three are written . The raised number is the exponent; it tells us how many copies of are multiplied. Now : the groups of 2 and 3 contain 5 copies altogether. This is the rule we need, so we choose the exponent to record the amount. Here does not name a coin and we do not substitute the amount for it. The amount is already in the exponent.
For amount zero, there are no copies of to join. We write its record as . Why 1? Joining a payment to “pay nothing” must leave its amount unchanged, and multiplying by 1 leaves the other term unchanged too. This convention lets zero follow the same combination rule.
Each choice contributes one term. If two different choices both give total 2, they contribute . The 2 in front is the coefficient: it counts ways. The 2 above is the exponent: it records the total. If there is no way to make 5, its term is , which we usually leave unwritten. Our table therefore becomes:
Follow one choice into its term
Choose the quantities first. The blue 1-coins each contribute one unit; the gold 2-coins each contribute two. Keep that choice while revealing its record one step at a time.
Compare two 1-coins with one 2-coin. The amount stays 2; the quantities change. Each contributes its own x², which is why their combined coefficient will be 2.
For example, the term reads “total 2, two ways”. The constant term 1 reads “total zero, one way”. To answer a question about one total, we find that exponent and read its coefficient; we do not evaluate the whole expression at that total.
Why do we sometimes add and sometimes multiply?
Begin with addition. Return to our two opening pictures: take two 1s, or take one 2. These choices do not overlap, because the first has no two-unit coin and the second has one. Each contributes , giving : two ways to make total 2. Addition counts alternatives when the groups of choices do not overlap. If the same choice appears in both lists, adding their counts would count it twice.
Now return to the coins. We are not choosing one kind instead of the other: we make a choice for each kind. Taking 0, 1 or 2 one-unit coins gives . Taking 0 or 1 two-unit coin gives . We need one term from A together with one term from . Each expression being multiplied is called a factor, so and are our two factors.
When we combine two choices, their totals must add. Multiplication does exactly this: . The two groups contain copies of . In general, joining copies and copies gives copies, so . If either exponent is zero, its factor is 1 and leaves the other total unchanged, just as choosing nothing should.
Why does multiplying the brackets list every choice? By the distributive law, each term of A multiplies the whole of . This means multiplying it by each of ’s two terms and adding the results. Each choice from A is therefore paired once with every choice from . Writing every intermediate term lets us check the pairs:
The table below keeps all six pairings visible. Choose a target, then press “Count next pair”. Each matching cell contributes 1, and its contribution stays in the running count. The other cells remain visible too. Blue coins are marked 1 and gold coins are marked 2; their written values identify them as well as their colours.
| 1-unit ↓ / 2-unit → | 0 two-unit tokens | 1 two-unit token |
|---|---|---|
| 0 one-unit tokens | Different total | Matches; not counted yet |
| 1 one-unit tokens | Different total | Different total |
| 2 one-unit tokens | Matches; not counted yet | Different total |
No pair has been counted. Press “Count next pair”; the counted cells stay visible. Nothing advances automatically.
For target 2, one pair uses the constant term of and the term of . This means zero one-unit coins and one two-unit coin. Another uses the term of and the constant term of : two one-unit coins and zero two-unit coins. Each pair contributes , so the coefficient becomes . The two contributions retain the two payments we began with.
Read the general rule from the small table
Later we write for the number of ways to make total . The subscript tells us which total we mean: is the count for zero and the count for one. They are not a multiplied by 0 or by 1. The expression that places these numbers beside successive powers of is called the ordinary generating function of the sequence. This is the kind of generating function we use on this page.
names the whole record. Why does the name still contain “function”? A finite record is a polynomial, and an infinite record can also define a numerical function under suitable conditions. Here we use its coefficient-recording meaning. We will specify exactly how infinite records are handled, without needing to draw a curve or assume numerical convergence.
In each individual factor so far, an allowed amount had only one choice. For example, using only one-unit coins, the only way to make 2 was to take two coins. Now let the first record have choices of amount , and the second record have choices of amount . The subscripts identify amounts; the coefficients count choices. Each of the first choices can be paired with all second choices. There are groups of pairs, giving pairs altogether, each of amount .
To make total , we need . Since neither total is negative, can only run from 0 to ; once is fixed, must be . We therefore check (0,n), , and so on through (n,0), then add their numbers of pairs. This is the coefficient of in the product.
The symbol means “take the coefficient of in the expression that follows”; it does not mean multiply by . The symbol abbreviates the addition we just described: let take each value 0, 1, …, , form , then add all these terms. This rule for multiplying coefficients and grouping by total exponent is also called convolution. The name adds no new operation.
The justification works in both directions. Every legal pair has definite totals and , so it appears in one of the listed terms and cannot be missed. Conversely, a listed term uses only a choice of total together with one of total , so it cannot include a pair of the wrong total. Different values of cannot describe the same pair, because that pair’s A-choice has one definite total. There is no duplication between terms.
There is one more check: does a final object correspond to exactly one pair of choices? For the coins, the final collection tells us uniquely how many of each kind were taken, so it does: the correspondence is one-to-one. If the factors record the same decision twice, or some pairs are forbidden, we cannot multiply them unchanged. The algebra follows the rules we encode; it does not repair an incorrectly chosen counting model.
What if the supply has no upper limit?
Now remove the limit on one-unit coins. We may take 0, 1, 2, 3, … coins, with one choice for each quantity, so the record becomes . The dots mean continue by this stated rule: the coefficient of every nonnegative integer power is 1. We are defining the whole record, not guessing it from a few samples.
A record arranged by nonnegative integer powers, with infinitely many terms allowed, is called a formal power series. “Formal” reminds us to read coefficients first, rather than substitute a number for and ask for an infinite numerical sum. Two such series are equal when their coefficients agree at every exponent. Addition adds corresponding coefficients, and multiplication uses the pairing rule from the previous section.
Does this multiplication demand an infinite addition for a single coefficient? No. For a fixed , nonnegative and with give only the pairs from (0,n) to (n,0). Higher powers cannot lower their exponent to help make . That is why a finite calculation can determine one requested coefficient exactly.
We would like a shorter expression for . Multiplying by adds 1 to every exponent, so . Subtract the two records. Their constant coefficients give , while every positive-power coefficient gives . Thus:
We have not cancelled a mysterious “last term”. We performed a finite subtraction at each specified coefficient. Negative coefficients are allowed in this intermediate algebra: for example, the coefficient of in is . This intermediate factor is not counting “minus one choice”. The final counting record still has nonnegative integer coefficients.
An expression that multiplies to give 1 is its multiplicative inverse, written . Here that fraction means an inverse in formal power series. We have just shown that is such an expression.
Derivation: why names only one record
Could another record also give 1 when multiplied by ? No. If has coefficients , …, then first gives . The coefficient of gives , so . This determines , then , and every later coefficient in turn: they are exactly the coefficients of .
Each column represents an exponent, and each cell contains a coefficient, not a value of . Keep the row visible. Shifting a copy one column to the right gives , so we can subtract matching columns to find . Each row continues beyond the visible window.
| Record | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ⋯ |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ⋯ |
The proof above covers every exponent, not only the eight shown.
If each coin has positive integer value , taking 0, 1, 2, … coins contributes , …. Multiplying by moves each allowed exponent to the next allowed exponent. Subtraction leaves only the constant 1; at exponents that are not multiples of , the coefficients remain . Therefore:
Derivation: why also names only one record
This inverse is unique too. Suppose another record satisfies , with coefficients , …. The constant term gives . For an exponent with , the term cannot yet contribute, so . For , comparing coefficients gives , hence . Repeatedly subtracting from the index eventually reaches one of 0, 1, …, . Thus every coefficient is determined: 1 at a multiple of , and 0 elsewhere. When , there is no integer with , and this reduces to our earlier case.
Here must be positive. If we allowed unlimited zero-value coins and regarded different quantities as different choices, there would already be infinitely many ways to make total zero. A finite integer coefficient could no longer record the count. The condition matters. We have also not substituted : the formal identity does not claim that is a finite number.
Example 1: making a total with 1, 2 and 5-unit coins
Now use coins of values 1, 2 and 5, with any nonnegative integer quantity of each allowed. Coins of the same value are indistinguishable, and rearranging them does not create a new way. We want total 5. The previous section gives one record for each coin value, and multiplication combines the three quantity choices:
May we multiply, regroup and reorder these infinite records as we do ordinary brackets? Yes. For any one target exponent , only exponents from 0 to can take part, so each coefficient comes from finitely many ordinary integer products. This is what permits the denominator cancellations below; the full reason is in the optional note.
Derivation: why regrouping and reordering are allowed
May we regroup and reorder three infinite records as we do finite polynomials? Fix a target exponent . The three exponents , and must satisfy and be nonnegative, so each is an integer from 0 to . Only finitely many triples can be considered. Multiplying the first two factors first groups by before adding ; multiplying the last two first groups by before adding . Both visit every triple of total exponent exactly once, with the same product of three coefficients. Exchanging factors just exchanges the corresponding positions in the triple, without changing its total exponent or coefficient product. Every coefficient therefore agrees, which means the records agree.
The same reason applies to any finite number of factors: after fixing , every exponent is still between 0 and , so there are still only finitely many combinations. Even with negative intermediate coefficients, we are using ordinary addition and multiplication on finitely many integers, so regrouping and reordering remain valid. This is what permits the denominator cancellations below.
The final fraction combines the three inverses: multiplying by the three denominator factors makes each factor cancel its corresponding series, leaving 1. This shortens the record, but it has not yet supplied a numerical answer for our target. We still need the coefficient of .
Derivation: why this fraction names only the we constructed
The combined inverse cannot have two different answers either. Temporarily call the product of the three denominators ; we have proved . If another record satisfies , the coefficientwise regrouping just justified gives . Thus the fraction uniquely identifies the we constructed.
If we take , and coins, the selected terms are and , whose product is . We therefore list the nonnegative integer counts satisfying . First fix . It can only be 0 or 1, because would contribute at least 10 from the five-unit coins alone, already above 5.
When . The possible b-values are 0, 1 and 2, giving , and . The triples are (5,0,0), (3,1,0) and (1,2,0).
When . Since and are nonnegative, both must be zero, giving (0,0,1). There are ways, so .
The construction below fixes the number of 5s first, increases the number of 2s one at a time, and uses 1s to fill the remainder. Blue is marked 1, gold 2 and pink 5. “Add next choice” retains both its coins and its equation. Changing the target clears the old list so that counts for different questions are not mixed.
We have not listed a choice yet. This empty display does not mean that the answer is zero.
Why is this enumeration complete? For target , every legal choice has one definite . Once is fixed it also has one definite , and must equal . We allow only and , then calculate this nonnegative a. Thus every generated triple is legal, and every legal triple is generated exactly once. The proof comes from these two directions, not from noticing a pattern in a few pictures.
As a nearby check, change the target to 6. With , the values give , 0: four choices. With , only 1 remains, so and : one more choice. The total is . This differs from the four choices for target 5, and the list shows the actual objects behind the change.
This also connects to Diophantine equations: we are counting the nonnegative integer solutions of . A generating function places the answers for different in one record. It preserves the integer and nonnegativity restrictions through the exponents allowed in each factor.
Example 2: at most, or at least, a given number
An unlimited supply cannot stand in for a bounded one. If a coin has positive integer value and we may take at most of them, where is a nonnegative integer, the allowed quantities are only 0 through . Its record must stop at :
As before, multiplying by gives . In , the coefficients from power through become . The constant 1 remains. The highest term appears only in the expression being subtracted, so it leaves . Thus:
The last step multiplies both sides by the formal inverse already constructed. It is not division by a numerical expression that might vanish at some substituted . When there are no middle terms to cancel: , and the formula likewise gives .
For example, with only two two-unit coins available, we may take 0, 1 or 2, giving . Substituting and gives , where 6 comes from . If one-unit coins are unlimited and the target is 5, taking 0, 1 or 2 twos leaves 5, 3 or 1 ones. There are three ways.
If we must take at least coins of value , first set aside the required coins, contributing . Then take 0, 1, 2, … extra coins, contributing , …. The record is . Every permitted choice can uniquely have those required coins removed, leaving a nonnegative quantity. This gives a complete decomposition without duplication.
For example, require at least one two-unit coin, allow unlimited one-unit coins, and make total 5. Remove the required 2 first, leaving . Make that remainder with three 1s, or with one 2 and one 1: two choices. Restoring the required 2 gives (number of 1s, number of 2s) and . The record is ; the initial increases every original total by 2.
Example 3: why two dice keep their identities
Change the objects being counted. We have a first die and a second die, each with possible face values 1 through 6. We are counting outcomes, without assuming fairness or calculating probabilities. A 1 on the first die with a 6 on the second is different from a 6 on the first with a 1 on the second, because the dice are distinguishable.
Each face value occurs once on a die, so its record is . There is no constant term because zero is not an outcome for a die. The two factors have the same expression, but the first records the first die and the second the second die. Their positions are retained when we pair choices.
If the first die is , the second must be . Let ; the second values are then 6, 5, 4, 3, 2, 1, all allowed. The pairs are (1,6), (2,5), (3,4), (4,3), (5,2), (6,1). Each contributes , so there are six outcomes.
The rows below retain the first die’s value and the columns the second die’s value. Each cell shows their sum; ✓ marks a match to the target. Change the target, inspect the matching cells, then read the count written as an addition below. For example, target 2 has only (1,1), while target 12 has only (6,6).
| First ↓ / second → | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 ✓ |
| 2 | 3 | 4 | 5 | 6 | 7 ✓ | 8 |
| 3 | 4 | 5 | 6 | 7 ✓ | 8 | 9 |
| 4 | 5 | 6 | 7 ✓ | 8 | 9 | 10 |
| 5 | 6 | 7 ✓ | 8 | 9 | 10 | 11 |
| 6 | 7 ✓ | 8 | 9 | 10 | 11 | 12 |
The fact that multiplication permits exchanging the two factors does not erase the identities of the objects. Exchanging the names of the dice sends (1,6) to (6,1), showing that the two naming conventions have the same total count; it does not merge the outcomes. For coins we chose the quantity of each kind from the start. For dice we chose each die’s value separately from the start. Different counting questions require different records.
Example 4: derive a generating function from a tiling recurrence
Our earlier factors came from kinds of object whose quantities could be chosen separately. Now consider a different problem: cover a single row of length with tiles of length 1 and 2, without overlap or gaps. All tiles lie horizontally. Length-1 tiles are identical to one another, as are length-2 tiles, but left-to-right order is part of a tiling. On a row of length 3, a 1 followed by a 2 occupies different positions from a 2 followed by a 1.
Write for the number of tilings of length . For length zero there are no cells to cover: placing no tiles completes the task, so . For length one, only one length-1 tile fits, so . These starting values need to be explicit because every later count will grow from them.
For , classify a tiling by its last tile. If that tile has length 1, removing it leaves a complete tiling of length . Conversely, append a length-1 tile to any tiling of length and get one unique tiling whose last tile is 1. These operations undo one another, so this class has exactly tilings.
If the last tile has length 2, removing it leaves a tiling of length , and appending a 2 uniquely restores the original. This class has tilings. A final tile cannot have both lengths, so the classes do not overlap. There are only these two tile lengths, so every tiling belongs to a class. We can therefore add them:
A rule that calculates later values from earlier ones is a recurrence relation. Here ; ; ; and . This is a Fibonacci-type recurrence, but our reason for it is the complete classification by the last tile, not its name.
In the picture below, blue short tiles are marked 1 and gold long tiles are marked 2. Keep length 4 first and press “Next tiling” to inspect and . Each tile’s width corresponds to its length, and the addition below connects the picture to the total length.
Choose a last-tile class to inspect only its members. “All” restores the full list. The two class counts come from the shorter rows proved in the text, and the removal picture lets us check the correspondence.
A tile labelled 2 occupies twice the horizontal length of a tile labelled 1. Read from left to right; their order is part of the tiling.
Remove the last tile, of length 1. The remaining row has length .
The dashed part is removed, not another tile in the shorter row. Append exactly that last tile to the shorter row to recover this complete tiling. The picture preserves all earlier tiles and their order.
Now shift the same record by one position
Put all the tiling counts into . To place and beside the same power, use , and . Multiplying by turns into ; multiplying by turns into . We choose these multipliers to align the three terms of the recurrence.
Form . Only supplies a constant term, so it is . The coefficient of is , because has no term. For every , the coefficient of is , which is zero by our proved recurrence. We handle the two starting coefficients separately: the recurrence applies only from .
The fraction again denotes a formal multiplicative inverse: is the record that gives 1 when multiplied by , and we constructed it from actual tiling counts.
Derivation: why no other record satisfies this equation
The fraction again denotes a formal multiplicative inverse. Why does it exist and why is it determined? We constructed from actual counts and proved that multiplying it by gives 1, so it exists. If another record had this property, its constant coefficient would force . Its coefficient would force , hence . Each later coefficient would force . The same starting values and rule determine every coefficient successively, so must equal .
A short fraction therefore stores both the recurrence and its starting values. It does not remove all calculation: to find the coefficient of , we may still use the proved rule to get 1, 1, 2, 3, 5, 8, then read the 8 beside the fifth power. The recurrence retains counts rather than listing every tiling, while the generating function turns the recurrence into an algebraic record that can be multiplied with other records or shifted in exponent.
For example, attach one fixed end piece of length 2 to a tiled row, making a decorative board of total length 6. There is one end-piece choice, contributing , so the record is . To make it must pair with from , whose coefficient is . Thus there are five boards. Removing the fixed end piece gives exactly the five tilings of length 4, checking the algebra against the objects once again.
Try it, then check each step
For each question, picture the actual choices and check them using coefficients. The worked answer is available below; no memorised counting formula is required. At each step, explain which objects you are counting and why each is counted exactly once.
1. With at most one 2 and at most one 3, can we make 4? Why is the constant term still 1?
Each quantity can be 0 or 1, giving . Distributing gives . There is no term, so its coefficient is zero and total 4 is impossible. The constant term comes from taking neither kind. This is one definite choice, so its coefficient is 1.
2. With at most two 1s and at most two 2s, how many ways make 4?
The record is . Let count the 2s. If we need four 1s, exceeding the limit. If we need two 1s, which is allowed. If we need zero 1s, also allowed. There are two quantity pairs: (2,1) and (0,2). The only products giving are and , each contributing coefficient 1, for a total of 2.
3. If , what is the coefficient of in ?
Multiply by each term to get , so the coefficient is 4. The exponent increases by 3, from 2 to 5, but the original four choices remain four choices: adding one fixed contribution of total 3 introduces no new alternatives. Generally, for , the coefficient of in is the coefficient of in A, because . If , no nonnegative exponent exists, so the coefficient is zero.
4. How many tilings have length 6? Why is counting only the number of 2-tiles insufficient?
Classifying by the last tile gives . The same quantities can occur in different orders: for length 3, and both use one tile of each kind but are different tilings. Recording only the two quantities, as in the coin problem, would merge them. Our generating function came from a recurrence that retains tile order, not from multiplying two geometric series that record quantities alone.
We began with two ways to make 2, recorded each choice as a term, and proved that addition records disjoint alternatives while multiplication records combinations that can be uniquely separated. Exponents track totals; coefficients track counts. Unlimited supplies, bounded supplies, dice and tilings give different expressions, but the final check is the same: which actual choice does each contribution to this coefficient represent? Answering that is what makes the generating function more than a short expression.
