Generating Functions

A generating function stores a sequence as the coefficients of a power series: the sequence a_0, a_1, a_2, \ldots becomes G(x) = a_0 + a_1 x + a_2 x^2 + \cdots. Generating functions turn counting problems into polynomial multiplication, and they turn recurrence relations into algebra that you can solve for G(x) to get a closed formula.

Before this lesson: Solving Linear Recurrence Relations, Combinations with Repetition, Infinite Geometric Series and Partial Fraction Decomposition

The idea

A sequence is an infinite list of numbers, which is awkward to work with as a whole. A generating function packs the whole list into one expression: the term a_n becomes the coefficient of x^n. The powers of x act only as labels that keep the terms in order, so you never plug in a number for x.

For example, the sequence 1, 3, 9, 27, … has generating function 1 + 3x + 9x^2 + 27x^3 + \cdots. This is a geometric series with ratio 3x, so it equals \dfrac{1}{1 - 3x}. One short fraction now carries every term of the sequence.

The sequence 1, 3, 9, 27, 81 in a row of boxes labeled a sub 0 through a sub 4. An arrow from each box points down to its term in the power series 1 + 3x + 9x squared + 27x cubed + 81x to the fourth, which equals 1 over (1 − 3x).
Each term of the sequence becomes the coefficient of one power of x, so the whole sequence 3ⁿ is stored in 1/(1 − 3x).

Two facts make this useful. Multiplying generating functions matches the way choices combine when you count, and shifting a sequence matches multiplying by x, which turns a recurrence into an equation for G(x).

Generating function rules

The ordinary generating function of a sequence (a_n) is

G(x) = \sum_{n=0}^{\infty} a_n x^n = a_0 + a_1 x + a_2 x^2 + \cdots

We treat it as a formal power series: an algebraic object whose coefficients we track, with no question of convergence. The table lists the generating functions used most often. Each comes from the geometric series \frac{1}{1 - r} = 1 + r + r^2 + \cdots or from the binomial theorem.

Sequence a_nFirst termsGenerating function
11, 1, 1, 1, …\dfrac{1}{1 - x}
c^n1, c, c², c³, …\dfrac{1}{1 - cx}
n + 11, 2, 3, 4, …\dfrac{1}{(1 - x)^2}
\binom{n + k - 1}{k - 1}1, k, …\dfrac{1}{(1 - x)^k}
\binom{m}{n}1, m, …, 1, 0, 0, …(1 + x)^m
1 for n ≤ m, then 01, 1, …, 1, 0, 0, …1 + x + \cdots + x^m = \dfrac{1 - x^{m+1}}{1 - x}

Three operations do most of the work.

  • Shift: if G(x) generates a_n, then x^k G(x) generates the sequence moved k places to the right, with k zeros in front.
  • Sum: adding generating functions adds the sequences term by term.
  • Product: if A(x) generates a_n and B(x) generates b_n, then A(x)B(x) generates c_n = a_0 b_n + a_1 b_{n-1} + \cdots + a_n b_0, called the convolution.

The product rule is what makes counting work. If one choice can contribute i to a total and a second choice can contribute j, then x^i \cdot x^j = x^{i+j}, so the coefficient of x^n in the product counts the ways the two choices add to n.

How to use generating functions

To count selections:

  1. Write one factor for each type of item. Its terms are x^k for each amount k that type can contribute.
  2. Multiply the factors.
  3. The coefficient of x^n is the number of ways to reach a total of n.

To solve a recurrence relation:

  1. Let G(x) = \sum a_n x^n. Multiply the recurrence by x^n and add over every n where it holds.
  2. Rewrite each sum in terms of G(x), subtracting the initial terms that each sum leaves out.
  3. Solve the result for G(x).
  4. Split G(x) into partial fractions of the form \frac{A}{1 - rx}.
  5. Read off a_n: each \frac{A}{1 - rx} contributes A r^n.

Generating function examples

Example 1: From a generating function to its sequence

Find a formula for a_n if the generating function is G(x) = \dfrac{2}{1 - 3x} + \dfrac{5}{1 + x}, and list the first four terms.

