> For the complete documentation index, see [llms.txt](https://evarga.gitbook.io/sh-aofa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://evarga.gitbook.io/sh-aofa/discrete-mathematical-methods/chapter-2-recurrence-relations.md).

# Chapter 2: Recurrence Relations

## Exercise 2.1

```python
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 a
```

The recursive version needlessly recomputes the same values and has an exponential running time. The iterative version eliminates this deficiency. The computation remains efficient for $$F\_{20}=6765$$, even with the recursive implementation; however, as values exceed 30, performance differences become increasingly apparent.

{% hint style="info" %}
Solve the [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) problem on LeetCode.
{% endhint %}

## Exercise 2.2

The total number of arithmetic operations (additions, subtractions and divisions), including loop increments and index calculations, is

$$
C\_{N\_{max}}=\sum\_{N=1}^{N\_{max}}\left(1+1+\sum\_{k=0}^{N-1}(1+5)\right)=N\_{max}(3N\_{max}+5).
$$

## Exercise 2.3

```python
def c(n):
    return (1 + 1 / n) * c(n - 1) + 2 if n > 1 else 2
```

It uses only $$5(N\_{max}-1)$$ arithmetic operations to compute the same value as Program 2.1.

## Exercise 2.4

A non-recursive program would require $$\Theta(N)$$ time to compute values using recurrence (2) and $$\Theta(N^2)$$ for recurrence (3), as shown in [Exercise 2.2](#exercise-2.2).

A recursive program would also require $$\Theta(N)$$ 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 $$3^N$$. This can be formally proven by induction on $$N$$, using the recurrence $$r\_N=1+2\sum\_{k=0}^{N-1}r\_k$$ (with $$r\_0=1$$) as the inductive hypothesis. Therefore, the number of arithmetic operations is $$\Theta(3^N)$$.&#x20;

Classifying variants with tight asymptotic bounds is convenient here. For example, we don’t need more details to understand that $$\Theta(3^N)$$ 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 $$C\_N$$ we need to know $$C\_N$$. Let’s transform it

$$
\begin{align\*}
C\_N &= N+\frac{1}{2^N} \sum\_{k=0}^N {\binom{N}{k}(C\_k+C\_{N-k})} \\
&= N+\frac{2}{2^N} \sum\_{k=0}^N {\binom{N}{k}C\_k} && \text{$\left(\binom{N}{k}=\binom{N}{N-k}\right)$} \\
&= N+\frac{1}{2^{N-1}} \left(\left(\sum\_{k=0}^{N-1} {\binom{N}{k}C\_k}\right)+C\_N\right) \\
&= N+\frac{1}{2^{N-1}} \left(\sum\_{k=0}^{N-1} {\binom{N}{k}C\_k}\right)+\frac{1}{2^{N-1}}C\_N \\
&= \frac{2^{N-1}}{2^{N-1}-1} \left(N+\frac{1}{2^{N-1}} \sum\_{k=0}^{N-1} {\binom{N}{k}C\_k}\right).
\end{align\*}
$$

{% code expandable="true" %}

```java
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.HashMap;
import java.util.Map;

public class CalculateValues {
    private static final int MAXN = 5000;
    private static final int STEP = 1000;

    public static void main(String[] args) {
        System.out.printf("%8s | %8s | %8s| %8s%n", "n", "standard", "med-3", "radix");
        System.out.println("----------------------------------------");
        for (int n = STEP; n <= MAXN; n += STEP) {
            int m = n - STEP + 1;
            int standard = standardQuickSort(m, n);
            int med3 = medianOfThreeQuickSort(m, n);
            int radix = radixExchangeSort(m, n);
            System.out.printf("%8d | %8d | %8d| %8d%n", n, standard, med3, radix);
        }
    }

    private static final double[] c1 = new double[MAXN + 1];

    private static int standardQuickSort(int minN, int maxN) {
        for (int n = minN; n <= maxN; n++)
            c1[n] = (n + 1) * c1[n - 1] / n + 2;
        return (int) c1[maxN];
    }

    private static final double[] c2 = new double[MAXN + 1];

    private static int medianOfThreeQuickSort(int minN, int maxN) {
        for (int n = Integer.max(minN, 3); n <= maxN; n++) {
            long d = (long) n * (n - 1) * (n - 2) / 6;
            c2[n] = n + 1;
            for (int k = 1; k <= n; k++)
                c2[n] += ((k - 1D) * (n - k) / d) * (c2[k - 1] + c2[n - k]);
        }
        return (int) c2[maxN];
    }

    private static final MathContext MC = MathContext.DECIMAL128;
    private static final Map<Long, BigInteger> cache = new HashMap<>();

    private static BigInteger nChooseK(int n, int k) {
        if (k < 0 || k > n) return BigInteger.ZERO;
        if (k == 0 || k == n) return BigInteger.ONE;
        if (k > n - k) k = n - k;
        long key = (((long) n) << 32) | (k & 0xffffffffL);
        BigInteger cached = cache.get(key);
        if (cached != null) return cached;
        BigInteger value = nChooseK(n - 1, k - 1).add(nChooseK(n - 1, k));
        cache.put(key, value);
        return value;
    }

    private static final BigDecimal[] c3 = new BigDecimal[MAXN + 1];

    private static int radixExchangeSort(int minN, int maxN) {
        for (int i = minN; i <= maxN; i++) c3[i] = BigDecimal.ZERO;

        for (int n = Integer.max(minN, 2); n <= maxN; n++) {
            for (int k = 2; k < n; k++) {
                BigDecimal binom = new BigDecimal(nChooseK(n, k));
                c3[n] = c3[n].add(binom.multiply(c3[k], MC), MC);
            }
            BigDecimal f = new BigDecimal(BigInteger.ONE.shiftLeft(n - 1));
            BigDecimal d = f.subtract(BigDecimal.ONE);
            BigDecimal term1 = BigDecimal.valueOf(n).multiply(f).divide(d, MC);
            BigDecimal term2 = c3[n].divide(d, MC);
            c3[n] = term1.add(term2, MC);
        }
        return c3[maxN].toBigInteger().intValue();
    }
}
```

{% endcode %}

For efficiently calculating the binomial coefficients, the program employs [memoization](https://en.wikipedia.org/wiki/Memoization). Here’s the output:

{% code title="Table 2.5 - Estimated average number of comparisons of quicksort variants" %}

```
       n | standard |    med-3|    radix
----------------------------------------
    1000 |    12985 |    10027|    11297
    2000 |    28729 |    22420|    24596
    3000 |    45519 |    35709|    38649
    4000 |    62988 |    49581|    53193
    5000 |    80963 |    63885|    68101
```

{% endcode %}

#### Analysis of Table 2.5

We’d expect that the average number of compares approach the following asymptotic approximations for sufficiently large $$N$$:&#x20;

* Standard quicksort (full-key comparisons): $$\sim 2N\ln N \approx1.386N\lg N$$
* Median-of-three quicksort (full-key comparisons): $$\sim (12/7)N\ln N \approx 1.188 N\lg N$$
* Radix exchange sort (bit-inspections): $$\sim N\lg N$$

Nonetheless, for smaller values of $$N$$ 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 $$U\_n$$ and $$V\_n$$ solutions to homogeneous recurrences $$u\_n$$ and $$v\_n$$, respectively, where $$f(x,y)=x+y$$.

$$u\_n=f(u\_{n-1},u\_{n-2}) \text{ for $n > 1$ with $u\_0=1$ and $u\_1=0$} ,\ v\_n=f(v\_{n-1},v\_{n-2}) \text{ for $n > 1$ with $v\_0=0$ and $v\_1=1$} .$$

$$a\_n=pU\_n+qV\_n$$, where $$U\_n=F\_{n-1}$$ and $$V\_n=F\_n$$, thus $$a\_n=pF\_{n-1}+qF\_n$$ for $$n>1$$. $$U\_n=F\_{n-1}$$ 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 $$r$$ "obeys" the same Fibonacci like growth pattern, with a difference, that in each iteration we also add one additional instance of $$r$$. Imagine always starting a new Fibonacci sequence and adding it to the existing count. We get $$a\_n=pF\_{n-1}+qF\_n+r\sum\_{k=1}^{n-1}F\_k$$ for $$n>1$$. [Exercise 3.35](https://evarga.gitbook.io/sh-aofa/discrete-mathematical-methods/pages/haY46WTvhNwGF6h7VSOn#exercise-3.35) shows how to attain a closed-form solution for the sum of Fibonacci numbers.

## Exercise 2.8 🌟

{% hint style="success" %}
This is a generalization of the previous two exercises.
{% endhint %}

For $$f$$ linear, let’s express the solution to the recurrence $$a\_n=f(a\_{n-1},a\_{n-2})$$ for $$n>1$$ in terms of $$a\_0$$, $$a\_1$$, $$f(0,0)$$, $$U\_n$$ and $$V\_n$$, where $$U\_n$$ and $$V\_n$$ are solutions to homogeneous recurrences $$u\_n$$ and $$v\_n$$, respectively:

$$u\_n=f(u\_{n-1},u\_{n-2})-f(0,0) \text{ for $n > 1$ with $u\_0=1$ and $u\_1=0$} ,\ v\_n=f(v\_{n-1},v\_{n-2})-f(0,0) \text{ for $n > 1$ with $v\_0=0$ and $v\_1=1$} .$$

$$\boxed{a\_n=a\_0U\_n+a\_1V\_n+f(0,0)\sum\_{k=1}^{n-1}V\_k \qquad \text{for $n>1$}.}$$

## Exercise 2.9

This immediately reduces to a product

$$
\begin{align\*}
a\_n &= \prod\_{k=1}^n \frac{k}{k+2} \\
&=\frac{1}{3} \cdot  \frac{2}{4} \cdot\cdot\cdot \frac{n-1}{n+1} \cdot  \frac{n}{n+2}  \\
&= \frac{n!}{(n+2)!/2} \\
&= \frac{2}{(n+1)(n+2)}.
\end{align\*}
$$

## Exercise 2.10

This immediately reduces to a sum

$$
\begin{align\*}
a\_n &= 1+\sum\_{k=1}^n (-1)^kk \\
&= 1+(-1+2- 3+\dots + (-1)^nn) \\
&= 1+\begin{cases}
\dfrac{n}{2} &\text{if $n$ is even}, \\
-\dfrac{n+1}{2} &\text{if $n$ is odd}.
\end{cases} \\\[0.6cm]
&=1+ (-1)^n\bigg\lceil \frac{n}{2} \bigg\rceil.
\end{align\*}
$$

## Exercise 2.11 🌟

{% hint style="success" %}
This exercise reminds us that sometimes there is a remarkably easy solution once we start [thinking outside the box](https://en.wikipedia.org/wiki/Thinking_outside_the_box)
{% endhint %}

The solution is posted on the book’s [website](https://aofa.cs.princeton.edu/20recurrence/).

## Exercise 2.12

Following the hint from the book, we get

$$
\begin{align\*}
\frac{a\_n}{2^n} &= \frac{a\_{n-1}}{2^{n-1}}+\frac{1}{2^n} \\
&= \frac{a\_{n-2}}{2^{n-2}}+\frac{1}{2^{n-1}}+\frac{1}{2^n} \\
&= \dots \\
&= \frac{a\_1}{2^1}+\frac{1}{2^2}+\dots+\frac{1}{2^n} \\
&\therefore \boxed{a\_n=\sum\_{k=0}^{n-1}2^k=2^n-1}.
\end{align\*}
$$

## Exercise 2.13

Multiply both sides by $$(n+1)$$ and iterate

$$
\begin{align\*}
(n+1)a\_n &= na\_{n-1}+(n+1) \\
&=(n-1)a\_{n-2}+n+(n+1) \\
&\dots \\
&= a\_0+2+\dots+n+(n+1) \\
&= \sum\_{k=1}^{n+1} k \\
&= \frac{(n+1)(n+2)}{2} \\
&\therefore \boxed{a\_n=\frac{n+2}{2}}.
\end{align\*}
$$

## Exercise 2.14 🌟

{% hint style="success" %}
This is a generalization of Theorem 2.1.
{% endhint %}

Follow virtually the same steps as in the proof of Theorem 2.1 and stop at $$a\_t$$

$$
a\_n=y\_n+a\_t(x\_nx\_{n-1}\dots x\_{t+1})+\sum\_{t+1 \le j \<n}y\_jx\_{j+1}x\_{j+2}\dots x\_n \text{ for $n>t$}.
$$

{% hint style="info" %}
As a sanity check, shift the recurrence in [Exercise 2.13](#exercise-2.13) and compute values as described here. For example, use $$t=2$$ and $$a\_t=2$$ to get $$a\_7=9/2$$ in both ways, via the original recurrence and this synthesized formula. When $$x\_n=n/(n+1)$$ then $$x\_nx\_{n-1}\dots x\_{t+1}=(t+1)/(n+1)$$.
{% endhint %}

## Exercise 2.15

After dividing both sides by $$n$$ we’ve

$$
\begin{align\*}
a\_n&=\frac{n+1}{n}a\_{n-1}+2 \\
&=2+2(n+1)(H\_n-1) && \text{(by Theorem 2.1)} \\
&=2(n+1)(H\_{n+1}-1) && \text{($2=2(n+1) \cdot \frac{1}{n+1}$)}.
\end{align\*}
$$

{% hint style="warning" %}
Notice, that the recurrence in section 2.2 for $$C\_N$$ isn’t the same as in section 1.5 (the initial conditions are different). Consequently, the solution based on Theorem 2.1 is exactly as given in the book. The reported errata to change -1 to -3/2 is incorrect, since it assumes the recurrence from section 1.5.
{% endhint %}

## Exercise 2.16 🌟

{% hint style="success" %}
This exercise perfectly illustrates the generalized Theorem 2.1 (see [Exercise 2.14](#exercise-2.14)).&#x20;
{% endhint %}

After dividing both sides by $$n$$, we get a first-order linear recurrence

$$
a\_n=\frac{n-4}{n}a\_{n-1}+12H\_n \qquad \text{for $n>4$ and $a\_n=0$ for all $n \le 4$}.
$$

Use the generalized Theorem 2.1 with $$t=4$$. The reduced summation factor $$s\_{j+1}=x\_{j+1}x\_{j+2}\dots x\_n$$ equals to

$$
\begin{align\*}
s\_{j+1}&=\frac{j-3}{j+1} \cdot \frac{j-2}{j+2} \cdot \cdot \cdot \frac{n-4}{n} \\
&= \frac{j(j-1)(j-2)(j-3)}{n(n-1)(n-2)(n-3)} \\
&= \frac{\dbinom{j}{4}}{\dbinom{n}{4}}.
\end{align\*}
$$

Substitute back $$s\_{j+1}$$ into the main recurrence

$$
\begin{align\*}
a\_n&=12H\_n+\frac{12}{\dbinom{n}{4}}\sum\_{j=5}^{n-1} {\binom{j}{4}H\_j} \\
&=\frac{12}{\dbinom{n}{4}}\sum\_{j=5}^n {\binom{j}{4}H\_j} \\
&= \frac{12}{\dbinom{n}{4}}\left(\sum\_{j=4}^n \left({\binom{j}{4}H\_j}\right)-\binom{4}{4}H\_4\right) \\\[0.8cm]
&= \frac{12}{\dbinom{n}{4}}\left(\binom{n+1}{5}\left(H\_{n+1}-\frac{1}{5}\right)-H\_4\right) && \text{(by formula (6.70))}.
\end{align\*}
$$

The identity (6.70) comes from the book [Concrete Mathematics: A Foundation for Computer Science (2nd ed.)](https://www-cs-faculty.stanford.edu/~knuth/gkp.html).

## Exercise 2.17 🌟

{% hint style="success" %}
This exercise highlights an important detail not explicitly mentioned in the text; the summation factor must be nonzero.&#x20;
{% endhint %}

Simplify the expression for $$A\_N$$ to get

$$
A\_N=\frac{N-6}{N}A\_{N-1}+2 \qquad \text{for $N>1$ and $A\_1=1$}.
$$

Before proceeding, we must resolve the base cases for $$N \le 6$$, ensuring that the summation factor is nonzero. Using the original recurrence for $$A\_N$$ we get $$A\_2=0, A\_3=2,A\_4=1,A\_5=9/5,A\_6=2$$. Following the same steps from the previous exercise (see also Table 2.2 from the book), we get

$$
A\_N=\frac{2(N+1)}{7}  \qquad \text{ for $N>6$}.
$$

## Exercise 2.18

From the signed error term $$b\_n = a\_n - \alpha$$, we’ve $$a\_{n-1} = b\_{n-1} + \alpha$$. Substitute it back into the main recurrence

$$
a\_n = \frac{1}{1 + a\_{n-1}} = \frac{1}{1 + \alpha + b\_{n-1}}.
$$

Therefore,

$$
\begin{align\*}
b\_n &= a\_n - \alpha \\
&= \frac{1}{1 + \alpha + b\_{n-1}} - \alpha \\
&= \frac{1 - \alpha(1 + \alpha + b\_{n-1})}{1 + \alpha + b\_{n-1}} \\
&= \frac{1 - \alpha - \alpha^2 - \alpha b\_{n-1}}{1 + \alpha + b\_{n-1}} \\
&= -\frac{\alpha}{1 + \alpha + b\_{n-1}} \cdot b\_{n-1} && \text{($1 - \alpha - \alpha^2 = 0$)}.
\end{align\*}
$$

As $$n \to \infty$$, $$a\_n \to \alpha$$, thus, $$b\_n \to 0$$; see the next exercise why this holds when $$a\_0$$ is between 0 and 1. For small $$b\_{n-1}$$ we’ve

$$
\frac{1}{1 + \alpha + b\_{n-1}} \approx \frac{1}{1 + \alpha} \implies b\_n \approx -\frac{\alpha}{1 + \alpha} b\_{n-1}= -\alpha^2 b\_{n-1}.
$$

Notice that $$\alpha = \frac{1}{1+\alpha} \implies \frac{\alpha}{1 + \alpha} = \alpha^2$$. At any rate, $$|b\_n| \approx 0.4|b\_{n-1}|$$, which explains why each iteration increases the number of significant digits available by a constant number of digits (about half a digit).&#x20;

## Exercise 2.19 🌟

{% hint style="success" %}
The solution is known as the [Dottie number](https://en.wikipedia.org/wiki/Dottie_number) and revolves around the concept of a [fixed point](https://en.wikipedia.org/wiki/Fixed_point_\(mathematics\)). We must show that all conditions are satisfied for applying the [Banach fixed point theorem](https://en.wikipedia.org/wiki/Banach_fixed-point_theorem); it then guarantees that the function $$\cos(x)$$ converges to a unique fixed point for any $$a\_0 \in \[0,1]$$.
{% endhint %}

Let $$f(x)=\cos(x)$$ and $$x \in \[0,1]$$. We know that $$f:\[0,1] \mapsto \[0,1]$$ and is differentiable on this whole interval. Thus, by the [mean value theorem](https://en.wikipedia.org/wiki/Mean_value_theorem), for any distinct $$x,y \in \[0,1]$$, there’s a point $$c \in (x,y)$$ such that $$f(x)-f(y)=f'(c)(x-y)$$. Consequently, we can always express the distance between $$f(x)$$ and $$f(y)$$ in terms of the distance between $$x$$ and $$y$$. If $$|f'(c)| <1$$ for all $$c \in(0,1)$$, then $$f$$ has a Lipschitz constant less than 1, therefore $$f$$ is a contraction mapping on $$\[0,1]$$. This is indeed the case, since $$|f'(x)|=\sin(x)$$ on this interval and $$\sin(1)<1$$ (sine is strictly increasing in the interval $$\[0,1]$$, see [Exercise 2.22](#exercise-2.22)).

We can also represent the continued fraction as $$f(x)=1/(1+x)$$ for $$x \in \[0,1]$$. I’s easy to show that all conditions hold for applying the Banach theorem.

## Exercise 2.20

Suppose, for the sake of contradiction, that $$a\_n=0.5(a\_{n-1}-1/a\_{n-1})$$ has a real fixed point for $$n>0$$ with $$a\_0\neq 0$$. Since, $$a\_0$$ is a real number, then $$a\_1=0.5(a\_0-1/a\_0)$$ is also a real number. By induction on $$n$$, we can conclude that all $$a\_n$$ are real numbers for $$n>0$$. On the other hand, the fixed point $$\sqrt{-1}=i$$ isn’t a real number. This is a contradiction, so $${a\_n}\_{n>0}$$ diverges.

***

The sequence exhibits chaotic behavior. Let’s use a substitution $$a\_n=\cot(\theta\_n)$$. Recall the [double-angle identity for cotangent](https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Double-angle_formulae):

$$
\cot(2\theta) = \frac{\cot^2\theta - 1}{2\cot\theta} = \frac{1}{2}\left(\cot\theta - \frac{1}{\cot\theta}\right).
$$

This exactly matches our recurrence relation. If $$a\_0=\cot(\theta\_0)$$, then $$a\_1=\cot(2\theta\_0)$$. Therefore, $$a\_n = \cot(2^n \theta\_0)$$ and the sequence will fluctuate chaotically, governed by the doubling of the angle in the cotangent function. It’ll either terminate or not. If $$a\_n$$ becomes zero, then the next iteration fails due to division by zero. For example, $$a\_0=\cot(\pi/4)=1$$ will terminate the sequence in the second iteration, since $$a\_1=0$$. It turns out that the sequence terminates if and only if $$\theta\_0$$ is a [dyadic rational](https://en.wikipedia.org/wiki/Dyadic_rational) multiple of $$\pi$$. That is

$$
\theta\_0 = \frac{k}{2^m}\pi,
$$

where $$k$$ and $$m>0$$ are integers. Both directions are trivial to prove.

## Exercise 2.21 🌟

{% hint style="success" %}
This exercise introduces the [Stolz–Cesàro theorem](https://en.wikipedia.org/wiki/Stolz%E2%80%93Ces%C3%A0ro_theorem), which is a discrete version of [L'Hôpital’s rule](https://en.wikipedia.org/wiki/L%27H%C3%B4pital%27s_rule).&#x20;
{% endhint %}

We prove the lower bound in a similar fashion, as the book does for the upper bound

$$
\begin{align\*}
\frac{1}{a\_n} &= \frac{1}{a\_{n-1}} \left(\frac{1}{1-a\_{n-1}}\right) &&  \text{(for $n>0$)}\\
&=\frac{1}{a\_{n-1}}+\frac{1}{1-a\_{n-1}} \\
&< \frac{1}{a\_{n-1}}+2 && \text{($1-a\_{n-1} > 1/2$ for sufficiently large $n$)} \\
&\dots  \\
&< 2(n+1) && \text{(telescoping sum)} \\
&\le 3n && \text{(for all $n>1$)} \\
&\therefore \boxed{a\_n \ge \frac{1}{3} \cdot \frac{1}{n} = \Omega(1/n)}.
\end{align\*}
$$

Both asymptotic bounds match, hence $$a\_n=\Theta(1/n) \implies \lim\_{n \to \infty}a\_n=0$$.

***

After computing initial terms, it seems that $$a\_n \sim c/n$$, where $$c=1$$. Let’s prove that $$\lim\_{n \to \infty} na\_n=\lim\_{n \to \infty}\frac{n}{1/a\_n}=1$$, which matches the $$\infty/\infty$$ case of the Stolz–Cesàro theorem. It states that if the limit of the differences $$\frac{n - (n-1)}{1/a\_n - 1/a\_{n-1}}$$ exists, then $$\lim\_{n \to \infty} \frac{n}{1/a\_n}$$ is equal to that same limit. The numerator is 1, and the denominator is $$1/(1-a\_{n-1})$$ (see the derivation of the lower bound). Thus,

$$
\lim\_{n \to \infty} \frac{1}{1/(1-a\_{n-1})} = \lim\_{n \to \infty} (1-a\_{n-1}) = 1.
$$

Since the limit of the differences is 1, the limit of the original sequence is also 1.

## Exercise 2.22

First, we prove that $$\lim\_{n \to \infty}a\_n=0$$. For $$x \in (0,1]$$ we have that $$0\<sin(x)\<x$$; the [geometric proof](https://qr.ae/pCC7VJ) relies on the definition of [radian](https://en.wikipedia.org/wiki/Radian). Consequently, $$a\_n$$ is a strictly decreasing sequence bounded from below by zero. According to the [monotone convergence theorem](https://en.wikipedia.org/wiki/Monotone_convergence_theorem), it converges to the [infimum](https://en.wikipedia.org/wiki/Infimum_and_supremum), which is zero.

***

Since $$a\_n \to 0$$, we can use the [Taylor series](https://en.wikipedia.org/wiki/Taylor_series) for $$\sin(x)$$ around $$x=0$$:

$$
\sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \dots = x - \frac{x^3}{6} + O(x^5).
$$

Substitute this expansion into the recurrence $$a\_n = \sin(a\_{n-1})$$ to get

$$
a\_n \sim a\_{n-1} - \frac{a\_{n-1}^3}{6}=a\_{n-1}\left(1 - \frac{a\_{n-1}^2}{6}\right).
$$

If we square both sides of the equation, then we can use the approximation $$(1-x)^2 \sim 1-2x$$ for small $$x$$. Thus,

$$
\begin{align\*}
a\_n^2 &\sim a\_{n-1}^2\left(1 - \frac{a\_{n-1}^2}{6}\right)^2 \\
&\sim a\_{n-1}^2\left(1 - \frac{a\_{n-1}^2}{3}\right).
\end{align\*}
$$

Following the hint from the book to consider change of variable $$b\_n=1/a\_n$$ , let’s flip the above equation to get

$$
\begin{align\*}
b\_n^2 &\sim \frac{b\_{n-1}^2}{ \left(1 - \frac{a\_{n-1}^2}{3}\right)} \\
&\sim b\_{n-1}^2 \left(1 + \frac{a\_{n-1}^2}{3}\right) && \text{($\frac{1}{1-x} \sim 1+x$ for small $x$)} \\
&= b\_{n-1}^2 + \frac{1}{3} \\
&= 1+\frac{n}{3} && \text{(telescoping sum)}.
\end{align\*}
$$

Therefore, $$b\_n=O(\sqrt n) \implies a\_n=O(1/\sqrt n)$$.

## Exercise 2.23

If $$f'(\alpha) > 1$$ then $$\alpha$$ is a repelling fixed point. The sequence $$a\_n = f(a\_{n-1})$$, starting at any point $$a\_0$$ near $$\alpha$$ (but not exactly $$\alpha$$), will diverge from the fixed point $$\alpha$$. To develop an intuition why, let’s use the familiar error term from the book $$b\_n=a\_n-\alpha$$ and monitor how it changes in each iteration.

$$
\begin{align\*}
a\_n &= f(a\_{n-1}) \\
\alpha + b\_n &= f(\alpha + b\_{n-1}) \\
&\approx f(\alpha) + f'(\alpha) \cdot b\_{n-1} && \text{(by Taylor expansion near $\alpha$, $f(\alpha + b\_{n-1}) \approx f(\alpha) + f'(\alpha) \cdot b\_{n-1}$)} \\
&\therefore \boxed{|b\_n| \approx f'(\alpha) \cdot |b\_{n-1}|} && \text{($\alpha=f(\alpha$))}.
\end{align\*}
$$

Since $$f'(\alpha) > 1$$ 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 $$b\_n$$ becomes relevant and it’ll grow again.

## Exercise 2.24

The analysis relies on the approximation of the error term $$b\_n$$ from the previous exercise and the precondition stated in the book that $$a\_0$$ is **sufficiently close** to $$\alpha$$. The three cases are:

1. If the fixed point $$\alpha$$ is attracting then local convergence is guaranteed. The error drops by a constant factor $$|f'(\alpha)|$$ at each step, hence the number of significant digits increases by a fixed amount in each iteration.
2. If the fixed point $$\alpha$$ 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 $$f$$ is at least twice continuously differentiable, for example, $$b\_n \approx \frac12 \left| f''(\alpha) \right| \cdot b\_{n-1}^2$$. Each iteration approximately doubles the number of significant digits available.
3. If the fixed point $$\alpha$$ is neutral then convergence isn’t guaranteed. The criteria is more complicated, as described in the [excerpt about nonhyperbolic fixed points](https://www.liverpool.ac.uk/~maryrees/homepagemath206/241elaydi20-29.pdf).
4. If the fixed point $$\alpha$$ is repelling then convergence isn’t possible.

{% hint style="info" %}
The [lecture notes](https://www.math.utoronto.ca/burbulla/lecturenotes335/Chapter5.pdf) by Burbulla nicely recaps the basic theory around fixed points with lots of examples. It contains a case study of a unilaterally attracting/repelling neutral fixed point.&#x20;
{% endhint %}

## 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 $$a\_n=2^n-1=2^n-1^n$$ the "characteristic polynomial" is $$(1-2x)(1-x)$$ in factored form.
* For $$a\_n=3^n-2^n$$ the "characteristic polynomial" is $$(1-3x)(1-2x)$$ in factored form.

&#x20;In our case, we need to work with a cubic polynomial

$$
(1-4x)(1-3x)(1-2x)
\= 1x^0-9x^1+26x^2-24x^3.
$$

The term $$x^d$$ is associated with $$a\_{n-d}$$. Therefore, we can immediately construct our recurrence

$$
a\_n=9a\_{n-1}-26a\_{n-2}+24a\_{n-3} \space\space\space\space\space\space\text{ for $n>2$}.
$$

The initial values are

$$
\begin{align\*}
a\_0 &=4^0-3^0+2^0=1 \\
a\_1 &=4^1-3^1+2^1=3 \\
a\_2 &=4^2-3^2+2^2=11.
\end{align\*}
$$

## Exercise 2.26 🌟

{% hint style="success" %}
This exercise explains how to solve an inhomogeneous recurrence of the form

*a*<sub>*n*</sub> = *x*<sub>1</sub>*a*<sub>*n*</sub> <sub></sub><sub>– 1</sub> + *x*<sub>2</sub>*a*<sub>*n*</sub> <sub></sub><sub>– 2</sub> + ... + *x*<sub>*t*</sub>*a*<sub>*n*</sub> <sub>–</sub> <sub></sub><sub>*t*</sub> + *r*     for *n* ≥ *t*.
{% endhint %}

The main idea is the same as in [Exercise 2.8](#exercise-2.8). The [notes for recitation 12](https://dspace.mit.edu/bitstream/handle/1721.1/104427/6-042j-spring-2005/contents/recitations/rec12.pdf), 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](https://dl.acm.org/doi/10.1145/356827.356832) 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 $$a\_n=c\_03^n+c\_12^n$$, as stated in the book. Furthermore, we have

$$
\begin{align\*}
a\_0&=c\_0+c\_1 \\
a\_1&=3c\_0+2c\_1.
\end{align\*}
$$

We need to work backwards to find initial conditions resulting in $$c\_0=0$$ and $$c\_1=1$$. We immediately get $$a\_0=1$$ and $$a\_1=2$$.

***

There are no initial conditions for which the solution is $$a\_n=2^n-1$$. Irrespectively of boundary conditions, $$a\_n$$ must satisfy the recurrence, which isn’t the case here.

## Exercise 2.28

{% hint style="danger" %}
The exercise from the book mentions $$a\_n=2a\_{n-1}-a\_{n-2}+2a\_{n-3}$$ for $$n>2$$. The characteristic equation has two complex roots, so the general solution is $$a\_n = c\_02^n + c\_1i^n + c\_2(-i)^n$$. However, there is no way to set initial conditions for $$a\_n$$ to be constant, except trivially being zero, which is only a special case. So, we assume that this exercise is about the recurrence given in the main text.
{% endhint %}

By tweaking the initial conditions, we can fundamentally alter the growth rate of the solution to

$$
a\_n=2a\_{n-1}+a\_{n-2}-2a\_{n-3} \space\space\space\space \text{ for $n>2$}.
$$

We use the same approach as in the previous exercise. Here are the initial conditions leading to each desired growth rate of $$a\_n$$:

* **Constant** is achieved when $$a\_0=a\_1=a\_2=c\_0$$. For example, $$a\_0=a\_1=a\_2=2$$ makes $$a\_n=2$$.
* **Exponential** is achieved when $$a\_0=c\_2$$, $$a\_1=2c\_2$$ and $$a\_2=4c\_2$$. For example, $$a\_0=1$$, $$a\_1=2$$ and $$a\_2=4$$ makes $$a\_n=2^n$$.
* **Fluctuating in sign** is achieved when  $$a\_0=c\_1$$, $$a\_1=-c\_1$$ and $$a\_2=c\_1$$. For example, $$a\_0=1$$, $$a\_1=-1$$ and $$a\_2=1$$ makes $$a\_n=(-1)^n$$.

## Exercise 2.29

The characteristic equation is $$z^2-2z-4=0$$ whose roots are $$z\_1=1+\sqrt5$$ and $$z\_2=1-\sqrt5$$. Therefore, the general solution is $$a\_n=c\_0(1+\sqrt 5)^n+c\_1(1-\sqrt5)^n$$ for $$n>1$$. We also have the following system of linear equations:

$$
\begin{align\*}
a\_0&=1=c\_0+c\_1 \\
a\_1&=2=c\_0(1+\sqrt 5)+c\_1(1-\sqrt 5).
\end{align\*}
$$

The constants are $$c\_0=\frac{\sqrt5+1}{2\sqrt5}$$ and $$c\_1=\frac{\sqrt5-1}{2\sqrt5}$$.

## Exercise 2.30

The characteristic equation is $$z^2-2z+1=0$$ whose double root is $$z\_1=1$$. Therefore, the general solution is $$a\_n=c\_0+c\_1n$$ for $$n>1$$.&#x20;

***

We also have the following system of linear equations:

$$
\begin{align\*}
a\_0&=0=c\_0\\
a\_1&=1=c\_0+c\_1.
\end{align\*}
$$

The constants are $$c\_0=0$$ and $$c\_1=1$$.

***

If we change the initial conditions, then we get another system of linear equations:

$$
\begin{align\*}
a\_0&=1=c\_0\\
a\_1&=1=c\_0+c\_1.
\end{align\*}
$$

The constants are $$c\_0=1$$ and $$c\_1=0$$.

## Exercise 2.31 🌟

{% hint style="success" %}
This exercise illustrates how to handle complex roots and transform them into trigonometric form.
{% endhint %}

The characteristic equation is $$z^2-z+1=0$$ whose complex roots are $$z\_1=(1+\sqrt3i)/2$$ and $$z\_2=(1-\sqrt3i)/2$$. We could continue as usual, but the expression for $$a\_n$$ would be awkward. Let’s use [Euler’s formula](https://en.wikipedia.org/wiki/Euler%27s_formula) to convert complex numbers into trigonometric form. The roots can be represented as $$e^{±i\frac{\pi}{3}}=\cos(\frac{\pi}{3})±i\sin(\frac{\pi}{3})$$. Thankfully to Euler’s formula, exponentiation becomes trivial. The general solution is

$$
\begin{align\*}
a\_n&=c\_0e^{in\frac{\pi}{3}}+c\_1e^{-in\frac{\pi}{3}}\\
&=(c\_0+c\_1)\cos\left(n\frac{\pi}{3}\right)+i(c\_0-c\_1)\sin\left(n\frac{\pi}{3}\right) \\
&=c'\_0\cos\left(n\frac{\pi}{3}\right)+c'\_1\sin\left(n\frac{\pi}{3}\right).
\end{align\*}
$$

Since the sequence $$a\_n$$ is real (it starts with real numbers $$a\_0, a\_1$$ and has real coefficients), the constants $$c\_0'$$ and $$c\_1'$$ must be reals, too. Consequently, the constants $$c\_0$$ and $$c\_1$$ must be complex conjugates. We also have the following system of linear equations:

$$
\begin{align\*}
a\_0 &=0= c'\_0 \cos(0) + c'\_1 \sin(0) \\
a\_1&=1=c'\_0\cos\left(\frac{\pi}{3}\right) + c'\_1 \sin\left(\frac{\pi}{3}\right).
\end{align\*}
$$

The constants are $$c'\_0=0$$ and $$c'\_1=2/\sqrt3$$. Thus,

$$
a\_n=\frac{2}{\sqrt3}sin\left(n\frac{\pi}{3}\right) \space\space\space\space\text{ for $n>1$}.
$$

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 $$2z^3 - 3z^2 + 3z - 1 = 0$$. 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](https://en.wikipedia.org/wiki/Rational_root_theorem) enumerates all rational candidates. It turns out that ½ is a root, thus $$(2z-1)$$ is a factor.

Using a classical polynomial division algorithm, we get that the characteristic equation in factored form is

$$
(z^2 - z + 1)(2z-1)=0.
$$

The other two complex roots are $$z\_{2,3} = \frac{1}{2} ± i\frac{\sqrt{3}}{2}$$. Using the same transformation as in the previous exercise, we can write down the general solution as

$$
a\_n = c\_0 \left(\frac{1}{2}\right)^n + c\_1 \cos\left(\frac{n\pi}{3}\right) + c\_2 \sin\left(\frac{n\pi}{3}\right).
$$

We also have the following system of linear equations:

$$
\begin{align\*}
a\_0 &= 0=c\_0 + c\_1 \\
a\_1&=1 = \frac{1}{2} c\_0 + \frac{1}{2} c\_1 + \frac{\sqrt{3}}{2} c\_2 \\
a\_2&=2 = \frac{1}{4} c\_0 - \frac{1}{2} c\_1 + \frac{\sqrt{3}}{2} c\_2.
\end{align\*}
$$

The constants are $$c\_0=4/3$$, $$c\_1=-4/3$$ and $$c\_2= 2/\sqrt{3}$$.

## Exercise 2.33

Let’s split the problem into two subproblems based on parity of $$n>1$$

$$
a\_n=\begin{cases}
2a\_{n-2} &\text{if $n$ is even,} \\
\cfrac{1}{2}a\_{n-2} &\text{if $n$ is odd}.
\end{cases}
$$

We can combine both cases into a single recurrence $$a\_n=2^{(-1)^n}a\_{n-2}$$ for $$n>1$$. The initial conditions are $$a\_0=a\_1=1$$.

## Exercise 2.34

The sequence is known as [Tribonacci numbers](https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Tribonacci_numbers). The characteristic equation is $$z^3-z^2-z-1=0$$, whose single real root is the Tribonacci constant $$\alpha \approx 1.839286755.$$ The other two complex roots $$\beta$$ and $$\gamma$$ have magnitudes less than one, hence they tend to zero for large $$N$$. Therefore, the approximate general solution is

$$
\boxed{F\_N^{(3)} \approx c\_0 \alpha^N}.
$$

{% hint style="info" %}
Tools are useful in finding roots of the characteristic polynomial. For example, issuing `x^3-x^2-x-1=0` in WolframAlpha gives three solutions (one real and two complex). Switching to trigonometric form reveals the magnitudes of complex roots.
{% endhint %}

#### Finding the Unknown Constant

To find $$c\_0$$ we need to use the exact general solution

$$
F\_N^{(3)} = c\_0 \alpha^N + c\_1 \beta^N + c\_2 \gamma^N.
$$

We have the following system of linear equations:

$$
\begin{align\*}
0& = c\_0 + c\_1 + c\_2 \\
0 &= c\_0 \alpha + c\_1 \beta + c\_2 \gamma \\
1 &= c\_0 \alpha^2 + c\_1 \beta^2 + c\_2 \gamma^2.
\end{align\*}
$$

Let’s solve for $$c\_0$$ by running the next Sage script:

```
var('c0 c1 c2 alpha beta gamma')

eq1 = 0 == c0 + c1 + c2
eq2 = 0 == c0 * alpha + c1 * beta + c2 * gamma
eq3 = 1 == c0 * alpha^2 + c1 * beta^2 + c2 * gamma^2

sol = solve([eq1, eq2, eq3], [c0, c1, c2], solution_dict=True)
c0 = sol[0][c0]
show(c0.factor())
```

It outputs

$$
c\_0 = \frac{1}{(\alpha - \beta)(\alpha - \gamma)}.
$$

The characteristic polynomial is $$P(z)=z^3-z^2-z-1=(z-\alpha)(z-\beta)(z-\gamma)$$. Let’s take the derivative of $$P(z)$$ on both sides and make them equal for $$\alpha$$. On the left we have

$$
\begin{align\*}
P'(z) &= \frac{d}{dz} \[z^3-z^2-z-1] \\
&= 3z^2-2z-1 \\
&\therefore P'(\alpha)= 3{\alpha}^2 - 2{\alpha} - 1
\end{align\*}
$$

On the right we get

$$
\begin{align\*}
P'(z) &= \frac{d}{dz} \[(z-\alpha)(z-\beta)(z-\gamma)] \\
&= (z-\beta)(z-\gamma) + (z-\alpha)(z-\gamma) + (z-\alpha)(z-\beta) \\
&\therefore P'(\alpha) = (\alpha-\beta)(\alpha-\gamma).
\end{align\*}
$$

Consequently, $$(\alpha-\beta)(\alpha-\gamma)=3{\alpha}^2 - 2{\alpha} - 1$$, which gives us the value of $$c\_0$$ in terms of $$\alpha$$

$$
c\_0 =  \frac{1}{3\alpha^2 - 2\alpha - 1}.
$$

#### Estimating the Accuracy

We can calculate the exact and approximate value of $$F\_{20}^{(3)}$$ using the following Python script.

```python
a = b = 0; c = 1
n = 20
for _ in range(n - 2):
    a, b, c = b, c, a + b + c

a = 1.839286755
c0 = 1 / (3 * a**2 - 2 * a - 1)
print(f"{c} ~ {c0 * a**20}")
```

It outputs `35890 ~ 35889.999620092305`. They are indeed very close.

## Exercise 2.35

Let’s define a new sequence $$b\_n = n! \cdot a\_n \implies a\_n = \frac{b\_n}{n!}$$. If we substitute this back into the main recurrence, we get

$$
\begin{align\*}n(n - 1) \cdot \frac{b\_n}{n!} &= (n - 1) \cdot \frac{b\_{n - 1}}{(n - 1)!} + \frac{b\_{n - 2}}{(n - 2)!} \\
\frac{b\_n}{(n-2)!} &= \frac{b\_{n-1}}{(n-2)!} + \frac{b\_{n - 2}}{(n - 2)!}  \       &\therefore \boxed{b\_n = b\_{n - 1} + b\_{n - 2}} && \text{(for $n>1$)}.\end{align\*}
$$

The initial conditions are $$b\_1=b\_0=1$$, thus $$b\_n=F\_{n+1}$$. Therefore,

$$
a\_n=\frac{F\_{n+1}}{n!}.
$$

## 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 $$\Theta(p\lg p + q\lg q)$$.

```python
def is_valid_monomial(s, t, n):
    """
    This function checks if a given monomial, represented by two
    lists s and t, appears in the expansion of a[n], for n > 1.
    The function returns True if the monomial is valid, otherwise False.
    """
    assert(n > 1)

    # Insert sentinel values to avoid index errors.
    s.append(0)
    t.append(0)

    i = len(s) - 1
    j = len(t) - 1

    s.sort()
    t.sort()

    while n > 1:
        # Simple reduction rules:
        # 1. If the largest element is in s,
        #    then the sub-monomial comes from the previous iteration.
        # 2. If the largest element is in t,
        #    then the sub-monomial comes from two iterations back.
        # The premise is that the current monomial is valid
        # iff built from a valid sub-monomial. Easily provable by induction.
        if s[i] > t[j]:
            if s[i] != n - 1:
                return False
            i -= 1
            n -= 1
        else:
            if t[j] != n - 2:
                return False
            j -= 1
            n -= 2

    return n > 0 and i == j == 0
```

In the expansion of $$a\_n$$, for $$n>1$$, we have $$F\_n-1-\frac{1-(-1)^n}{2}$$ mixed monomials (consisted of both symbols $$s\_i$$ and $$t\_j$$). The total number of such monomials, across all iterations, is

$$
\begin{align\*}
\sum\_{k=2}^n\left(F\_k-1-\frac{1-(-1)^k}{2}\right) &= F\_{n+2}-F\_3-(n-1)-\bigg\lfloor\frac{n-1}{2}\bigg\rfloor \\
&= F\_{n+2}-n-1-\bigg\lfloor\frac{n-1}{2}\bigg\rfloor.
\end{align\*}
$$

For example, when $$n=6$$ this formula evaluates to 12, which matches the number of mixed monomials listed in the book.&#x20;

{% hint style="info" %}
If we count all monomials, including initial values, then the formula for their total number is $$F\_{n+2}-1$$.
{% endhint %}

## Exercise 2.37

Solving $$b\_n$$ proceeds along the same steps as in [Exercise 2.29](#exercise-2.29) and won’t be repeated here. The exact solution is

$$
b\_n=\frac{2}{3}\left(1-\left(-\frac{1}{2}\right)^n\right) \space\space\space\space\text{ for $n>1$},
$$

and $$a\_n=2^{b\_n}$$ for $$n>1$$.

## Exercise 2.38

Let’s square both sides of the initial recurrence to get $$a\_n^2=1+a\_{n-1}^2$$ for $$n>0$$. If we introduce a substitution $$b\_n=a\_n^2$$ then the recurrence transforms into $$b\_n=1+b\_{n-1}$$, thus $$b\_n=n$$. Therefore, $$a\_n=\sqrt n$$ for $$n>0$$.

## Exercise 2.39

For $$a\_0=3$$ the solution is

$$
a\_n = \left(\frac{3 + \sqrt{5}}{2}\right)^{2^n} + \left(\frac{3 - \sqrt{5}}{2}\right)^{2^n}.
$$

As the book notes, only the larger of the two roots predominates in this expression—the one with the plus sign.

***

For $$a\_0=4$$ the solution is

$$
a\_n = (2 + \sqrt{3})^{2^n} + (2 - \sqrt{3})^{2^n}.
$$

Again, only the larger of the two roots predominates in this expression—the one with the plus sign.

***

For $$a\_0=3/2$$ the solution is fundamentally different. The roots $$b\_0$$ are complex numbers

$$
b\_0=\frac{1}{2}\left(\frac{3}{2} ± \sqrt{-7/4}\right) = \frac{3}{4} ± i\frac{\sqrt{7}}{4}.
$$

We can use the same approach as in [Exercise 2.31](#exercise-2.31) to transform these into trigonometric form. Let $$b\_0=e^{±i\theta}$$, where $$\theta=\arccos(3/4)$$, thus the recurrence becomes

$$
\begin{align\*}
a\_n &= (e^{i\theta})^{2^n} + (e^{-i\theta})^{2^n} \\
&= e^{i(2^n\theta)} + e^{-i(2^n\theta)} \\
&= 2\cos(2^n\theta) && \text{($e^{ix} + e^{-ix} = 2\cos(x)$ because $\sin(-x)=-\sin(x)$)} .
\end{align\*}
$$

The sequence $$a\_n$$ fluctuates between -2 and 2, never settling on a single value.

## Exercise 2.40 🌟

{% hint style="success" %}
This exercise introduces the [bifurcation theory](https://en.wikipedia.org/wiki/Bifurcation_theory); as demonstrated in the previous exercise, a fundamentally different behavior occurs around the bifurcation point $$a\_0=2$$.&#x20;
{% endhint %}

The accurate approximate answer for large $$\epsilon$$ and sufficiently large $$n$$ is $$a\_n \approx a\_0^{2^n}$$. Usually, in expressions like $$a\_0=2+\epsilon$$, $$\epsilon$$ is a small positive constant, so the subsequent analysis assumes this situation. The critical term is&#x20;

$$
\begin{align\*}
\sqrt{a\_0^2 - 4} &= \sqrt{(a\_0-2)(a\_0+2)} \\
&= \sqrt{\epsilon(4 + \epsilon)} \\
&\approx 2\sqrt{\epsilon} && \text{($\epsilon \ll 1 \implies \epsilon \gg \epsilon^2$)}.
\end{align\*}
$$

For the larger root we have

$$
\begin{align\*}
b\_0^+ &= \frac{1}{2}(a\_0 + \sqrt{a\_0^2 - 4}) \\
&\approx \frac{1}{2}( (2 + \epsilon) + 2\sqrt{\epsilon}) \\
&= 1 + \frac{\epsilon}{2} + \sqrt{\epsilon} \\
&\approx 1 + \sqrt{\epsilon} && \text{($\epsilon \ll 1 \implies \sqrt{\epsilon} \gg \epsilon$)}.
\end{align\*}
$$

For the smaller root we have

$$
\begin{align\*}
b\_0^- &= \frac{1}{2}(a\_0 - \sqrt{a\_0^2 - 4}) \\
&\approx \frac{1}{2}( (2 + \epsilon) - 2\sqrt{\epsilon}) \\
&= 1 + \frac{\epsilon}{2} - \sqrt{\epsilon} \\
&\approx 1 - \sqrt{\epsilon}.
\end{align\*}
$$

As $$n$$ increases, $${(b\_0^-)}^{2^n}$$ will rapidly vanish to 0. Consequently, we get $$a\_n \approx (1 + \sqrt{\epsilon})^{2^n}$$. This shows that even for an infinitesimally small $$\epsilon$$, the sequence $$a\_n$$ diverges to infinity at a double-exponential rate.

## Exercise 2.41 🌟

{% hint style="success" %}
This exercise shows a mechanism of transforming generic quadratic recurrences into the register allocation type recurrence, whose solution space is known.&#x20;
{% endhint %}

{% hint style="warning" %}
Assume that $$f \neq 0$$. Otherwise, we can define $$g=2$$ and all sequences $$a\_n$$ would "turn" into $$b\_n=b\_{n-1}^2-2$$ by using the transformation $$b\_n=0\cdot a\_n+g$$.
{% endhint %}

We expand $$b\_n$$ to match parameters

$$
\begin{align\*}
b\_n &= b\_{n-1}^2 - 2 \\
&= (f a\_{n-1} + g)^2 - 2 && \text{($b\_{n-1}=f a\_{n-1} + g$)} \\
&= f^2 a\_{n-1}^2 + 2fg a\_{n-1} + g^2 - 2.
\end{align\*}
$$

Let’s express $$a\_n$$ in terms of $$f$$ and $$g$$ starting with $$b\_n=f a\_n + g$$

$$
\begin{align\*}
f a\_n + g &= f^2 a\_{n-1}^2 + 2fg a\_{n-1} + g^2 - 2 \\
f a\_n &= f^2 a\_{n-1}^2 + 2fg a\_{n-1} + (g^2 - g - 2) \\
a\_n &= \left(\frac{f^2}{f}\right) a\_{n-1}^2 + \left(\frac{2fg}{f}\right) a\_{n-1} + \left(\frac{g^2 - g - 2}{f}\right) \\
&= \boxed{f a\_{n-1}^2 + 2g a\_{n-1} + \frac{g^2 - g - 2}{f}}.
\end{align\*}
$$

We now match the coefficients of this result with the given recurrence $$a\_n = \alpha a\_{n-1}^2 + \beta a\_{n-1} + \gamma$$ and derive the necessary and sufficient condition for transforming $$a\_n$$ into $$b\_n=b\_{n-1}^2-2$$:

* $$\alpha = f$$
* $$\beta = 2g \implies g=\beta/2$$
* $$\gamma = \frac{g^2 - g - 2}{f}=\frac{\beta^2/4 - \beta/2 - 2}{\alpha} \implies \boxed{4\alpha\gamma = \beta^2 - 2\beta - 8}$$.

***

For $$a\_n = a\_{n-1}^2 + 1$$ the parameters are $$\alpha=1$$, $$\beta=0$$ and $$\gamma=1$$. Clearly

$$
4\alpha\gamma =4 \bold{\neq} \beta^2 - 2\beta - 8=-8.
$$

This shows that it doesn’t reduce to the target form.

## Exercise 2.42

We need to solve the recurrence $$a\_n=2a\_{n-1} \sqrt{1-a\_{n-1}^2}$$ for $$n>0$$ and different initial values. Let’s square both sides and use the substitution $$b\_n=a\_n^2$$. We get

$$
\begin{align\*}
a\_n^2&=4a\_{n-1}^2(1-a\_{n-1}^2) \\
&\implies b\_n=4b\_{n-1}(1-b\_{n-1})=\boxed{-4b\_{n-1}^2+4b\_{n-1}}.
\end{align\*}
$$

The idea is to leverage the result from the previous exercise and transform $$b\_n$$ into $$c\_n=c\_{n-1}^2-2$$. The parameters are $$\alpha=-4=f$$, $$\beta=4=2g$$ and $$\gamma=0$$. Since $$4 \cdot (-4) \cdot 0=4^2-2 \cdot 4-8$$, we can apply the transformation, thus $$c\_n=-4b\_n+2$$.

***

If $$a\_0=1/2$$, then $$b\_0=1/4$$, hence $$c\_0=1$$. We already know the solution from the book for this special case, namely $$c\_n=-1$$. Therefore, $$b\_n=3/4$$ which implies that $$a\_n=\sqrt 3/2$$ for $$n>0$$.

***

If $$a\_0=1/3$$, then $$b\_0=1/9$$, thus $$c\_0=14/9$$. We hit the third case in [Exercise 2.39](#exercise-2.39), so the solution is $$c\_n=2\cos(2^n\theta)$$, where $$\theta=\arccos(7/9)$$. Therefore, $$b\_n=(1-\cos(2^n\theta))/2$$, which implies that $$a\_n=\sqrt {(1-\cos(2^n\theta))/2}$$.

***

Here’s the Python script to plot $$a\_6$$ as a function of $$a\_0$$.

{% code expandable="true" %}

```python
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt

def a(a0):
    x = a0
    for _ in range(6):
        x = 2 * x * sqrt(1 - x * x)
    return x

def plot_a(num_points=1000, save_path="a_plot.png",
           label_fontsize=18, tick_fontsize=14, title_fontsize=20):
    xs = np.linspace(0.0, 1.0, num_points)
    ys = [a(float(x)) for x in xs]

    plt.figure(figsize=(8, 5))
    plt.plot(xs, ys, marker='o', linestyle='-', markersize=4)
    plt.grid(True)
    plt.xlabel(r"$a_0$", fontsize=label_fontsize)
    plt.ylabel(r"$a_6$", fontsize=label_fontsize)
    plt.title(r"Values of $a_6$ for $a_0$ on the interval $[0,1]$",
              fontsize=title_fontsize)
    plt.xticks(fontsize=tick_fontsize)
    plt.yticks(fontsize=tick_fontsize)
    plt.tight_layout()
    plt.savefig(save_path, dpi=150)
    plt.show()

if __name__ == "__main__":
    plot_a()
```

{% endcode %}

<figure><img src="/files/uBNddxADIszcphrkqho1" alt=""><figcaption><p>A change in the initial condition <span class="math">a_0</span> has an unpredictable effect on <span class="math">a_6</span>. This is especially evident as <span class="math">a_0</span> approaches 1. At large scale, we see some regularities. These traits are typical for dynamic systems investigated by the <a href="https://en.wikipedia.org/wiki/Chaos_theory">chaos theory</a>.</p></figcaption></figure>

## Exercise 2.43 🌟

{% hint style="success" %}
This exercise shows a way to express general classes of “continued fraction” representations as solutions to recurrences.
{% endhint %}

Let’s represent the recurrence as a function $$f(x) = (\alpha x+\beta)/(\gamma x+\delta)$$, where $$\gamma \neq 0$$ (otherwise, we won’t have a continued fraction). This function defines a [Möbius transformation](https://en.wikipedia.org/wiki/M%C3%B6bius_transformation). We first need to [determine the fixed points](https://en.wikipedia.org/wiki/M%C3%B6bius_transformation#Determining_the_fixed_points) denoted by $$r$$ and $$s$$. Afterward we can leverage a change of variable to linearize the original recurrence.

#### Distinct Roots

$$
\begin{align\*}
b\_n &= \frac{a\_n - r}{a\_n - s} \\\[0.3cm]
&= \frac{f(a\_{n-1}) - r}{f(a\_{n-1}) - s} \\\[0.3cm]
&= \frac{\cfrac{\alpha a\_{n-1}+\beta}{\gamma a\_{n-1}+\delta}-r}{\cfrac{\alpha a\_{n-1}+\beta}{\gamma a\_{n-1}+\delta}-s} \\\[0.9cm]
&= \frac{\cfrac{\alpha a\_{n-1}+\beta-r\gamma a\_{n-1}-r\delta}{\gamma a\_{n-1}+\delta}}{\cfrac{\alpha a\_{n-1}+\beta-s\gamma a\_{n-1}-s\delta}{\gamma a\_{n-1}+\delta}} \\\[0.9cm]
&= \frac{\alpha a\_{n-1}+\beta-r\gamma a\_{n-1}-r\delta}{\alpha a\_{n-1}+\beta-s\gamma a\_{n-1}-s\delta} && \text{(assuming $\gamma a\_{n-1}+\delta \neq 0$)}\\\[0.3cm]
&= \frac{\alpha \cfrac{r-sb\_{n-1}}{1-b\_{n-1}}+\beta-r\gamma \cfrac{r-sb\_{n-1}}{1-b\_{n-1}}-r\delta}{\alpha \cfrac{r-sb\_{n-1}}{1-b\_{n-1}}+\beta-s\gamma \cfrac{r-sb\_{n-1}}{1-b\_{n-1}}-s\delta} && \text{$\left(a\_{n-1}=\cfrac{r-sb\_{n-1}}{1-b\_{n-1}}\right)$}\\\[0.9cm]
&= \frac{\cfrac{\alpha r - \alpha sb\_{n-1} +\beta - \beta b\_{n-1} -r^2\gamma + r\gamma sb\_{n-1} -r\delta +r\delta b\_{n-1}}{1-b\_{n-1}}}
{\cfrac{\alpha r - \alpha sb\_{n-1} +\beta - \beta b\_{n-1} -sr\gamma + s^2\gamma b\_{n-1} -s\delta +s\delta b\_{n-1}}{1-b\_{n-1}}} \\\[0.9cm]
&= \frac{\alpha r - \alpha sb\_{n-1} +\beta - \beta b\_{n-1} -r^2\gamma + r\gamma sb\_{n-1} -r\delta +r\delta b\_{n-1}}
{\alpha r - \alpha sb\_{n-1} +\beta - \beta b\_{n-1} -sr\gamma + s^2\gamma b\_{n-1} -s\delta + s\delta b\_{n-1}} && \text{(assuming $b\_{n-1} \neq 1$)} \\\[0.3cm]
&= \frac{(-s\alpha - \beta + rs\gamma +r\delta) b\_{n-1} -(r^2\gamma + r\delta -r\alpha-\beta)}
{r\alpha +\beta -sr\gamma -s\delta + (s^2\gamma + s\delta -s\alpha  - \beta)b\_{n-1}} \\\[0.3cm]
&= \frac{-s\alpha - \beta + rs\gamma +r\delta}
{r\alpha +\beta -sr\gamma -s\delta} \cdot  b\_{n-1} && \text{($r$ and $s$ are roots of the quadratics)} \\\[0.3cm]
&= \frac{\gamma s + \delta}{\gamma r + \delta} \cdot b\_{n-1} && \text{($-s\alpha - \beta=-s^2\gamma - s\delta$ and $r\alpha +\beta=r^2\gamma + r\delta$)}\\\[0.3cm]
&= \left(\frac{\gamma s + \delta}{\gamma r + \delta}\right)^n b\_0 && \text{(iterating)}.
\end{align\*}
$$

The initial value is $$b\_0=(a\_0-r)/(a\_0-s)=(1-r)/(1-s)$$. If we substitute $$b\_0$$ into the expression for $$a\_n$$, assuming $$s \neq 1$$, we get

$$
\boxed{a\_n=\frac{r(1-s) - s(1-r)\left(\cfrac{\gamma s+\delta}{\gamma r+\delta}\right)^n}{(1-s) - (1-r)\left(\cfrac{\gamma s+\delta}{\gamma r+\delta}\right)^n}} \space\space\space\space \text{ for $n>0$}.
$$

{% hint style="info" %}
As a quick check, we should get the closed-form solution for the continued fraction example from the book by using $$\alpha=0,\beta=1,\gamma=1$$ and $$\delta=1$$. The roots of the equation $$x^2+x-1=0$$ are $$r=-\phi$$ and $$s=-\hat \phi$$. If we substitute these values into the formula for $$a\_n$$, and use the properties of the [golden ratio](https://en.wikipedia.org/wiki/Golden_ratio), then we get  $$a\_n=F\_{n+1}/F\_{n+2}$$ for $$n \ge 0$$.
{% endhint %}

{% hint style="danger" %}
The book erroneously states that $$b\_n=F\_{n+1}$$ instead of $$b\_n=F\_{n+2}$$. The correct initial conditions are $$b\_0=1$$ and $$b\_1=2$$.
{% endhint %}

#### Repeated Roots

When a Möbius transformation has a single, repeated fixed point $$r=s$$, it is classified in dynamical systems as a parabolic transformation. We need to use a different change of variable

$$
b\_n = \frac{1}{a\_n - r}.
$$

Without repeating a similar grueling algebra, the outcome is

$$
b\_n = b\_{n-1} +  \frac{2\gamma}{\alpha + \delta} \implies b\_n= b\_0 + n\frac{2\gamma}{\alpha + \delta}.
$$

Flip the substitution back to get the closed form for $$a\_n$$.

## Exercise 2.44 🌟

{% hint style="success" %}
This exercise demonstrates the use of substitution to simplify sequences. Calculating values directly is often difficult and can result in accuracy loss over time. By using an integer sequence, exactness is maintained, allowing any value to be expressed as a rational number at each step.
{% endhint %}

Let $$a\_n=b\_n/b\_{n+1}$$ for $$n\ge0$$. We need to specify a linear recurrence relation on $$b\_n$$ such that $$a\_n$$ also satisfies the original non-linear recurrence. We have

$$
\begin{align\*}
a\_n &= \frac{1}{s\_n + t\_n a\_{n-1}}  \\
\cfrac{b\_n}{b\_{n+1}} &= \frac{1}{s\_n + t\_n \cfrac{b\_{n-1}}{b\_n}} \\
\cfrac{b\_n}{b\_{b+1}} &= \cfrac{1}{\cfrac{s\_n b\_n + t\_n b\_{n-1}}{b\_n}} \\
\cfrac{b\_n}{b\_{n+1}} &= \cfrac{b\_n}{s\_n b\_n + t\_n b\_{n-1}} \\
&\therefore \boxed{b\_{n+1} = s\_n b\_n + t\_n b\_{n-1}}.
\end{align\*}
$$

The initial conditions must satisfy

$$
\begin{align\*}
a\_0 &= \frac{b\_0}{b\_1} \\
1 &= \frac{b\_0}{b\_1} \\
&\implies b\_1=b\_0
\end{align\*}
$$

We can choose any starting value for $$b\_0$$.

{% hint style="info" %}
As a quick check, we should get the closed-form solution for the continued fraction example from the book by using $$b\_0=1,s\_n=1$$ and $$t\_n=1$$. Indeed, $$b\_n=F\_{n+1}$$ implying $$a\_n=F\_{n+1}/F\_{n+2}$$ for $$n \ge 0$$.
{% endhint %}

## Exercise 2.45 🌟

{% hint style="success" %}
This exercise replaces the bad example in the book and demonstrates how a repertoire table streamlines analysis by allowing reuse of many entries.&#x20;
{% endhint %}

We are given a relaxed quicksort recurrence

$$
a\_n=f(n)+\frac{2}{n} \sum\_{1 \le j \le n} a\_{j-1} \space\space\space\space \text{ for $n>0$ with $a\_0=0$}.
$$

Let’s solve it when $$f(n)=n^3$$.&#x20;

***

The repertoire table from the book has many reusable entries except for the $$n^3$$ term. If we set $$a\_n=n^3$$ then  $$a\_n-(2/n)\sum\_{j=1}^n a\_{j-1}=n^3/2+n^2-n/2$$. We need to combine entries from the expanded table to get $$f(n)=n^3$$, thus we have

$$
n^3=2 \left(\frac{n^3}{2} +n^2 - \frac{n}{2}\right) - 6 \left(\frac{n^2 - 1}{3}+ n - 1\right) + 14\left(\frac{n - 1}{2}+H\_n\right) + 14(-H\_n+2) - 29 \cdot 1.
$$

Therefore, the solution is

$$
a\_n=2n^3-6n(n-1)+14(n+1)H\_n-29n \space\space\space\space\text{ for $n\ge0$}.
$$

## Exercise 2.46

The [post](https://math.stackexchange.com/a/1023510/753413) on *StackExchange for Mathematics* explains the basics of the repertoire method and illustrates its application by solving this exercise.

## Exercise 2.47 🌟

{% hint style="success" %}
The solution of this problem illustrates the basic form of the bootstrapping method.
{% endhint %}

Let’s use the next Python script to calculate numerical values of $$a\_n$$, alongside our guess that $$a\_n\sim2/n$$. This hypothesis is based on the observation that $$a\_{n-1} \to 0$$ as $$n \to \infty$$.

```
a = 1
for n in range(1, 11):
    a = 2/(n+a)
    print(f"a({n})={a}, â({n})={2/n}")
```

The output shows that we’re on the right track.

```
a(1)=1.0, â(1)=2.0
a(2)=0.6666666666666666, â(2)=1.0
a(3)=0.5454545454545455, â(3)=0.6666666666666666
a(4)=0.43999999999999995, â(4)=0.5
a(5)=0.36764705882352944, â(5)=0.4
a(6)=0.3140877598152425, â(6)=0.3333333333333333
a(7)=0.2734449005367856, â(7)=0.2857142857142857
a(8)=0.2417372719639722, â(8)=0.25
a(9)=0.2164095278998315, â(9)=0.2222222222222222
a(10)=0.19576349152197078, â(10)=0.2
```

If we employ bootstrapping and substitute our guess back into the recurrence, we get

$$
a\_n \sim \frac{2(n-1)}{n^2-n+2}=\frac{2n}{n^2-n+2}+O(1/n^2).
$$

The new version of the script reflects this change.

```
a = 1
for n in range(1, 11):
    a = 2/(n+a)
    print(f"a({n})={a}, â({n})={2*(n-1)/(n*n-n+2)}")
```

The results are better aligned as $$n$$ increases.

```
a(1)=1.0, â(1)=0.0
a(2)=0.6666666666666666, â(2)=0.5
a(3)=0.5454545454545455, â(3)=0.5
a(4)=0.43999999999999995, â(4)=0.42857142857142855
a(5)=0.36764705882352944, â(5)=0.36363636363636365
a(6)=0.3140877598152425, â(6)=0.3125
a(7)=0.2734449005367856, â(7)=0.2727272727272727
a(8)=0.2417372719639722, â(8)=0.2413793103448276
a(9)=0.2164095278998315, â(9)=0.21621621621621623
a(10)=0.19576349152197078, â(10)=0.1956521739130435
```

After an additional iteration we have

$$
a\_n \sim \frac{2 (n^2 - 3 n + 4)}{(n-1)(n^2 - 2 n + 4)}=\frac{2n^2}{(n-1)(n^2 - 2 n + 4)}-\frac{6n}{(n-1)(n^2 - 2 n + 4)}+O(1/n^3) \quad \text{ for $n>1$}.
$$

We see that the approximation of $$a\_n$$ is improved again.

```
a(2)=0.6666666666666666, â(2)=1.0
a(3)=0.5454545454545455, â(3)=0.5714285714285714
a(4)=0.43999999999999995, â(4)=0.4444444444444444
a(5)=0.36764705882352944, â(5)=0.3684210526315789
a(6)=0.3140877598152425, â(6)=0.3142857142857143
a(7)=0.2734449005367856, â(7)=0.27350427350427353
a(8)=0.2417372719639722, â(8)=0.24175824175824176
a(9)=0.2164095278998315, â(9)=0.21641791044776118
a(10)=0.19576349152197078, â(10)=0.19576719576719576
```

This process may continue depending on a desired accuracy level.

{% hint style="info" %}
There’s no simple closed-form solution. The standard method to "solve" it is based on the technique from [Exercise 2.44](#exercise-2.44). In this case, the substitution should be $$a\_n=2b\_{n-1}/b\_n$$ for $$n\ge1$$, that yields $$b\_n=s\_nb\_{n-1}+2t\_nb\_{n-2}$$, where $$s\_n=n$$ and $$t\_n=1$$. The initial values are $$b\_0=1$$ and $$b\_1=2$$.
{% endhint %}

## 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

$$
C\_N = N+1 + \frac{2}{\binom{N}{3}} \sum\_{k=1}^{N} (k-1)(N-k) C\_{k-1} \space\space\space\space \text{ for $N>2$}.
$$

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.

{% stepper %}
{% step %}

### Converting the Discrete Recurrence

Let’s simplify the constant multiplier of the sum for large enough $$N$$

$$
\begin{align\*}
\frac{2}{\binom{N}{3}} &= \frac{2}{\cfrac{N(N-1)(N-2)}{6}} \\
&\sim \frac{2}{N^3/6} \\
&= \frac{12}{N^3}.
\end{align\*}
$$

Let’s set the pivot’s rank $$k$$ relative to the size $$N$$; for this we use the continuous variable $$x = k/N \implies k = xN \implies dk = N dx$$. Finally, we treat $$k-1 \sim k$$ and $$N+1 \sim N$$. If we substitute these changes into our original expression and replace the summation with an integral, we have

$$
C\_N \sim N+ 12 \int\_{0}^{1} (x-x^2) C\_{xN} ,dx.
$$
{% endstep %}

{% step %}

### Bootstrapping the Solution

Our initial hypothesis is $$C\_N \sim \alpha N \ln N$$. After all, we expect to have a familiar asymptotic form $$\Theta(N \ln N)$$ as other quicksort variants. Let’s substitute this hypothesis into our recurrence relation

$$
\begin{align\*}
C\_N &\approx N + 12 \int\_{0}^{1} C\_{xN} (x-x^2) ,dx \\
&\approx N + 12 \int\_{0}^{1} \alpha (xN) \ln(xN) (x-x^2) ,dx \\
&= N + 12 \alpha N \int\_{0}^{1} x (\ln N + \ln x)(x-x^2) ,dx && \text{($\ln(xN) = \ln N + \ln x$)} \\
&= N + 12 \alpha N \int\_{0}^{1} (x^2 \ln N - x^3 \ln N + x^2 \ln x - x^3 \ln x) ,dx.
\end{align\*}
$$

Let’s split this integral into the $$\ln N$$ part and the $$\ln x$$ part.

$$
\begin{align\*}
12 \alpha N \ln N \int\_{0}^{1} (x^2 - x^3) ,dx &= 12 \alpha N \ln N \left\[ \frac{x^3}{3} - \frac{x^4}{4} \right]\_{0}^{1} \\
&= \mathbf{\alpha N \ln N}.
\end{align\*}
$$

$$
\begin{align\*}
N+12 \alpha N \int\_{0}^{1} (x^2 \ln x - x^3 \ln x) ,dx &= N+12 \alpha N \left( \left\[ \int\_{0}^{1} x^2 \ln x ,dx \right]-\left\[ \int\_{0}^{1} x^3 \ln x ,dx \right] \right) \\
&= N+12 \alpha N \left( \left\[ -\frac{1}{(2+1)^2} \right] - \left\[ -\frac{1}{(3+1)^2} \right] \right) \\

```
&= N- \frac{7\alpha}{12} N \\
&= \mathbf{\left(1 - \frac{7\alpha}{12}\right) N}.
```

\end{align\*}
$$

In the second line, we used the identity $$\int\_0^1 x^k \ln x ,dx = -\frac{1}{(k+1)^2}$$.
{% endstep %}

{% step %}

### Determining the Value of $$\alpha$$

Bootstrapping confirms the statement from the book that

$$
\begin{align\*}
C\_N &\approx  \underbrace{(\alpha N \ln N)}*{\text{Initial}} + \underbrace{\left(1 - \frac{7\alpha}{12}\right) N}*{\text{Expanded}} \\
&= \alpha N \ln N + O(N).
\end{align\*}
$$

If we want to retain only the leading factor, then we should choose $$\alpha=12/7$$ to "eradicate" the $$O(N)$$ summand.

{% hint style="warning" %}
To be rigorous, we should check that this form is "stable"; if we assume $$\alpha N \ln N + O(N)$$, we must get $$\alpha N \ln N + O(N)$$ back. This is indeed true, since the definite integral

$$N + 12 \int\_{0}^{1} \[\alpha (xN) \ln(xN)+\beta N] (x-x^2) ,dx$$

resolves again to the same form. $$\beta$$ denotes the hidden constant inside the $$O(N)$$ term.
{% endhint %}
{% endstep %}
{% endstepper %}

## Exercise 2.49 🌟

{% hint style="success" %}
This exercise showcases an advanced usage of the bootstrapping method.
{% endhint %}

With bootstrapping we start with a rough bound and then improve it step by step. The altered recurrence is

$$
a\_n = \frac{1}{n^2} + \frac{1}{n} \sum\_{1 \le k < n} \frac{a\_k}{n-k} \qquad \text{ for $n>0$ with $a\_0=1$},
$$

where the $$k=0$$ 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 $$a\_n = O(1/n)$$ for $$n>0$$. Thus, there exists some constant $$c>0$$ such that $$a\_n \le c/n$$ for sufficiently large $$n$$. Note that $$a\_n>0$$ for all $$n\ge0$$. If we substitute this into the recurrence, we have

$$
\begin{align\*}
a\_n &\le \frac{1}{n^2} + \frac{1}{n} \sum\_{1 \le k < n} \frac{c}{k(n-k)} \\
&= \frac{1}{n^2} + \frac{c}{n^2} \sum\_{1 \le k < n} \left( \frac{1}{k} + \frac{1}{n-k} \right) && \text{$\left(\cfrac{1}{k(n-k)} = \cfrac{1}{n} \left( \cfrac{1}{k} + \cfrac{1}{n-k} \right)\right)$} \\
&= \frac{1}{n^2} + \frac{c}{n^2} (2 H\_{n-1}) \\\[0.4cm]
&\sim \frac{1}{n^2} + \frac{2c \ln n}{n^2} \\
&\therefore \boxed{a\_n=O\left(\frac{\log n}{n^2}\right)}=O(1/n).
\end{align\*}
$$

#### Iteration 2

We assume that $$a\_n \le c \frac{\ln n}{n^2}$$ for some constant $$c>0$$ and $$n>0$$. If we substitute this into the recurrence, we have

$$
a\_n \le \frac{1}{n^2} + \frac{c}{n} \sum\_{1 \le k < n} \frac{\ln k}{k^2 (n-k)}.
$$

To upper bound the sum $$S = \sum\_{1 \le k < n} \frac{\ln k}{k^2 (n-k)}$$, we split it into two parts. The split typically occurs at the midpoint (e.g., $$n/2$$). 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 $$n$$ is even.

When $$1 \le k \le n/2$$ then $$n-k \ge n/2 \implies 1/(n-k) \le 2/n$$. We have

$$
S\_1 \le \frac{2}{n} \sum\_{1 \le k \le n/2} \frac{\ln k}{k^2} < \frac{2}{n} \sum\_{k=1}^\infty \frac{\ln k}{k^2}=O(1/n).
$$

The infinite series $$\sum\_{k=1}^{\infty} \frac{\ln k}{k^2}$$ [converges to a constant](https://en.wikipedia.org/wiki/Glaisher%E2%80%93Kinkelin_constant#Series_expressions).

When $$n/2 < k < n$$ then $$\frac{\ln k}{k^2}$$ attains its maximum value at the start of the range, bounded by $$\approx \frac{\ln(n/2)}{(n/2)^2} = O(\frac{\log n}{n^2})$$. Therefore,

$$
S\_2\le O\left(\frac{\log n}{n^2}\right) \sum\_{n/2 < k < n} \frac{1}{n-k}= O\left(\frac{\log n}{n^2}\right) \cdot O(\log n) = O\left( \frac{(\log n)^2}{n^2} \right).
$$

Thus,

$$
a\_n \le \frac{1}{n^2} + \frac{c}{n} \left(O(1/n)+O\left( \frac{(\log n)^2}{n^2} \right)\right)=\boxed{O(1/n^2)}\subset O\left(\frac{\log n}{n^2}\right).
$$

#### Iteration 3

To be rigorous, we should check that this form is "stable"; if we assume $$O(1/n^2)$$, we must get $$O(1/n^2)$$ back. So, we take that $$a\_n \le c/n^2$$ for some constant $$c>0$$ and $$n>0$$. If we substitute this into the recurrence, we have

$$
a\_n \le \frac{1}{n^2} + \frac{c}{n} \sum\_{1 \le k < n} \frac{1}{k^2(n-k)}.
$$

We split the summation again into two parts. When $$1 \le k \le n/2$$ then $$\frac{1}{n-k} \le \frac{2}{n}$$, hence

$$
S\_1\le \frac{2}{n} \sum\_{k=1}^\infty \frac{1}{k^2} = O(1/n).
$$

The sum $$\sum\_{k=1}^\infty \frac{1}{k^2}=\zeta(2)$$, where $$\zeta$$ is the [Riemann zeta function](https://en.wikipedia.org/wiki/Riemann_zeta_function).

When $$n/2 < k < n$$ then $$\frac{1}{k^2} \le \frac{4}{n^2}$$. Thus, $$S\_2\le\frac{4}{n^2} \sum \frac{1}{n-k} = O(\frac{\log n}{n^2})$$. Therefore,

$$
a\_n \le \frac{1}{n^2} + \frac{c}{n} \cdot \left(O(1/n) +O\left(\frac{\log n}{n^2}\right)\right)= O(1/n^2).
$$

#### Final Result

From $$a\_n=O(1/n^2)$$ it follows that $$n^2a\_n=O(1)$$.

## Exercise 2.50 🌟

{% hint style="success" %}
We can expect many hardships with the perturbation method. This and the next few exercises demonstrate different obstacles that we may encounter. Here, we must tune the initial growth rate after failing to bound the error rate with the initial guess.
{% endhint %}

Find the asymptotic growth of the solution to the “perturbed” Fibonacci recurrence

$$
a\_{n+1} = \left(1 + \frac{1}{n}\right)a\_n + \left(1 - \frac{1}{n}\right)a\_{n-1} \space\space\space\space\text{ for $n>0$}.
$$

{% hint style="danger" %}
Notice that $$n>0$$, otherwise $$a\_2$$ would be missing, since initial values are $$a\_0=0$$ and $$a\_1=1$$.
{% endhint %}

***

We can view the recurrence as&#x20;

$$
a\_{n+1}=\underbrace{a\_n + a\_{n-1}}*{\text{Standard Fibonacci}} + \underbrace{\frac{1}{n}(a\_n - a*{n-1})}\_{\text{Perturbation Term}}.
$$

The solution to a simpler recurrence $$b\_{n+1}=b\_n+b\_{n-1}$$ for $$n>0$$ with $$b\_0=0$$ and $$b\_1=1$$ is $$b\_n=F\_n =\Theta( \phi^n)$$, where $$\phi$$ is the [golden ratio](https://en.wikipedia.org/wiki/Golden_ratio).

Following the steps from the book, we need to compare the two recurrences by forming the ratio

$$
\rho\_n=\frac{a\_n}{b\_n}=\frac{a\_n}{\Theta(\phi^n)}.
$$

If we plug $$\Theta(\phi^n)\rho\_n$$ into the original recurrence, we won’t be able to bound $$\rho\_n$$. Consequently, the solution must grow faster. The book contains a hint as part of [Exercise 2.53](#exercise-2.53); let’s add an extra $$n^\alpha$$ multiplier and find the value of $$\alpha$$ such that $$\rho\_n=O(1)$$. This gives

$$
\rho\_n\sim\frac{a\_n}{n^\alpha\phi^n} \space\space\space\space \text{ for $n>1$},
$$

with $$\rho\_1=1/\phi$$. We have

$$
\begin{align\*}
(n+1)^\alpha \phi^{n+1} \rho\_{n+1} &= \left(1 + \frac{1}{n}\right) n^\alpha \phi^n \rho\_n + \left(1 - \frac{1}{n}\right) (n-1)^\alpha \phi^{n-1} \rho\_{n-1} \\\[0.4cm]
\phi^2 \frac{(n+1)^\alpha}{n^\alpha} \rho\_{n+1} &= \phi \left(1 + \frac{1}{n}\right) \rho\_n + \left(1 - \frac{1}{n}\right) \frac{(n-1)^\alpha}{n^\alpha} \rho\_{n-1} \\\[0.4cm]
\phi^2 \left(1 + \frac{1}{n}\right)^\alpha \rho\_{n+1} &= \phi \left(1 + \frac{1}{n}\right) \rho\_n + \left(1 - \frac{1}{n}\right) \left(1 - \frac{1}{n}\right)^\alpha \rho\_{n-1} \\\[0.4cm]
\phi^2 \left(1 + \frac{1}{n}\right)^\alpha \rho\_{n+1} &\le \left\[\phi \left(1 + \frac{1}{n}\right) + \left(1 - \frac{1}{n}\right) \left(1 - \frac{1}{n}\right)^\alpha \right] \rho\_n && \text{(assume $\rho\_n$ is non-decreasing)} \\\[0.4cm]
\phi^2 \left(1 + \frac{\alpha}{n}\right) \rho\_{n+1} &\le \left\[\phi \left(1 + \frac{1}{n}\right) + \left(1 - \frac{1}{n}\right) \left(1 - \frac{\alpha}{n}\right)\right] \rho\_n && \text{(Taylor expansion $(1 + x)^\alpha \approx 1 + \alpha x$)} \\\[0.4cm]
\left(\phi^2 + \frac{\alpha \phi^2}{n}\right) \rho\_{n+1} &\le \left\[\phi + \frac{\phi}{n} + \left(1 - \frac{\alpha}{n} - \frac{1}{n} + O\left(\frac{1}{n^2}\right)\right)\right] \rho\_n \\\[0.4cm]
\left(\phi^2 + \frac{\alpha \phi^2}{n}\right) \rho\_{n+1} &\le \left\[(\phi + 1) + \frac{\phi - 1 - \alpha}{n}\right] \rho\_n && \text{(ignore $O(1/n^2$)}\\\[0.4cm]
\left(\phi^2 + \frac{\alpha \phi^2}{n}\right) \rho\_{n+1} &\le \left\[\phi^2 + \frac{\phi - 1 - \alpha}{n}\right] \rho\_n.
\end{align\*}
$$

To solve for $$\alpha$$ we can set $$\alpha \phi^2 = \phi - 1 - \alpha$$, which implies that

$$
\alpha = \frac{\phi - 1}{\phi^2 + 1} = \frac{\phi - 1}{\phi + 2}.
$$

Thus, the asymptotic growth of the solution is $$a\_n=O(n^\frac{\phi - 1}{\phi + 2} \phi^n)$$.

## Exercise 2.51 🌟

{% hint style="success" %}
Often, we have to employ the perturbation method iteratively. This exercise illustrates the process.
{% endhint %}

The coefficient $$n$$ on $$a\_{n-1}$$ suggests $$a\_n=\omega(n!)$$. Let’s employ a change of variable $$b\_n=a\_n/n!$$ to find a solution to a simpler recurrence

$$
\begin{align\*}
n! \cdot b\_n &= n(n-1)!\cdot  b\_{n-1} + n^2 (n-2)! \cdot b\_{n-2} \\
b\_n &= b\_{n-1} + \frac{n^2}{n(n-1)} b\_{n-2} \\
&= b\_{n-1} + \frac{n}{n-1} b\_{n-2} \\
&= b\_{n-1} + \left(1+\frac{1}{n-1}\right) b\_{n-2}
\end{align\*}
$$

As $$n \to \infty$$, $$\frac{1}{n-1} \to 0$$. Therefore, applying the perturbation method again, we end up with another simpler recurrence $$c\_n = c\_{n-1} + c\_{n-2}$$ for $$n>1$$ with $$c\_0=0$$ and $$c\_1=1$$. Clearly, $$c\_n=F\_n \sim \phi^n$$, where $$\phi$$ is the [golden ratio](https://en.wikipedia.org/wiki/Golden_ratio).

To account for the perturbation factor, we reuse the approach from the previous exercise. This gives

$$
\rho\_n\sim\frac{b\_n}{n^\alpha\phi^n} \space\space\space\space \text{ for $n>1$},
$$

with $$\rho\_1=1/\phi$$. We have

$$
\begin{align\*}
n^\alpha \phi^n \rho\_n &=  (n-1)^\alpha \phi^{n-1} \rho\_{n-1}  + \frac{n}{n-1}  (n-2)^\alpha \phi^{n-2} \rho\_{n-2} \\\[0.4cm]
\phi^2 \rho\_n &= \left(1 - \frac{1}{n}\right)^\alpha \phi \rho\_{n-1}  + \frac{n}{n-1}  \left(1 - \frac{2}{n}\right)^\alpha \rho\_{n-2} \\\[0.4cm]
\phi^2 \rho\_n &= \left(1 - \frac{\alpha}{n}\right) \phi \rho\_{n-1}  + \frac{n}{n-1}  \left(1 - \frac{2\alpha}{n}\right) \rho\_{n-2} && \text{(Taylor expansion $(1 + x)^\alpha \approx 1 + \alpha x$)} \\\[0.4cm]
\phi^2 \rho\_n &\le \rho\_{n-1} \left\[\left(1 - \frac{\alpha}{n}\right) \phi + \frac{n}{n-1}  \left(1 - \frac{2\alpha}{n}\right)\right] && \text{(assume $\rho\_n$ is non-decreasing)} \\\[0.4cm]
\left(1 - \frac{1}{n}\right)\phi^2 \rho\_n &\le \rho\_{n-1} \left\[\left(1 - \frac{1}{n}\right) \left(1 - \frac{\alpha}{n}\right) \phi + \left(1 - \frac{2\alpha}{n}\right)\right] \\\[0.4cm]
\left(\phi^2 - \frac{\phi^2}{n}\right) \rho\_n &\le \rho\_{n-1} \left\[\phi \left(1 - \frac{1}{n} - \frac{\alpha}{n} + O\left(\frac{1}{n^2}\right)\right) + \left(1 - \frac{2\alpha}{n}\right)\right] \\\[0.4cm]
\left(\phi^2 - \frac{\phi^2}{n}\right) \rho\_n &\le \rho\_{n-1} \left\[\phi \left(1 - \frac{1}{n} - \frac{\alpha}{n}\right) + \left(1 - \frac{2\alpha}{n}\right)\right] && \text{(ignore $O(1/n^2$)} \\\[0.4cm]
\left(\phi^2 - \frac{\phi^2}{n}\right) \rho\_n &\le \rho\_{n-1} \left\[\phi + 1 - \frac{1}{n}(\phi + \phi \alpha + 2\alpha)\right] \\\[0.4cm]
\left(\phi^2 - \frac{\phi^2}{n}\right) \rho\_n &\le \rho\_{n-1} \left\[\phi^2 - \frac{1}{n}(\phi + \phi \alpha + 2\alpha)\right].
\end{align\*}
$$

To solve for $$\alpha$$ we can set $$\phi^2 = \phi + \phi \alpha + 2\alpha$$, which implies that

$$
\begin{align\*}
\phi^2 - \phi &= \alpha(\phi + 2) \\
1 &= \alpha(\phi + 2) \\
&\therefore \boxed{\alpha = \frac{1}{\phi + 2}}.
\end{align\*}
$$

Thus, the asymptotic growth of the solution is $$a\_n=O(n!n^\frac{1}{\phi + 2} \phi^n)$$.

## Exercise 2.52

The paper [Some Doubly Exponential Sequences](https://www.fq.math.ca/Scanned/11-4/aho-a.pdf) by Aho and Sloane elaborates the solution for a more general recurrence. It’s also instructive to read about how variants of the generic form appear in practical problems.&#x20;

The recurrence from the book, where $$g\_n=1$$, generates a sequence [A003095](https://oeis.org/A003095) shifted by one (the first element should be skipped, since in our case $$a\_0=1$$). Using the relationship to the constant $$c=1.225902443528748\dots$$, our $$\alpha$$ corresponds to $$c^2$$ (due to the previously mentioned index shift). The digits of $$\alpha$$ are listed in [A077496](https://oeis.org/A077496).

## Exercise 2.53

This is a small variation of [Exercise 2.50](#exercise-2.50) with $$\alpha = -\frac{\phi^2}{\phi^2 + 1}=-\frac{\phi+1}{\phi + 2}$$.&#x20;

The perturbation factor is $$(1 - 1/n)<1$$, hence, the sequence is asymptotically slightly less than the standard Fibonacci sequence. The polynomial correction must be decay, which explains why $$\alpha$$ is negative. Of course, we could have forgotten about the polynomial factor, since the upper bound is valid even without it. But it’s assumed that the O-notation expresses a tight upper bound.&#x20;

## Exercise 2.54

In the *best* case, we always choose the smaller subinterval of size $$\lfloor(N-1)/2\rfloor$$ (see the proof of Theorem 2.3 in the book). Therefore, the recurrence is

$$
B\_N=B\_{\lfloor(N-1)/2\rfloor}+1 \space \space \space \space \text{ for $N >1$ with $B\_1=1$}.
$$

After one iteration, we get $$B\_N=B\_{\lfloor(N-3)/4\rfloor}+2$$  leveraging a useful property of [floor and ceiling functions](https://en.wikipedia.org/wiki/Floor_and_ceiling_functions) that $$\lfloor\lfloor x\rfloor/2 \rfloor=\lfloor x/2 \rfloor$$.  At the end, we must have

$$
\frac{N-(2^n-1)}{2^n} \ge 1 \implies n=\lfloor\lg(N+1)\rfloor-1.
$$

The number of comparisons is $$B\_N=n+1=\lfloor\lg(N+1)\rfloor$$ for $$N \ge1$$.

## Exercise 2.55

We always choose the largest subinterval of size $$\lceil N/3\rceil$$. Therefore, the recurrence is

$$
B\_N=B\_{\lceil N/3\rceil}+2 \space \space \space \space \text{ for $N >1$ with  $B\_1=1$ and $B\_0=0$}.
$$

The number of comparisons is $$B\_N\sim2\log\_3 N$$ for $$N>1$$. If we convert the formula of ternary search to use logarithm of base two, then we can see that the leading term is $$2/\lg 3\approx 1.26 > 1$$. Ternary search is less efficient than binary search in terms of comparisons. Even though it reduces the problem size faster (dividing by 3 instead of 2), the "cost" of doing so (2 comparisons instead of 1) is too high to be worth it.

## Exercise 2.56

The solution is part of the [course material about recurrences](https://aofa.cs.princeton.edu/online/slides/AA02-Recurrences.pdf) (slides 36-37) by RS.

## Exercise 2.57

The recurrence (4) from the book may be regarded as a function $$f:\N \to \N$$, thus cannot have two closed-form solutions that map the same input to different outputs.

[Exercise 1.5](https://evarga.gitbook.io/sh-aofa/discrete-mathematical-methods/pages/pdkZlUOzG8HcgHKTEtet#exercise-1.5) contains an inductive proof for the RHS of an identity mentioned in this exercise. The LHS is the Theorem 2.4 from the book. Both are closed-form solutions to the same recurrence, so they must resolve to the same value for all $$N>0$$.

## Exercise 2.58

{% hint style="danger" %}
Figure 2.5 in the book is wrong. $$P\_N<0.5N\lg N$$ (just by looking entries of Table 2.3), hence $$f\_N=O(N)$$ must be negative; the plot of $$P\_N-0.5N\lg N$$ cannot be at the top. As a matter of fact, the top and bottom plots must be swapped. Furthermore, $$P\_{84}=252$$ instead of 215. Finally, the cumulated number of zero bits must include zero itself, otherwise we cannot recreate the plots.
{% endhint %}

<p align="center">{# 1 bits in numbers less than <span class="math">N</span>} – <span class="math">(N \lg N)/2</span></p>

The function is specified as $$T\_N=P\_N – (N \lg N)/2$$, where

$$
P\_N=P\_{\lceil N/2 \rceil}+P\_{\lfloor N/2 \rfloor}+\lfloor N/2 \rfloor  \space \space \space \space \text{ for $N>1$ with $P\_1=0$ (see the book).}
$$

***

<p align="center">{# 0 bits in numbers less than <span class="math">N</span>} – <span class="math">(N \lg N)/2</span></p>

The function is specified as $$T\_N=Z\_N – (N \lg N)/2$$, where

$$
Z\_N = Z\_{\lceil N/2 \rceil} + Z\_{\lfloor N/2 \rfloor} + \lceil N/2 \rceil-1 \space \space \space \space \text{ for $N>1$ with $Z\_1=1$ (we also count zero).}
$$

***

<p align="center">{# bits in numbers less than <span class="math">N</span>} – <span class="math">N \lg N</span></p>

The function is specified as $$T\_N=S\_N – N \lg N$$, where $$S\_N$$ is given in [Exercise 2.56](#exercise-2.56).

## Exercise 2.59

The recurrences for $$R\_N$$ are (for $$N>1$$ with $$R\_1=R\_0=0$$)

$$
\begin{align\*}
R\_N &= R\_{N-2^{\lfloor \lg N \rfloor}}+R\_{2^{\lfloor \lg N \rfloor}-1}+\lfloor \lg N \rfloor && \text{(considering the leftmost bit)} \\
&= R\_{\lfloor N/2 \rfloor}+\lfloor N/2 \rfloor && \text{(considering the rightmost bit).}
\end{align\*}
$$

## Exercise 2.60 🌟

{% hint style="success" %}
This and the next couple of exercises illustrate how seemingly smooth asymptotic bounds don’t reflect what actually happens under the hood, due to usage of floor and ceiling functions (see Table 2.4 in the book).
{% endhint %}

The Python 3 script below generates the required plot.

{% code expandable="true" %}

```python
from functools import lru_cache
from math import floor, log2
import matplotlib.pyplot as plt

@lru_cache(maxsize=None)
def f(n):
    return f(n//2) + f((n + 1)//2) + floor(log2(n)) if n > 1 else 0

def plot(save_path="exercise_2_60.png", label_fontsize=18, tick_fontsize=14, title_fontsize=20):
    xs = list(range(1, 513))
    ys = [f(n) for n in xs]

    # Theoretical comparison curve (scaled to match roughly) 𝛩(N).
    zs = [2 * n for n in xs]

    plt.figure(figsize=(12, 9))
    plt.plot(xs, ys, label="$A_N$", linestyle='-', color='blue', linewidth=1)
    plt.plot(xs, zs, label="$\\Theta(N)$", color='red', linestyle='--', alpha=0.3)

    plt.grid(True)
    plt.legend(fontsize=16)
    plt.xlabel(r"$N$", fontsize=label_fontsize)
    plt.ylabel(r"$A_N$", fontsize=label_fontsize)
    plt.title(r"$A_N = A_{\lfloor N/2 \rfloor} + A_{\lceil N/2 \rceil} + \lfloor \lg N \rfloor$",
              fontsize=title_fontsize)
    plt.xticks(fontsize=tick_fontsize)
    plt.yticks(fontsize=tick_fontsize)
    plt.tight_layout()
    plt.savefig(save_path, dpi=150)
    plt.show()

if __name__ == "__main__":
    plot()
```

{% endcode %}

<figure><img src="/files/oGYanpniD65AI30YFKlx" alt=""><figcaption><p>Apparently, the function is continuous and periodic. It runs slightly below the line <span class="math">y=2N</span>.</p></figcaption></figure>

## Exercise 2.61

The script from the previous exercise needs to be tweaked to produce the desired plot.

<figure><img src="/files/81QgR0DGLggkB6EkFIWV" alt=""><figcaption><p>This time the function is extremely "spiky." Large jumps occur just after powers of 2.</p></figcaption></figure>

## Exercise 2.62

The following Python 3 script creates the required plot and could be used to test out the variants, as mentioned in the book.

{% code expandable="true" %}

```python
from functools import lru_cache
import matplotlib.pyplot as plt

@lru_cache(maxsize=None)
def c(n):
    return c((n + 1)//2) + c((n + 1)//2) + n if n > 1 else 0

@lru_cache(maxsize=None)
def d(n):
    return d((n + 1)//2) + d((n + 1)//2) + c(n) if n > 1 else 0

def plot(save_path="exercise_2_62.png", label_fontsize=18, tick_fontsize=14, title_fontsize=20):
    xs = list(range(1, 513))
    ys = [d(n) for n in xs]

    plt.figure(figsize=(12, 9))
    plt.plot(xs, ys, label="$D_N$", linestyle='-', color='blue', linewidth=1)

    plt.grid(True)
    plt.xlabel(r"$N$", fontsize=label_fontsize)
    plt.ylabel(r"$D_N$", fontsize=label_fontsize)
    plt.title(r"$D_N = D_{\lceil N/2 \rceil} + D_{\lceil N/2 \rceil} + C_N$, "
              r"where $C_N = C_{\lceil N/2 \rceil} + C_{\lceil N/2 \rceil} + N$",
              fontsize=title_fontsize)
    plt.xticks(fontsize=tick_fontsize)
    plt.yticks(fontsize=tick_fontsize)
    plt.tight_layout()
    plt.savefig(save_path, dpi=150)
    plt.show()

if __name__ == "__main__":
    plot()
```

{% endcode %}

<figure><img src="/files/wiP6uJhhALTPHZOR7PM7" alt=""><figcaption><p>We see a similar figure as in <a href="#exercise-2.61">Exercise 2.61</a>. If <span class="math">N</span> isn’t a power of 2, then many recursive calls happen on larger indices due to the usage of the <code>ceil</code> function. These effects accumulate and produce large jumps right after these border values. When only floors are used, jumps happen on transitioning to powers of 2. Balancing floors and ceilings in both recurrences completely smooths out the curve.</p></figcaption></figure>

## Exercise 2.63

<figure><img src="/files/WTDZmEJCgMCWZt0EtMnD" alt=""><figcaption><p>The function wildly oscillates and is fractal. Let <span class="math">m=\lg N</span>. Values of <span class="math">g(N)</span> for <span class="math">N \in [2^m,2^{m+1})</span> can be generated from previously computed values in the interval <span class="math">[2^{m-1},2^m)</span> by adding <span class="math">2^m</span> depending on parity of <span class="math">N</span>. Therefore, every subsequent period is a scaled version of its predecessor.</p></figcaption></figure>

## Exercise 2.64

We’ll develop two equivalent recurrence relations, although they appear to be completely different. As the book hinted in Chapter 1, it’s often easier to derive the total amount $$T\_N$$ and calculate the average as $$T\_N/N$$; we do include zero in the range of numbers less than $$N$$.

#### Considering the Leftmost Bit

<img src="/files/DXPjjGgYR2WfYCwy8dKm" alt="The constituent parts of the recursive formula for the cumulated number of leading 1s in numbers less than ." class="gitbook-drawing">

Let’s focus on the leftmost column and define $$m=\lfloor \lg N \rfloor$$. There are $$2^m$$ zeros, so we should add $$N-2^m$$ 1s to our count. This is represented as (1) in the image. There are two major sections demarcated by a horizontal line between the last 0 and first 1 in the leftmost column. The upper region has $$m+T\_{2^m-1}$$ 1s, where (2) denotes those $$m$$ 1s. The total number of 1s in the bottom section depends whether it contains the row (3). If it doesn’t include (3), then the count is simply zero. Otherwise, it is $$T\_{N-2^m}-T\_{2^{m-1}}$$. After combining all these ingredients into a single formula, we get

$$
T\_N=N-2^{\lfloor \lg N \rfloor}+\lfloor \lg N \rfloor+T\_{2^{{\lfloor \lg N \rfloor}}-1}+\begin{cases}
0 &\text{if } N-2^{\lfloor \lg N \rfloor} < 2^{\lfloor \lg N \rfloor-1}, \\
T\_{N-2^{\lfloor \lg N \rfloor}}-T\_{2^{{\lfloor \lg N \rfloor}-1}} &\text{otherwise}.
\end{cases}
$$

The base cases are $$T\_1=T\_0=0$$.

#### Considering the Rightmost Bit

If we focus on the rightmost bit, we see that the range can be split onto numbers ending with 1 and those ending with 0. Suppose we count how many leading 1s are there in those two subintervals. What we’re only missing is the $$\lfloor \lg N \rfloor$$ 1s associated with the number $$2^{\lfloor \lg N \rfloor}-1 \in \[1,N)$$. Thus, we need to add it to the total. Therefore, the recurrence becomes $$T\_N=T\_{\lfloor N/2 \rfloor} + T\_{\lceil N/2 \rceil} + \lfloor \lg N \rfloor$$ for $$N>1$$ with $$T\_1=0$$. Notice, that it’s exactly the one from [Exercise 2.60](#exercise-2.60).

For example, when $$N = 2^n$$, the average simplifies to $$2 - \frac{n+2}{N}$$, which tends to 2 as $$N \to \infty$$.

## Exercise 2.65

A bitstring of length $$N$$ has the form $$b\_1b\_2\dots b\_N$$, where each $$b\_i \in {0,1}$$. The initial run of 1s ends at the first 0 (or continues to the end if all bits are 1). Let $$X$$ be a random variable denoting the length of the initial string of 1s in a random bitstring of length $$N$$. Its [probability mass function](https://en.wikipedia.org/wiki/Probability_mass_function) is defined as

$$
\Pr{X=k}=\begin{cases}
\left(\cfrac{1}{2}\right)^k \cdot \cfrac{1}{2}=\left(\cfrac{1}{2}\right)^{k+1} &\text{if } 0 \le k\<N, \\\[0.5cm]
\left(\cfrac{1}{2}\right)^N &\text{if } k=N.
\end{cases}
$$

The event $$X=k$$ for $$0 \le k\<N$$ occurs if the first $$k$$ bits are 1 and the $$(k+1)$$-th bit is 0. The expected value is

$$
\begin{align\*}
\mathbb{E}\[X] &= \sum\_{k=0}^N k \cdot \Pr{X=k}\\
&=\sum\_{k=1}^N  \Pr{X\ge k} && \text{($\Pr{X=k}=\Pr{X\ge k}-\Pr{X\ge k+1}$, so the sum telescopes)}\\
&=\sum\_{k=1}^N  2^{-k} \\
&= 1 - \frac{1}{2^N}.
\end{align\*}
$$

## Exercise 2.66

Based on [Exercise 2.65](#exercise-2.65), we have that $$\lim\_{n \to \infty}\mu=\mathbb{E}\[X]=1$$.&#x20;

The variance is given by

$$
\text{Var}(X) = \mathbb{E}\[X^2] - \mathbb{E}\[X]^2.
$$

We need to compute

$$
\begin{align\*}
\mathbb{E}\[X^2] &= \sum\_{k=0}^{\infty} k^2 \cdot \Pr{X = k} \\
&= \sum\_{k=0}^{\infty} \frac{k^2}{2^{k+1}} \\
&= \frac{1}{2} \cdot \frac{\frac{1}{2} \cdot \frac{3}{2}}{(1 - 1/2)^3} && \text{$\left(\sum\_{k=0}^{\infty} k^2 x^k = \cfrac{x(1 + x)}{(1 - x)^3} \quad \text{for } |x|=1/2 < 1\right)$} \\
&= 3.
\end{align\*}
$$

Thus, the variance is $$3-1=2$$.

{% hint style="info" %}
This problem actually maps perfectly to the geometric distribution. If we let $$Y$$ be the total number of bits we examine until we see the first 0, $$Y$$ follows a geometric distribution with probability $$p = 1/2$$. Our random variable would be defined as $$X=Y-1$$. At any rate, both the mean and variance are given.
{% endhint %}

## Exercise 2.67

The total number of carries made when a binary counter increments $$N$$ times, from $$0$$ to $$N$$, matches $$R\_N$$, as defined in the book (see also [Exercise 2.59](#exercise-2.59)).

## Exercise 2.68

In both subproblems, we follow the steps from the proof of Theorem 2.5 outlined in the book.

For $$\delta=0$$ the exact representation of the solution is

$$
a(x)=x^\gamma\left(1+\left(\frac{\alpha}{\beta^\gamma}\right)+\left(\frac{\alpha}{\beta^\gamma}\right)^2+\dots \left(\frac{\alpha}{\beta^\gamma}\right)^t\right) \text{, where }t=\lfloor \log\_\beta x\rfloor.
$$

If $$\alpha<\beta^\gamma \implies \gamma > \log\_\beta \alpha$$ then the sum converges and

$$
a(x) \sim x^\gamma \sum\_{j \ge 0} \left(\frac{\alpha}{\beta^\gamma}\right)^j=\underbrace{\frac{\beta^\gamma}{\beta^\gamma-\alpha}}\_{c\_1}x^\gamma.
$$

If $$\alpha=\beta^\gamma \implies \gamma =\log\_\beta \alpha$$ then each of the terms in the sum is 1 and the solution is simply

$$
a(x) \sim \underbrace{\frac{1}{\log \beta}}\_{c\_2}\cdot x^\gamma \log x.
$$

***

For $$\delta \neq0$$ the exact representation of the solution is

$$
a(x)=x^\gamma\left((\log x)^\delta+\left(\frac{\alpha}{\beta^\gamma}\right)\left(\log \frac{x}{\beta}\right)^\delta+\left(\frac{\alpha}{\beta^\gamma}\right)^2\left(\log \frac{x}{\beta^2}\right)^\delta+\dots \left(\frac{\alpha}{\beta^\gamma}\right)^t\left(\log \frac{x}{\beta^t}\right)^\delta\right) \text{, where }t=\lfloor \log\_\beta x\rfloor.
$$

Note that

$$
\begin{align\*}
\left(\log \frac{x}{\beta^k}\right)^\delta &= (\log x - k\log \beta)^\delta \\
&= (\log x)^\delta\left(1-\frac{k\log \beta}{\log x}\right)^\delta.
\end{align\*}
$$

As $$x \to \infty \implies \log x \to \infty$$, so for fixed k

$$
\left(1-\frac{k\log \beta}{\log x}\right)^\delta \to 1.
$$

If $$\alpha<\beta^\gamma \implies \gamma > \log\_\beta \alpha$$ then the contribution of terms where $$k$$ is close to $$t$$ is negligible and the sum converges. We have

$$
a(x) \sim x^\gamma (\log x)^\delta\sum\_{j \ge 0} \left(\frac{\alpha}{\beta^\gamma}\right)^j=\underbrace{\frac{\beta^\gamma}{\beta^\gamma-\alpha}}\_{c\_1}x^\gamma(\log x)^\delta.
$$

If $$\alpha=\beta^\gamma \implies \gamma =\log\_\beta \alpha$$ then

$$
a(x) \sim x^\gamma \sum\_{k=0}^{\log\_\beta x} (\log x - k \log \beta)^\delta.
$$

This sum can be approximated by an integral. Let $$t$$ be a continuous variable for $$k$$ and $$u = \log x - t \log \beta$$, so $$du = -\log \beta , dt$$. This gives

$$
a(x) \sim x^\gamma \frac{1}{\log \beta} \int\_{0}^{\log x} u^\delta , du \sim\underbrace{\frac{1}{\log \beta (\delta +1)}}\_{c\_2}x^\gamma(\log x)^{\delta+1}.
$$

{% hint style="warning" %}
While the book states $$c\_1, c\_2$$ depend on $$\alpha, \beta, \gamma$$, the derivation shows $$c\_2$$ also depends on $$\delta$$ due to the integration of the logarithmic term.
{% endhint %}

## Exercise 2.69 🌟

{% hint style="success" %}
This exercise reminds us that the asymptotic approximations given by the Master theorem differ in nature from exact solutions.
{% endhint %}

The recurrence falls under case 2 of the Master theorem. The exact solution takes the form

$$
a\_N = N \log\_3 N + N \cdot P(N).
$$

The function $$P$$ represents the fluctuations caused by the floor function $$\lfloor N/3 \rfloor$$. We have

$$
P(N)=\frac{a\_N}{N}-\log\_3 N \space \space \space \space \text{ for $N>3$}.
$$

<figure><img src="/files/nC96OvsM1ZX8JcYmrjPa" alt=""><figcaption><p>The plot of the periodic part shows a fractal image, where jumps occur at values of <span class="math">N=4 \cdot 3^k</span> for <span class="math">k>0</span>.</p></figcaption></figure>

## Exercise 2.70

Here is the Python script for trying out different configurations. Currently, it is setup to draw a variant where floors are replaced by ceils.

{% code expandable="true" %}

```
from functools import lru_cache
from math import log
import matplotlib.pyplot as plt

@lru_cache(maxsize=None)
def a(n):
    return 3 * a((n+1)//3) + n if n > 3 else 1

def plot(save_path="exercise_2_70.png", label_fontsize=18, tick_fontsize=14, title_fontsize=20):
    xs = list(range(1, 973))
    ys = [a(n) / n - log(n, 3) for n in xs]

    plt.figure(figsize=(12, 9))
    plt.plot(xs, ys, linestyle='-', color='blue', linewidth=1)

    plt.grid(True, which='both', linestyle='--', alpha=0.4)
    plt.xlabel(r"$N$", fontsize=label_fontsize)
    plt.ylabel(r"$\frac{a_N}{N} - \log_3 N$", fontsize=label_fontsize)
    plt.title(r"Periodic Part of $a_N = 3a_{\lceil N/3 \rceil} + N$",
              fontsize=title_fontsize)
    plt.xticks(fontsize=tick_fontsize)
    plt.yticks(fontsize=tick_fontsize)
    plt.tight_layout()
    plt.savefig(save_path, dpi=150)

    plt.show()

if __name__ == "__main__":
    plot()
```

{% endcode %}

<figure><img src="/files/zjLffTkAyTULPRxKKmwv" alt=""><figcaption><p>This is the plot where 2 floors are replaced by ceils. Apparently, the jumps aren’t so steep as in <a href="#exercise-2.69">Exercise 2.69</a>.</p></figcaption></figure>

## Exercise 2.71 🌟

{% hint style="success" %}
This exercise shows what happens when the function $$f(x)$$ grows exponentially fast.
{% endhint %}

The exact solution takes the form

$$
\begin{align\*}
a(x) &= 2^x + \alpha 2^{x/\beta} + \alpha^2 2^{x/\beta^2} + \cdots + \alpha^t 2^{x/\beta^t} \\
&= \sum\_{k=0}^t \alpha^k 2^{x/\beta^k} \\
&= 2^x\left(1 + \sum\_{k=1}^t \alpha^k 2^{x \cdot \frac{1-\beta^k}{\beta^k}}\right) \text{, where }t=\lfloor \log\_\beta x\rfloor.
\end{align\*}
$$

Since $$\beta>1$$, we have $$\frac{1-\beta^k}{\beta^k}<0$$ for $$k \ge 1$$. The $$2^{c \cdot x}$$ term grows faster than any polynomial $$\alpha^x$$, hence $$a^x/2^{c \cdot x} \to 0$$ as $$x \to \infty$$, for any positive constant $$c$$.

Therefore, we can conclude that $$a(x) = \Theta(2^x)$$.

## Exercise 2.72

There are many ways to give an asymptotic solution to the recurrence. One approach is to use the more general [Akra-Bazzi method](https://en.wikipedia.org/wiki/Akra%E2%80%93Bazzi_method). Another one is to draw a recursion tree and discover patterns. There’s a tool called [VisuAlgo](https://visualgo.net/en/recursion) to see recursive trees in action. It has lots of prebuilt recurrences, but it’s also possible to define a custom recurrence by selecting the *Custom Code* option from the drop-down list. The following snippet contains the definition for this exercise.

```
if (n <= 3) /* base case */
  return 1;
else /* recursive case */
  return f(3*n/4) + f(n/4) + n;
```

After entering a value for $$N$$ and pressing the *Run* button, the tool visualizes the whole tree. In general, the cost per level is $$N$$. The largest subproblem size decreases by a factor of 3/4 at each step, so the depth is $$\sim \log\_{4/3} N$$. Therefore, the recurrence satisfies $$a\_N = \Theta(N \lg N)$$.

{% hint style="info" %}
If we want to be rigorous, then we must prove the hypothesis just formulated, for example, using induction on $$N$$.
{% endhint %}

***

We can even determine the leading coefficient by substituting $$a\_N \sim cN\log N$$ into the recurrence (bootstrapping). This gives $$a\_N \sim \frac{N \log N}{\log 4 - \frac{3}{4} \log 3}$$.

## Exercise 2.73 🌟

{% hint style="success" %}
This exercise shows how the interplay between the Master theorem (or Theorem 2.6 with a more specific function) and bootstrapping may reveal the leading coefficient.
{% endhint %}

The same remark applies here as in the previous exercise. By drawing a recursion tree, we can see that the depth is $$\sim \lg N$$ and the amount of work at each step drops by a factor of 3/4. In other words, the cost at level $$k$$ is $$(3/4)^kN$$. After summing up costs, we can conclude that the recurrence satisfies $$a\_N=\Theta(N)$$.

***

We can make one step further and discover the leading coefficient. Assuming that $$a\_N \sim cN$$, we can substitute $$a\_N$$ into the recurrence and find that $$a\_N \sim 4N$$.

## Exercise 2.74

We must prove that the solution to the recurrence

$$
a\_n = a\_{f(n)}+a\_{g(n)}+a\_{h(n)}+1 \space\space\space\space \text{ for $n>t$ with $a\_n=1$ for $n \le t$},
$$

with the constraint that $$f(n) + g(n) + h(n) = n$$, is $$a\_n=\Theta(n)$$. We assume that all index functions are positive. We’ll prove the lower and upper bounds separately.

Suppose $$a\_n=O(n) \implies a\_n \le c\_2n-d\_2$$ for some positive constants $$c\_2$$ and $$d\_2$$ as well as sufficiently large $$n$$. Substituting this hypothesis into our recurrence, we get

$$
\begin{align\*}
a\_n &= a\_{f(n)}+a\_{g(n)}+a\_{h(n)}+1 \\
&\le c\_2(f(n)+g(n)+h(n))-3d\_2+1 && \text{(by the inductive hypothesis)} \\
&= c\_2n-d\_2-(2d\_2-1) \\
&\le c\_2n-d\_2 && \text{(if we make $2d\_2-1 \ge 0 \implies d\_2 \ge 1/2$)} \\
&\therefore a\_n=O(n)  \space\space\space\space \checkmark
\end{align\*}
$$

The lower bound is even easier.  Suppose $$a\_n=\Omega(n) \implies a\_n \ge c\_1n$$ for some positive constant $$c\_1$$ and sufficiently large $$n$$. Substituting this hypothesis into our recurrence, we get

$$
\begin{align\*}
a\_n &= a\_{f(n)}+a\_{g(n)}+a\_{h(n)}+1 \\
&\ge c\_1(f(n)+g(n)+h(n))+1 && \text{(by the inductive hypothesis)} \\
&= c\_1n+1 \\
&\ge c\_1n \\
&\therefore a\_n=\Omega(n) \space\space\space\space \checkmark
\end{align\*}
$$

Since both bounds are the same, we can conclude that $$a\_n=\Theta(n)$$. Observe that we can choose suitable values for our constants to satisfy the base cases, too. For example, $$c\_1=1/t, c\_2=3/2$$ and $$d\_2=1/2$$ configuration works.

## Exercise 2.75

The recurrence $$a\_n= a\_{f(n)}+a\_{g(n)}+1$$ for $$n>t$$ with $$a\_n=1$$ for $$n \le t$$, where $$f(n)+g(n)=n-h(n)$$, describes the number of nodes in a recursion tree of a divide-and-conquer algorithm with a "coarsening effect" (subproblems of size $$\le t$$ are handled as base cases). We assume that all index functions are positive:

* The $$+1$$ represents the current node.
* The problem is split into two subproblems of size $$f(n)$$ and $$g(n)$$.

We are also given the condition $$f(n) + g(n) = n - h(n)$$. This equation tells us how the "size" of the problem decreases at each step. At any internal node with size $$n$$, the combined size of its children is $$n-h(n)$$. This means that at every step of the recursion, a size drops by $$h(n)$$. Since the recursion starts with $$n$$, and ends when reaching the "leaves" (blocks of size $$\le t$$), the sum of reductions at every internal node must be equal to the initial size $$n$$ minus the residuals at the leaves. This can be expressed as

$$
n \approx \sum\_{\text{all internal nodes } i} h(\text{size}*i) \approx \sum*{i=1}^{a\_n/2} h(\text{size}\_i).
$$

Let’s assume that $$h(n)=O(1) \implies h(n) <=c$$ for some positive constant $$c$$ and sufficiently large $$n$$. Substituting this into the above equation, we get

$$
n \approx \sum\_{i=1}^{a\_n/2} c \approx ca\_n \implies a\_n/n >0 \text{ as $n \to \infty$}.
$$

Roughly speaking $$n \approx a\_n \cdot h\_{\text{average}}$$, which implies

$$
\frac{a\_n}{n} \approx \frac{1}{h\_{\text{average}}} \to 0.
$$

So, we just need to ensure that $$h(n)=\omega(1)$$ somehow grows unboundedly as $$n \to \infty$$.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://evarga.gitbook.io/sh-aofa/discrete-mathematical-methods/chapter-2-recurrence-relations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
