## Section 5.1 Generating Functions

There is an extremely powerful tool in discrete mathematics used to manipulate sequences called the generating function. The idea is this: instead of an infinite sequence (for example: \(2, 3, 5, 8, 12, \ldots\)) we look at a single function which encodes the sequence. But not a function which gives the \(n\)th term as output. Instead, a function whose power series (like from calculus) â€śdisplaysâ€ť the terms of the sequence. So for example, we would look at the power series \(2 + 3x + 5x^2 + 8x^3 + 12x^4 + \cdots\) which displays the sequence \(2, 3, 5, 8, 12, \ldots\) as coefficients.

An infinite power series is simply an infinite sum of terms of the form \(c_nx^n\) were \(c_n\) is some constant. So we might write a power series like this:

or expanded like this

When viewed in the context of generating functions, we call such a power series a *generating series*. The generating series generates the sequence

In other words, the sequence generated by a generating series is simply the sequence of *coefficients* of the infinite polynomial.

### Example 5.1.1.

What sequence is represented by the generating series \(3 + 8x^2 + x^3 + \frac{x^5}{7} + 100x^6 + \cdots\text{?}\)

We just read off the coefficients of each \(x^n\) term. So \(a_0 = 3\) since the coefficient of \(x^0\) is 3 (\(x^0 = 1\) so this is the constant term). What is \(a_1\text{?}\) It is NOT 8, since 8 is the coefficient of \(x^2\text{,}\) so 8 is the term \(a_2\) of the sequence. To find \(a_1\) we need to look for the coefficient of \(x^1\) which in this case is 0. So \(a_1 = 0\text{.}\) Continuing, we have \(a_2 = 8\text{,}\) \(a_3 = 1\text{,}\) \(a_4 = 0\text{,}\) and \(a_5 = \frac{1}{7}\text{.}\) So we have the sequence

Note that when discussing generating functions, we always start our sequence with \(a_0\text{.}\)

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, \ldots, 24, 41,\ldots\) it is impossible to determine which term \(24\) is (even if we agreed that the first term was supposed to be \(a_0\)). However, if we wrote the generating series instead, we would have \(1 + 3x + 4x^2 + 6x^3 + 9x^4 + \cdots + 24 x^{17} + 41 x^{18} + \cdots\text{.}\) Now it is clear that 24 is the 17th term of the sequence (that is, \(a_{17} = 24\)). Of course to get this benefit we could have displayed our sequence in any number of ways, perhaps \(\fbox{1}_0 \fbox{3}_1 \fbox{4}_2 \fbox{6}_3 \fbox{9}_4 \cdots \fbox{24}_{17}\fbox{41}_{18}\cdots\text{,}\) but we do not do this. The reason is that the generating series looks like an ordinary power series (although we are interpreting it differently) so we can do things with it that we ordinarily do with power series such as write down what it converges to.

For example, from calculus we know that the power series \(1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \cdots + \frac{x^n}{n!} + \cdots\) converges to the function \(e^x\text{.}\) So we can use \(e^x\) as a way of talking about the sequence of coefficients of the power series for \(e^x\text{.}\) When we write down a nice compact function which has an infinite power series that we view as a generating series, then we call that function a *generating function*. In this example, we would say

### Subsection Building Generating Functions

The \(e^x\) example is very specific. We have a rather odd sequence, and the only reason we know its generating function is because we happen to know the Taylor series for \(e^x\text{.}\) Our goal now is to gather some tools to build the generating function of a particular given sequence.

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^2 + x^3 + x^4 + \cdots\text{.}\) Now, can we find a closed formula for this power series? Yes! This particular series is really just a geometric series with common ratio \(x\text{.}\) So if we use our â€śmultiply, shift and subtractâ€ť technique from SectionÂ 2.2, we have

Therefore we see that

You might remember from calculus that this is only true on the interval of convergence for the power series, in this case when \(|x| \lt 1\text{.}\) That is true for us, but we don't care. We are never going to plug anything in for \(x\text{,}\) 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,\ldots\).

The generating function for \(1,1,1,1,1,1,\ldots\) is \(\dfrac{1}{1-x}\)

Let's use this basic generating function to find generating functions for more sequences. What if we replace \(x\) by \(-x\text{.}\) We get

If we replace \(x\) by \(3x\) we get

By replacing the \(x\) in \(\frac{1}{1-x}\) we can get generating functions for a variety of sequences, but not all. For example, you cannot plug in anything for \(x\) to get the generating function for \(2,2,2,2, \ldots\text{.}\) However, we are not lost yet. Notice that each term of \(2, 2, 2, 2, \ldots\) is the result of multiplying the terms of \(1, 1, 1, 1, \ldots\) by the constant 2. So multiply the generating function by 2 as well.

