Skip to content

Recursion


The heart of recursion is to assume that you know previous elements of a sequence and then find a way to construct the next element in that sequence. Not only will explicit recursion problems appear of the AIME, USAMO, or USAJMO, often counting or combinatoric problems include them as well.

These notes are adapted in part from Jeffrey Chen’s Recursion in the AIME handout and the generating-functions section of the summations notes.

A recurrence relation expresses a term of a sequence in terms of earlier terms. For example,

an=3anβˆ’1a_n = 3a_{n-1}

means that every term is three times the previous one. If a1=1a_1=1, then

a2=3,a3=9,a4=27,a_2=3,\qquad a_3=9,\qquad a_4=27,

so the sequence begins

1,3,9,27,81,…1,3,9,27,81,\dots

Because the relation holds for every valid nn, we can repeatedly substitute backwards. For instance, if

an=3anβˆ’1andanβˆ’1=3anβˆ’2,a_n=3a_{n-1} \qquad\text{and}\qquad a_{n-1}=3a_{n-2},

then

an=9anβˆ’2.a_n=9a_{n-2}.

In contest math, we often do not need a closed form.

Recursion becomes especially useful when direct counting is messy but a β€œlast move” or β€œfinal case split” is easy to describe.

Example 2.1. Fred wants to climb a 1010-step staircase. He can climb either 11 or 22 steps at a time. In how many ways can he climb the staircase?

Solution. Instead of counting only the 1010-step case, define

an=theΒ numberΒ ofΒ waysΒ toΒ climbΒ nΒ stairs.a_n = \text{the number of ways to climb } n \text{ stairs}.

Now think about Fred’s last move:

  • If his last move is a single step, then before that he had climbed nβˆ’1n-1 stairs.
  • If his last move is a double step, then before that he had climbed nβˆ’2n-2 stairs.

These are the only possibilities, so

an=anβˆ’1+anβˆ’2.a_n = a_{n-1} + a_{n-2}.

The initial values are

a1=1,a2=2.a_1=1,\qquad a_2=2.

So the sequence begins

1,2,3,5,8,13,21,34,55,89.1,2,3,5,8,13,21,34,55,89.

Therefore,

a10=89.a_{10}=89.

This is the Fibonacci recurrence in disguise, which appears constantly in AMC and AIME counting problems.

Example 3.1 (2007 AMC 12). Call a set of integers spacy if it contains no more than one out of any three consecutive integers. How many subsets of {1,2,3,…,12}\{1,2,3,\dots,12\}, including the empty set, are spacy?

Solution. Let

an=theΒ numberΒ ofΒ spacyΒ subsetsΒ ofΒ {1,2,…,n}.a_n = \text{the number of spacy subsets of } \{1,2,\dots,n\}.

Now split into two cases:

  • If a spacy subset contains nn, then it cannot contain nβˆ’1n-1 or nβˆ’2n-2. So the rest of the set is a spacy subset of {1,2,…,nβˆ’3}\{1,2,\dots,n-3\}, giving anβˆ’3a_{n-3} possibilities.
  • If a spacy subset does not contain nn, then it is just a spacy subset of {1,2,…,nβˆ’1}\{1,2,\dots,n-1\}, giving anβˆ’1a_{n-1} possibilities.

Hence

an=anβˆ’1+anβˆ’3.a_n=a_{n-1}+a_{n-3}.

The initial values are

a1=2,a2=3,a3=4,a_1=2,\qquad a_2=3,\qquad a_3=4,

since the spacy subsets are:

  • for n=1n=1: βˆ…,{1}\varnothing,\{1\},
  • for n=2n=2: βˆ…,{1},{2}\varnothing,\{1\},\{2\},
  • for n=3n=3: βˆ…,{1},{2},{3}\varnothing,\{1\},\{2\},\{3\}.

Now compute forward:

