Chapter 2: Recurrence Relations
Exercise 2.1
def fib_recursive(n):
return fib_recursive(n - 1) + fib_recursive(n - 2) if n > 1 else n
def fib_iterative(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return aThe recursive version needlessly recomputes the same values and has an exponential running time. The iterative version eliminates this deficiency. The computation remains efficient for , even with the recursive implementation; however, as values exceed 30, performance differences become increasingly apparent.
Exercise 2.2
The total number of arithmetic operations (additions, subtractions and divisions), including loop increments and index calculations, is
Exercise 2.3
It uses only arithmetic operations to compute the same value as Program 2.1.
Exercise 2.4
A non-recursive program would require time to compute values using recurrence (2) and for recurrence (3), as shown in Exercise 2.2.
A recursive program would also require time to compute values using recurrence (2). Despite an exponentially growing number of subproblems, their sizes are also exponentially dropping. On the other hand, recursively calculating values via recurrence (3), as mentioned in the book, has unacceptable performance. By instrumenting the code, we get that the number of function calls is . This can be formally proven by induction on , using the recurrence (with ) as the inductive hypothesis. Therefore, the number of arithmetic operations is .
Classifying variants with tight asymptotic bounds is convenient here. For example, we don’t need more details to understand that is prohibitive.
Exercise 2.5
We can’t directly implement the recurrence for the radix exchange sort using the technique demonstrated in Program 2.1. To calculate we need to know . Let’s transform it
For efficiently calculating the binomial coefficients, the program employs memoization. Here’s the output:
Analysis of Table 2.5
We’d expect that the average number of compares approach the following asymptotic approximations for sufficiently large :
Standard quicksort (full-key comparisons):
Median-of-three quicksort (full-key comparisons):
Radix exchange sort (bit-inspections):
Nonetheless, for smaller values of we can’t ignore lower-order terms, as the data in Table 2.5 shows. This exercise serves as a perfect example of why relying only on the leading asymptotic term can lead to the wrong conclusion for practical input sizes. The book highlights this fact many times. Gradual asymptotic expansion allows fine-tuning the model to reflect the desired accuracy in relation to input size ranges.
Exercise 2.6
Denote by and solutions to homogeneous recurrences and , respectively, where .
, where and , thus for . because it starts as 1,0,1,1,..., hence behaves like a right shifted Fibonacci sequence.
Exercise 2.7
The homogeneous part remains the same as in the previous exercise. The constant term "obeys" the same Fibonacci like growth pattern, with a difference, that in each iteration we also add one additional instance of . Imagine always starting a new Fibonacci sequence and adding it to the existing count. We get for . Exercise 3.35 shows how to attain a closed-form solution for the sum of Fibonacci numbers.
Exercise 2.8 🌟
For linear, let’s express the solution to the recurrence for in terms of , , , and , where and are solutions to homogeneous recurrences and , respectively:
Exercise 2.9
This immediately reduces to a product
Exercise 2.10
This immediately reduces to a sum
Exercise 2.11 🌟
The solution is posted on the book’s website.
Exercise 2.12
Following the hint from the book, we get
Exercise 2.13
Multiply both sides by and iterate
Exercise 2.14 🌟
Follow virtually the same steps as in the proof of Theorem 2.1 and stop at
Exercise 2.15
After dividing both sides by we’ve
Exercise 2.16 🌟
After dividing both sides by , we get a first-order linear recurrence
Use the generalized Theorem 2.1 with . The reduced summation factor equals to
Substitute back into the main recurrence
The identity (6.70) comes from the book Concrete Mathematics: A Foundation for Computer Science (2nd ed.).
Exercise 2.17 🌟
Simplify the expression for to get
Before proceeding, we must resolve the base cases for , ensuring that the summation factor is nonzero. Using the original recurrence for we get . Following the same steps from the previous exercise (see also Table 2.2 from the book), we get
Exercise 2.18
From the signed error term , we’ve . Substitute it back into the main recurrence
Therefore,
As , , thus, ; see the next exercise why this holds when is between 0 and 1. For small we’ve
Notice that . At any rate, , which explains why each iteration increases the number of significant digits available by a constant number of digits (about half a digit).
Exercise 2.19 🌟
Let and . We know that and is differentiable on this whole interval. Thus, by the mean value theorem, for any distinct , there’s a point such that . Consequently, we can always express the distance between and in terms of the distance between and . If for all , then has a Lipschitz constant less than 1, therefore is a contraction mapping on . This is indeed the case, since on this interval and (sine is strictly increasing in the interval , see Exercise 2.22).
We can also represent the continued fraction as for . I’s easy to show that all conditions hold for applying the Banach theorem.
Exercise 2.20
Suppose, for the sake of contradiction, that has a real fixed point for with . Since, is a real number, then is also a real number. By induction on , we can conclude that all are real numbers for . On the other hand, the fixed point isn’t a real number. This is a contradiction, so diverges.
The sequence exhibits chaotic behavior. Let’s use a substitution . Recall the double-angle identity for cotangent:
This exactly matches our recurrence relation. If , then . Therefore, and the sequence will fluctuate chaotically, governed by the doubling of the angle in the cotangent function. It’ll either terminate or not. If becomes zero, then the next iteration fails due to division by zero. For example, will terminate the sequence in the second iteration, since . It turns out that the sequence terminates if and only if is a dyadic rational multiple of . That is
where and are integers. Both directions are trivial to prove.
Exercise 2.21 🌟
We prove the lower bound in a similar fashion, as the book does for the upper bound
Both asymptotic bounds match, hence .
After computing initial terms, it seems that , where . Let’s prove that , which matches the case of the Stolz–Cesàro theorem. It states that if the limit of the differences exists, then is equal to that same limit. The numerator is 1, and the denominator is (see the derivation of the lower bound). Thus,
Since the limit of the differences is 1, the limit of the original sequence is also 1.
Exercise 2.22
First, we prove that . For we have that ; the geometric proof relies on the definition of radian. Consequently, is a strictly decreasing sequence bounded from below by zero. According to the monotone convergence theorem, it converges to the infimum, which is zero.
Since , we can use the Taylor series for around :
Substitute this expansion into the recurrence to get
If we square both sides of the equation, then we can use the approximation for small . Thus,
Following the hint from the book to consider change of variable , let’s flip the above equation to get
Therefore, .
Exercise 2.23
If then is a repelling fixed point. The sequence , starting at any point near (but not exactly ), will diverge from the fixed point . To develop an intuition why, let’s use the familiar error term from the book and monitor how it changes in each iteration.
Since the magnitude of the error term grows instead of shrinks. In general, what happens in later iterations highly depends on the global properties of the function. The point is, once the error term becomes small, the above approximation for becomes relevant and it’ll grow again.
Exercise 2.24
The analysis relies on the approximation of the error term from the previous exercise and the precondition stated in the book that is sufficiently close to . The three cases are:
If the fixed point is attracting then local convergence is guaranteed. The error drops by a constant factor at each step, hence the number of significant digits increases by a fixed amount in each iteration.
If the fixed point is super-attracting, we have guaranteed and fast local convergence. Here, the error term must be more precisely described using a higher-order derivative, assuming is at least twice continuously differentiable, for example, . Each iteration approximately doubles the number of significant digits available.
If the fixed point is neutral then convergence isn’t guaranteed. The criteria is more complicated, as described in the excerpt about nonhyperbolic fixed points.
If the fixed point is repelling then convergence isn’t possible.
Exercise 2.25
The book mentions that restructuring the terms of a recurrence corresponds to factoring the polynomial whose degree equals the order of the recurrence. There are two worked-out examples:
For the "characteristic polynomial" is in factored form.
For the "characteristic polynomial" is in factored form.
In our case, we need to work with a cubic polynomial
The term is associated with . Therefore, we can immediately construct our recurrence
The initial values are
Exercise 2.26 🌟
The main idea is the same as in Exercise 2.8. The notes for recitation 12, from the course 6.042/18.062J Mathematics for Computer Science (2005), summarize the methodology of solving inhomogeneous linear recurrences. It contains additional examples for each recurrence type.
The paper Some Techniques for Solving Recurrences by GL is a superb tutorial on solving recurrences with advanced material on systematic search (annihilator method) for particular solutions of inhomogeneous linear recurrences. The text covers many recurrence types that occur in practical applications.
At any rate, generating functions handle linear recurrences of this form in a unified manner without worrying about guessing a particular solution or doing any additional searches. Chapter 3 of the book is devoted to this topic.
Exercise 2.27
We know that , as stated in the book. Furthermore, we have
We need to work backwards to find initial conditions resulting in and . We immediately get and .
There are no initial conditions for which the solution is . Irrespectively of boundary conditions, must satisfy the recurrence, which isn’t the case here.
Exercise 2.28
By tweaking the initial conditions, we can fundamentally alter the growth rate of the solution to
We use the same approach as in the previous exercise. Here are the initial conditions leading to each desired growth rate of :
Constant is achieved when . For example, makes .
Exponential is achieved when , and . For example, , and makes .
Fluctuating in sign is achieved when , and . For example, , and makes .
Exercise 2.29
The characteristic equation is whose roots are and . Therefore, the general solution is for . We also have the following system of linear equations:
The constants are and .
Exercise 2.30
The characteristic equation is whose double root is . Therefore, the general solution is for .
We also have the following system of linear equations:
The constants are and .
If we change the initial conditions, then we get another system of linear equations:
The constants are and .
Exercise 2.31 🌟
The characteristic equation is whose complex roots are and . We could continue as usual, but the expression for would be awkward. Let’s use Euler’s formula to convert complex numbers into trigonometric form. The roots can be represented as . Thankfully to Euler’s formula, exponentiation becomes trivial. The general solution is
Since the sequence is real (it starts with real numbers and has real coefficients), the constants and must be reals, too. Consequently, the constants and must be complex conjugates. We also have the following system of linear equations:
The constants are and . Thus,
This was an amazing journey from a seemingly straightforward second-order linear recurrence to the trigonometric solution via complex algebra.
Exercise 2.32
The characteristic equation is . Let’s first search for a rational root. If we can find one, then by dividing the original polynomial with the corresponding factor, we get a quadratic equation. The latter is then directly solvable. The rational root theorem enumerates all rational candidates. It turns out that ½ is a root, thus is a factor.
Using a classical polynomial division algorithm, we get that the characteristic equation in factored form is
The other two complex roots are . Using the same transformation as in the previous exercise, we can write down the general solution as
We also have the following system of linear equations:
The constants are , and .
Exercise 2.33
Let’s split the problem into two subproblems based on parity of
We can combine both cases into a single recurrence for . The initial conditions are .
Exercise 2.34
The sequence is known as Tribonacci numbers. The characteristic equation is , whose single real root is the Tribonacci constant The other two complex roots and have magnitudes less than one, hence they tend to zero for large . Therefore, the approximate general solution is
Finding the Unknown Constant
To find we need to use the exact general solution
We have the following system of linear equations:
Let’s solve for by running the next Sage script:
It outputs
The characteristic polynomial is . Let’s take the derivative of on both sides and make them equal for . On the left we have
On the right we get
Consequently, , which gives us the value of in terms of
Estimating the Accuracy
We can calculate the exact and approximate value of using the following Python script.
It outputs 35890 ~ 35889.999620092305. They are indeed very close.
Exercise 2.35
Let’s define a new sequence . If we substitute this back into the main recurrence, we get
The initial conditions are , thus . Therefore,
Exercise 2.36
The following Python script implements the algorithm for checking the validity of a monomial; covers both types of monomials (pure and mixed). The running time is .
In the expansion of , for , we have mixed monomials (consisted of both symbols and ). The total number of such monomials, across all iterations, is
For example, when this formula evaluates to 12, which matches the number of mixed monomials listed in the book.
Exercise 2.37
Solving proceeds along the same steps as in Exercise 2.29 and won’t be repeated here. The exact solution is
and for .
Exercise 2.38
Let’s square both sides of the initial recurrence to get for . If we introduce a substitution then the recurrence transforms into , thus . Therefore, for .
Exercise 2.39
For the solution is
As the book notes, only the larger of the two roots predominates in this expression—the one with the plus sign.
For the solution is
Again, only the larger of the two roots predominates in this expression—the one with the plus sign.
For the solution is fundamentally different. The roots are complex numbers
We can use the same approach as in Exercise 2.31 to transform these into trigonometric form. Let , where , thus the recurrence becomes
The sequence fluctuates between -2 and 2, never settling on a single value.
Exercise 2.40 🌟
The accurate approximate answer for large and sufficiently large is . Usually, in expressions like , is a small positive constant, so the subsequent analysis assumes this situation. The critical term is
For the larger root we have
For the smaller root we have
As increases, will rapidly vanish to 0. Consequently, we get . This shows that even for an infinitesimally small , the sequence diverges to infinity at a double-exponential rate.
Exercise 2.41 🌟
We expand to match parameters
Let’s express in terms of and starting with
We now match the coefficients of this result with the given recurrence and derive the necessary and sufficient condition for transforming into :
.
For the parameters are , and . Clearly
This shows that it doesn’t reduce to the target form.
Exercise 2.42
We need to solve the recurrence for and different initial values. Let’s square both sides and use the substitution . We get
The idea is to leverage the result from the previous exercise and transform into . The parameters are , and . Since , we can apply the transformation, thus .
If , then , hence . We already know the solution from the book for this special case, namely . Therefore, which implies that for .
If , then , thus . We hit the third case in Exercise 2.39, so the solution is , where . Therefore, , which implies that .
Here’s the Python script to plot as a function of .

Exercise 2.43 🌟
Let’s represent the recurrence as a function , where (otherwise, we won’t have a continued fraction). This function defines a Möbius transformation. We first need to determine the fixed points denoted by and . Afterward we can leverage a change of variable to linearize the original recurrence.
Distinct Roots
The initial value is . If we substitute into the expression for , assuming , we get
Repeated Roots
When a Möbius transformation has a single, repeated fixed point , it is classified in dynamical systems as a parabolic transformation. We need to use a different change of variable
Without repeating a similar grueling algebra, the outcome is
Flip the substitution back to get the closed form for .
Exercise 2.44 🌟
Let for . We need to specify a linear recurrence relation on such that also satisfies the original non-linear recurrence. We have
The initial conditions must satisfy
We can choose any starting value for .
Exercise 2.45 🌟
We are given a relaxed quicksort recurrence
Let’s solve it when .
The repertoire table from the book has many reusable entries except for the term. If we set then . We need to combine entries from the expanded table to get , thus we have
Therefore, the solution is
Exercise 2.46
The post on StackExchange for Mathematics explains the basics of the repertoire method and illustrates its application by solving this exercise.
Exercise 2.47 🌟
Let’s use the next Python script to calculate numerical values of , alongside our guess that . This hypothesis is based on the observation that as .
The output shows that we’re on the right track.
If we employ bootstrapping and substitute our guess back into the recurrence, we get
The new version of the script reflects this change.
The results are better aligned as increases.
After an additional iteration we have
We see that the approximation of is improved again.
This process may continue depending on a desired accuracy level.
Exercise 2.48
The book assumes (without stating it explicitly) that we need to find the approximation of the average number of compares used by median-of-three quicksort. For the sake of completeness, it’s repeated here
The key is to convert this discrete recurrence into its continuous form, by approximating the sum with a definite integral. Converting a discrete sum into a definite integral is a good choice for expected (average) values. The integral elegantly smooths out local discrete fluctuations, providing a highly accurate asymptotic approximation without the need for cumbersome error tracking. At any rate, the process can be broken down into three major stages.
Converting the Discrete Recurrence
Let’s simplify the constant multiplier of the sum for large enough
Let’s set the pivot’s rank relative to the size ; for this we use the continuous variable . Finally, we treat and . If we substitute these changes into our original expression and replace the summation with an integral, we have
Exercise 2.49 🌟
With bootstrapping we start with a rough bound and then improve it step by step. The altered recurrence is
where the term is separated out. When the goal is to establish strict upper bounds, converting a sum to an integral introduces unnecessary complications. We would need to carefully cover the error gaps. It’s far more easier to apply a splitting technique, as explained below.
Iteration 1
Let’s start with the rough guess that for . Thus, there exists some constant such that for sufficiently large . Note that for all . If we substitute this into the recurrence, we have
Iteration 2
We assume that for some constant and . If we substitute this into the recurrence, we have
To upper bound the sum , we split it into two parts. The split typically occurs at the midpoint (e.g., ). The approach involves independently constraining each region—either by limiting the growth of an expanding component or by anchoring a diminishing component at its maximal value. This method generally yields two independent asymptotic approximations, which are subsequently combined to bound the entire sum. For convenience assume that is even.
When then . We have
The infinite series converges to a constant.
When then attains its maximum value at the start of the range, bounded by . Therefore,
Thus,
Iteration 3
To be rigorous, we should check that this form is "stable"; if we assume , we must get back. So, we take that for some constant and . If we substitute this into the recurrence, we have
We split the summation again into two parts. When then , hence
The sum , where is the Riemann zeta function.
When then . Thus, . Therefore,
Final Result
From it follows that .
Exercise 2.50 🌟
Find the asymptotic growth of the solution to the “perturbed” Fibonacci recurrence
We can view the recurrence as
The solution to a simpler recurrence for with and is , where is the golden ratio.
Following the steps from the book, we need to compare the two recurrences by forming the ratio
If we plug into the original recurrence, we won’t be able to bound . Consequently, the solution must grow faster. The book contains a hint as part of Exercise 2.53; let’s add an extra multiplier and find the value of such that . This gives
with . We have
To solve for we can set , which implies that
Thus, the asymptotic growth of the solution is .
Exercise 2.51 🌟
The coefficient on suggests . Let’s employ a change of variable to find a solution to a simpler recurrence
As , . Therefore, applying the perturbation method again, we end up with another simpler recurrence for with and . Clearly, , where is the golden ratio.
To account for the perturbation factor, we reuse the approach from the previous exercise. This gives
with . We have