Similarly, to find the generating function for the sequence \(3, 9, 27, 81, \ldots\text{,}\) we note that this sequence is the result of multiplying each term of \(1, 3, 9, 27, \ldots\) by 3. Since we have the generating function for \(1, 3, 9, 27, \ldots\) we can say

What about the sequence \(2, 4, 10, 28, 82, \ldots\text{?}\) Here the terms are always 1 more than powers of 3. That is, we have added the sequences \(1,1,1,1,\ldots\) and \(1,3,9, 27,\ldots\) term by term. Therefore we can get a generating function by adding the respective generating functions:

The fun does not stop there: if we replace \(x\) in our original generating function by \(x^2\) we get

How could we get \(0,1,0,1,0,1,\ldots\text{?}\) 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, \ldots\text{.}\) We know that \(\frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots\text{.}\) To get the zero out front, we need the generating series to look like \(x + 3x^2 + 9x^3 + 27x^4+ \cdots\) (so there is no constant term). Multiplying by \(x\) has this effect. So the generating function for \(0, 1, 3, 9, 27, \ldots\) is \(\frac{x}{1-3x}\text{.}\) This will also work to get the generating function for \(0,1,0,1,0,1,\ldots\text{:}\)

What if we add the sequences \(1,0,1,0,1,0,\ldots\) and \(0,1,0,1,0,1,\ldots\) term by term? We should get \(1,1,1,1,1,1\ldots\text{.}\) What happens when we add the generating functions? It works (try it)!

Here's a sneaky one: what happens if you take the *derivative* of \(\frac{1}{1-x}\text{?}\) We get \(\frac{1}{(1-x)^2}\text{.}\) On the other hand, if we differentiate term by term in the power series, we get \((1 + x + x^2 + x^3 + \cdots)' = 1 + 2x + 3x^2 + 4x^3 + \cdots\) which is the generating series for \(1, 2, 3, 4, \ldots\text{.}\) This says

#### \(1,2,3,\ldots\).

The generating function for \(1, 2, 3, 4, 5, \ldots\) is \(\d\frac{1}{(1-x)^2}\text{.}\)

Take a second derivative: \(\frac{2}{(1-x)^3} = 2 + 6x + 12x^2 + 20x^3 + \cdots\text{.}\) So \(\frac{1}{(1-x)^3} = 1 + 3x + 6x^2 + 10x^3 + \cdots\) is a generating function for the triangular numbers, \(1,3,6,10\ldots\) (although here we have \(a_0 = 1\) while \(T_0 = 0\) usually).

### Subsection Differencing

We have seen how to find generating functions from \(\frac{1}{1-x}\) using multiplication (by a constant or by \(x\)), substitution, addition, and differentiation. To use each of these, you must notice a way to transform the sequence \(1,1,1,1,1\ldots\) into your desired sequence. This is not always easy. It is also not really the way we have analyzed sequences. One thing we have considered often is the sequence of differences between terms of a sequence. This will turn out to be helpful in finding generating functions as well. The sequence of differences is often simpler than the original sequence. So if we know a generating function for the differences, we would like to use this to find a generating function for the original sequence.

For example, consider the sequence \(2, 4, 10, 28, 82, \ldots\text{.}\) How could we move to the sequence of first differences: \(2, 6, 18, 54,\ldots\text{?}\) 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,\ldots\) from \(2, 4, 10, 28\ldots\text{,}\) we will be set. We can get the generating function for \(0,2,4,10,28,\ldots\) from the generating function for \(2,4,10,28\ldots\) by multiplying by \(x\text{.}\) Use \(A\) to represent the generating function for \(2, 4, 10, 28, 82, \ldots\) Then:

While we don't get exactly the sequence of differences, we do get something close. In this particular case, we already know the generating function \(A\) (we found it in the previous section) but most of the time we will use this differencing technique to *find* \(A\text{:}\) if we have the generating function for the sequence of differences, we can then solve for \(A\text{.}\)

#### Example 5.1.2.

Find a generating function for \(1, 3, 5, 7, 9,\ldots\text{.}\)

Notice that the sequence of differences is constant. We know how to find the generating function for any constant sequence. So denote the generating function for \(1, 3, 5, 7, 9, \ldots\) by \(A\text{.}\) We have

We know that \(2x + 2x^2 + 2x^3 + 2x^4 + \cdots = \dfrac{2x}{1-x}\text{.}\) Thus