a4=6,a5=9,a6=13,a7=19,a8=28,a9=41,a10=60,a11=88,a12=129.\begin{aligned} a_4&=6, & a_5&=9, & a_6&=13, \\ a_7&=19, & a_8&=28, & a_9&=41, \\ a_{10}&=60, & a_{11}&=88, & a_{12}&=129. \end{aligned}

So the answer is

129.129.

Example 3.2 (AIME 2006). A collection of eight cubes consists of one cube with edge length kk for each integer kk from 11 to 88. A tower is built using all eight cubes, and the cube immediately on top of a cube with edge length kk must have edge length at most k+2k+2. How many towers can be constructed?

Solution. Define

an=theΒ numberΒ ofΒ validΒ towersΒ usingΒ cubesΒ 1,2,…,n.a_n=\text{the number of valid towers using cubes }1,2,\dots,n.

Look at the cube of edge length nn. In a valid tower, it can be:

  • at the bottom,
  • directly above cube nβˆ’1n-1,
  • directly above cube nβˆ’2n-2.

So for each valid tower on nβˆ’1n-1 cubes, there are exactly three ways to insert cube nn. Thus

an=3anβˆ’1.a_n=3a_{n-1}.

We begin with

a2=2,a_2=2,

since either the cube of side length 11 is on the bottom and 22 is on top, or vice versa.

Then

a8=36β‹…a2=36β‹…2=1458.a_8 = 3^6 \cdot a_2 = 3^6 \cdot 2 = 1458.

This is a good reminder that some recurrences only become valid after the first few terms; here the relation is not meant to be applied at n=2n=2 itself.

Catalan numbers arise in many recursive counting problems where a structure splits naturally into a left part and a right part.

Example 4.1. How many paths are there from (0,0)(0,0) to (n,n)(n,n) using only right and up steps, such that the path never goes above the line y=xy=x?

Let

Cn=theΒ numberΒ ofΒ suchΒ pathsΒ fromΒ (0,0)Β toΒ (n,n).C_n=\text{the number of such paths from }(0,0)\text{ to }(n,n).

To get a recurrence, look at the first point (i,i)(i,i) after (0,0)(0,0) where the path returns to the diagonal.

Then:

  • the initial portion of the path contributes Ciβˆ’1C_{i-1} possibilities,
  • the remaining portion contributes Cnβˆ’iC_{n-i} possibilities.

Summing over all possible first return points gives

Cn=C0Cnβˆ’1+C1Cnβˆ’2+β‹―+Cnβˆ’1C0=βˆ‘i=0nβˆ’1CiCnβˆ’iβˆ’1.C_n = C_0C_{n-1}+C_1C_{n-2}+\cdots+C_{n-1}C_0 =\sum_{i=0}^{n-1} C_iC_{n-i-1}.

For example, when n=3n=3, there are 55 such paths.

Theorem 4.2 (Catalan Numbers). The nnth Catalan number has explicit formula

Cn=1n+1(2nn).C_n=\frac{1}{n+1}\binom{2n}{n}.

We will not prove the closed form here, but it is one of the most important counting sequences in olympiad combinatorics.

Example 4.3. How many ways are there to arrange nn open brackets and nn closed brackets so that, reading from left to right, the number of closed brackets never exceeds the number of open brackets?

Solution. This is a Catalan-number problem.

Make the following bijection:

  • each open bracket corresponds to a step to the right,
  • each closed bracket corresponds to a step upward.

Then a bracket string with nn opens and nn closes becomes a path from (0,0)(0,0) to (n,n)(n,n). The condition that the number of closed brackets never exceeds the number of open brackets means the path never goes above the line y=xy=x.

So the answer is simply

Cn=1n+1(2nn).C_n=\frac{1}{n+1}\binom{2n}{n}.

These are good recursion problems to revisit after you are comfortable with the examples above.

Exercise 5.1. How many ways are there to tile a 10Γ—210 \times 2 board with 1Γ—21 \times 2 dominoes?

Exercise 5.2 (AMC 12 2019). How many binary sequences of length 1919 begin with 00, end with 00, contain no two consecutive 00β€˜s, and contain no three consecutive 11β€˜s?

