# 6.1: Generating Functions

- Last updated

- Save as PDF

- Page ID
- 15347

- Contributed by Oscar Levin
- Associate Professor (Mathematics) at University of Northern Colorado

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\( \def\d{\displaystyle}\)

\( \newcommand{\f}[1]{\mathfrak #1}\)

\( \newcommand{\s}[1]{\mathscr #1}\)

\( \def\N{\mathbb N}\)

\( \def\B{\mathbf{B}}\)

\( \def\circleA{(-.5,0) circle (1)}\)

\( \def\Z{\mathbb Z}\)

\( \def\circleAlabel{(-1.5,.6) node[above]{$A$}}\)

\( \def\Q{\mathbb Q}\)

\( \def\circleB{(.5,0) circle (1)}\)

\( \def\R{\mathbb R}\)

\( \def\circleBlabel{(1.5,.6) node[above]{$B$}}\)

\( \def\C{\mathbb C}\)

\( \def\circleC{(0,-1) circle (1)}\)

\( \def\F{\mathbb F}\)

\( \def\circleClabel{(.5,-2) node[right]{$C$}}\)

\( \def\A{\mathbb A}\)

\( \def\twosetbox{(-2,-1.5) rectangle (2,1.5)}\)

\( \def\X{\mathbb X}\)

\( \def\threesetbox{(-2,-2.5) rectangle (2,1.5)}\)

\( \def\E{\mathbb E}\)

\( \def\O{\mathbb O}\)

\( \def\U{\mathcal U}\)

\( \def\pow{\mathcal P}\)

\( \def\inv{^{-1}}\)

\( \def\nrml{\triangleleft}\)

\( \def\st{:}\)

\( \def\~{\widetilde}\)

\( \def\rem{\mathcal R}\)

\( \def\sigalg{$\sigma$-algebra }\)

\( \def\Gal{\mbox{Gal}}\)

\( \def\iff{\leftrightarrow}\)

\( \def\Iff{\Leftrightarrow}\)

\( \def\land{\wedge}\)

\( \def\And{\bigwedge}\)

\( \def\entry{\entry}\)

\( \def\AAnd{\d\bigwedge\mkern-18mu\bigwedge}\)

\( \def\Vee{\bigvee}\)

\( \def\VVee{\d\Vee\mkern-18mu\Vee}\)

\( \def\imp{\rightarrow}\)

\( \def\Imp{\Rightarrow}\)

\( \def\Fi{\Leftarrow}\)

\( \def\var{\mbox{var}}\)

\( \def\Th{\mbox{Th}}\)

\( \def\entry{\entry}\)

\( \def\sat{\mbox{Sat}}\)

\( \def\con{\mbox{Con}}\)

\( \def\iffmodels{\bmodels\models}\)

\( \def\dbland{\bigwedge \!\!\bigwedge}\)

\( \def\dom{\mbox{dom}}\)

\( \def\rng{\mbox{range}}\)

\( \def\isom{\cong}\)

\(\DeclareMathOperator{\wgt}{wgt}\)

\( \newcommand{\vtx}[2]{node[fill,circle,inner sep=0pt, minimum size=4pt,label=#1:#2]{}}\)

\( \newcommand{\va}[1]{\vtx{above}{#1}}\)

\( \newcommand{\vb}[1]{\vtx{below}{#1}}\)

\( \newcommand{\vr}[1]{\vtx{right}{#1}}\)

\( \newcommand{\vl}[1]{\vtx{left}{#1}}\)

\( \renewcommand{\v}{\vtx{above}{}}\)

\( \def\circleA{(-.5,0) circle (1)}\)

\( \def\circleAlabel{(-1.5,.6) node[above]{$A$}}\)

\( \def\circleB{(.5,0) circle (1)}\)

\( \def\circleBlabel{(1.5,.6) node[above]{$B$}}\)

\( \def\circleC{(0,-1) circle (1)}\)

\( \def\circleClabel{(.5,-2) node[right]{$C$}}\)

\( \def\twosetbox{(-2,-1.4) rectangle (2,1.4)}\)

\( \def\threesetbox{(-2.5,-2.4) rectangle (2.5,1.4)}\)

\( \def\ansfilename{practice-answers}\)

\( \def\shadowprops

Callstack: at (Template:MathJaxLevin), /content/body/div/p[1]/span, line 1, column 11 at template() at (Bookshelves/Combinatorics_and_Discrete_Mathematics/Book:_Discrete_Mathematics_(Levin)/5:_Additional_Topics/5.1:_Generating_Functions), /content/body/p[1]/span, line 1, column 22 at wiki.page() at (Courses/Saint_Mary's_College,_Notre_Dame,_IN/SMC:_MATH_339_-_Discrete_Mathematics_(Rohatgi)/Text/6:_Additional_Topics/6.1:_Generating_Functions), /content/body/div/pre, line 2, column 10

\( \renewcommand{\bar}{\overline}\)

\( \newcommand{\card}[1]{\left| #1 \right|}\)

\( \newcommand{\twoline}[2]{\begin{pmatrix}#1 \\ #2 \end{pmatrix}}\)

\( \newcommand{\lt}{<}\)

\( \newcommand{\gt}{>}\)

\( \newcommand{\amp}{&}\)

\( \newcommand{\hexbox}[3]{

\def\x{-cos{30}*\r*#1+cos{30}*#2*\r*2}

\def\y{-\r*#1-sin{30}*\r*#1}

\draw (\x,\y) +(90:\r) -- +(30:\r) -- +(-30:\r) -- +(-90:\r) -- +(-150:\r) -- +(150:\r) -- cycle;

\draw (\x,\y) node{#3};

}\)

\(\renewcommand{\bar}{\overline}\)

\(\newcommand{\card}[1]{\left| #1 \right|}\)

\(\newcommand{\twoline}[2]{\begin{pmatrix}#1 \\ #2 \end{pmatrix}}\)

\(\newcommand{\lt}{<}\)

\(\newcommand{\gt}{>;}\)

\(\newcommand{\amp}{&}\)

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:

\begin{equation*} \sum_{k=0}^\infty c_k x^k. \end{equation*}or expanded like this

\begin{equation*} c_0 + c_1x + c_2x^2 + c_3x^3 + c_4x^4 + c_5x^5 + \cdots. \end{equation*}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 \(\PageIndex{1}\)

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

**Solution**-
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

\begin{equation*} 3, 0, 8, 1, \frac{1}{7}, 100, \ldots \end{equation*}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

### 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

\begin{equation*} 1 + x + x^2 + x^3 \cdots = \dfrac{1}{1-x} \end{equation*}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

\begin{equation*} \frac{1}{1+x} = 1 - x + x^2 - x^3 + \cdots \mbox{ which generates } 1, -1, 1, -1, \ldots \end{equation*}If we replace \(x\) by \(3x\) we get

\begin{equation*} \frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots \mbox{ which generates } 1, 3, 9, 27, \ldots \end{equation*}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.

\begin{equation*} \frac{2}{1-x} = 2 + 2x + 2x^2 + 2x^3 + \cdots \mbox{ which generates } 2, 2, 2, 2, \ldots \end{equation*}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

\begin{equation*} \frac{3}{1-3x} = 3\cdot 1 + 3\cdot 3x + 3\cdot 9x^2 + 3\cdot 27x^3 + \cdots \mbox{ which generates } 3, 9, 27, 81, \ldots \end{equation*}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:

\begin{align*} 2 + 4x + 10x^2 + 28x^3 + \cdots \amp = (1 + 1) + (1 + 3)x + (1 + 9)x^2 + (1 + 27)x^3 + \cdots\\ \amp = 1 + x + x^2 + x^3 + \cdots + 1 + 3x + 9x^2 + 27x^3 + \cdots\\ \amp = \frac{1}{1-x} + \frac{1}{1-3x} \end{align*}The fun does not stop there: if we replace \(x\) in our original generating function by \(x^2\) we get

\begin{equation*} \frac{1}{1-x^2} = 1 + x^2 + x^4 + x^6\cdots \mbox{ which generates } 1, 0, 1, 0, 1, 0, \ldots. \end{equation*}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)!

\begin{equation*} \frac{1}{1-x^2} + \frac{x}{1-x^2} = \frac{1}{1-x}. \end{equation*}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}.\)

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).

### 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:

\begin{align*} A \amp = 2 + 4x + 10x^2 +28x^3 + 82x^4 + \cdots\\ \underline{-xA} \amp \underline{\,\,= 0 + 2x + 4x^2 + 10x^3 + 28 x^4 + 82x^5 + \cdots}\\ (1-x)A \amp = 2 + 2x + 6x^2 + 18x^3 + 54x^4 + \cdots \end{align*}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 \(\PageIndex{2}\)

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

**Solution**-
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

\begin{align*} A \amp = 1 + 3x + 5x^2 + 7x^3 + 9x^4 + \cdots\\ \underline{-xA} \amp \underline{\,\,= 0 + x + 3x^2 + 5x^3 + 7x^4 + 9x^5 + \cdots}\\ (1-x)A \amp = 1 + 2x + 2x^2 + 2x^3 + 2x^4 + \cdots \end{align*}We know that \(2x + 2x^2 + 2x^3 + 2x^4 + \cdots = \dfrac{2x}{1-x}\text{.}\) Thus

\begin{equation*} (1-x)A = 1 + \frac{2x}{1-x}. \end{equation*}Now solve for \(A\text{:}\)

\begin{equation*} A = \frac{1}{1-x} + \frac{2x}{(1-x)^2} = \frac{1+x}{(1-x)^2}. \end{equation*}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 \(\PageIndex{3}\)

Find the generating function for \(1, 4, 9, 16, \ldots\text{.}\) Note we take \(1 = a_0\text{.}\)

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

\begin{align*} A \amp = 1 + 4x + 9x^2 + 16x^3 + \cdots\\ \underline{- xA} \amp \underline{\,\, = 0 + x + 4x^2 + 9x^3 + 16x^4 + \cdots}\\ (1-x)A \amp = 1 + 3x + 5x^2 + 7x^3 + \cdots \end{align*}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 \(\PageIndex{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.

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

\begin{align*} A \amp = 1 + 3x + 7x^2 + 15x^3 + 31x^4 + \cdots + a_nx^n + \cdots\\ -3xA \amp = 0 - 3x - 9x^2 - 21x^3 - 45x^4 - \cdots - 3a_{n-1}x^n - \cdots\\ \underline{+~~~2x^2A_{~}^{~^{~}}} \amp \underline{\,\, = 0 + 0x + 2x^2 + 6x^3 + 14x^4 + \cdots + 2a_{n-2}x^n + \cdots}\\ (1-3x+2x^2)A \amp = 1 \end{align*}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{:}\)

\begin{equation*} A = \frac{1}{1 - 3x + 2x^2}. \end{equation*}

### 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:

\begin{equation*} AB = a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + (a_0b_3 + a_1b_2 + a_2b_1 + a_3b_0)x^3 + \cdots \end{equation*}Example \(\PageIndex{5}\)

“Multiply” the sequence \(1, 2, 3, 4, \ldots\) by the sequence \(1, 2, 4, 8, 16, \ldots\text{.}\)

**Solution**-
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 = 28\text{.}\) The resulting sequence is

\begin{equation*} 1, 4, 11, 28, 57, \ldots \end{equation*}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, 28, 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.

### 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 \(\PageIndex{6}\)

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

**Solution**-
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:

\begin{equation*} \frac{1}{1-3x + 2x^2} = \frac{1}{(1-x)(1-2x)}. \end{equation*}Partial fraction decomposition tells us that we can write this faction as the sum of two fractions (we decompose the given fraction):

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{a}{1-x} + \frac{b}{1-2x} \text{ ~~ for some constants } a \text{ and } b. \end{equation*}To find \(a\) and \(b\) we add the two decomposed fractions using a common denominator. This gives

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{a(1-2x) + b(1-x)}{(1-x)(1-2x)}. \end{equation*}so

\begin{equation*} 1 = a(1-2x) + b(1-x). \end{equation*}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:

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{-1}{1-x} + \frac{2}{1-2x}. \end{equation*}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.

\begin{equation*} \frac{-1}{1-x} = -1 - x - x^2 -x^3 - x^4 - \cdots \mbox{ which generates } -1, -1, -1, -1, -1, \ldots. \end{equation*} \begin{equation*} \frac{2}{1-2x} = 2 + 4x + 8x^2 + 16x^3 + 32x^4 + \cdots \mbox{ which generates } 2, 4, 8, 16, 32, \ldots. \end{equation*}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

\begin{equation*} a_n = 2^{n+1} - 1 \end{equation*}

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