Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Generating functions

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 rn, 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 a0,a1,a2,…, its generating function is

A(x)=a0+a1x+a2x2+a3x3+⋯

The power of x is a label: the coefficient of xn is the nth term, and nothing else is stored. The row 1,3,3,1 of Pascal's triangle becomes (1+x)3, the binomial theorem read backwards. The notation [xn]A(x) means the coefficient of xn in A(x), so [x2](1+x)3=3.

Two series are added term by term and multiplied as polynomials are, so the coefficient of xn in A(x)B(x) is a0bn+a1bn-1+⋯+anb0. 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 x never replaced by a number. The series 1+x+2x2+6x3+24x4+⋯, whose coefficients are the factorials n!, converges for no x other than 0, 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 A(x) has a nonzero constant term, there is exactly one series B(x) with A(x)B(x)=1, found one coefficient at a time: the constant term of the product fixes b0, the coefficient of x then fixes b1, and so on. So fractions like x1-x-x2 below are genuine series, not symbols awaiting a value of x.

The geometric series and its relatives

The most important series has every coefficient equal to 1. Multiply it by 1-x:

(1-x)(1+x+x2+x3+⋯)=1+(x-x)+(x2-x2)+⋯=1

Every coefficient after the constant cancels, and each cancellation involves only two terms, so this is a formal identity with no condition on x. Therefore

11-x=1+x+x2+x3+⋯

The same cancellation works with cx in place of x, for any constant c, so 11-cx generates the powers 1,c,c2,c3,…. With c=2 it generates 2n; with c=-1 it generates 1,-1,1,-1,…. Putting x2 in place of x gives 11-x2=1+x2+x4+⋯, which has a 1 at every even power and a 0 at every odd one, and in general 11-xc marks the multiples of c.

Multiplying by x shifts a sequence one place to the right, so x31-x is x3+x4+⋯. A finite sequence is a polynomial: 1+x+x2+x3 equals 1-x41-x, the finite geometric sum.

These pieces are enough to read off many coefficients. For example, [x8](x21-3x+51-x) is the coefficient of x6 in 11-3x, which is 36=729, plus 5, giving 734. 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 ak ways to make a choice of size k from one source and bj ways from a second, independent source. A combined choice of total size n takes some size k from the first and the remaining n-k from the second, in akbn-k ways by the product rule. Adding over k by the sum rule gives

cn=∑k=0nakbn-k

which is exactly the coefficient of xn in A(x)B(x). This sum is the convolution of the two sequences, and it is the whole reason generating functions count: exponents add, so xk from one factor meets xn-k from the other on xn.

One die generates x+x2+⋯+x6, the exponent recording the score. In its square the coefficient of x7 collects x1x6 up to x6x1, the six ways to throw 7 with two dice, and the cube gives the 27 ways to throw 10 with three.

Now take k copies of 11-x, each standing for an unlimited supply of one kind of object, with the exponent counting how many are taken. The coefficient of xn in 1(1-x)k counts the ways to write n=n1+n2+⋯+nk with every ni≥0, which is a selection of n objects from k kinds with repetition. Stars and bars, from an earlier lesson (arrange n stars and k-1 dividing bars in a row), counts these as (n+k-1k-1). So

1(1-x)k=∑n≥0(n+k-1k-1)xn

and in particular 1(1-x)2 generates the numbers n+1.

Example. A shop sells lemon, cherry and mint sweets. How many bags of 10 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, 11-x; cherry must be even, 11-x2; mint is 0 or 1, 1+x. The bags are counted by the product

11-x⋅11-x2⋅(1+x)=1(1-x)2

because 1-x2=(1-x)(1+x) and the factor 1+x cancels. So there are n+1 bags of n sweets, and 11 bags of 10. A direct check agrees: with no mint the cherries number 0,2,…,10, six ways, and with one mint they number 0,2,…,8 out of nine, five ways.

Now you. A basket of fruit holds an even number of apples, a multiple of 5 bananas, at most 4 oranges and at most 1 pear. How many baskets of 20 fruit are there?

Answer

The factors are 11-x2, 11-x5, 1-x51-x for the oranges (the finite sum 1+x+⋯+x4) and 1+x. The 1-x5 cancels, and 1+x1-x2=11-x, so the product is 1(1-x)2 again and there are 21 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 1+x5+x10+⋯=11-x5, and the answer is the coefficient of x50 in

P(x)=1(1-x)(1-x2)(1-x5)(1-x10)(1-x20)

Its coefficients have no tidy closed form, but they are fast to compute. Suppose A(x) counts the ways using some coins, and B(x)=A(x)1-xc adds a coin worth c. Then B(x)(1-xc)=A(x), and comparing coefficients of xn gives bn-bn-c=an, that is,

bn=an+bn-c

with bn=an for n<c. In words, a payment either uses no coin worth c or uses one that can be removed. So each new coin is one pass along a row, adding the entry c places back.