Exercise 5.3 (AIME 2015). There are 210=10242^{10}=1024 possible 1010-letter strings using only AA and BB. How many do not contain more than three adjacent identical letters?

Exercise 5.4. Given a regular 2n2n-gon, how many ways are there to pair vertices with nonintersecting chords?

Exercise 5.5 (AIME 2001). A mail carrier delivers mail to the nineteen houses on one side of Elm Street. No two adjacent houses receive mail on the same day, and there are never more than two consecutive houses that get no mail. How many delivery patterns are possible?

Generating functions turn linear recurrences into algebraic equations and can also help evaluate sums. This section adapts the generating-functions material from the summations handout to recurrence problems.

The basic idea is this: given a sequence ana_n, package it into the formal power series

A(x)=βˆ‘nβ‰₯0anxn.A(x) = \sum_{n \ge 0} a_n x^n.

That lets us turn a whole sequence into one algebraic object. This is useful because:

  • A(x)A(x) may have a nice closed form.
  • The coefficients of A(x)A(x) encode the entire sequence.
  • Recurrence relations often turn into algebraic equations in A(x)A(x).
  • Once you get a rational expression for A(x)A(x), partial fractions can recover a closed form.

Example 5.1. Show that

βˆ‘nβ‰₯0(1000n)=21000.\sum_{n \ge 0} \binom{1000}{n} = 2^{1000}.

Let an=(1000n)a_n=\binom{1000}{n}. Then

A(x)=βˆ‘nβ‰₯0anxn=βˆ‘nβ‰₯0(1000n)xn=(1+x)1000.A(x)=\sum_{n \ge 0} a_nx^n=\sum_{n \ge 0}\binom{1000}{n}x^n=(1+x)^{1000}.

Plugging in x=1x=1 gives the identity immediately.

Example 5.2. Compute

βˆ‘nβ‰₯0n(1000n).\sum_{n \ge 0} n\binom{1000}{n}.

Differentiate the previous generating function:

βˆ‘nβ‰₯1n(1000n)xnβˆ’1=1000(1+x)999.\sum_{n \ge 1} n\binom{1000}{n}x^{n-1} = 1000(1+x)^{999}.

Now set x=1x=1:

βˆ‘nβ‰₯0n(1000n)=1000β‹…2999.\sum_{n \ge 0} n\binom{1000}{n}=1000\cdot 2^{999}.

Exercise 5.3. Compute

βˆ‘nβ‰₯0n2(1000n).\sum_{n \ge 0} n^2\binom{1000}{n}.

Consider the Lucas numbers LnL_n defined by

L0=2,L1=1,Ln+2=Ln+1+Ln.L_0=2,\qquad L_1=1,\qquad L_{n+2}=L_{n+1}+L_n.

The first few terms are

2,1,3,4,7,11,18,29,…2,1,3,4,7,11,18,29,\dots

Theorem 5.4 (Closed Form for Lucas Numbers). Let

Ξ±=1+52,Ξ²=1βˆ’52.\alpha=\frac{1+\sqrt5}{2}, \qquad \beta=\frac{1-\sqrt5}{2}.

Then

Ln=Ξ±n+Ξ²n.L_n=\alpha^n+\beta^n.

Proof. Consider the generating function

L(x)=βˆ‘nβ‰₯0Lnxn=2+x+3x2+4x3+7x4+⋯ .L(x)=\sum_{n \ge 0} L_nx^n = 2+x+3x^2+4x^3+7x^4+\cdots.

Now write shifted copies:

xL(x)=2x+x2+3x3+4x4+⋯ ,xL(x)=2x+x^2+3x^3+4x^4+\cdots, x2L(x)=2x2+x3+3x4+⋯ .x^2L(x)=2x^2+x^3+3x^4+\cdots.

Because Ln+2=Ln+1+LnL_{n+2}=L_{n+1}+L_n, subtracting gives