\frac{1}{1 - 3x} = \sum_{n=0}^{\infty} 3^n x^n

Use \frac{1}{1 - cx} with c = 3 (see infinite geometric series).

\frac{1}{1 + x} = \sum_{n=0}^{\infty} (-1)^n x^n

Write 1 + x as 1 − (−1)x, so c = −1.

G(x) = \sum_{n=0}^{\infty} \left(2 \cdot 3^n + 5(-1)^n\right) x^n

Multiply by the constants and add the two series term by term.

a_n = 2 \cdot 3^n + 5(-1)^n

Read off the coefficient of x^n.

a_0 = 2 + 5 = 7

Substitute n = 0.

a_1 = 6 - 5 = 1

Substitute n = 1.

a_2 = 18 + 5 = 23

Substitute n = 2.

a_3 = 54 - 5 = 49

Substitute n = 3.

Answer: a_n = 2 \cdot 3^n + 5(-1)^n, so the sequence starts 7, 1, 23, 49.

Example 2: Choosing fruit (word problem)

A student packs 6 pieces of fruit. She takes at most 2 apples and at most 3 bananas, and oranges come only in bags of 2. In how many ways can she choose the fruit?

A(x) = 1 + x + x^2

Apples: 0, 1 or 2.

B(x) = 1 + x + x^2 + x^3

Bananas: 0 to 3.

C(x) = 1 + x^2 + x^4 + x^6

Oranges: 0, 2, 4 or 6 (more than 6 cannot fit).

A(x)B(x) = 1 + 2x + 3x^2 + 3x^3 + 2x^4 + x^5

Multiply the first two factors (see multiplying polynomials).

[x^6]\,A B C = [x^6]AB + [x^4]AB + [x^2]AB + [x^0]AB

Pair each power of x in C(x) with the power of x in AB that makes 6.

= 0 + 2 + 3 + 1

Read the coefficients from A(x)B(x); it has no x^6 term.

= 6

Add.

Answer: there are 6 ways. (The notation [x^n]\,F(x) means the coefficient of x^n in F(x).) The six choices of (apples, bananas, oranges) are (0, 0, 6), (1, 1, 4), (0, 2, 4), (2, 0, 4), (1, 3, 2) and (2, 2, 2).

Example 3: Selections with repetition

A shop sells 3 flavors of donut. How many ways are there to choose 7 donuts if you take at least one of each flavor?

x + x^2 + x^3 + \cdots = \frac{x}{1 - x}

One flavor, with at least 1: factor out x from the geometric series.

G(x) = \left(\frac{x}{1 - x}\right)^3 = \frac{x^3}{(1 - x)^3}

Multiply one factor for each of the 3 flavors.

[x^7]\,\frac{x^3}{(1 - x)^3} = [x^4]\,\frac{1}{(1 - x)^3}

The factor x^3 shifts the coefficients by 3, and 7 − 3 = 4.

\frac{1}{(1 - x)^3} = \sum_{n=0}^{\infty} \binom{n + 2}{2} x^n

Use the table row \frac{1}{(1 - x)^k} with k = 3.

[x^4] = \binom{6}{2}

Substitute n = 4.

\binom{6}{2} = \frac{6 \cdot 5}{2} = 15

Evaluate the binomial coefficient (see permutations and combinations).

Answer: 15 ways. This matches stars and bars: give each flavor one donut first, then choose the other 4 from 3 flavors in \binom{4 + 2}{2} = 15 ways.

Example 4: A first-order recurrence

Use a generating function to solve a_n = 2a_{n-1} + 3 for n ≥ 1, with a_0 = 1.

\sum_{n \ge 1} a_n x^n = 2\sum_{n \ge 1} a_{n-1} x^n + 3\sum_{n \ge 1} x^n

Multiply the recurrence by x^n and add for n ≥ 1.

G(x) - 1 = 2xG(x) + \frac{3x}{1 - x}

The left sum misses a_0 = 1; the middle sum is x times G(x); the last is a geometric series that starts at x.

G(x) - 2xG(x) = 1 + \frac{3x}{1 - x}

Move 2xG(x) to the left and 1 to the right.

G(x)(1 - 2x) = \frac{1 + 2x}{1 - x}