Now solve for \(A\text{:}\)

Does this makes sense? Before we simplified the two fractions into one, we were adding the generating function for the sequence \(1,1,1,1,\ldots\) to the generating function for the sequence \(0, 2, 4, 6, 8, 10, \ldots\) (remember \(\frac{1}{(1-x)^2}\) generates \(1,2,3,4,5, \ldots\text{,}\) multiplying by \(2x\) shifts it over, putting the zero out front, and doubles each term). If we add these term by term, we get the correct sequence \(1,3,5,7, 9, \ldots\text{.}\)

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, \ldots\text{.}\) Note we take \(1 = a_0\text{.}\)

Again we call the generating function for the sequence \(A\text{.}\) Using differencing:

Since \(1 + 3x + 5x^2 + 7x^3 + \cdots = \d\frac{1+x}{(1-x)^2}\) we have \(A = \d\frac{1+x}{(1-x)^3}\text{.}\)

In each of the examples above, we found the difference between consecutive terms which gave us a sequence of differences for which we knew a generating function. We can generalize this to more complicated relationships between terms of the sequence. For example, if we know that the sequence satisfies the recurrence relation \(a_n = 3a_{n-1} - 2a_{n-2}\text{?}\) In other words, if we take a term of the sequence and subtract 3 times the previous term and then add 2 times the term before that, we get 0 (since \(a_n - 3a_{n-1} + 2a_{n-2} = 0\)). That will hold for all but the 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, \ldots\) satisfies the recurrence relation \(a_n = 3a_{n-1} - 2a_{n-2}\text{.}\) Find the generating function for the sequence.

Call the generating function for the sequence \(A\text{.}\) We have

We multiplied \(A\) by \(-3x\) which shifts every term over one spot and multiplies them by \(-3\text{.}\) On the third line, we multiplied \(A\) by \(2x^2\text{,}\) which shifted every term over two spots and multiplied them by 2. When we add up the corresponding terms, we are taking each term, subtracting 3 times the previous term, and adding 2 times the term before that. This will happen for each term after \(a_1\) because \(a_n - 3a_{n-1} + 2a_{n-2} = 0\text{.}\) In general, we might have two terms from the beginning of the generating series, although in this case the second term happens to be 0 as well.

Now we just need to solve for \(A\text{:}\)

### Subsection Multiplication and Partial Sums

What happens to the sequences when you multiply two generating functions? Let's see: \(A = a_0 + a_1x + a_2x^2 + \cdots\) and \(B = b_0 + b_1x + b_2x^2 + \cdots\text{.}\) To multiply \(A\) and \(B\text{,}\) 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_0b_0\text{.}\) The coefficient of \(x\) is \(a_0b_1 + a_1b_0\text{.}\) And so on. We get:

#### Example 5.1.5.

â€śMultiplyâ€ť the sequence \(1, 2, 3, 4, \ldots\) by the sequence \(1, 2, 4, 8, 16, \ldots\text{.}\)

The new constant term is just \(1 \cdot 1\text{.}\) The next term will be \(1\cdot 2 + 2 \cdot 1 = 4\text{.}\) The next term: \(1 \cdot 4 + 2 \cdot 2 + 3 \cdot 1 = 11\text{.}\) One more: \(1 \cdot 8 + 2 \cdot 4 + 3 \cdot 2 + 4 \cdot 1 = 26\text{.}\) The resulting sequence is

Since the generating function for \(1,2,3,4, \ldots\) is \(\frac{1}{(1-x)^2}\) and the generating function for \(1,2,4,8, 16, \ldots\) is \(\frac{1}{1-2x}\text{,}\) we have that the generating function for \(1,4, 11, 26, 57, \ldots\) is \(\frac{1}{(1-x)^2(1-2x)}\)

Consider the special case when you multiply a sequence by \(1, 1, 1, \ldots\text{.}\) For example, multiply \(1,1,1,\ldots\) by \(1, 2, 3, 4, 5\ldots\text{.}\) The first term is \(1\cdot 1 = 1\text{.}\) Then \(1\cdot 2 + 1 \cdot 1 = 3\text{.}\) Then \(1\cdot 3 + 1\cdot 2 + 1 \cdot 1 = 6\text{.}\) The next term will be 10. We are getting the triangular numbers. More precisely, we get the sequence of partial sums of \(1,2,3,4,5, \ldots\text{.}\) In terms of generating functions, we take \(\frac{1}{1-x}\) (generating \(1,1,1,1,1\ldots\)) and multiply it by \(\frac{1}{(1-x)^2}\) (generating \(1,2,3,4,5,\ldots\)) and this give \(\frac{1}{(1-x)^3}\text{.}\) This should not be a surprise as we found the same generating function for the triangular numbers earlier.

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 \(\frac{1}{1-x}\text{.}\) To go back from the sequence of partial 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\text{,}\) or equivalently, dividing by \(\frac{1}{1-x}\text{.}\) Multiplying by \(\frac{1}{1-x}\) gives partial sums, dividing by \(\frac{1}{1-x}\) gives differences.