Start with 1p coins alone, which give one way for every amount. Adding 2p coins gives 1,1,2,2,3,3,4,4,5,5,6 for amounts 0 to 10, the value ⌊n/2⌋+1 from choosing how many 2p coins to use. Adding 5p coins then gives 1,1,2,2,3,4,5,6,7,8,10. Carried on to 50p, the rows end at 26 with 1p and 2p only, then 146 with 5p, 341 with 10p, and finally

[x50]P(x)=450

A brute force count by computer agrees. Allowing a 50p coin as well adds exactly one way, the coin itself, for 451.

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 10, from the step b10=a10+b5=6+4: 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 0, 1 or 2 of the 2p coins, three ways, and with none the 10p takes 0 to 5 of the 2p coins, six ways, and 1+3+6=10.

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 20, where it ends at 11, then add 5p coins to get 1,1,2,2,3,4,5,6,7,8,10,11,13,14,16,18,20,22,24,26,29. Adding 10p coins changes each entry from 10 on by adding the new entry ten places back: b10=10+1=11 and b20=29+b10=29+11=40. There are 40 ways.

Fibonacci by partial fractions

A recurrence becomes an equation for its generating function. Let F(x)=∑Fnxn for the Fibonacci numbers, with F0=0, F1=1 and Fn=Fn-1+Fn-2. Shifting by one and two places gives xF(x) and x2F(x), whose coefficients of xn are Fn-1 and Fn-2. So in F(x)-xF(x)-x2F(x) the coefficient of xn is Fn-Fn-1-Fn-2=0 for every n≥2, and only the first two terms survive: the constant F0=0 and (F1-F0)x=x. Hence

F(x)=x1-x-x2

The whole sequence is now one fraction. To read its coefficients, factor the denominator as (1-φx)(1-ψx). Expanding, this needs φ+ψ=1 and φψ=-1, so φ and ψ are the roots of t2=t+1: the golden ratio φ=1+52 and ψ=1-52, with φ-ψ=5. Then split the fraction into partial fractions, a sum of the simple kind whose coefficients are known:

11-φx-11-ψx=(1-ψx)-(1-φx)(1-φx)(1-ψx)=(φ-ψ)x1-x-x2

Dividing by φ-ψ=5 gives F(x). Each piece is a geometric series, 11-φx generating φn, so

Fn=φn-ψn5

which is Binet's formula from the previous lesson. There it came from guessing that solutions look like rn; 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 Tn=2Tn-1+1, T0=0, with a generating function.

For n≥1 the coefficient of xn in T(x)-2xT(x) is Tn-2Tn-1=1, and the constant term is T0=0. So (1-2x)T(x)=x+x2+⋯=x1-x, and

T(x)=x(1-x)(1-2x)=11-2x-11-x

where the split checks because (1-x)-(1-2x)=x. Reading coefficients, Tn=2n-1, as unrolling found.

Now you. Solve an=an-1+6an-2 with a0=1 and a1=8 by a generating function, and find a6.

Answer

(1-x-6x2)A(x)=a0+(a1-a0)x=1+7x, and 1-x-6x2=(1-3x)(1+2x). Writing 1+7x(1-3x)(1+2x)=P1-3x+Q1+2x needs P+Q=1 and 2P-3Q=7, so P=2 and Q=-1. So an=2⋅3n-(-2)n, and a6=1458-64=1394, which the recurrence confirms: 14,62,146,518,1394.

The Catalan numbers

How many ways can n pairs of brackets be written so that they match? With three pairs there are five: ((())), (()()), (())(), ()(()) and ()()(). Call the number Cn, with C0=1 for the empty string. A nonempty matched string opens with a bracket, and that bracket closes somewhere, so the string is (A)B where A and B are matched strings. If the whole has n+1 pairs and A has k, then B has n-k, and

Cn+1=∑k=0nCkCn-k

This recurrence multiplies earlier terms together, so no characteristic equation applies. But the sum is a convolution, the coefficient of xn in C(x)2. Multiplying by x to shift it into place and adding back C0 gives

C(x)=1+xC(x)2

a quadratic in C(x). The quadratic formula gives C(x)=1±1-4x2x, and the sign is forced: with the plus sign the numerator has constant term 2, so dividing by 2x would leave a term 1x. Taking the minus sign, the formal square root 1-4x is the one series with constant term 1 whose square is 1-4x, and Newton's generalisation of the binomial theorem to the power 12 gives it:

1-4x=1-2x-2x2-4x3-10x4-28x5-⋯

Subtracting from 1 and dividing by 2x gives C(x)=1+x+2x2+5x3+14x4+⋯, matching the count of five for three pairs. The general coefficient of xm in the root, for m≥1, is (1/2m)(-4)m. Its numerator 12(12-1)⋯(12-m+1) is (-1)m-1 times 1⋅3⋅5⋯(2m-3) over 2m, and the odd product equals (2m-2)!2m-1(m-1)!. Multiplying by (-4)m and dividing by m! leaves -2m(2m-2m-1). Put m=n+1, negate and halve, and

Cn=1n+1(2nn)

So C4=705=14 and C10=18475611=16796. The same numbers count the ways to cut a convex polygon with n+2 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 (1+x)n; stars and bars is the coefficient of 1(1-x)k; 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, P(x) 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.