Definition 10.1.1 · Recursion
Recursion is the process of finding smaller values and using them to calculate larger values.
Solve a big counting problem by building it up from small ones: each answer is a sum of earlier answers.
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.
Recursion is the process of finding smaller values and using them to calculate larger values.
Look: the numbers 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.
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?
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)?
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?
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?
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.
To solve a problem with recursion, follow these steps:
You can also use 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!
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 or ), you can always go back and work out more base cases by hand.
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?
Before the last jump, Jeff uses jumps of 1 or 2, just like Example 10.1. So there. What is ?
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?
Start with the base cases. How many ways can Mike reach the 1st stair?
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?
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?
Call a set of integers spacy if it contains no more than one out of any three consecutive integers. How many subsets of , including the empty set, are spacy?
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.
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 .
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?
Call the starting leaf A. Let be the chance the cricket is on A after hops. So . What is ?
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.
A collection of 8 cubes consists of one cube with edge-length for each integer , . 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 must have edge-length at most . How many towers can be constructed?