### Subsection Solving Recurrence Relations with Generating Functions

We conclude with an example of one of the many reasons studying generating functions is helpful. We can use generating functions to solve recurrence relations.

#### Example 5.1.6.

Solve the recurrence relation \(a_n = 3a_{n-1} - 2a_{n-2}\) with initial conditions \(a_0 = 1\) and \(a_1 = 3\text{.}\)

We saw in an example above that this recurrence relation gives the sequence \(1, 3, 7, 15, 31, 63, \ldots\) which has generating function \(\dfrac{1}{1 - 3x + 2x^2}\text{.}\) We did this by calling the generating function \(A\) and then computing \(A - 3xA + 2x^2A\) which was just 1, since every other term canceled out.

But how does knowing the generating function help us? First, break up the generating function into two simpler ones. For this, we can use partial fraction decomposition. Start by factoring the denominator:

Partial fraction decomposition tells us that we can write this faction as the sum of two fractions (we decompose the given fraction):

To find \(a\) and \(b\) we add the two decomposed fractions using a common denominator. This gives

so

This must be true for all values of \(x\text{.}\) If \(x = 1\text{,}\) then the equation becomes \(1 = -a\) so \(a = -1\text{.}\) When \(x = \frac{1}{2}\) we get \(1 = b/2\) so \(b = 2\text{.}\) This tells us that we can decompose the fraction like this:

This completes the partial fraction decomposition. Notice that these two fractions are generating functions we know. In fact, we should be able to expand each of them.

We can give a closed formula for the \(n\)th term of each of these sequences. The first is just \(a_n = -1\text{.}\) The second is \(a_n = 2^{n+1}\text{.}\) The sequence we are interested in is just the sum of these, so the solution to the recurrence relation is

We can now add generating functions to our list of methods for solving recurrence relations.

### Exercises Exercises

#### 1.

Find the generating function for each of the following sequences by relating them back to a sequence with known generating function.

\(4,4,4,4,4,\ldots\text{.}\)

\(2, 4, 6, 8, 10, \ldots\text{.}\)

\(0,0,0,2,4,6,8,10,\ldots\text{.}\)

\(1, 5, 25, 125, \ldots\text{.}\)

\(1, -3, 9, -27, 81, \ldots\text{.}\)

\(1, 0, 5, 0, 25, 0, 125, 0, \ldots\text{.}\)

\(0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, \ldots\text{.}\)

\(\dfrac{4}{1-x}\text{.}\)

\(\dfrac{2}{(1-x)^2}\text{.}\)

\(\dfrac{2x^3}{(1-x)^2}\text{.}\)

\(\dfrac{1}{1-5x}\text{.}\)

\(\dfrac{1}{1+3x}\text{.}\)

\(\dfrac{1}{1-5x^2}\text{.}\)

\(\dfrac{x}{(1-x^3)^2}\text{.}\)

#### 2.

Find the sequence generated by the following generating functions:

\(\dfrac{4x}{1-x}\text{.}\)

\(\dfrac{1}{1-4x}\text{.}\)

\(\dfrac{x}{1+x}\text{.}\)

\(\dfrac{3x}{(1+x)^2}\text{.}\)

\(\dfrac{1+x+x^2}{(1-x)^2}\) (Hint: multiplication).

\(0, 4, 4, 4, 4, 4, \ldots\text{.}\)

\(1, 4, 16, 64, 256, \ldots\text{.}\)

\(0, 1, -1, 1, -1, 1, -1, \ldots\text{.}\)

\(0, 3, -6, 9, -12, 15, -18, \ldots\text{.}\)

\(1, 3, 6, 9, 12, 15, \ldots\text{.}\)

#### 3.

Show how you can get the generating function for the triangular numbers in three different ways:

Take two derivatives of the generating function for \(1,1,1,1,1, \ldots\)

Use differencing.

Multiply two known generating functions.

#### 4.

Use differencing to find the generating function for \(4, 5, 7, 10, 14, 19, 25, \ldots\text{.}\)

