Recursion
Recursion
Recursion
Ananth Shyamal, Divya Shyamal, Kevin Yang, and Reece Yang
July 11, 2020
1 Recursive Sequences
A recursive sequence, also known as a recurrence relation, is a sequence in which the
n-th term is defined in terms of the first n − 1 terms of the sequence and n (usually,
the definition does not involve all of the first n − 1 terms). Put simpler, a recursive
sequence is defined in terms of itself. For example, the Fibonacci sequence is a recursive
sequence and can be defined by Fn = Fn−1 + Fn−2 with F0 = 0 and F1 = 1, where n is
any integer greater than 1. We then see that
F2 =1+0=1
F3 =1+1=2
F4 =2+1=3
F5 = 3 + 2 = 5,
and so on. When defining a recursive function, you may notice that
Checkpoint 1.1. Find the 5th term (n = 5) in the following recursive sequence:
a1 = 3, an = 2an−1 + 3.
In a later section, we will discuss a general method for finding a closed-form expression
(i.e. one that doesn’t involve other terms of the sequence) for the n-th term of some
recursive sequences. However, For some types of recurrence relations, we may be able
to find a closed-form for the n-th term more easily (by noticing a pattern). Consider
the recurrence defined by a0 = 1 and an = n · an−1 for n ∈ Z+ . We see that
an = n · an−1
= n · (n − 1) · an−2
= n · (n − 1) · (n − 2) · an−3
= n · (n − 1) · · · 1 · a0
= n!.
1
From the factorial recurrence relation and other examples we will see in combinatorics,
recursion helps us solve problems faster, computationally and practically.
Checkpoint 1.2. Find a closed-form expression for the n-th term of the recursive
sequence defined by a1 = 2 and an = n(n − 1) · an−1
In summary, the main objective of recursion is to represent the solution to the current
case in terms of the solutions of smaller cases (similar to the method of induction!). In
later sections, we’ll see how we can use this principle idea of recursion to solve harder
problems in other topics.
Solution. Let’s take a look at what this sequence does. When we evaluate the first few
terms of this sequence, we get 4, −1, −6, −11, −16, etc. This is clearly an arithmetic
sequence, with first term 4 and common difference −5. We can now use the arithmetic
sequence equation to get a closed form: an = 4 + −5(n − 1) or an = 9 − 5n. Note that
we had an n − 1 term instead of n in the common difference because the first term of
our sequence is at n = 1, not n = 0. 4
This is one of the simpler equations to find a closed form because it doesn’t involve many
complicated equations. Try this similar problem with the same strategy, by starting
with listing the first few terms of the sequence.
There are many sequences that involve complicated expressions when finding a closed
form. Those will be discussed in the next chapter.
2
Solution. The first thing to note is that the question is asking for the 2020th term.
This means that trying to evaluate it directly would simply just take too long, so let’s
try to look at the problem in a different way. It is likely that this problem has a cycle,
because if not then the problem’s answer would likely be too complicated to compute
without a calculator. Let’s try evaluating the first few terms in this sequence.
To simplify things, let a = t1 and b = t2 . Then, t3 = 5b+1 25a
, t4 = 5a+5b+1
125ab
, t5 =
5a+1
25b
, t6 = a, and t7 = b. We can then see quickly that t 6 = t 1 and t2 = t7 . Since this
recursive sequence only uses the last two terms, we know that this sequence has a cycle
of 5. Now, let’s take a look at what the problem asks for. It asks for the 2020th term,
but since the sequence has a cycle of 5, we really only need to find the 5th term (2020
101
and 5 are both multiples of 5). It now suffices to evaluate t5 : t5 = 5a+125b
= . 4
525
Checkpoint 2.1. Let an be the remainder when Fn is divided by 3 (equivalently,
an = Fn mod 3), where Fn is the Fibonacci sequence, defined by Fn = Fn−1 + Fn−2
and F0 = 0, F1 = 1. Determine the value of a2020 .
for all n ≥ k, where the ci ’s are real constants and m and k are non-negative integers
with k > m and cm 6= 0.
Example 3.1. Which of the following recurrence relations are linear homogeneous
recurrence relations? For the ones that are not, which of the three criteria (linearity,
homogeneity, and constant coefficients) do they not meet?
1. an = nan−1
2. an = an−1 an−2
4. an = 2an−1 + an−2 + 3
5. an = a2n−1 + a2n−2
3
6. an = 2an+1 − 3an−1
7. an = 3a2n−1 + 2an−2
8. an = 2an−1 − 3an−2
1. This relation is linear and homogeneous, but does not have constant coefficients
since the coefficient of an−1 , namely n, is not a constant.
2. This relation is not linear (it’s quadratic since the term an−1 an−2 has degree 2) and
doesn’t have constant coefficients. It is homogeneous, though, as it’s right-hand
side contains one term.
3. This relation is linear, homogeneous (all the terms have degree 1), and has con-
stant coefficients (namely 2 and 3).
4. This relation is linear and has constant coefficients, but is not homogeneous since
the last term is constant and has degree 0 (whereas the other terms have degree
1).
5. This relation is not linear but is homogeneous since each of its terms have degree
2. However, it has constant coefficients.
7. This relation is not linear nor homogeneous since its two terms have degrees 2
and 1, respectively. It has constant coefficients, though.
8. This relation is linear, homogeneous (all terms have degree 1), and has constant
coefficients.
4
Now, we’ll introduce another definition. The characteristic polynomial of the linear
homogeneous recurrence
is the polynomial
Now, let’s state a theorem that gives us a solution to any linear homogeneous recurrence
whose characteristic polynomial has distinct roots.
4
Theorem 3.1. Suppose we have the recurrence relation
Then if p(x) has distinct roots r1 , r2 , . . . , rm , any sequence {an } satisfies this recurrence
if and only if
ak = b1 r1k + b2 r2k + · · · + bm rm k
,
for all k = 0, 1, . . . , n, where b1 , b2 , . . . , bm are constants.
You may wonder how the above theorem gives us a closed form, considering the bi ’s are
unknown constants. We can get around this by plugging in m values in the sequence
{an } to get an m × m system of linear equations, which can be solved without much
trouble. Let’s understand this theorem with an example.
Example 3.2. Find a closed-form expression for the n-th term of the Fibonacci se-
quence.
Solution. The Fibonacci sequence is given by the recurrence Fn = Fn−1 + Fn−1 . As
this is clearly a linear homogeneous relation, we see that its characteristic polynomial
√
is p(x) = x2 − x − 1. Using the quadratic formula, the roots of the polynomial are 1±2 5 .
Applying the previous theorem, we get that the n-th term of the Fibonacci sequence is
given by
√ !n √ !n
1+ 5 1− 5
F n = b1 + b2 ,
2 2
for some constants b1 and b2 . But how do we find these constants? We know that
F0 = 0 and F1 = 1. Plugging this in, we have that
(
b1
+ b2 √ =0
√
1+ 5
b1 2
+ b2 1−2 5 = 1.
5
Checkpoint 3.1. Defined below is the Pell sequence, {Pn }, as follows:
0
if n = 0
Pn = 1 if n = 1
2Pn−1 + Pn−2 if n > 1.
6
Adding the two equations and dividing by three gives us c2 = 13 . Plugging this into the
first equation gives us c1 = − 13 . Hence,
1 1 2n − (−1)n
bn = − (−1)n + 2n = .
3 3 3
Now, the problem is asking us to find (or at least consider the value of) a1 a2 · · · ak . We
bn
can rearrange the equation bn = 19 log2 an to get an = 2 19 . Thus,
b1 b2 bk
a1 a2 · · · ak = 2 19 2 19 · · · 2 19
b1 +b2 +···+bk
=2 19 .
From our closed form for the sequence bn , we can compute b1 + b2 + · · · + bn for two
cases: if n is even or if n is odd. If n is even, we get that
2 − (−1) + 22 − 1 + 23 − (−1) + · · · + 2n − 1
b 1 + b2 + · · · + bn =
3
2 n
2 + 2 + ··· + 2
=
3
2n+1 − 2
= ,
3
using the geometric series formula. For n odd,
4 Applications in Combinatorics
Recursion can be a powerful technique to represent combinatorics problems in a sim-
pler fashion and make solving them more straightforward. To apply recursion to such
problems, we will first have to find the base case that is easy to compute. Then, we can
find the recursive step, which is the relationship between the solution to the k-th case
and the solutions to previous cases. This bottom-up nature of recursion is what makes
is quite efficient.
7
Example 4.1. Find the number of 10 digit positive binary numbers that do not have
a pair of consecutive 0s.
Solution. We can tackle this problem with casework, but that can get messy very
quickly and is prone to error. Let’s try recursion here, since it seems like we can start
with smaller cases.
The base case here is simply just one digit, and with that we have one number: 1.
We can then see that for two digits we have 2 numbers: 10 and 11. Now, let’s take
a look at the recursive step. We would have to divide this into two cases: whether
the number ends with a 0 or a 1. If the number ends in 1, then we have 2 possi-
ble ways to add an extra digit (0 or 1) to the end, but if the number ends in 0, then
we only have one way to add the digit since we can’t append a 0 to that binary number.
With all of that in mind, we can now set up our recurrence. Let an be the amount of
binary numbers with n digits that end in 0, and let bn be the amount of numbers with
n digits that end in 1. We find that a1 and b1 are both 1. Now, let’s write the recursive
step that we discussed above into equations:
an = bn−1
bn = an−1 + bn−1
We can use these equations to compute the final answer, as shown in the table.
n an bn
1 0 1
2 1 1
3 1 2
4 2 3
5 3 5
6 5 8
7 8 13
8 13 21
9 21 34
10 34 55
Since a10 = 34 and b10 = 55 from the above table, we have a total of 34 + 55 = 89
binary numbers.
Remark: Do you see something special about the sequences an and bn from the ta-
ble? Why do you think that is the case? 4
In this problem, we showed that recursion can save us some time when counting directly
seems to be difficult. Recursion is best used when there is a clear base case and there
are not a lot of steps to the case that the question asks (although this can be ignored if
the recursion is simple, cyclic, or linear homogeneous). The trickiest part in recursion
8
is finding the recursive relationship between cases. If you can figure that out, solving
the rest of the problem will be relatively easy.
Example 4.2. Consider sequences that consist entirely of A’s and B’s and that have the
property that every run of consecutive A’s has even length, and every run of consecutive
B’s has odd length. Examples of such sequences are AA, B, and AABAA, while BBAB
is not such a sequence. How many such sequences have length 14? Source: AIME
Solution. Like the previous problem, we can solve this by direct counting. However,
doing so is prone to mistakes because of the complexity of the problem. So, let’s try
recursion again.
The base cases are pretty simple - there is only one such sequence of length one (B)
and one sequence of length two (AA). Let’s now focus on the recursive case. Like the
other problem, let’s denote an to be the number of sequences with length n that end
with A, and let bn be the number of length n sequences that end in B.
Let’s focus on an first. We can generate a sequence with length n that ends with
A by appending a string of As, with even length, to a sequence that ends with B. For
example, we can generate a sequence of length 9 by appending AA to a sequence with
length 7 that ends with B. Turning this into a equation yields us
We can use a similar logic for bn , but instead of adding a string of Bs to a sequence
that has a length with the same even-odd parity, we need to add it to a sequence with
a length of the opposite parity. We can now write an equation for bn :
Now that we have both equations, we can now evaluate a14 and b14 using the following
table.
9
n an bn
0 1 1
1 0 1
2 1 0
3 1 2
4 1 1
5 3 3
6 2 4
7 6 5
8 6 10
9 11 11
10 16 21
11 22 27
12 37 43
13 49 64
14 80 92
We can now add up a14 and b14 , or 80 and 92, to get our final answer of 172 .
4
Checkpoint 4.1. Ben is jumping up a flight of stairs. He can take one step or two
steps in a jump. Find the number of ways he can climb up n steps, if n = 10.
10
We can then compute
f (2018)+f (2015)−(f (2015)+f (2012))+f (2012)+f (2009)−· · ·−(f (5)+f (2)) = f (2018)−f (2)
which equals
6 Exercises
1. ? Find the 5th term of the following recursive sequence: a1 = 1, a2 = 2, an =
an−1 an−2 .
2. ? Find the closed form for the recursive sequence: a1 = 10, an = 3an−1 − 2.
4. ? How many sequences of 0s and 1s of length 19 are there that begin with a 0,
end with a 0, contain no two consecutive 0s, and contain no three consecutive 1s?
Source: AMC
5. ? Consider the triangular array of numbers with 0, 1, 2, 3, . . . along the sides and
interior numbers obtained by adding the two adjacent numbers in the previous
row. Rows 1 through 6 are shown.
0
1 1
2 2 2
3 4 4 3
4 7 8 7 4
5 11 15 15 11 5
Find the sum of the numbers in the 100th row. Your answer should use exponen-
tial notation, in simplest form. Source: AoPS
7. ? Given a0 = 1, a1 = 3, and the general relation a2n − an−1 an+1 = (−1)n for n ≥ 1,
find a3 . Source: AHSME
11
8. ? Stacy has d dollars. She enters a mall with 10 shops and a lottery stall. First
she goes to the lottery and her money is doubled, then she goes into the first shop
and spends 1024 dollars. After that she alternates playing the lottery and getting
her money doubled (Stacy always wins) then going into a new shop and spending
$1024. When she comes out of the last shop she has no money left. What is the
minimum possible value of d? Source: HMMT
9. ? For each positive integer n, the mean of the first n terms of a sequence is n.
What is the 2008th term of the sequence? Source: AMC
10. ? The sequence of numbers t1 , t2 , t3 , . . . is defined by t1 = 2 and
tn − 1
tn+1 =
tn + 1
for every positive integer n. Determine the value of t999 . Source: COMC
11. ? A parking lot in a workplace has 8 slots. There are smaller cars that only take
up 1 slot, and larger vans that take up two slots. There are three possible types
of cars and two types of larger vans. How many possible ways can the parking
lot be arranged if it is full?
12. ?? In terms of n, how many regions can we make in a plane of n lines? (For
example, we can make 4 regions with 2 lines in a plane)
13. ?? A sequence of numbers is defined recursively by a1 = 1, a2 = 37 , and
an−2 · an−1
an =
2an−2 − an−1
for all n ≥ 3 Find a2019 . Source: AMC
14. ?? How many sequences of 0s and 1s of length 19 are there that begin with a 0,
end with a 0, contain no two consecutive 0s, and contain no three consecutive 1s?
Source: AMC
15. ?? In the sequence 2001, 2002, 2003, . . . , each term after the third is found by
subtracting the previous term from the sum of the two terms that precede that
term. For example, the fourth term is 2001 + 2002 − 2003 = 2000. What is the
2004th term in this sequence? Source: AMC
16. ?? Let {ak } be a sequence of integers such that a1 = 1 and am+n = am + an + mn
for all positive integers m and n. Find a12 . Source: AMC
an+1 + 1
17. ?? If a1 = a2 = 1 and an+2 = for n ≥ 1, compute at where t = 19985 .
an
Source: ARML
18. ?? Define a function on the positive integers recursively by f (1) = 2, f (n) =
f (n − 1) + 1 if n is even, and f (n) = f (n − 2) + 2 if n is odd and greater than 1.
What is f (2017)? Source: AMC
12
19. ?? 5 people are playing in a golf tournament. If ties are allowed, how many
possible final standings are there? One possible configuration is that the 2nd and
4th person tied for 1st, the 3rd person gets 3rd, and the 1st and 5th person tied
for 4th.
20. ?? 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? Source: AMC 8
21. ?? There are 210 = 1024 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. Source: AIME Ans: 548
22. ? ? ? A collection of 8 cubes consists of one cube with edge-length k for each
integer k, 1 ≤ k ≤ 8. A tower is to be built using all 8 cubes according to the
rules:
Let T be the number of different towers than can be constructed. What is the
remainder when T is divided by 1000? Source: AIME
23. ? ? ? 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}, including the empty
set, are spacy? Source: AMC
24. ? ? ? 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? Source:
AIME
25. ? ? ? A solitaire game is played as follows. Six distinct pairs of matched tiles are
placed in a bag. The player randomly draws tiles one at a time from the bag and
retains them, except that matching tiles are put aside as soon as they appear in
the player’s hand. The game ends if the player ever holds three tiles, no two of
which match; otherwise the drawing continues until the bag is empty. Find the
probability that the player wins the game (by emptying the bag). Source: AIME
13