Exercises from Section 1.1

Tord M. Johnson

April 25, 2020

1. [10] The text showed how to interchange the values of variables m and n, using the replacement notation, by setting t m, m n, n t. Show how the values of four variables (a,b,c,d) can be rearranged to (b,c,d,a) by a sequence of replacements. In other words, the new value of a is to be the original value of b, etc. Try to use the minimum number of replacements.

The sequence of replacements is as shown below.

t a a b b c c d d t

2. [15] Prove that m is always greater than n at the beginning of step E1, except possibly the first time this step occurs.

We will prove that m > n holds as a precondition to step E1, except possibly the first time this step occurs.

Proposition. m > n holds as a precondition to step E1, except possibly the first time this step occurs.

Proof. Initially, before the first time step E1 occurs, either m > n or m n.

First, we consider the case when m > n. After E1, we have 0 r < n. If r = 0, the algorithm terminates by step E2, proving one variant of the first case. Otherwise, if r > 0, after the assignment of step E3, we have m > n as a precondition to step E1, concluding the proof for the first case.

Second, we consider the case wehn m n. After E1, we have 0 r < n. If m = n, then r = 0 and the algorithm terminates by step E2, proving one variant of the second case. Otherwise, if m < n, after the assignment of step E3, we have m > n as a precondition to step E1, concluding the proof for the second case.

Therefore, having proved all possible cases, m > n holds as a precondition to step E1, except possibly the first time this step occurs. □

3. [20] Change Algorithm E (for the sake of efficiency) so that all trivial replacement operations such as “m n” are avoided. Write this new algorithm in the style of Algorithm E, and call it Algorithm F.

Algorithm F (Efficient Euclid’s algorithm). Given two positive integers m and n, find their greatest common divisor, that is, the largest positive integer that evenly divides both m and n. For the sake of efficiency, all trivial replacement operations are avoided.

F1.
[Find first remainder.] Divide m by n and let m be the remainder. (We will have 0 m < n.)
F2.
[Is first remainder zero?] If m = 0, the algorithm terminates; n is the answer.
F3.
[Find second remainder.] Divide n by m and let n be the remainder. (We will have 0 n < m.)
F4.
[Is second remainder zero?] If n = 0, the algorithm terminates; m is the answer.
F5.
[Repeat.] Go back to step F1.

4. [16] What is the greatest common divisor of 2166 and 6099?

We will determine the greatest common divisor of 2166 and 6099 using iterations i of Algorithm E.

iminiri




1 2166 6099 2166
2 6099 2166 1767
3 2166 1767 399
4 1767 399 171
5 399 171 57
6 171 57 0

Therefore, the greatest common divisor of 2166 and 6099 is 57.

5. [12] Show that the “Procedure for Reading This Set of Books” that appears after the preface actually fails to be a genuine algorithm on at least three of our five counts! Also mention some differences in format between it and Algorithm E.

The “Procedure for Reading This Set of Books” that appears after the preface fails to be a genuine algorithm on at least three of five counts.

The procedure lacks finiteness, as it will not always terminate after a finite number of steps. In particular, when we reach step 18, we are to go back to step 3, causing us to repeat the procedure an infinite number of times.

The procedure lacks definiteness, as some steps are not precisely defined. As an example, consider step 10, which instructs us to report errors to the author.

For the procedure, the output is not clearly defined.

The procedure also lacks effectiveness, as some steps are not sufficiently basic and in principle can’t be done exactly and in a finite length of time. Consider steps 12 and 13 which instruct us to work on exercises in the book. In the case of exercises rated 50 or higher, research problems not yet solved satisfactorily, work on such exercises is clearly not bounded in time to be considered effective.

There are also differences in format. In particular: it is not headed by a label specifying it as an algorithm and with an identifying letter, such as Algorithm A; it is not accompanied by a brief description; step numbers are not preceded by the (missing) identifying letter, such as A1; each step is not introduced with a brief description in square brackets; parenthetical remarks do not appear to be limited to asserting invariant conditions; and the procedure is not terminated by a square symbol, such as .

6. [20] What is T5, the average number of times step E1 is performed when n = 5?

We want to determine T5, the average number of times step E1 is performed when n = 5. It is sufficient to consider only those cases where 1 m n. We count the steps s for each case below.

smsnsrs




1 1 5 1
2 5 1 0
smsnsrs




