taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.1-2

Exercise 1.1-2

not stated yet in Lean solution written

Rating15
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Show that at the start of step E1, apart from the first time, the value of mm is larger than the value of nn.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Ask which step leads back to step E1, and what values mm and nn have just before that step.

Hint 2

Use the fact that the remainder rr is less than nn.

Solution

Show the solution

The first execution of step E1 is at the start of the algorithm. Each later execution of E1 comes from step E3. Take such a later execution and call the values of mm, nn and rr in the previous pass of step E1 m0m_0, n0n_0 and r0r_0. Step E1 gives 0≤r0<n00 \le r_0 < n_0. Step E2 sends control to E3 only when r0≠0r_0 \ne 0, and step E3 sets m←n0m \leftarrow n_0 and n←r0n \leftarrow r_0. So at the new execution of E1 the values are m=n0m = n_0 and n=r0n = r_0, and r0<n0r_0 < n_0 gives m>nm > n. □\square

Check

Take m=30m = 30 and n=84n = 84. At the first E1 the pair is (30,84)(30, 84), so m<nm < n. At the second it is (84,30)(84, 30). At the third it is (30,24)(30, 24), because 84=2⋅30+2484 = 2 \cdot 30 + 24. At the fourth it is (24,6)(24, 6), because 30=24+630 = 24 + 6, and there r=0r = 0. In each case after the first, m>nm > n.

Remarks

This is why the first pass of E1 is wasted when m<nm < n: it only swaps the numbers. Exercise 1.1-3 and the swap step in the C1 chapter of Section 1.1 deal with that cost. Exercise 1.1-7 counts the pass again.

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.