Factor out G(x); on the right, 1 − x + 3x = 1 + 2x.

G(x) = \frac{1 + 2x}{(1 - x)(1 - 2x)}

Divide both sides by 1 − 2x.

1 + 2x = A(1 - 2x) + B(1 - x)

Set up partial fractions \frac{A}{1 - x} + \frac{B}{1 - 2x} (see partial fraction decomposition).

3 = -A

Substitute x = 1.

A = -3

Multiply both sides by −1.

2 = \tfrac{1}{2}B

Substitute x = 1/2.

B = 4

Multiply both sides by 2.

G(x) = \frac{4}{1 - 2x} - \frac{3}{1 - x}

Write the partial fractions.

a_n = 4 \cdot 2^n - 3

Read off coefficients: 4 \cdot 2^n and -3 \cdot 1^n.

a_1 = 2(1) + 3 = 5

Check with the recurrence.

4 \cdot 2 - 3 = 5

The formula gives the same value.

Answer: a_n = 4 \cdot 2^n - 3, giving 1, 5, 13, 29, ….

Example 5: A second-order recurrence

Use a generating function to solve a_n = a_{n-1} + 6a_{n-2} for n ≥ 2, with a_0 = 1 and a_1 = 8.

\sum_{n \ge 2} a_n x^n = \sum_{n \ge 2} a_{n-1} x^n + 6\sum_{n \ge 2} a_{n-2} x^n

Multiply by x^n and add for n ≥ 2.

G(x) - 1 - 8x = x\left(G(x) - 1\right) + 6x^2 G(x)

Restore the missing terms: the left misses a_0 + a_1 x, the middle misses a_0.

G(x) - xG(x) - 6x^2 G(x) = 1 + 8x - x

Collect the G(x) terms on the left and the rest on the right.

G(x)(1 - x - 6x^2) = 1 + 7x

Factor out G(x) and combine like terms.

1 - x - 6x^2 = (1 - 3x)(1 + 2x)

Factor the trinomial (see factoring trinomials).

G(x) = \frac{1 + 7x}{(1 - 3x)(1 + 2x)}

Divide both sides by the factored trinomial.

1 + 7x = A(1 + 2x) + B(1 - 3x)

Set up partial fractions \frac{A}{1 - 3x} + \frac{B}{1 + 2x}.

\tfrac{10}{3} = \tfrac{5}{3}A

Substitute x = 1/3.

A = 2

Divide both sides by 5/3.

-\tfrac{5}{2} = \tfrac{5}{2}B

Substitute x = −1/2.

B = -1

Divide both sides by 5/2.

G(x) = \frac{2}{1 - 3x} - \frac{1}{1 + 2x}

Write the partial fractions.

a_n = 2 \cdot 3^n - (-2)^n

Read off coefficients with c = 3 and c = −2.

a_2 = 8 + 6(1) = 14

Check with the recurrence.

2 \cdot 9 - 4 = 14

The formula gives the same value.

Answer: a_n = 2 \cdot 3^n - (-2)^n. The denominator 1 - x - 6x^2 holds the characteristic roots 3 and −2, the same roots the characteristic equation r^2 - r - 6 = 0 gives.

Common mistakes

MistakeExample of the errorFix
Dropping the initial terms when shifting a sumWriting \sum_{n \ge 1} a_n x^n = G(x)The sum misses a_0: it equals G(x) - a_0.
Starting a geometric series at the wrong power\sum_{n \ge 1} 3x^n = \frac{3}{1 - x}It starts at x, so it is \frac{3x}{1 - x}.
Losing the sign in \frac{1}{1 + cx}Reading \frac{1}{1 + 2x} as \sum 2^n x^nWrite it as \frac{1}{1 - (-2)x}, so the coefficients are (-2)^n.
Adding factors for “and” choicesA(x) + B(x) for apples and bananas togetherChoices made together multiply: use A(x)B(x).
Using an infinite factor for a limited supply\frac{1}{1 - x} for “at most 2 apples”A limit of 2 means 1 + x + x^2.
Forgetting the shift from a factor x^k[x^7]\frac{x^3}{(1 - x)^3} taken as \binom{9}{2}Subtract the shift first: [x^4]\frac{1}{(1 - x)^3} = \binom{6}{2}.

