Generating Functions1
Generating Functions1
coefficients.
k
∑ ck x .
k=0
When viewed in the context of generating functions, we call such a power series
a generating series. The generating series generates the sequence
c0 , c1 , c2 , c3 , c4 , c5 , … .
Example 5.1.1.
What sequence is represented by the generating series
?
5
2 3 x 6
3 + 8x + x + + 100x + ⋯
7
Solution
Now you might very naturally ask why we would do such a thing. One reason is
that encoding a sequence with a power series helps us keep track of which term
is which in the sequence. For example, if we write the sequence
1, 3, 4, 6, 9, … , 24, 41, … it is impossible to determine which term 24 is (even if
we agreed that the first term was supposed to be a ). However, if we wrote the 0
https://discrete.openmathbooks.org/dmoi2/section-27.html 1/9
15/11/2022, 17:42 Generating Functions
the 17th term of the sequence (that is, a = 24). Of course to get this benefit we 17
use e as a way of talking about the sequence of coefficients of the power series
x
for e . When we write down a nice compact function which has an infinite power
x
series that we view as a generating series, then we call that function a generating
function. In this example, we would say
1 1 1 1
1, 1, , , ,…, ,… has generating function e x
2 6 24 n!
Let's see what the generating functions are for some very simple sequences. The
simplest of all: 1, 1, 1, 1, 1, …. What does the generating series look like? It is
simply 1 + x + x + x + x + ⋯ . Now, can we find a closed formula for this
2 3 4
power series? Yes! This particular series is really just a geometric series with
common ratio x. So if we use our “multiply, shift and subtract” technique from
Section 2.2, we have
2 3
S = 1 + x + x + x + ⋯
2 3 4
−xS = x + x + x + x + ⋯
−−−−−−−−−−−−−−−−−−−−−−−−−−−−
(1 − x)S = 1
You might remember from calculus that this is only true on the interval of
convergence for the power series, in this case when |x| < 1. That is true for us,
but we don't care. We are never going to plug anything in for x, so as long as
there is some value of x for which the generating function and generating series
agree, we are happy. And in this case we are happy.
1, 1, 1, … .
https://discrete.openmathbooks.org/dmoi2/section-27.html 2/9
15/11/2022, 17:42 Generating Functions
1
The generating function for 1, 1, 1, 1, 1, 1, … is
1 − x
Let's use this basic generating function to find generating functions for more
sequences. What if we replace x by −x. We get
1
= 1 − x + x
2
− x
3
+ ⋯ which generates 1, −1, 1, −1, …
1 + x
If we replace x by 3x we get
1
= 1 + 3x + 9x
2
+ 27x
3
+ ⋯ which generates 1, 3, 9, 27, …
1 − 3x
sequences, but not all. For example, you cannot plug in anything for x to get the
generating function for 2, 2, 2, 2, … . However, we are not lost yet. Notice that
each term of 2, 2, 2, 2, … is the result of multiplying the terms of 1, 1, 1, 1, … by
the constant 2. So multiply the generating function by 2 as well.
2
= 2 + 2x + 2x
2
+ 2x
3
+ ⋯ which generates 2, 2, 2, 2, …
1 − x
Similarly, to find the generating function for the sequence 3, 9, 27, 81, … , we
note that this sequence is the result of multiplying each term of 1, 3, 9, 27, … by
3. Since we have the generating function for 1, 3, 9, 27, … we can say
3
= 3 ⋅ 1 + 3 ⋅ 3x + 3 ⋅ 9x
2
+ 3 ⋅ 27x
3
+ ⋯ which generates 3, 9, 27, 81, …
1 − 3x
What about the sequence 2, 4, 10, 28, 82, … ? Here the terms are always 1 more
than powers of 3. That is, we have added the sequences 1, 1, 1, 1, … and
1, 3, 9, 27, … term by term. Therefore we can get a generating function by
2 3 2 3
= 1 + x + x + x + ⋯ + 1 + 3x + 9x + 27x + ⋯
1 1
= +
1 − x 1 − 3x
The fun does not stop there: if we replace x in our original generating function
by x we get
2
2
= 1 + x
2
+ x
4
+ x
6
⋯ which generates 1, 0, 1, 0, 1, 0, … .
1 − x
How could we get 0, 1, 0, 1, 0, 1, … ? Start with the previous sequence and shift it
over by 1. But how do you do this? To see how shifting works, let's first try to get
the generating function for the sequence 0, 1, 3, 9, 27, … . We know that
= 1 + 3x + 9x + 27x + ⋯ . To get the zero out front, we need the
1 2 3
1−3x
https://discrete.openmathbooks.org/dmoi2/section-27.html 3/9
15/11/2022, 17:42 Generating Functions
1−3x
0, 1, 0, 1, 0, 1, … :
x
2
= x + x
3
+ x
5
+ ⋯ which generates 0, 1, 0, 1, 0, 1 …
1 − x
Here's a sneaky one: what happens if you take the derivative of ? We get 1
1−x
1
. On the other hand, if we differentiate term by term in the power series,
2
(1−x)
we get (1 + x + x + x + ⋯) = 1 + 2x + 3x
2 3 ′ 2
+ 4x
3
+ ⋯ which is the
generating series for 1, 2, 3, 4, … . This says
1, 2, 3, … .
1
The generating function for 1, 2, 3, 4, 5, … is 2
.
(1 − x)
3
= 2 + 6x + 12x
2
+ 20x
3
+ ⋯ . So
1
(1−x)
3
= 1 + 3x + 6x
2
+ 10x
3
+ ⋯ is a generating function for the triangular
numbers, 1, 3, 6, 10 … (although here we have a 0 = 1 while T 0 = 0 usually).
Differencing ¶
We have seen how to find generating functions from using multiplication 1−x
1
For example, consider the sequence 2, 4, 10, 28, 82, … . How could we move to
the sequence of first differences: 2, 6, 18, 54, … ? We want to subtract 2 from the
4, 4 from the 10, 10 from the 28, and so on. So if we subtract (term by term) the
sequence 0, 2, 4, 10, 28, … from 2, 4, 10, 28 … , we will be set. We can get the
generating function for 0, 2, 4, 10, 28, … from the generating function for
https://discrete.openmathbooks.org/dmoi2/section-27.html 4/9
15/11/2022, 17:42 Generating Functions
2 3 4
A = 2 + 4x + 10x + 28x + 82x + ⋯
2 3 4 5
−xA = 0 + 2x + 4x + 10x + 28x + 82x + ⋯
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 3 4
(1 − x)A = 2 + 2x + 6x + 18x + 54x + ⋯
Example 5.1.2.
Find a generating function for 1, 3, 5, 7, 9, … .
Solution
Now that we have a generating function for the odd numbers, we can use that
to find the generating function for the squares:
Example 5.1.3.
Find the generating function for 1, 4, 9, 16, … . Note we take
1 = a0 .
Solution
sequence and subtract 3 times the previous term and then add 2 times the term
before that, we get 0 (since a − 3a + 2a = 0). That will hold for all but the
n n−1 n−2
first two terms of the sequence. So after the first two terms, the sequence of
results of these calculations would be a sequence of 0's, for which we definitely
know a generating function.
Example 5.1.4.
The sequence 1, 3, 7, 15, 31, 63, … satisfies the recurrence
relation a n = 3an−1 − 2an−2 . Find the generating function for the sequence.
Solution
and B, we need to do a lot of distributing (infinite FOIL?) but keep in mind we will
group like terms and only need to write down the first few terms to see the
pattern. The constant term is a b . The coefficient of x is a b + a b . And so on.
0 0 0 1 1 0
We get:
https://discrete.openmathbooks.org/dmoi2/section-27.html 5/9
15/11/2022, 17:42 Generating Functions
2 3
AB = a0 b0 + (a0 b1 + a1 b0 )x + (a0 b2 + a1 b1 + a2 b0 )x + (a0 b3 + a1 b2 + a2 b1 + a3 b0 )x + ⋯
Example 5.1.5.
“Multiply” the sequence 1, 2, 3, 4, … by the sequence
1, 2, 4, 8, 16, … .
Solution
getting the triangular numbers. More precisely, we get the sequence of partial
sums of 1, 2, 3, 4, 5, … . In terms of generating functions, we take 1
1−x
(1−x)
2
this give . This should not be a surprise as we found the same generating
1
(1−x)
3
The point is, if you need to find a generating function for the sum of the first n
terms of a particular sequence, and you know the generating function for that
sequence, you can multiply it by . To go back from the sequence of partial
1
1−x
sums to the original sequence, you look at the sequence of differences. When
you get the sequence of differences you end up multiplying by 1 − x, or
equivalently, dividing by . Multiplying by
1−x
1
gives partial sums, dividing by
1
1−x
1−x
gives differences.
Example 5.1.6.
Solve the recurrence relation a n
= 3an−1 − 2an−2 with initial
conditions a 0 = 1 and a 1 = 3 .
Solution
We can now add generating functions to our list of methods for solving
recurrence relations.
Exercises ¶
1. Find the generating function for each of the following sequences by relating
them back to a sequence with known generating function.
a. 4, 4, 4, 4, 4, … .
b. 2, 4, 6, 8, 10, … .
https://discrete.openmathbooks.org/dmoi2/section-27.html 6/9
15/11/2022, 17:42 Generating Functions
c. 0, 0, 0, 2, 4, 6, 8, 10, … .
d. 1, 5, 25, 125, … .
e. 1, −3, 9, −27, 81, … .
f. 1, 0, 5, 0, 25, 0, 125, 0, … .
g. 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, … .
Solution
4x
a. .
1 − x
1
b. .
1 − 4x
x
c. .
1 + x
3x
d. 2
.
(1 + x)
2
1 + x + x
e. 2
(Hint: multiplication).
(1 − x)
Solution
3. Show how you can get the generating function for the triangular numbers in
three different ways:
4. Use differencing to find the generating function for 4, 5, 7, 10, 14, 19, 25, … .
Solution
5. Find a generating function for the sequence with recurrence relation
an = 3an−1 − an−2 with initial terms a 0 = 1 and a 1 = 5 .
Solution
6. Use the recurrence relation for the Fibonacci numbers to find the generating
function for the Fibonacci sequence.
Solution
7. Use multiplication to find the generating function for the sequence of partial
sums of Fibonacci numbers, S 0
, S1 , S2 , … where S 0
= F0 ,S 1
= F0 + F1 ,
https://discrete.openmathbooks.org/dmoi2/section-27.html 7/9
15/11/2022, 17:42 Generating Functions
S2 = F 0 + F 1 + F 2 ,S 3
= F0 + F1 + F2 + F3 and so on.
Solution
8. Find the generating function for the sequence with closed formula
n
an = 2(5 ) + 7(−3)
n
.
Solution
9. Find a closed formula for the nth term of the sequence with generating
3x 1
function + .
1 − 4x 1 − x
Solution
2 x
10. Find a 7 for the sequence with generating function 2
⋅
2
.
(1 − x) 1 − x − x
Solution
1
11. Explain how we know that is the generating function for
(1 − x)2
1, 2, 3, 4, … .
Solution
a. 1, 0, 2, 0, 3, 0, 4, … .
b. 1, −2, 3, −4, 5, −6, … .
c. 0, 3, 6, 9, 12, 15, 18, … .
d. 0, 3, 9, 18, 30, 45, 63, … . (Hint: relate this sequence to the previous one.)
Solution
1
13. You may assume that 1, 1, 2, 3, 5, 8, … has generating function 2
1 − x − x
(because it does). Use this fact to find the sequence generated by each of the
following generating functions.
a. .
2
x
2
1−x−x
b. 1
2
1−x −x
4
.
c. 1
1−3x−9x
2
.
d. 1
(1−x−x )(1−x)
2
.
Solution
14. Find the generating function for the sequence 1, −2, 4, −8, 16, … .
https://discrete.openmathbooks.org/dmoi2/section-27.html 8/9
15/11/2022, 17:42 Generating Functions
Solution
16. Suppose A is the generating function for the sequence 3, 5, 9, 15, 23, 33, … .
https://discrete.openmathbooks.org/dmoi2/section-27.html 9/9