1 2 5 2
2 5 2 1
3 2 1 0
smsnsrs




1 3 5 3
2 5 3 2
3 3 2 1
4 2 1 0
smsnsrs




1 4 5 4
2 5 4 1
3 4 1 0
smsnsrs




1 5 5 0

The average number of steps is (2 + 3 + 4 + 3 + 1)5 = 135 = 2.6. That is, T5 = 2.6.

7. [M21] Suppose that m is known and that n is allowed to range over all positive integers; let Um be the average number of times that step E1 is executed in Algorithm E. Show that Um is well defined. Is Um in any way related to Tm?

Assume that the value of m is known but n is allowed to range over all positive integers. We want to know the average number of times, Um that step E1 of Algorithm E will be performed.

Um is well defined. For m < n, the first iteration of the algorithm simply switches m and n (constituting one performance of step E1); thereafter, on average, Tn additional steps are required. Otherwise, if m n, we only have a finite number of cases to consider for 1 n m, which as n , will contribute to the average.

Therefore, Um = Tn + 1.

8. [M25] Give an “effective” formal algorithm for computing the greatest common divisor of positive integers m and n, by specifying 𝜃j, ϕj, aj, bj as in Eqs. (3). Let the input be represented by the string ambn, that is, m a’s followed by n b’s. Try to make your solution as simple as possible. [Hint: Use Algorithm E, but instead of division in step E1, set r |m n|, n min (m,n).]

First, we develop an algorithm that doesn’t rely on division to determine the greatest common divisor.

Algorithm G (Euclid’s algorithm without division). Given two positive integers m and n, find their greatest common divisor, without division.

G1.
[Find difference.] Find the difference |m n| and let r be this difference. Also set n min (m,n). (We will have 0 r < n.)
G2.
[Is it zero?] If r = 0, the algorithm terminates with answer n.
G3.
[Reduce.] Set m n, n r, and go back to step G1.

Next, we express this algorithm formally, without any initial consideration of whether it is “effective” or not.

Let Q be the set of all ordered pairs (m,n); all ordered triples (m,n,1); all ordered quadruples (m,n,r,2), and (m,n,r,3); and all singletons (n); where m and n are positive integers and r is a nonnegative integer. Let I be the subset of all pairs (m,n), and let Ω be the subset of all singletons (n). Let f be defined as follows:

f((m,n)) = (m,n,1) f((m,n,1)) = (m,min (m,n),|m n|,2) f((m,n,r,2)) = (n) if r = 0(m,n,r,3) otherwise f((m,n,r,3)) = (n,r,1) f((n)) = (n)

Lastly, we redefine this algorithm formally to ensure it is also “effective.”

Let A = {a,b,c} be our alphabet, and N = 5. Given input ambn, our algorithm will terminate with agcd (m,n). Let 𝜃j, ϕj, aj, and bj be specified as follows:

j𝜃jϕjajbjNote






0ab𝜖21cr
1𝜖c00
2ab32bmin (m,n)
3ca43ar
4bb50repeat if r > 0
5𝜖𝜖55agcd (m,n)

(Note that here, 𝜖 denotes the empty string.)

9. [M30] Suppose that C1 = (Q1,I1,Ω1,f1) and C2 = (Q2,I2,Ω2,f2) are computational methods. For example, C1 might stand for Algorithm E as in Eqs. (2) except that m and n are restricted in magnitude, and C2 might stand for a computer program implementation of Algorithm E. (Thus Q2 might be the set of all states of the machine, i.e., all possible configurations of its memory and registers; f2 might be the definition of single machine actions; and I2 might be the set of initial states, each including the program that determines the greatest common divisor as well as the particular values of m and n.)

Formulate a set-theoretic definition for the concept “C2 is a representation of C1” or “C2 simulates C1.” This is to mean intuitively that any computation sequence of C1 is mimicked by C2, except that C2 might take more steps in which to do the computation and it might retain more information in its states. (We thereby obtain a rigorous interpretation of the statement, “Program X is an implementation of Algorithm Y .”)

Let C1 = (Q1,I1,Ω1,f1) and C2 = (Q2,I2,Ω2,f2) be computational methods. We say “C2 is a representation of C1” (or alternatively, “C2 simulates C1”) when the following properties hold:

1.
There exists a function tQ2 : Q2 Q1 that translates every state of C2 to one of C1. tQ2 is not necessarily injective or surjective, as C2 could very well enter additional states of computation not entered by C1.
2.
tQ2 is bijective over I2 I1, such that tQ21 is well defined for all i1 I1. If C2 accepts additional input not accepted by C1, suitable assignments for such additional input may be established in the defintion of f2 over I2.
3.
q2 Q2k +(f1(tQ2(q2)) = tQ2(f2k(q2))), where f2k(q2) = f2(q2) if k = 1, or f2k1(f2(q2)) otherwise. That is, k is the number of iterations of f2, and could be specified as a function tf2 : Q2 +.
4.
tQ2 is total over Ω2 Ω1, such that tQ2 covers the full domain of Ω2. That is to say, ω2 Ω2(ω2 Ω2tQ2(ω2) Ω1). tQ2 is not necessarily injective or surjective over Ω2 Ω1, as C2 could very well provide additional output not computed by C1.

This can be demonstrated as follows. Suppose C1 = (Q1,I1,Ω1,f1) where: I1 = {(m,n)}; Ω1 = {(n)}; Q1 = I1 {(m,n,r,1),(m,n,r,2),(m,n,p,3)} Ω1 for positive integers m, n, p, and nonnegative integer r; and f1 is defined as follows:

f1((m,n)) = (m,n,0,1) f1((m,n,r,1)) = (m,n,m mod n,2) f1((m,n,r,2)) = (n) if r = 0(m,n,r,3) otherwise f1((m,n,p,3)) = (n,p,p,1) f1((n)) = (n)

And suppose C2 = (Q2,I2,Ω2,f2) where: I2 = {(m,n)}; Ω1 = {(m,n,d)};
Q2 = I1 {(m,n,a,b,1),(m,n,a,b,r,2),(m,n,a,b,r,3),(m,n,a,b,r,4),(m,n,a,b,5)} Ω2 for positive integers a, b, m, n, and nonnegative integer r; and f2 is defined as follows:

f2((m,n)) = (m,n,m,n,1) f2((m,n,a,b,1)) = (m,n,a,b,a mod b,2) f2((m,n,a,b,r,2)) = (m,n,b) if r = 0(m,n,a,b,r,3) otherwise f2((m,n,a,b,r,3)) = (m,n,b,b,r,4) f2((m,n,a,b,r,4)) = (m,n,a,r,5) f2((m,n,a,b,5)) = f2(m,n,a,b,1) = (m,n,a,b,a mod b,2) f2((m,n,d)) = (m,n,d)

We then let tQ2((m,n)) = (m,n) so that tQ21((m,n)) = (m,n), and tQ2((m,n,d)) = (d). We additionally define:

tQ2((m,n,a,b,1)) = (a,b,0,1) tQ2((m,n,a,b,r,2)) = (a,b,r,2) tQ2((m,n,a,b,r,3)) = (m,n,a,b,r,3) tQ2((m,n,a,b,r,4)) = (a,b,b,1) tQ2((m,n,a,b,5)) = (a,b,b,1)

and tf2((m,n,a,b,r,3)) = tf2((m,n,a,b,r,4)) = 2 or tf2(q) = 1 otherwise. Observe that f1(tQ2(q2)) = tQ2(f2k(q2)) as required:

q2tQ2(q2)
f1(tQ2(q2))
= tQ2(f2k(q2))
kf2k(q2)Case






(m,n) (m,n)(m,n,0,1) 1(m,n,m,n,1)
(m,n,a,b,1) (a,b,0,1)(a,b,a mod b,2) 1(m,n,a,b,a mod b,2)
(m,n,a,b,r,2) (a,b,r,2)(b) 1(m,n,b)r = 0
(m,n,a,b,r,2) (a,b,r,2)(a,b,r,3) 1(m,n,a,b,r,3)r > 0
(m,n,a,b,r,3) (a,b,r,3)(b,r,r,1) 2(m,n,b,r,5)
(m,n,a,b,r,4) (a,b,b,1)(a,b,a mod b,2) 2(m,n,a,b,a mod b,2)
(m,n,a,b,r,5) (a,b,b,1)(a,b,a mod b,2) 1(m,n,a,b,a mod b,2)
(m,n,d) (d)(d) 1(m,n,d)

Hence, “C2 is a a representation of C1.”

________________________________________________________________________________

For an alternative approach to simulation, see R. W. Floyd and R. Beigel, The Language of Machines (Computer Science Press, 1994), Section 3.3.