L(x)βˆ’xL(x)βˆ’x2L(x)=2βˆ’x.L(x)-xL(x)-x^2L(x)=2-x.

So

(1βˆ’xβˆ’x2)L(x)=2βˆ’x,(1-x-x^2)L(x)=2-x,

hence

L(x)=2βˆ’x1βˆ’xβˆ’x2.L(x)=\frac{2-x}{1-x-x^2}.

Factor the denominator:

1βˆ’xβˆ’x2=(1βˆ’Ξ±x)(1βˆ’Ξ²x).1-x-x^2=(1-\alpha x)(1-\beta x).

Then partial fractions give

L(x)=11βˆ’Ξ±x+11βˆ’Ξ²x.L(x)=\frac{1}{1-\alpha x}+\frac{1}{1-\beta x}.

Expand each as a geometric series:

L(x)=βˆ‘nβ‰₯0(Ξ±x)n+βˆ‘nβ‰₯0(Ξ²x)n=βˆ‘nβ‰₯0(Ξ±n+Ξ²n)xn.L(x)=\sum_{n \ge 0}(\alpha x)^n + \sum_{n \ge 0}(\beta x)^n =\sum_{n \ge 0}(\alpha^n+\beta^n)x^n.

Matching coefficients yields

Ln=Ξ±n+Ξ²n.L_n=\alpha^n+\beta^n.

Exercise 5.5. Derive Binet’s formula for Fibonacci numbers:

Fn=15(Ξ±nβˆ’Ξ²n).F_n=\frac{1}{\sqrt5}(\alpha^n-\beta^n).

As an intermediate step, show that the Fibonacci generating function is

x1βˆ’xβˆ’x2.\frac{x}{1-x-x^2}.

We already know

11βˆ’x=1+x+x2+β‹―\frac{1}{1-x}=1+x+x^2+\cdots

and

(1+x)n=βˆ‘kβ‰₯0(nk)xk.(1+x)^n=\sum_{k \ge 0}\binom{n}{k}x^k.

The next result gives many more useful expansions.

Theorem 5.6 (Generalized Binomial Theorem). For any real number rr,

(1+x)r=βˆ‘nβ‰₯0(rn)xn,(1+x)^r = \sum_{n \ge 0} \binom{r}{n}x^n,

where

(rn)=r(rβˆ’1)β‹―(rβˆ’n+1)n!.\binom{r}{n} = \frac{r(r-1)\cdots(r-n+1)}{n!}.

Proof sketch. Write

(1+x)r=a0+a1x+a2x2+a3x3+β‹―(1+x)^r = a_0+a_1x+a_2x^2+a_3x^3+\cdots

and differentiate enough times to isolate the coefficient you want. For example, comparing constant terms after three derivatives gives

a3=r(rβˆ’1)(rβˆ’2)3!.a_3=\frac{r(r-1)(r-2)}{3!}.

Important consequences:

  • Setting r=βˆ’1r=-1 gives 11+x=βˆ‘nβ‰₯0(βˆ’x)n.\frac{1}{1+x}=\sum_{n \ge 0}(-x)^n.
  • More generally, 1(1βˆ’x)m+1=βˆ‘kβ‰₯0(k+mm)xk.\frac{1}{(1-x)^{m+1}}=\sum_{k \ge 0}\binom{k+m}{m}x^k.
  • Also, xm(1βˆ’x)m+1=βˆ‘kβ‰₯0(km)xk.\frac{x^m}{(1-x)^{m+1}}=\sum_{k \ge 0}\binom{k}{m}x^k.
  • A very important identity is 11βˆ’4x=βˆ‘kβ‰₯0(2kk)xk.\frac{1}{\sqrt{1-4x}}=\sum_{k \ge 0}\binom{2k}{k}x^k.
  • Integrating that identity gives the Catalan generating function: 1βˆ’1βˆ’4x2x=βˆ‘kβ‰₯0Ckxk,\frac{1-\sqrt{1-4x}}{2x}=\sum_{k \ge 0} C_kx^k, where Ck=1k+1(2kk).C_k=\frac{1}{k+1}\binom{2k}{k}.
  • The exponential series is ex=βˆ‘kβ‰₯0xkk!.e^x=\sum_{k \ge 0}\frac{x^k}{k!}.

