Skip to content
Combinatorics · Chapter 10

Recursion

Solve a big counting problem by building it up from small ones: each answer is a sum of earlier answers.

You'll need
open on YouTube
10.10%

Recursion Fundamentals

In this section you will be able to count the ways up a staircase by building a table from the bottom step.

Jeff is climbing 6 stairs, 1 or 2 at a time. Listing every climb by hand is slow and easy to mess up. Can the small staircases do the work for you?

Here’s the big idea. Think about Jeff’s last jump. He lands on the top stair from one stair below, or from two stairs below.

So the ways to reach the top are the ways to reach those two stairs, added together. Start at the bottom and fill a table one stair at a time.

Look:each box in the table only needs the boxes just before it.

Exploreplay first, formulas later

6 stairs, jumps of 1 or 2: f(6) = 13 ways

stairs6
jumps:
0123456top
Find f(6): the ways to reach stair 6

Jumps of 1 or 2.

Jumps of 1 or 2. Table filled up to stair 0.

Name it

Definition 10.1.1 · Recursion

Recursion is the process of finding smaller values and using them to calculate larger values.

Remark 10.1.2

Look: the numbers 1,2,3,5,8,131, 2, 3, 5, 8, 13 follow the Fibonacci sequence. That's because the recursion for our sequence and the Fibonacci recursion are the same. However, this is not always the case for all recursion problems.

Watch itpredict, then reveal

Example 10.1

Jeff is climbing a 6 stair staircase. If he can climb 1 or 2 stairs at a time, in how many distinct ways can he get to the top?

6 stairs, jumps of 1 or 2: f(6) = 13 ways

0123456top
Find f(6): the ways to reach stair 6

Jumps of 1 or 2.

Jumps of 1 or 2. Table filled up to stair 0.

  1. 1

    You could do casework, but that takes a long time. Solve it for small values first. How many ways are there for Jeff to reach the 0th stair (the ground)?

    Predict first:

Try it

Problem 10.4.1

Jeff is climbing a 7 step staircase. If he can climb 1 or 2 steps at a time, in how many ways can he get to the top?

Contest cornerreal contest problems

Problem 10.4.2

AMC 8

Everyday at school, Jo climbs a flight of 6 stairs. Jo can take the stairs 1, 2, or 3 at a time. For example, Jo could climb 3, then 1, then 2. In how many ways can Jo climb the stairs?

Checkpoint for section 10.1

Get 2 of 3 right to master this section. One try each, no hints.
1

Jeff is climbing a 7 step staircase. If he can climb 1 or 2 steps at a time, in how many ways can he get to the top?

2

Use f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2) with f(1)=1f(1) = 1 and f(2)=2f(2) = 2. What is f(8)f(8)?

3

Sejal climbs stairs with jumps of 1 or 3 only. Which recursion counts her ways to reach stair nn?

10.20%

Recursion with Constraints

In this section you will be able to handle special rules, like a different last jump or a stair you can't land on.

This time Jeff gets a bonus. On his very last jump, he may leap 3 stairs! Does the whole table change, or just the top box?

A rule that only touches the last jump only changes the last box. Fill the table as usual, then look again at the top stair: which stairs can the final jump start from?

Recursion works for more than stairs. Whenever a big case can be built from smaller cases, you can make a table.

Exploreplay first, formulas later

7 stairs, jumps of 1 or 2 (last jump 1, 2 or 3): f(7) = 26 ways

stairs7
jumps:
01234567top
Find f(7): the ways to reach stair 7

Jumps of 1 or 2; the last jump onto stair 7 may be 1, 2 or 3.

Jumps of 1 or 2. The last jump may be 1, 2 or 3. Table filled up to stair 0.

Name it

Concept 10.2.1 · Solving with Recursion

To solve a problem with recursion, follow these steps:

  1. Solve the problem for a few smaller base cases by hand.
  2. In general, you need as many base cases as the number of terms on the right-hand side of your recursion.
  3. For a given f(n)f(n), find a recursive equation in terms of earlier values of the function. Think about the different ways to get to that point.
  4. Calculate the values of f(n)f(n) one by one until you reach the number you want.

Remark 10.2.2

You can also use f(0)=1f(0) = 1 as a base case. To get to the 0th stair you take no steps, and there is only 1 way to do that. This can be a little faster, but it can also be a little confusing, so the examples don't use it. Feel free to use it in the problems!

Remark 10.2.3

In general, it never hurts to find too many base cases. If your recursion runs into a value you're not sure how to calculate (like f(2)f(2) or f(3)f(3)), you can always go back and work out more base cases by hand.

Watch itpredict, then reveal

Example 10.2

Jeff is climbing a 7 stair staircase. Jeff can climb 1 or 2 stairs at a time on every step, but on the last step, he is allowed to climb 3 stairs. In how many distinct ways can he get to the top?

7 stairs, jumps of 1 or 2 (last jump 1, 2 or 3): f(7) = 26 ways

01234567top
Find f(7): the ways to reach stair 7

Jumps of 1 or 2; the last jump onto stair 7 may be 1, 2 or 3.