Call the generating function \(A\text{.}\) Compute \(A - xA = 4 + x + 2x^2 + 3x^3 + 4x^4 + \cdots\text{.}\) Thus \(A - xA = 4 + \dfrac{x}{(1-x)^2}\text{.}\) Solving for \(A\) gives \(\d\frac{4}{1-x} + \frac{x}{(1-x)^3}\text{.}\)

#### 5.

Find a generating function for the sequence with recurrence relation \(a_n = 3a_{n-1} - a_{n-2}\) with initial terms \(a_0 = 1\) and \(a_1 = 5\text{.}\)

\(\dfrac{1+2x}{1-3x + x^2}\text{.}\)

#### 6.

Use the recurrence relation for the Fibonacci numbers to find the generating function for the Fibonacci sequence.

Compute \(A - xA - x^2A\) and the solve for \(A\text{.}\) The generating function will be \(\dfrac{x}{1-x-x^2}\text{.}\)

#### 7.

Use multiplication to find the generating function for the sequence of partial sums of Fibonacci numbers, \(S_0, S_1, S_2, \ldots\) where \(S_0 = F_0\text{,}\) \(S_1 = F_0 + F_1\text{,}\) \(S_2 = F_0 + F_1 + F_2\text{,}\) \(S_3 = F_0 + F_1 + F_2 + F_3\) and so on.

\(\dfrac{x}{(1-x)(1-x-x^2)}\text{.}\)

#### 8.

Find the generating function for the sequence with closed formula \(a_n = 2(5^n) + 7(-3)^n\text{.}\)

\(\dfrac{2}{1-5x} + \dfrac{7}{1+3x}\text{.}\)

#### 9.

Find a closed formula for the \(n\)th term of the sequence with generating function \(\dfrac{3x}{1-4x} + \dfrac{1}{1-x}\text{.}\)

\(a_n = 3\cdot 4^{n-1} + 1\text{.}\)

#### 10.

Find \(a_7\) for the sequence with generating function \(\dfrac{2}{(1-x)^2}\cdot\dfrac{x}{1-x-x^2}\text{.}\)

You should â€śmultiplyâ€ť the two sequences.

#### 11.

Explain how we know that \(\dfrac{1}{(1-x)^2}\) is the generating function for \(1, 2, 3, 4, \ldots\text{.}\)

#### 12.

Starting with the generating function for \(1,2,3,4, \ldots\text{,}\) find a generating function for each of the following sequences.

\(1, 0, 2, 0, 3, 0, 4,\ldots\text{.}\)

\(1, -2, 3, -4, 5, -6, \ldots\text{.}\)

\(0, 3, 6, 9, 12, 15, 18, \ldots\text{.}\)

\(0, 3, 9, 18, 30, 45, 63,\ldots\text{.}\) (Hint: relate this sequence to the previous one.)

\(\frac{1}{(1-x^2)^2}\text{.}\)

\(\frac{1}{(1+x)^2}\text{.}\)

\(\frac{3x}{(1-x)^2}\text{.}\)

\(\frac{3x}{(1-x)^3}\text{.}\) (partial sums).

#### 13.

You may assume that \(1, 1, 2, 3, 5, 8,\ldots\) has generating function \(\dfrac{1}{1-x-x^2}\) (because it does). Use this fact to find the sequence generated by each of the following generating functions.

\(\frac{x^2}{1-x-x^2}\text{.}\)

\(\frac{1}{1-x^2-x^4}\text{.}\)

\(\frac{1}{1-3x-9x^2}\text{.}\)

\(\frac{1}{(1-x-x^2)(1-x)}\text{.}\)

\(0,0,1,1,2,3,5,8, \ldots\text{.}\)

\(1, 0, 1, 0, 2, 0, 3, 0, 5, 0, 8, 0, \ldots\text{.}\)

\(1, 3, 18, 81, 405, \ldots\text{.}\)

\(1, 2, 4, 7, 12, 20, \ldots\text{.}\)

#### 14.

Find the generating function for the sequence \(1, -2, 4, -8, 16, \ldots\text{.}\)

#### 15.

Find the generating function for the sequence \(1, 1, 1, 2, 3, 4, 5, 6, \ldots\text{.}\)

\(\frac{x^3}{(1-x)^2} + \frac{1}{1-x}\text{.}\)

#### 16.

Suppose \(A\) is the generating function for the sequence \(3, 5, 9, 15, 23, 33, \ldots\text{.}\)

Find a generating function (in terms of \(A\)) for the sequence of differences between terms.

Write the sequence of differences between terms and find a generating function for it (without referencing \(A\)).

Use your answers to parts (a) and (b) to find the generating function for the original sequence.