Skip to main content

Exercises: Mathematical Background

These exercises practice the tools of the two lessons of the appendix: sums, logarithms and factorials and induction, correctness and recurrences. Try each one before reading its solution: the solutions are grouped at the end of the page.


Sums​

A.1. Odd numbers. Compute 1+3+5+⋯+(2n−1)1 + 3 + 5 + \dots + (2n - 1) in two ways: with the formula of the arithmetic series, and by induction.

Solution to A.1

A.2. A weighted sum. Prove that ∑i=1ni⋅2i=(n−1)2n+1+2\sum_{i=1}^{n} i \cdot 2^i = (n - 1) 2^{n+1} + 2. What is its order? Check the formula for n=10n = 10.

Solution to A.2

A.3. Bounding. Without computing it exactly, find the order of ∑i=1ni\sum_{i=1}^{n} \sqrt{i}, giving both an upper and a lower bound.

Solution to A.3

Logarithms and factorials​

A.4. Simplify. Simplify each expression (logarithms in base 2):

  • (a) 4log⁡n4^{\log n}
  • (b) log⁡(8n3)\log (8 n^3)
  • (c) n1/log⁡nn^{1 / \log n}
  • (d) log⁡(log⁡(22k))\log (\log (2^{2^k}))

Solution to A.4

A.5. Counting digits. How many decimal digits does 21002^{100} have? Answer without computing it, using logarithms.

Solution to A.5

A.6. Stirling. Estimate 50!50! with Stirling's formula. How many digits does it have? How accurate is the estimate?

Solution to A.6

A.7. Running maximum. A loop scans an array of nn distinct numbers in random order and updates a variable max every time it finds a larger value. On average, how many times is max updated, for n=1,000n = 1{,}000?

Solution to A.7

Induction and correctness​

A.8. Find the error. "Theorem: all horses are the same color. Proof by induction on the number nn of horses in a group. Base: a group of one horse has a single color. Step: take a group of n+1n + 1 horses. Removing the first, the remaining nn have the same color by the hypothesis; removing the last, the first nn also have the same color. Both groups share the horses in the middle, so all n+1n + 1 have the same color." Where is the mistake?

Solution to A.8

A.9. Euclid's invariant. Euclid's algorithm repeats (a, b) = (b, a % b) while b != 0, and returns a. State the loop invariant that proves it computes gcd⁡(a,b)\gcd(a, b), and explain why the loop terminates.

Solution to A.9

Recurrences​

A.10. Unroll. Solve T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 with T(1)=1T(1) = 1, for nn a power of 2. Which algorithm has this recurrence?

Solution to A.10

A.11. A recursion tree. Find the order of T(n)=T(n/2)+T(n/4)+nT(n) = T(n/2) + T(n/4) + n, which the formulas of the division scheme do not cover.

Solution to A.11

A.12. Guess and verify. Prove by substitution that T(n)=T(n−1)+2n−1T(n) = T(n-1) + 2n - 1, with T(0)=0T(0) = 0, satisfies T(n)=n2T(n) = n^2.

Solution to A.12


Solutions​

A.1. It is an arithmetic series with nn terms, first term 1 and last term 2n−12n - 1, so its sum is n⋅1+(2n−1)2=n2n \cdot \frac{1 + (2n - 1)}{2} = n^2. By induction: for n=1n = 1, 1=121 = 1^2; if the sum of the first n−1n - 1 odd numbers is (n−1)2(n-1)^2, adding the next one gives (n−1)2+(2n−1)=n2(n-1)^2 + (2n - 1) = n^2. For n=100n = 100 the sum is 10,000. (Geometrically: each odd number adds an L-shaped border to a square.)

Back to exercise A.1

A.2. By induction. For n=1n = 1: 1⋅2=21 \cdot 2 = 2 and (1−1)⋅4+2=2(1 - 1) \cdot 4 + 2 = 2. Assuming the formula for n−1n - 1:

∑i=1ni⋅2i=(n−2)2n+2+n⋅2n=(2n−2)2n+2=(n−1)2n+1+2\sum_{i=1}^{n} i \cdot 2^i = (n - 2) 2^{n} + 2 + n \cdot 2^n = (2n - 2) 2^n + 2 = (n - 1) 2^{n+1} + 2

Its order is Θ(n⋅2n)\Theta(n \cdot 2^n): the last term alone is n⋅2nn \cdot 2^n, and the whole sum is less than twice that. For n=10n = 10: 9⋅211+2=18,4349 \cdot 2^{11} + 2 = 18{,}434, which matches the direct sum.

Back to exercise A.2

A.3. Upper bound: each of the nn terms is at most n\sqrt{n}, so the sum is at most nn=n1.5n \sqrt{n} = n^{1.5}. Lower bound: the largest half of the terms (those with i≥n/2i \ge n/2) are each at least n/2\sqrt{n/2}, so the sum is at least n2n2=n1.521.5\frac{n}{2} \sqrt{\frac{n}{2}} = \frac{n^{1.5}}{2^{1.5}}. Both are constants times n1.5n^{1.5}: the sum is Θ(n1.5)\Theta(n^{1.5}). The integral gives the constant: ∫0nx dx=23n1.5\int_0^n \sqrt{x}\,dx = \frac{2}{3} n^{1.5}.

Back to exercise A.3

