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 in two ways: with the formula of the arithmetic series, and by induction.
A.2. A weighted sum. Prove that . What is its order? Check the formula for .
A.3. Bounding. Without computing it exactly, find the order of , giving both an upper and a lower bound.
Logarithms and factorials
A.4. Simplify. Simplify each expression (logarithms in base 2):
- (a)
- (b)
- (c)
- (d)
A.5. Counting digits. How many decimal digits does have? Answer without computing it, using logarithms.
A.6. Stirling. Estimate with Stirling's formula. How many digits does it have? How accurate is the estimate?
A.7. Running maximum. A loop scans an array of 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 ?
Induction and correctness
A.8. Find the error. "Theorem: all horses are the same color. Proof by induction on the number of horses in a group. Base: a group of one horse has a single color. Step: take a group of horses. Removing the first, the remaining have the same color by the hypothesis; removing the last, the first also have the same color. Both groups share the horses in the middle, so all have the same color." Where is the mistake?
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 , and explain why the loop terminates.
Recurrences
A.10. Unroll. Solve with , for a power of 2. Which algorithm has this recurrence?
A.11. A recursion tree. Find the order of , which the formulas of the division scheme do not cover.
A.12. Guess and verify. Prove by substitution that , with , satisfies .
Solutions
A.1. It is an arithmetic series with terms, first term 1 and last term , so its sum is . By induction: for , ; if the sum of the first odd numbers is , adding the next one gives . For the sum is 10,000. (Geometrically: each odd number adds an L-shaped border to a square.)
A.2. By induction. For : and . Assuming the formula for :
Its order is : the last term alone is , and the whole sum is less than twice that. For : , which matches the direct sum.
A.3. Upper bound: each of the terms is at most , so the sum is at most . Lower bound: the largest half of the terms (those with ) are each at least , so the sum is at least . Both are constants times : the sum is . The integral gives the constant: .
A.4.
- (a) .
- (b) .
- (c) , a constant.
- (d) . The "log log" of a number is tiny: for , .
A.5. A positive integer has decimal digits. , so has 31 digits. (It is about .)
A.6. , while the exact value is about . The estimate is only 0.17% too small. The number has 65 digits. (The relative error of Stirling's formula is about , here .)
A.7. The -th element is a new maximum exactly when it is the largest of the first , which happens with probability in a random order. Adding these probabilities, the expected number of updates is . For , : 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 times.
A.8. The step from to 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 , and the base case only covers . The lesson: the inductive step must work for every from the base case on, including the first one.
A.9. Invariant: is the same before and after each iteration, equal to the gcd of the original values. It holds because : any number that divides and also divides , and conversely. When the loop ends, and , so the result is correct. It terminates because is a non-negative integer that strictly decreases at each iteration (), so it must reach 0. In fact it decreases fast: every two iterations the value at least halves, so the loop runs times.
A.10. Unrolling: . For example and . 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 : the formula with , , gives and .
A.11. Level 0 of the tree costs ; level 1 has sizes and , total ; level 2 costs , and so on. The levels form a geometric series of ratio , dominated by its first term: . Since , it is . Computing it exactly with , is 3.47 for , 3.94 for and 3.99 for , approaching 4. The general rule: if the sizes of the subproblems add up to less than , a linear non-recursive cost gives linear total time.
A.12. Base: . Step: assuming , . This is the cost, counted exactly, of a loop whose -th iteration does operations, and the same identity as exercise A.1.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.