Jumps of 1 or 2. The last jump may be 1, 2 or 3. Table filled up to stair 0.

  1. 1

    Before the last jump, Jeff uses jumps of 1 or 2, just like Example 10.1. So f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2) there. What is f(6)f(6)?

    Predict first:
open on YouTube

Example 10.3

Mike is climbing a staircase with 10 stairs. He is in a rush, so he will climb 2 or 3 steps at a time for most of the journey. If he reaches the 9th stair, he is allowed to climb 1 stair to reach the 10th stair. How many ways are there for Mike to reach the top of the staircase?

10 stairs, jumps of 2 or 3 (last jump 1, 2 or 3): f(10) = 12 ways

012345678910top
Find f(10): the ways to reach stair 10

Jumps of 2 or 3; the last jump onto stair 10 may be 1, 2 or 3.

Jumps of 2 or 3. The last jump may be 1, 2 or 3. Table filled up to stair 0.

  1. 1

    Start with the base cases. How many ways can Mike reach the 1st stair?

    Predict first:

Try it

Problem 10.4.6(additional)

Jeff is climbing a 10 step staircase. However, there are spiders on the 3rd and 9th steps, and he refuses to step on those stairs. If he can climb 1 or 2 steps at a time, in how many different ways can he get to the top?

Contest cornerreal contest problems

Problem 10.4.3

Omega Learn Math Contest

Brandon is trying to reach the top of a 7 step staircase. For each jump except the last jump, he can either jump 1 or 3 steps forward. On the last jump, he can jump 1, 2, or 3 steps forward. How many different ways can he reach the top of the staircase?

Problem 10.4.4

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, \ldots, 12\}, including the empty set, are spacy?

Checkpoint for section 10.2

Get 2 of 3 right to master this section. One try each, no hints.
1

Jeff is climbing a 10 step staircase. However, there are spiders on the 3rd and 9th steps, and he refuses to step on those stairs. If he can climb 1 or 2 steps at a time, in how many different ways can he get to the top?

2

Sohil climbs 5 stairs using only jumps of 2 or 3. In how many ways can he reach the top?

3

Your recursion is f(n)=f(n−1)+f(n−2)+f(n−3)f(n) = f(n-1) + f(n-2) + f(n-3). How many base cases do you need to work out by hand before you can use it?

10.30%

Probability Recursions

In this section you will be able to track the chance of being in each place, one step at a time, with a table.

A cricket hops between 4 leaves. Each hop goes to one of the other 3 leaves, chosen at random. Where is it likely to be after 4 hops?

You can’t list every hop for long. Instead, track the chance of being on each leaf after every hop. Each new row of the table comes from the row before it.

Look:the cricket is always on some leaf, so every row adds up to 1.

Exploreplay first, formulas later

4 leaves, after 0 hops: chance of being on the start leaf = 1

leaves4
A★1B0C0D0
After 0 hops: back on A = 11
Chance of each leaf after every hop
HopA★each otherall leaves (total = 1)
010

Each bar is split by leaf colour. Every bar is full: the chances always add up to 1.

After 0 hops: leaf A (start) has chance 1, each other leaf 0. Total 1.

Name it

Concept 10.3.1 · Probability Recursions

For probability recursions, it can be a good idea to simply make a table and find the probability of being at each location after every time step. Remember: the total probability after each hop must always be 11.

Watch itpredict, then reveal

Example 10.4

AMC 8

A cricket randomly hops between 4 leaves, on each turn hopping to one of the other 3 leaves with equal probability. After 4 hops what is the probability that the cricket has returned to the leaf where it started?

4 leaves, after 0 hops: chance of being on the start leaf = 1

A★1B0C0D0
After 0 hops: back on A = 11
Chance of each leaf after every hop
HopA★each otherall leaves (total = 1)
010

Each bar is split by leaf colour. Every bar is full: the chances always add up to 1.

After 0 hops: leaf A (start) has chance 1, each other leaf 0. Total 1.

  1. 1

    Call the starting leaf A. Let pkp_k be the chance the cricket is on A after kk hops. So p0=1p_0 = 1. What is p1p_1?

    Predict first:
open on YouTube

Checkpoint for section 10.3

Get 2 of 3 right to master this section. One try each, no hints.
1

A frog hops between 3 lily pads, each time to one of the other 2 pads with equal chance. What is the probability it is back on its starting pad after 3 hops?

2

The cricket from Example 10.4 is back on its starting leaf after 4 hops with probability 727\frac{7}{27}. What is the probability it is on one particular other leaf after 4 hops?

3

In a probability table for the cricket, what do the chances of all 4 leaves add up to after 5 hops?

10.4

Practice Problems

In this section you will be able to use recursion on a tough contest problem.

Here’s an AIME problem about building towers. The trick: grow the tower one cube at a time, and count where each new cube can go.

Want every problem from this chapter on one page? Open the problem set.

Contest cornerreal contest problems

Problem 10.4.5

AIME

A collection of 8 cubes consists of one cube with edge-length kk for each integer kk, 1≤k≤81 \le k \le 8. A tower is to be built using all 8 cubes according to the following rules: Any cube may be the bottom cube in the tower. 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?