Related methods

Exponential generating functions

The exponential generating function of a_n is \sum a_n \frac{x^n}{n!}. It suits counts of arrangements, where order matters. For example, the sequence 1, 1, 1, … has exponential generating function e^x, a series from the lesson on Maclaurin series.

Connections to calculus

If a generating function converges near x = 0, it is an ordinary power series, and calculus applies. Differentiating \frac{1}{1 - x} = \sum x^n gives \frac{1}{(1 - x)^2} = \sum (n + 1)x^n, which is the third row of the table. The method of solving a recurrence through G(x) is the discrete version of solving differential equations with Laplace transforms: transform, solve with algebra, then transform back.

Practice problems

  1. Find the generating function of 1, 5, 25, 125, … in closed form.
  2. Find a_n if G(x) = \dfrac{3}{1 - 2x} - \dfrac{1}{1 + x}.
  3. Find the generating function of the sequence 0, 1, 1, 1, … in closed form.
  4. Find the coefficient of x^4 in (1 + x)^6.
  5. How many ways are there to choose 5 coins from pennies, nickels and dimes, with any number of each type?
  6. Use the generating function (x + x^2 + \cdots + x^6)^2 to find the number of ways to roll a sum of 5 with two dice.
  7. Use a generating function to solve a_n = 3a_{n-1} for n ≥ 1 with a_0 = 2.
  8. Use a generating function to solve a_n = a_{n-1} + 2 for n ≥ 1 with a_0 = 1.
  9. Use a generating function to solve a_n = 4a_{n-1} - 3a_{n-2} for n ≥ 2 with a_0 = 2 and a_1 = 4.
  10. In how many ways can you make 15 cents with pennies, nickels and dimes? Use the generating function \frac{1}{(1 - x)(1 - x^5)(1 - x^{10})}.
Show answers

1.

a_n = 5^n

Each term is 5 times the one before.

G(x) = \sum_{n=0}^{\infty} 5^n x^n

Write the generating function.

G(x) = \frac{1}{1 - 5x}

Use \frac{1}{1 - cx} with c = 5.

Answer: \dfrac{1}{1 - 5x}

2.

\frac{3}{1 - 2x} = \sum 3 \cdot 2^n x^n

Use \frac{1}{1 - cx} with c = 2.

\frac{1}{1 + x} = \sum (-1)^n x^n

Use c = −1.

a_n = 3 \cdot 2^n - (-1)^n

Subtract the coefficients.

Answer: a_n = 3 \cdot 2^n - (-1)^n (2, 7, 11, 25, …)

3.

G(x) = x + x^2 + x^3 + \cdots

The terms with n ≥ 1 are 1, and a_0 = 0.

G(x) = x(1 + x + x^2 + \cdots)

Factor out x.

G(x) = \frac{x}{1 - x}

Use \frac{1}{1 - x}.

Answer: \dfrac{x}{1 - x}

4.

[x^4]\,(1 + x)^6 = \binom{6}{4}

Use the binomial theorem.

\binom{6}{4} = \frac{6 \cdot 5 \cdot 4 \cdot 3}{4 \cdot 3 \cdot 2 \cdot 1}

Write out the binomial coefficient.

= \frac{360}{24} = 15

Multiply, then divide.

Answer: 15

5.

G(x) = \frac{1}{(1 - x)^3}

One factor \frac{1}{1 - x} for each of the 3 coin types.

[x^5] = \binom{5 + 2}{2} = \binom{7}{2}

Use the table row with k = 3 and n = 5.

\binom{7}{2} = \frac{7 \cdot 6}{2} = 21

Evaluate.

Answer: 21 ways

6.

[x^5]\,(x + \cdots + x^6)^2

Each die contributes x^i for a roll of i.

x^1 x^4,\ x^2 x^3,\ x^3 x^2,\ x^4 x^1

List the products of two terms with exponents adding to 5.

[x^5] = 4

Count them.

Answer: 4 ways: (1, 4), (2, 3), (3, 2) and (4, 1)

7.

