Generating Functions
Generating Functions
Generating Functions
Wintercamp 2020
1 Motivating Example
In many problems, especially in recurrence relations, we encounter a sequence of numbers a0 , a1 , . . . , an , . . .,
indexed by the natural numbers. It turns out to be very convenient to be able to put all these num-
bers together in one object, like a laundry line. We can do this by making them the coefficients of
a formal power series:
∞
X
2 n
f (x) = a0 + a1 x + a2 x + · · · + an x + · · · = ai x i .
n=0
We will generally not worry about the convergence of this series and usually the x’s are just there
as place holders. However, we will encounter a couple of problems and some techniques where it
does become important that we can substitute values for x, so don’t completely forget about this.
The idea will be that we can manipulate the power series in ways that will help us find a closed
formula for the an as a function of n. This will usually happen by recognizing the power series
as representing well-known functions that we can easily manipulate. For instance, we know that
n+1
1 + r + r2 + · · · + rn = 1−r
1−r
. When we assume that |r| < 1, this implies that
∞
2
X 1
1 + x + x + ··· = xn =
n=0
1−x
This is one of the basic series we will be working with and derive others from it.
Here is a first problem to warm up to some of the basic ideas.
Example 1. Let an = 5an−1 − 6an−2 with a0 = 2 and a1 = 5. Find a closed formula for an as a
function of n.
1
The sequence (cn ) is called the Cauchy product of (an ) and (bn ).
It will often be convenient to recognize a sequence as a Cauchy product of two other sequences
and use the generating functions directly to obtain a solution. One general type of situation where
this occurs is:
Example 2. Find the number an of ways n dollars can be changed into 1 or 2 dollar coins (regardless
of order). For example, when n = 3, there are two ways, namely three 1 dollar coins or one 1 dollar
coin and one 2 dollar coin.
Here are two other natural operations on sequences: taking partial sums gives us a new sequence
for which the generating function can be expressed in terms of the original generating function. The
reverse operation is that of taking finite differences.
Proposition 2. Let A(x) = n≥0 an xn be the generating function of (an ) and define sn = a0 +
P
a1 + · · · + an . Then the generating function for (sn ) is
X A(x) X
S(x) = s n xn = = A(x)(1 + x + x2 + · · · ) = A(x) xn .
n≥0
1−x n≥0
Example 4. Derivatives and antiderivatives can be very helpful in creating new generating func-
tions.
1. Given that A(x) = n≥0 an xn , what is the generating function for bn = nan ?
P
2. For any polyomial p(x), what is the generating function for bn = p(n)an ? In particular, what
is the generating function for bn = p(n)?
2
an
3. Given that A(x) = n≥0 an xn , what is the generating function for cn =
P
n+1
?
xm Xk
2. m+1
= xk
(1 − x) k≥0
m
1 X 2k
3. √ = xk
1 − 4x k≥0 k
1 X xn
4. ln =
1 − x n≥1 n
xk − b1 xk−1 − b2 xk−2 − · · · − bk = 0
You can then find the ci by solving a system of linear equations to ensure that the first k values,
a0 , a1 , . . . , ak−1 are correct.
3
If there is a repeated root in this polynomial (or equivalently, there is a repeated factor in the
denominator of the generating function), say β has multiplicity m (this would mean that (1 − βx)m
is a factor of the denominator of the generating function), then we need to add c1 β n + c2 nβ n +
· · · + cm nm−1 β n to the sum for the solution. We see that in our partial fractions approach as well,
because we have to use fractions with all powers (1 − βx)` with 1 ≤ ` ≤ m. In particular, here we
see that we can just treat the generating functions as usual and we will get our solution.
Recurrence relations of the form an = b1 an−1 + b2 an−2 + · · · + bk an−k + f (n) are still linear,
but not homogeneous. In this case, we need to find one special solution for this system (not
thinking about the initial conditions at this point) and then the general solution can be obtained
by taking the special solution and adding the general solution of the homogeneous equation an =
b1 an−1 + b2 an−2 + · · · + bk an−k . If you do this with the generating functions, you will get the answer
in the usual way.
Here are some examples:
Example 5. Towers of Hanoi There are three pegs and there are n disks of all different sizes. At
the beginning of the game these disks are all stacked on one peg in order of decreasing size, with
the largest on the bottom. The goal of the game is to transfer all disks onto one of the other pegs,
but at no time can we put a larger disk on top of a smaller one. The third peg can be used to take
the extra steps needed to avoid this.
Let hn be the number of moves required to transfer n disks. Obviously, h0 = 0 andh1 = 1,
and it is not hard to check that h2 = 3. It s not hard to develop a recurrence relation for hn :
if we can the top transfer n − 1 disks to another peg, then move our largest disk to the empty
peg and then move the other n − 1 disks again, we will have them all in the right spot. So
hn = 2hn−1 + 1 for n ≥ 1 and h0 = 0. We could find a formula for hn by iteration. You may check
n −1
that hn = 2n−1 + · · · + 22 + 2 + 1 = 22−1 = 2n − 1.
However, things don’t always work so nicely, so it is useful to see how our more general method
can work here. Let g(x) = n≥0 hn xn . Then
P
g(x) = h0 + h1 x + h2 x2 + · · · + hn xn + · · ·
2xg(x) = 2h0 x + 2h1 x2 + · · · + 2hn−1 xn + · · ·
4
Using partial fractions we find
1 1
g(x) = −
1 − 2x 1 − x
X X
= (2x)n − xn
n≥0 n≥0
X
= (2 − 1)xn .
n
n≥0
5
When we expand this product, we pick one term from each factor in all possible ways, but only a
finite number of terms that are not a 1. If we take an x4 from the first factor, an x2 from the second
factor and an x25 from the fifth factor, this means that we have taken four 1’s, one 2, and five 5’s.
So we have the partition 1 + 1 + 1 + 1 + 2 + 5 + 5 + 5 + 5 + 5 = 31. We see that the coefficient for
xk gives the number of partitions of k. Note that although this is an infinite product, in practice
we only need a finite number of factorsand a finite number of terms to find a particular pn .
Example 7. Find p8 , the number of partitions of 8.
We can further manipulate the generating function above to obtain other generating functions.
Do this to solve the following problem.
Example 8. Find the generating function for partitions into all distinct parts, and the generating
function for partitions into odd parts. Show that for any positive integer n, the number of partitions
into distinct parts is the same as the number of partitions into odd parts.
Problems 2. 1. Find the number hn of bags of fruit that can be made out of apples, bananas,
oranges and pears, where, in each bag, the number of apples is even, the number of bananas
is a multiple of 5, the number of oranges is at most 4, and the number of pears is 0 or 1.
2. Let kn denote the number of nonnegative integral solutions of the equation
3e1 + 4e2 + 2e3 + 5e4 = n
Find the generating function g(x) for the hn .
3. There is available an unlimited number of pennies, nickles, dimes, quarters and half-dollar
pieces. Determine the generating funciton g(x) for the number fn of ways of making n cents
with these pieces.
4. Let hn denote the number of ways of dividing a convex polygonal region with n + 1 sides into
triangles be inserting diagonals that do not intersect in the interior of the region. Find both
the generating function and a closed formula for the hn . Note that these are the Catalan
numbers.
6
2. Use this to show that the probability distribution of rolling two normal six-sided dice is the
same as the probability distribution of rolling a die with sides 1, 2, 2, 3, 3, 4 and a die with
sides 1, 3, 4, 5, 6, 8.
Roots of unity have yet another role to play here. Let m be any positive integer and let λ be
an m-th root of unity other than 1. When we consider a power series
X
B(x) = bn x n
n≥0
we are sometimes interested in finding the sum of the coefficients that have an index that is a
multiple of m (for instance, we may consider the sum of the even-indexed coefficients),
bm + b2m + b3m + · · ·
Here is we can use the following
1 − λmj
1 + λj + λ2j + · · · + λ(m−1)j = =0
1 − λj
for any j that is not divisible by m (since λm = 1). On the other hand, if j is a multiple of m, we
see that
1 + λj + λ2j + · · · + λ(m−1)j = m
So we get that
m−1
1 X
B(λj ) = bm + b2m + b3m + · · ·
m j=0
so evaluating the generating function at this sum of m-th roots of unity allows us to filter out the
sum of coefficients indexed by the multiples of m.
For odd m, we have a further useful factorization:
1 + tm = (1 + t)(1 + λt)(1 + λ2 t) · · · (1 + λm−1 t)
since both polynomials are monic of degree m and have roots −1/λi for i = 0, 1, 2, . . . , m − 1.
Problems 3. 1. Can the set N of all positive integers be partitioned into morethan one, but still
a finite number of arithmetic progressions with no two having the same common differences?
2. (IMO 1995) Let p be an odd prime number. Find the number of subsets A of the set
{1, 2, . . . , 2p} such that
(a) A has exactly p elements, and
(b) the sum of all the elements in A is divisible by p.
3. An a × b rectangle can be tiled by a number of p × 1 and 1 × q types of rectangles, where
a, b, p, q are fixed positive integers. When we tile with these rectangles the orientation is fixed,
so a k × 1 and a 1 × k rectangle are different for this purpose. Prove that a is divisible by p
or b is divisible by q.
7
6 Further Problems
1. Let Fn be the n-th Fibonacci number. Show that
X Fn
=2
n≥1
2n
3. Show that
2n 2n 1 2n
− =
n n+1 n+1 n
4. Prove that for any positive integer n,
2 2 2 2
n n n n 2n
+ + + ··· + =
0 1 2 n n
5. (Putnam 1992, B2) For nonnegative integers n and k, define Q(n, k) to be the coefficient of
xk in the expansion of (1 + x + x2 + x3 )n . Prove that
k
X n n
Q(n, k) = .
j=0
j k − 2j
6. Let a0 = 1 and a1 = 1 and an = 4an−1 − 4an−2 for n ≥ 2. Find a formula for an in terms of n.
7. Let n be a positive integer. Find the number an of polynomials P (x) with coefficients in
{0, 1, 2, 3} such that P (2) = n.
8. Consider a 1-by-n chessboard, suppose that we colour each square of the chessboard with one
of the two colours red and blue. Let hn be the number of colourings in which no two squares
that are coloured red are adjacent. Find and verify a recurrence relation that hn satisfies.
Then derive a formula for hn .
9. Find a recursion for the number of ways to place flags on an n foot pole, where we have red
flags that are 2 feet high, blue flags that are 1 foot high and yellow flags that are 1 foot high.
The heights of the flags must add up to n. Solve the recursion.
10. Find a recurrence for the sequence (un ) such that un is the number of pairs (a, b) of nonnegative
numbers such that
2a + 5b = n
8
11. How many positive numbers are there less than 10, 000, 000 whose digits sum to 10?
12. Let
1 · 3 · 5 · · · (2n − 1)
an = .
2n n!
Show that a0 , a1 , a2 , . . . is the unique sequence satisfying a0 = 1 and
n
X
a)kan−k = 1
k=0
13. (HMMT 2007) Let S denote the set of triples (i, j, k) of positive integers where i + j + k = 17.
Compute X
ijk.
(i,j,k)∈S
14. Let Sn be the number of triples (a, b, c) of nonnegative integers such that a + 2b + 3c = n.
Compute the sum
∞
X Sn
n=0
3n
15. (Putnam 2003, A6) For a set S of nonnegative integers, let rS (n) denote the number of ordered
pairs (s1 , s2 ) such that s1 , s2 ∈ S, s1 6= s2 and s1 + s2 = n. Let A be the set of nonnegative
integers that have an odd number of ones in their binary representation, and let B be the set
of nonnegative integers that have an even number of ones in their binary representation.Show
that rA (n) = rB (n) for all n.
16. (IMO2008, problem 5) Let n and k be positive integers with k ≥ n and k − n an even number.
Let 2n lamps labelled 1, 2, . . . , 2n be given, each of which can be either on or off. Initially all
lamps are off. we consider sequences of steps: at each step one of hte lamps is switched (from
on to off or from off to on).
Let N be the number of such sequences consisting of k steps and resulting in the state where
the lamps 1 through n are all on, and lamps n + 1 through 2n are all off.
Let M be the number of such sequences consisting of k steps, resulting in the state where
lamps 1 through n are all on, and lamps n + 1 through 2n are all off, but where none of the
lamps n + 1 through 2n is ever switched on.
Determine the ratio M/N .
9
18. Let hn denote the number of regions into which a convex polygonal region with n + 2 sides
is divided by its diagonals, assuming that no three diagonals have a common point. Define
h0 = 0. Show that
n+1
hn = hn−1 + + n, (n ≥ 1).
3
Then determine the generating function for hn and obtain a formula for hn .
20. Consider a set of 2n people sitting around a table. In how many ways can we arrange for each
person to shake hands with another person at the table such that no two handshakes cross?
10