A.4.

  • (a) 4log⁡n=(22)log⁡n=(2log⁡n)2=n24^{\log n} = (2^2)^{\log n} = (2^{\log n})^2 = n^2.
  • (b) log⁡(8n3)=log⁡8+3log⁡n=3+3log⁡n\log (8 n^3) = \log 8 + 3 \log n = 3 + 3 \log n.
  • (c) n1/log⁡n=2log⁡n⋅(1/log⁡n)=21=2n^{1 / \log n} = 2^{\log n \cdot (1 / \log n)} = 2^1 = 2, a constant.
  • (d) log⁡(log⁡(22k))=log⁡(2k)=k\log (\log (2^{2^k})) = \log (2^k) = k. The "log log" of a number is tiny: for n=264n = 2^{64}, log⁡log⁡n=6\log \log n = 6.

Back to exercise A.4

A.5. A positive integer mm has ⌊log⁡10m⌋+1\lfloor \log_{10} m \rfloor + 1 decimal digits. log⁡102100=100log⁡102≈30.10\log_{10} 2^{100} = 100 \log_{10} 2 \approx 30.10, so 21002^{100} has 31 digits. (It is about 1.27×10301.27 \times 10^{30}.)

Back to exercise A.5

A.6. 2π⋅50(50e)50≈3.0363×1064\sqrt{2 \pi \cdot 50} \left( \frac{50}{e} \right)^{50} \approx 3.0363 \times 10^{64}, while the exact value is about 3.0414×10643.0414 \times 10^{64}. The estimate is only 0.17% too small. The number has 65 digits. (The relative error of Stirling's formula is about 112n\frac{1}{12n}, here 1600\frac{1}{600}.)

Back to exercise A.6

A.7. The ii-th element is a new maximum exactly when it is the largest of the first ii, which happens with probability 1/i1/i in a random order. Adding these probabilities, the expected number of updates is Hn=1+12+⋯+1nH_n = 1 + \frac{1}{2} + \dots + \frac{1}{n}. For n=1,000n = 1{,}000, H1000≈7.49H_{1000} \approx 7.49: on average max changes only about 7 or 8 times. A simulation with 20,000 random arrays gave 7.48. In the worst case (ascending order) it changes nn times.

Back to exercise A.7

A.8. The step from n=1n = 1 to n+1=2n + 1 = 2 fails. With 2 horses, removing the first leaves the second and removing the last leaves the first: the two groups have no horse in common, so nothing connects their colors. The argument "both groups share the horses in the middle" needs n≥2n \ge 2, and the base case only covers n=1n = 1. The lesson: the inductive step must work for every nn from the base case on, including the first one.

Back to exercise A.8

A.9. Invariant: gcd⁡(a,b)\gcd(a, b) is the same before and after each iteration, equal to the gcd of the original values. It holds because gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b): any number that divides aa and bb also divides a mod b=a−qba \bmod b = a - q b, and conversely. When the loop ends, b=0b = 0 and gcd⁡(a,0)=a\gcd(a, 0) = a, so the result is correct. It terminates because bb is a non-negative integer that strictly decreases at each iteration (a mod b<ba \bmod b \lt b), so it must reach 0. In fact it decreases fast: every two iterations the value at least halves, so the loop runs O(log⁡b)O(\log b) times.

Back to exercise A.9

A.10. Unrolling: T(n)=2T(n/2)+1=4T(n/4)+2+1=⋯=n T(1)+(n/2+⋯+2+1)=n+(n−1)=2n−1T(n) = 2T(n/2) + 1 = 4T(n/4) + 2 + 1 = \dots = n \, T(1) + (n/2 + \dots + 2 + 1) = n + (n - 1) = 2n - 1. For example T(8)=15T(8) = 15 and T(1024)=2047T(1024) = 2047. It is the recurrence of any divide and conquer algorithm that splits into two halves and combines in constant time: the recursive sum of an array or the search of the maximum by division. It is O(n)O(n): the formula with a=2a = 2, b=2b = 2, k=0k = 0 gives a>bka \gt b^k and O(nlog⁡22)=O(n)O(n^{\log_2 2}) = O(n).

Back to exercise A.10

A.11. Level 0 of the tree costs nn; level 1 has sizes n/2n/2 and n/4n/4, total 34n\frac{3}{4} n; level 2 costs (34)2n\left(\frac{3}{4}\right)^2 n, and so on. The levels form a geometric series of ratio 34<1\frac{3}{4} \lt 1, dominated by its first term: T(n)≤n(1+34+916+… )=4nT(n) \le n \left( 1 + \frac{3}{4} + \frac{9}{16} + \dots \right) = 4n. Since T(n)≥nT(n) \ge n, it is Θ(n)\Theta(n). Computing it exactly with T(1)=1T(1) = 1, T(n)/nT(n)/n is 3.47 for n=103n = 10^3, 3.94 for n=106n = 10^6 and 3.99 for n=109n = 10^9, approaching 4. The general rule: if the sizes of the subproblems add up to less than nn, a linear non-recursive cost gives linear total time.

Back to exercise A.11

A.12. Base: T(0)=0=02T(0) = 0 = 0^2. Step: assuming T(n−1)=(n−1)2T(n-1) = (n-1)^2, T(n)=(n−1)2+2n−1=n2−2n+1+2n−1=n2T(n) = (n - 1)^2 + 2n - 1 = n^2 - 2n + 1 + 2n - 1 = n^2. This is the cost, counted exactly, of a loop whose ii-th iteration does 2i−12i - 1 operations, and the same identity as exercise A.1.

Back to exercise A.12

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.