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

Exercise 1.1-3

not stated yet in Lean solution written

Rating20
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Rewrite Euclid's algorithm so that no step only moves one variable to another, and give the new algorithm the name F.

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

In Algorithm E, step E3 only renames: the old nn becomes mm and the remainder becomes nn.

Hint 2

Let the two variables swap their roles at each pass instead of moving their values. Divide mm by nn in one step and nn by mm in the next.

Solution

Show the solution

Algorithm F. Given two positive integers mm and nn, find their greatest common divisor.

  • F1. [Reduce mm.] Set m←m mod nm \leftarrow m \bmod n. If m=0m = 0, stop. The answer is nn.
  • F2. [Reduce nn.] Set n←n mod mn \leftarrow n \bmod m. If n=0n = 0, stop. The answer is mm. Otherwise go to F1.

The algorithm is correct. For positive integers uu and vv the pair (u mod v,v)(u \bmod v, v) has the same common divisors as (u,v)(u, v), because u=qv+(u mod v)u = qv + (u \bmod v) for an integer qq, so a number that divides vv divides uu if and only if it divides u mod vu \bmod v. Hence gcd⁡(m mod n,n)=gcd⁡(m,n)\gcd(m \bmod n, n) = \gcd(m, n) and gcd⁡(m,n mod m)=gcd⁡(m,n)\gcd(m, n \bmod m) = \gcd(m, n). At F1 both mm and nn are positive, since F2 reaches F1 only with n≠0n \ne 0 and F1 is entered first with the given positive values. After F1 with m=0m = 0 the answer is gcd⁡(0,n)=n\gcd(0, n) = n. The same holds in F2.

The algorithm stops. After F1, m<nm < n, and after the next F2, n<mn < m. So the values taken by mm after F1 and by nn after F2 form a sequence m1>n1>m2>n2>⋯m_1 > n_1 > m_2 > n_2 > \cdots of nonnegative integers, with mi+1<nim_{i+1} < n_i because F1 takes a remainder modulo nin_i and ni<min_i < m_i because F2 takes a remainder modulo mim_i. A strictly decreasing sequence of nonnegative integers is finite, so the algorithm reaches a zero and stops.

Check

Take m=2166m = 2166 and n=6099n = 6099. F1 gives m=2166m = 2166 since 2166<60992166 < 6099, and m≠0m \ne 0. F2 gives n=6099 mod 2166=1767n = 6099 \bmod 2166 = 1767. F1 gives m=2166 mod 1767=399m = 2166 \bmod 1767 = 399. F2 gives n=1767 mod 399=171n = 1767 \bmod 399 = 171. F1 gives m=399 mod 171=57m = 399 \bmod 171 = 57. F2 gives n=171 mod 57=0n = 171 \bmod 57 = 0, so the answer is m=57m = 57. The computation has 6 remainders and no move of a value.

Remarks

Algorithm F makes the same divisions as Algorithm E, so it has the same cost in divisions. It saves the two replacements of step E3 at each pass. The price is that the algorithm has two nearly equal steps, which is the usual price of unrolling a loop.

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.