Definition 10.1.1 · Recursion
Recursion is the process of finding smaller values and using them to calculate larger values.
Count something big by counting it small first: base cases, a recursion that builds each value from earlier ones, and two-state tables for harder rules.
In this section you will be able to count the ways to climb a staircase by building each answer from the smaller ones.
Jeff climbs a 6-stair staircase, 1 or 2 stairs at a time. Listing every climb is slow. Can you count them without listing?
Don’t attack stair 6 head-on. Ask a smaller question: how many ways reach stair 1? Stair 2? Then ask how the last jump onto a stair can start. That turns one big count into a chain of small ones.
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 is the same as Fibonacci's. This is not the case for every recursion problem.
You can often get the answer to a recursion problem by engineering induction: work out the first few values, spot the pattern, and trust it. (See the meta-solving section in Chapter 45.)
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?
Casework would take a long time. Start small instead. How many ways are there for Jeff to reach the 0th stair (the ground)?
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 set up base cases and a recursion when the rules change along the way.
Mike takes 2 or 3 stairs at a time, but from stair 9 he may take a single step to the top. One special rule at the end. Does it break the recursion?
Every recursion problem runs the same four steps: base cases by hand, a recursion from the ways to arrive, then the values one by one. Special rules just change which cells you add, and where.
Solving a problem with recursion is a multi-step process:
You can also use as a base case: to get to the 0th stair you take no steps, and there is exactly 1 way to do that. It can be faster, though a bit confusing at first, so the examples don't use it. Feel free to use it in the problems.
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 ), go back and count more base cases by hand.
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 base cases. How many ways can Mike reach the 1st stair?
In this section you will be able to find recursions by inserting the newest object, or by asking what happens to the last one.
Eight cubes, sizes 1 to 8, stacked so each cube is at most 2 bigger than the one below. There are thousands of orders. How many work?
The staircase trick still works when the objects aren’t stairs. Build a size- answer from a size- answer. For towers, insert the biggest cube into a smaller tower. For sets, ask whether itself is in or out.
To check your recursive equation, count a few small cases by hand and see whether the recursion gives the same numbers.
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 rules:
Let be the number of different towers that can be constructed. What is the remainder when is divided by 1000?
Let be the number of legal towers using cubes to . What is ?
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?
Let be the number of spacy subsets of . Count , and by hand. What is ?
Call a permutation of the integers quasi-increasing if for each . For example, and are quasi-increasing permutations of the integers , but is not. Find the number of quasi-increasing permutations of the integers .
Find the number of permutations of such that for each with , at least one of the first terms of the permutation is greater than .
In this section you will be able to track two counts at once, split by how the string ends.
A mail carrier on a street of 19 houses never delivers to two neighbors, and never skips three houses in a row. How many delivery patterns are possible?
One count isn’t enough here: what can come next depends on how the pattern ends. So keep two counts, for patterns ending with mail and for patterns ending without. Each one feeds the other.
Often it is simpler to define two recursive functions, say and , where the actual answer is some combination of the two (for example ).
A mail carrier delivers mail to the nineteen houses on the east side of Elm Street. The carrier notices that no two adjacent houses ever get mail on the same day, but that there are never more than two houses in a row that get no mail on the same day. How many different patterns of mail delivery are possible?
Let count the patterns for houses where the last house gets mail, and those where it doesn't. If the last house gets mail, what must the house before it be?
There are possible 10-letter strings in which each letter is either an A or a B. Find the number of such strings that do not have more than 3 adjacent letters that are identical.
For each positive integer , let be the number of sequences of length consisting solely of the letters and , with no more than three s in a row and no more than three s in a row. What is the remainder when is divided by 12?
In this section you will be able to use recursion on games, rings and shrinking triangles.
Three challenge problems. A brick game, a painted ring and a triangle that keeps shrinking: each one hides a recursion.
Want every problem from this chapter on one page? Open the problem set.
This problem also appears in the book as 3.3.14.
Arjun and Beth play a game in which they take turns removing one brick or two adjacent bricks from one "wall" among a set of several walls of bricks, with gaps possibly creating new walls. The walls are one brick tall. For example, a set of walls of sizes and can be changed into any of the following by one move: , , , , , or .
Arjun plays first, and the player who removes the last brick wins. For which starting configuration is there a strategy that guarantees a win for Beth?
The figure below shows a ring made of six small sections which you are to paint on a wall. You have four paint colors available and you will paint each of the six sections a solid color. Find the number of ways you can choose to paint the sections if no two adjacent sections can be painted with the same color.
Let be a triangle whose angle measures are exactly , , and . For each positive integer , define to be the foot of the altitude from to line . Likewise, define to be the foot of the altitude from to line , and to be the foot of the altitude from to line . What is the least positive integer for which is obtuse?