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

Exercise 1.1-6

not stated yet in Lean solution written

Rating20
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Find T5T_5, the average number of times step E1 runs in Euclid's algorithm when n=5n = 5 and mm runs over all positive integers.

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

The count of E1 depends only on m mod nm \bmod n, so only m=1,…,5m = 1, \ldots, 5 matter.

Hint 2

Run Algorithm E for each of the five values and add the counts.

Solution

Show the solution

Write D(m,5)D(m, 5) for the number of runs of step E1 on the input (m,5)(m, 5).

  • m=1m = 1: E1 gives r=1r = 1 and (m,n)=(5,1)(m, n) = (5, 1). Then E1 gives r=0r = 0. So D=2D = 2.
  • m=2m = 2: r=2r = 2, then (5,2)(5, 2) with r=1r = 1, then (2,1)(2, 1) with r=0r = 0. So D=3D = 3.
  • m=3m = 3: r=3r = 3, then (5,3)(5, 3) with r=2r = 2, then (3,2)(3, 2) with r=1r = 1, then (2,1)(2, 1) with r=0r = 0. So D=4D = 4.
  • m=4m = 4: r=4r = 4, then (5,4)(5, 4) with r=1r = 1, then (4,1)(4, 1) with r=0r = 0. So D=3D = 3.
  • m=5m = 5: r=0r = 0 at once. So D=1D = 1.

The sum is 2+3+4+3+1=132 + 3 + 4 + 3 + 1 = 13, so T5=135=2.6.T_5 = \frac{13}{5} = 2.6. Values m>5m > 5 give no new counts. Let r=m mod 5r = m \bmod 5. If r=0r = 0, one run of E1 ends the algorithm, as for m=5m = 5. If r>0r > 0, the first run gives the pair (5,r)(5, r), and the input (r,5)(r, 5) gives the same pair after its own first run, so D(m,5)=D(r,5)D(m, 5) = D(r, 5). Each residue occurs once in each block of five consecutive values of mm, so the average over all mm is the average over m=1,…,5m = 1, \ldots, 5.

Check

The counts for m=1,…,5m = 1, \ldots, 5 are 2,3,4,3,12, 3, 4, 3, 1, and a python3 run of the algorithm gives the same list. For m=6,7,8,9,10m = 6, 7, 8, 9, 10 the counts are the same: 2,3,4,3,12, 3, 4, 3, 1. The sum is 1313 and the average is 2.62.6.

Remarks

The table of TnT_n for other nn is in the C1 chapter of Section 1.1. For large nn, TnT_n grows like 0.843ln⁡n0.843 \ln n (Section 4.5.3). Exercise 1.1-7 asks the same question with the roles of mm and nn changed.

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.