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.4.1-4

Exercise 1.4.1-4

not stated yet in Lean solution written

Rating21
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 1.4.1 of Volume 1. Companion chapter: Subroutines

The task

Write a subroutine that finds the maximum of X[a],X[a+r],…,X[n]X[a], X[a+r], \ldots, X[n] with a=1+(n−1) mod ra = 1 + (n-1) \bmod r, with a second entrance for r=1r = 1, and state its characteristics.

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

Take MAXN of (5) and change only the decrement of the counter in the loop.

Hint 2

The counter runs through n,n−r,n−2r,…n, n - r, n - 2r, \ldots while it stays positive. Find the value at which it stops.

Hint 3

The extra entrance only has to load r=1r = 1 into the register that holds rr, and then fall into the general entrance.

Solution

Show the solution

Take rI1 as the register for rr and rI3 as the counter, as in MAXN.

SMAX1  ENT1 1          Entrance for r = 1.
SMAX   STJ  EXIT       Entrance for general r.
       JMP  2F
1H     CMPA X,3
       JGE  *+3
2H     ENT2 0,3        Position of the largest so far.
       LDA  X,3        Value of the largest so far.
       DEC3 0,1        k <- k - r.
       J3P  1B
EXIT   JMP  *

The instruction DEC3 0,1 subtracts rI1, which is rr, from rI3. The values of the counter are n,n−r,n−2r,…n, n - r, n - 2r, \ldots, and the loop goes on while the counter is positive. The last positive value is the smallest positive number congruent to nn modulo rr, which is a=1+(n−1) mod ra = 1 + (n - 1) \bmod r. The next value, a−r≤0a - r \le 0, ends the loop.

Characteristics.

  • Calling sequence: JMP SMAX, or JMP SMAX1 if r=1r = 1.
  • Entry conditions: rI3 =n≥1= n \ge 1. For the entrance SMAX, rI1 =r≥1= r \ge 1.
  • Exit conditions: rA =max⁡{X[n−jr]:0≤j<⌈n/r⌉}=CONTENTS(X+rI2)= \max\{X[n - jr] : 0 \le j < \lceil n/r \rceil\} = \text{CONTENTS}(X + \text{rI2}) with rI2 the largest index where it occurs, and rI3 =a−r=((n−1) mod r)+1−r= a - r = ((n-1) \bmod r) + 1 - r. After SMAX1, rI1 =1= 1. After SMAX, rI1 is unchanged. The registers rJ and the comparison indicator are changed.

Check

Take r=3r = 3 and n=10n = 10. The counter takes the values 10,7,4,110, 7, 4, 1, so a=1a = 1 and the elements X[10],X[7],X[4],X[1]X[10], X[7], X[4], X[1] are examined. The final value is rI3 =1−3=−2= 1 - 3 = -2, and the formula gives (10−1) mod 3+1−3=−2(10 - 1) \bmod 3 + 1 - 3 = -2. A simulator run for all r≤5r \le 5 and n≤12n \le 12 with random tables agreed with a direct computation of the maximum, of its index and of rI3.

Remarks

For r=1r = 1 the routine is MAXN again, with one extra instruction. The book of this exercise takes the same route. A call with r>nr > n examines only X[n]X[n].

This solution is drafted, 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.