G(x) - 2 = 3xG(x)

Multiply by x^n, add for n ≥ 1 and restore a_0 = 2.

G(x)(1 - 3x) = 2

Move 3xG(x) to the left and 2 to the right, then factor.

G(x) = \frac{2}{1 - 3x}

Divide by 1 − 3x.

Answer: a_n = 2 \cdot 3^n

8.

G(x) - 1 = xG(x) + \frac{2x}{1 - x}

Multiply by x^n, add for n ≥ 1 and restore a_0 = 1.

G(x)(1 - x) = 1 + \frac{2x}{1 - x}

Move xG(x) to the left and 1 to the right, then factor.

G(x) = \frac{1}{1 - x} + \frac{2x}{(1 - x)^2}

Divide by 1 − x.

\frac{x}{(1 - x)^2} = \sum n x^n

Shift \frac{1}{(1 - x)^2} = \sum (n + 1)x^n by one place.

a_n = 1 + 2n

Add the coefficients.

Answer: a_n = 2n + 1 (1, 3, 5, 7, …)

9.

G(x) - 2 - 4x = 4x(G(x) - 2) - 3x^2 G(x)

Multiply by x^n, add for n ≥ 2 and restore the missing terms.

G(x)(1 - 4x + 3x^2) = 2 + 4x - 8x

Collect the G(x) terms on the left and the rest on the right.

G(x) = \frac{2 - 4x}{(1 - x)(1 - 3x)}

Combine 4x − 8x = −4x, factor and divide.

2 - 4x = A(1 - 3x) + B(1 - x)

Set up partial fractions \frac{A}{1 - x} + \frac{B}{1 - 3x}.

-2 = -2A,\ A = 1

Substitute x = 1, then divide by −2.

\tfrac{2}{3} = \tfrac{2}{3}B,\ B = 1

Substitute x = 1/3, then divide by 2/3.

G(x) = \frac{1}{1 - x} + \frac{1}{1 - 3x}

Write the partial fractions.

Answer: a_n = 1 + 3^n (2, 4, 10, 28, …)

10.

[x^{15}] = \#\{(n, d) : 5n + 10d \le 15\}

Pennies fill whatever nickels n and dimes d leave, in exactly one way.

d = 0:\ n = 0, 1, 2, 3

With no dime, 5n ≤ 15 gives 4 choices.

d = 1:\ n = 0, 1

With one dime, 5n ≤ 5 gives 2 choices.

4 + 2 = 6

Add the cases (see sum and product rules for counting).

Answer: 6 ways

Generating functions FAQ

What is a generating function in discrete math?

It is a power series a_0 + a_1 x + a_2 x^2 + \cdots whose coefficients are the terms of a sequence. The powers of x only mark positions, so the series is used as an algebraic object rather than a function to evaluate.

How do you find the generating function of a sequence?

Write \sum a_n x^n and match it to a known series, such as \frac{1}{1 - cx} for c^n or (1 + x)^m for \binom{m}{n}. Use shifts, sums and products to build more complicated sequences from these.

How do you solve a recurrence relation with generating functions?

Multiply the recurrence by x^n, add over all n where it holds and rewrite each sum with G(x). Solve for G(x), split it into partial fractions and read off the coefficient of x^n.

What is the difference between ordinary and exponential generating functions?

An ordinary generating function uses a_n x^n and fits selections where order does not matter. An exponential generating function uses a_n x^n / n! and fits arrangements where order matters.

Summary

  • A generating function G(x) = \sum a_n x^n stores a sequence as coefficients.
  • Key pairs: \frac{1}{1 - cx} ↔ c^n, \frac{1}{(1 - x)^k} ↔ \binom{n + k - 1}{k - 1} and (1 + x)^m ↔ \binom{m}{n}.
  • For counting, multiply one factor per item type; the coefficient of x^n counts the ways to reach n.
  • For a recurrence, multiply by x^n, add, solve for G(x), use partial fractions and read off a_n.
  • Multiplying by x shifts a sequence; the product of two generating functions gives a convolution.

Previous lesson: Solving Linear Recurrence Relations

Next lesson: Properties of Relations

Back to the Discrete Math Index