For quick reference:

Generating functionSequence
(1+x)n(1+x)^nβˆ‘kβ‰₯0(nk)xk\displaystyle \sum_{k \ge 0}\binom{n}{k}x^k
1(1βˆ’x)m+1\dfrac{1}{(1-x)^{m+1}}βˆ‘kβ‰₯0(k+mm)xk\displaystyle \sum_{k \ge 0}\binom{k+m}{m}x^k
xm(1βˆ’x)m+1\dfrac{x^m}{(1-x)^{m+1}}βˆ‘kβ‰₯0(km)xk\displaystyle \sum_{k \ge 0}\binom{k}{m}x^k
11βˆ’4x\dfrac{1}{\sqrt{1-4x}}βˆ‘kβ‰₯0(2kk)xk\displaystyle \sum_{k \ge 0}\binom{2k}{k}x^k
1βˆ’1βˆ’4x2x\dfrac{1-\sqrt{1-4x}}{2x}βˆ‘kβ‰₯0Ckxk\displaystyle \sum_{k \ge 0} C_kx^k
exe^xβˆ‘kβ‰₯0xkk!\displaystyle \sum_{k \ge 0}\frac{x^k}{k!}

Example 5.8 (HMMT 2007 Combinatorics #9). Let SS be the set of triples (i,j,k)(i,j,k) of positive integers satisfying i+j+k=17i+j+k=17. Compute

βˆ‘(i,j,k)∈Sijk.\sum_{(i,j,k)\in S} ijk.

Consider

F(x)=(βˆ‘iβ‰₯0ixi)(βˆ‘jβ‰₯0jxj)(βˆ‘kβ‰₯0kxk).F(x)=\left(\sum_{i \ge 0} ix^i\right)\left(\sum_{j \ge 0} jx^j\right)\left(\sum_{k \ge 0} kx^k\right).

We want the coefficient of x17x^{17}. Since

βˆ‘nβ‰₯0nxn=x(1βˆ’x)2,\sum_{n \ge 0} nx^n = \frac{x}{(1-x)^2},

we get

F(x)=(x(1βˆ’x)2)3=x3(1βˆ’x)6.F(x)=\left(\frac{x}{(1-x)^2}\right)^3 = \frac{x^3}{(1-x)^6}.

So we need the coefficient of x14x^{14} in 1(1βˆ’x)6\frac{1}{(1-x)^6}, which is

(195).\binom{19}{5}.

The β€œSnake Oil” method is a systematic way to evaluate a sum depending on a free variable. Suppose

an=βˆ‘kF(k,n).a_n=\sum_k F(k,n).

Example 5.9. For nβ‰₯0n \ge 0, compute

βˆ‘kβ‰₯0(n+k2k)2nβˆ’k.\sum_{k \ge 0}\binom{n+k}{2k}2^{n-k}.

Let

A(x)=βˆ‘nβ‰₯0[βˆ‘kβ‰₯0(n+k2k)2nβˆ’k]xn.A(x)=\sum_{n \ge 0}\left[\sum_{k \ge 0}\binom{n+k}{2k}2^{n-k}\right]x^n.

Then

A(x)=βˆ‘kβ‰₯0βˆ‘nβ‰₯0(n+k2k)2nβˆ’kxn.A(x)=\sum_{k \ge 0}\sum_{n \ge 0}\binom{n+k}{2k}2^{n-k}x^n.

After shifting indices and using

βˆ‘aβ‰₯0(a+2k2k)(2x)a=1(1βˆ’2x)2k+1,\sum_{a \ge 0}\binom{a+2k}{2k}(2x)^a = \frac{1}{(1-2x)^{2k+1}},

we obtain

A(x)=βˆ‘kβ‰₯0xk1(1βˆ’2x)2k+1=11βˆ’2xβˆ‘kβ‰₯0(x(1βˆ’2x)2)k.A(x)=\sum_{k \ge 0} x^k\frac{1}{(1-2x)^{2k+1}} = \frac{1}{1-2x}\sum_{k \ge 0}\left(\frac{x}{(1-2x)^2}\right)^k.

This is geometric:

A(x)=11βˆ’2xβ‹…11βˆ’x(1βˆ’2x)2=1βˆ’2x1βˆ’5x+4x2.A(x)=\frac{1}{1-2x}\cdot \frac{1}{1-\frac{x}{(1-2x)^2}} = \frac{1-2x}{1-5x+4x^2}.

Partial fractions give

A(x)=13β‹…11βˆ’x+23β‹…11βˆ’4x.A(x)=\frac{1}{3}\cdot \frac{1}{1-x} + \frac{2}{3}\cdot \frac{1}{1-4x}.

Therefore

A(x)=βˆ‘nβ‰₯0(13+23β‹…4n)xn,A(x)=\sum_{n \ge 0}\left(\frac13+\frac23\cdot 4^n\right)x^n,

so

βˆ‘kβ‰₯0(n+k2k)2nβˆ’k=13+23β‹…4n.\sum_{k \ge 0}\binom{n+k}{2k}2^{n-k}=\frac13+\frac23\cdot 4^n.

Example 5.10. For nβ‰₯0n \ge 0, compute

βˆ‘kβ‰₯0(knβˆ’k).\sum_{k \ge 0}\binom{k}{n-k}.

Again,

βˆ‘nβ‰₯0[βˆ‘kβ‰₯0(knβˆ’k)]xn=βˆ‘kβ‰₯0βˆ‘nβ‰₯0(knβˆ’k)xn.\sum_{n \ge 0}\left[\sum_{k \ge 0}\binom{k}{n-k}\right]x^n = \sum_{k \ge 0}\sum_{n \ge 0}\binom{k}{n-k}x^n.

Rewrite using xkx^k:

=βˆ‘kβ‰₯0xkβˆ‘k≀n≀2k(knβˆ’k)xnβˆ’k=βˆ‘kβ‰₯0xk(1+x)k.= \sum_{k \ge 0} x^k \sum_{k \le n \le 2k}\binom{k}{n-k}x^{n-k} = \sum_{k \ge 0} x^k(1+x)^k.

Thus

=11βˆ’x(1+x)=11βˆ’xβˆ’x2.= \frac{1}{1-x(1+x)} = \frac{1}{1-x-x^2}.

This is the Fibonacci generating function, so the sum equals Fn+1F_{n+1}.

If the free variable appears in many places at once, Snake Oil often works better after you first generalize the identity.

Problem 5.11. Prove that for every integer nβ‰₯0n \ge 0,

βˆ‘a+b=n(2aa)(2bb)=4n.\sum_{a+b=n}\binom{2a}{a}\binom{2b}{b}=4^n.

Problem 5.12. For integers m,nβ‰₯0m,n \ge 0, prove that

βˆ‘a+b=m(βˆ’1)a(na)(n+bβˆ’1b)={1m=0,0m>0.\sum_{a+b=m}(-1)^a \binom{n}{a}\binom{n+b-1}{b} = \begin{cases} 1 & m=0, \\ 0 & m>0. \end{cases}

Problem 5.13. Let m≀nm \le n be positive integers. Compute

βˆ‘k=mn(nk)(km).\sum_{k=m}^{n}\binom{n}{k}\binom{k}{m}.

Problem 5.14. For m,nβ‰₯1m,n \ge 1, compute

βˆ‘kβ‰₯0(n+km+2k)(2kk)(βˆ’1)kk+1.\sum_{k \ge 0}\binom{n+k}{m+2k}\binom{2k}{k}\frac{(-1)^k}{k+1}.