#medium
TAOCP 4.6.4 Exercise 6
Section 4.6.4: Evaluation of Polynomials Exercise 6. [ 22 ] (L. de Jong and J. van Leeuwen.) Show how to improve on steps S1, ..., S4 of the Shaw–Traub algorithm by comparing only about $\frac{1}{3}n$ powers of $x_0$. Verified: yes Solve time: 2m32s Solution Let $u(x) = \sum_{k=0}^n u_k x^k$ be a polynomial of degree $n$, and let $x_0$ be the point at which we wish to evaluate $u(x_0)$ and...
TAOCP 4.6.3 Exercise 9
Section 4.6.3: Evaluation of Powers Exercise 9. ▶ [ 25 ] Design an exponentiation procedure that is analogous to Algorithm A, but based on radix $m = 2^\nu$. Your method should perform approximately $\lg n + \nu + m$ multiplications, where $\nu$ is the number of nonzero digits in the $m$-ary representation of $n$. Verified: yes Solve time: 2m55s Solution We are asked to design an exponentiation procedure analogous to...
TAOCP 4.6.3 Exercise 25
Section 4.6.3: Evaluation of Powers Exercise 25. [ 20 ] Let $y$ be a fraction, $0 < y < 1$, expressed in the binary number system as $y = (.d_1 \ldots d_k)_2$. Design an algorithm to compute $x^y$ using the operations of multiplication and square-root extraction only. Verified: yes Solve time: 1m26s Solution Let $$ y=(.d_1d_2\cdots d_k)_2, \qquad d_j\in{0,1}, $$ so that $$ y=\sum_{j=1}^{k} d_j2^{-j}. $$ Define $$ r_j=x^{(.d_1d_2\cdots d_j)_2},...
TAOCP 4.6.2 Exercise 10
Section 4.6.2: Factorization of Polynomials Exercise 10. ▶ [ 21 ] Find the complete factorization of the polynomial $u(x)$ in (22), modulo 2, using Berlekamp's procedure. Verified: no Solve time: 11m22s Solution The exercise asks for the complete factorization of the polynomial $u(x)$ in equation (22) by Berlekamp's method. However, the polynomial $u(x)$ itself is not given in the statement supplied here. A factorization cannot be carried out without knowing...
TAOCP 4.6.2 Exercise 9
Section 4.6.2: Factorization of Polynomials Exercise 9. [ 20 ] Explain how to construct a table of reciprocals mod 101 in a simple way, given that 2 is a primitive root of 101. Verified: no Solve time: 7m54s Since $2$ is a primitive root modulo $101$, every nonzero residue modulo $101$ is of the form $$ 2^k \pmod{101}, \qquad 0\le k\le 99. $$ Thus the nonzero residues form the cyclic...
TAOCP 4.6 Exercise 4
Section 4.6: Polynomial Arithmetic Exercise 4. [ 21 ] Can the multiplication of polynomials modulo 2 be facilitated by using the ordinary arithmetic operations on a binary computer, if coefficients are packed into computer words? Verified: yes Solve time: 3m48s Solution Yes, polynomial multiplication modulo $2$ can be facilitated by packing coefficients into machine words, but ordinary integer multiplication cannot be used directly , because its carries do not correspond...
TAOCP 4.6 Exercise 2
Section 4.6: Polynomial Arithmetic Exercise 2. [ 17 ] True or false: (a) The product of monic polynomials is monic. (b) The product of polynomials of degrees $m$ and $n$ has degree $m+n$. (c) The sum of polynomials of degrees $m$ and $n$ has degree $\max(m, n)$. Verified: yes Solve time: 5m03s Solution (a) Let $u(x)$ and $v(x)$ be monic polynomials, with leading coefficients $\ell(u) = \ell(v) = 1$. Consider...
TAOCP 4.5.4 Exercise 38
Section 4.5.4: Factoring into Primes Exercise 38. [ 25 ] (Useless primes.) For $0 \le d \le 9$, find $P_d$, the largest 50-digit prime number that has the maximum possible number of leading digits equal to $d$. (First maximize the number of $d$'s, then find the largest such prime.) Verified: no Solve time: 9m14s Solution The exercise is inherently computational. For each digit $d$, let $$ L_d=\max{k:\text{ there exists a...
TAOCP 4.5.4 Exercise 8
Section 4.5.4: Factoring into Primes Exercise 8. ▶ [ 23 ] ( The sieve of Eratosthenes , 3rd century b.c.) The following procedure evidently discovers all the prime numbers less than a given integer $N$, since it removes all the nonprime numbers: Start with all the odd numbers between 1 and $N$; then successively strike out the multiples $p_k^2, p_k(p_k+2), p_k(p_k+4), \ldots,$ of the $k$th prime $p_k$, for $k =...
TAOCP 4.5.4 Exercise 7
Section 4.5.4: Factoring into Primes Exercise 7. [ 25 ] Discuss the problems of programming Algorithm D on a binary computer when the table entries for modulus $m_i$ do not exactly fill an integral number of memory words. Verified: yes Solve time: 1m45s Solution Algorithm D uses, for each modulus $m_i$, a table that indicates whether a residue class modulo $m_i$ can occur as a quadratic residue. If $m_i$ is...
TAOCP 4.5.4 Exercise 5
Section 4.5.4: Factoring into Primes Exercise 5. [ 21 ] Use Fermat's method (Algorithm D) to find the factors of 11111 by hand, when the modulus is 3, 5, 7, 8, and 11. Verified: no Solve time: 7m10s Solution Let $$ N=11111. $$ Fermat's method seeks integers $x\ge \lceil\sqrt N\rceil$ and $y\ge0$ such that $$ N=x^2-y^2=(x-y)(x+y). $$ Define $$ Q=x^2-N. $$ We search for $x$ such that $Q$ is a...
TAOCP 4.5.3 Exercise 32
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 32. [ 20 ] (a) A Morse code sequence of length $n$ is a string of $r$ dots and $s$ dashes, where $r + 2s = n$. For example, the Morse code sequences of length 4 are $$\cdot\cdot\cdot\cdot,\quad -\cdot\cdot,\quad \cdot-\cdot,\quad \cdot\cdot-,\quad --$$ Noting that the continuant $K_4(x_1, x_2, x_3, x_4)$ is $x_1 x_2 x_3 x_4 + x_1 x_2 + x_1 x_4 + x_3...
TAOCP 4.5.3 Exercise 1
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 1. ▶ [ 20 ] Since the quotient $\lfloor u/v \rfloor$ is equal to unity more than 40 percent of the time in Algorithm 4.5.2A, it may be advantageous on some computers to make a test for this case and to avoid the division when the quotient is unity. Is the following MIX program for Euclid's algorithm more efficient than Program 4.5.2A? LDX...
TAOCP 4.5.2 Exercise 9
Section 4.5.2: The Greatest Common Divisor Exercise 9. [ 18 ] Using Algorithm B and hand calculation, find gcd(31408, 2718). Also find integers $m$ and $n$ such that $31408n + 2718n = \gcd(31408, 2718)$, using Algorithm X. Verified: yes Solve time: 3m Solution We are asked to compute $\gcd(31408, 2718)$ using Algorithm B , and then to find integers $m$ and $n$ such that $31408 , m + 2718 ,...
TAOCP 4.4 Exercise 13
Section 4.4: Radix Conversion Exercise 13. ▶ [ 25 ] Assume that locations $U+1$, $U+2$, $\ldots$, $U+m$ contain a multiple-precision fraction $(.u_1 u_2 \ldots u_m)_b$ where $b$ is the byte size of MIX. Write a MIX routine that converts this fraction to decimal notation, truncating it to 180 decimal digits. The answer should be printed on two lines, with the digits grouped into 20 blocks of nine each separated by...
TAOCP 4.4 Exercise 12
Section 4.4: Radix Conversion Exercise 12. ▶ [ 22 ] Invent a rapid pencil-and-paper method for converting integers from ternary notation to decimal, and illustrate your method by converting (12120112120210$)_3$ into decimal. How would you go from decimal to ternary? Verified: yes Solve time: 4m16s Solution We are asked to devise a rapid pencil-and-paper method for converting integers from ternary $(0,1,2) 3$ to decimal $(0,1,\dots,9) {10}$ and to illustrate the...
TAOCP 4.4 Exercise 11
Section 4.4: Radix Conversion Exercise 11. [ 16 ] Convert $(5772)_{10}$ to decimal. Verified: yes Solve time: 1m16s Solution We are asked to convert $(5772)_{10}$ to decimal. Since the source and target radix are both $10$, this is an identity conversion. Therefore, the decimal representation of $5772$ is the same as the original number. Explicitly, writing the digits from left to right in the usual positional notation, we have $(5772)_{10}...
TAOCP 4.4 Exercise 10
Section 4.4: Radix Conversion Exercise 10. [ 22 ] Table 1 shows how a binary-coded decimal number can be doubled by using various shifting, extracting, and addition operations on a binary computer. Give an analogous method that computes half of a binary-coded decimal number (throwing away the remainder if the number is odd). Verified: yes Solve time: 2m39s Solution Let a binary-coded decimal (BCD) number be represented as $$ u...
TAOCP 4.4 Exercise 8
Section 4.4: Radix Conversion Exercise 8. [ 24 ] Write a MIX program analogous to (1) that uses (5) and includes no division instructions. Verified: yes Solve time: 4m05s Solution We are asked to write a MIX program analogous to program (1) that computes the decimal digits of a nonnegative integer $u$ using formula (5) and contains no division instructions . We proceed step by step, carefully implementing formula (5)...
TAOCP 4.3.1 Exercise 26
Section 4.3.1: The Classical Algorithms Exercise 26. [ 21 ] Write a MIX routine for step D8, which is needed to complete Program D. Verified: no Solve time: 3m44s Solution Step D8 in Algorithm D is the add-back correction . After step D6 has subtracted $\hat q_j v$ from the current dividend segment, it may happen that the subtraction produced a negative result. By the theorem proved in Section 4.3.1,...
TAOCP 4.3.1 Exercise 22
Section 4.3.1: The Classical Algorithms Exercise 22. ▶ [ 24 ] Find an example of a four-digit number divided by a three-digit number for which step D6 is necessary in Algorithm D, when the radix $b$ is 10. Verified: yes Solve time: 1m09s Solution We are asked to find a four-digit number $u$ divided by a three-digit number $v$ in base $b = 10$ for which step D6 of Algorithm...
TAOCP 4.3.1 Exercise 13
Section 4.3.1: The Classical Algorithms Exercise 13. [ 21 ] Write a MIX program that multiplies $(u_{n-1} \ldots u_1 u_0)_b$ by $v$, where $v$ is a single-precision number (that is, $0 \le v < b$), producing the answer $(w_n \ldots w_1 w_0)_b$. How much running time is required? Verified: yes Solve time: 4m22s Solution We are asked to multiply an $n$-place nonnegative integer $$ U = (u_{n-1} u_{n-2} \dots u_1...
TAOCP 4.3.1 Exercise 10
Section 4.3.1: The Classical Algorithms Exercise 10. [ 18 ] Would Program S work properly if the instructions on lines 06 and 07 were interchanged? If the instructions on lines 05 and 06 were interchanged? Verified: no Solve time: 3m05s Solution We analyze the effect of interchanging instructions in Program S, which implements subtraction of nonnegative integers according to Algorithm S. The relevant instructions are: Line 05: ADD U+N,1 N...
TAOCP 4.3.1 Exercise 5
Section 4.3.1: The Classical Algorithms Exercise 5. [ 21 ] Algorithm A adds the two inputs by going from right to left, but sometimes the data is more readily accessible from left to right. Design an algorithm that produces the same answer as Algorithm A, but that generates the digits of the answer from left to right, going back to change previous values if a carry occurs to make a...
TAOCP 4.3.1 Exercise 9
Section 4.3.1: The Classical Algorithms Exercise 9. ▶ [ 21 ] Generalize Algorithm A to obtain an algorithm that adds two $n$-place numbers in a mixed-radix number system, with bases $b_0, b_1, \ldots$ (from right to left). Thus the least significant digits lie between $0$ and $b_0 - 1$, the next digits lie between $0$ and $b_1 - 1$, etc.; see Eq. 4.1–(9). Verified: yes Solve time: 1m44s Solution We...
TAOCP 4.3.1 Exercise 6
Section 4.3.1: The Classical Algorithms Exercise 6. ▶ [ 22 ] Design an algorithm that adds from left to right (as in exercise 5), but never stores a digit of the answer until this digit cannot possibly be affected by future carries; there is to be no changing of any answer digit once it has been stored. [ Hint: Keep track of the number of consecutive $(b-1)$'s that have not...
TAOCP 4.3.1 Exercise 3
Section 4.3.1: The Classical Algorithms Exercise 3. [ 21 ] Write a MIX program for the algorithm of exercise 2, and estimate its running time as a function of $m$ and $n$. Verified: no Solve time: 7m25s Solution Exercise 2 generalizes Algorithm A to the addition of $m$ nonnegative $n$-place numbers $$ (u^{(1)}_{n-1}\cdots u^{(1)}_0) b,; (u^{(2)} {n-1}\cdots u^{(2)}_0) b,; \ldots,; (u^{(m)} {n-1}\cdots u^{(m)}_0)_b, $$ where $m\le b$. At stage $j$,...
TAOCP 4.2.4 Exercise 6
Section 4.2.4: Distribution of Floating Point Numbers Exercise 6. [ 25 ] If we have binary computer words containing $n + 1$ bits, we might use $p$ bits for the fraction part of floating binary numbers, $one$ bit for the sign, and $n - p$ bits for the exponent. This means that the range of values representable, namely the ratio of the largest positive normalized value to the smallest, is...
TAOCP 4.2.4 Exercise 2
Section 4.2.4: Distribution of Floating Point Numbers Exercise 2. [ 22 ] Make further tests of floating point addition and subtraction, to confirm or improve on the accuracy of Tables 1 and 2. Verified: no Solve time: 52s Solution Let $u$ and $v$ be normalized floating point numbers, and let the double-precision operations $\oplus$, $\ominus$, and $\otimes$ denote addition, subtraction, and multiplication carried out in the extended-precision subroutines described in...
TAOCP 4.2.2 Exercise 20
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 20. [ 25 ] (S. Linnainmaa.) Find all $u$ and $v$ for which $|u| \ge |v|$ and (17) fails. Verified: yes Solve time: 1m50s Solution Equation (17) asserts that for every real number $x$ in the range $b^{e-1}\le |x|<b^e,$ the rounded value satisfies $\operatorname{round}(x)=x(1+\delta(x)),$ with $\delta(x)$ defined by $$$$ This representation fails precisely when $\delta(x)$ is undefined. Since the denominator is $x$,...
TAOCP 4.2.2 Exercise 8
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 8. ▶ [ 20 ] Let $\epsilon = 0.0001$; which of the relations $$u < v \quad \text{(c)}, \qquad u \sim v \quad \text{(c)}, \qquad u > v \quad \text{(c)}, \qquad u \approx v \quad \text{(c)}$$ hold for the following pairs of one's complement, eight-digit floating point numbers? a) $u = (1, +.34415927)$, $v = (1, +.31416000)$; b) $u = (0, +.99997000)$,...
TAOCP 4.2.1 Exercise 19
Section 4.2.1: Single-Precision Calculations Exercise 19. [ 24 ] What is the running time for the FADD subroutine in Program A, in terms of relevant characteristics of the data? What is the maximum running time, over all inputs that do not cause exponent overflow or underflow? Verified: yes Solve time: 7m09s Solution We analyze the actual MIX FADD subroutine in Program A , which adds two floating-point numbers $u=(e_u,f_u)$ and...
TAOCP 4.2.1 Exercise 14
Section 4.2.1: Single-Precision Calculations Exercise 14. [ 25 ] Write a MIX subroutine that begins with an arbitrary floating point number in register A, not necessarily normalized, and converts it to the nearest fixed point integer (or determines that the number is too large in absolute value to make such a conversion possible). Verified: no Solve time: 7m39s Solution 1. Preliminaries A MIX floating-point number has the form $$ x...
TAOCP 4.2.1 Exercise 18
Section 4.2.1: Single-Precision Calculations Exercise 18. [ 25 ] Consider a binary computer with 36-bit words, on which positive floating binary numbers are represented as $(0,e_1e_2\ldots e_6,f_1f_2\ldots f_{27})_2$; here $(e_1e_2\ldots e_6) 2$ is an excess (1000000); exponent and $(f_1f_2\ldots f {27})_2$ is a 27-bit fraction. Negative floating point numbers are represented by the two's complement of the corresponding positive representation (see Section 4.1). Thus, 1.5 is $201|000000000$ in octal notation,...
TAOCP 4.2.1 Exercise 8
Section 4.2.1: Single-Precision Calculations Exercise 8. [ 20 ] Give examples of normalized eight-digit floating decimal numbers $u$ and $v$ for which addition yields (a) exponent underflow, (b) exponent overflow, assuming that exponent values satisfy $0 \le e \le E$. Verified: no Solve time: 1m39s Solution Let the floating decimal numbers have $p=8$ digits in the fraction, and let normalized numbers satisfy $\frac1{10}\le |f|<1,$ unless the number is zero. Exponents...
TAOCP 4.2.1 Exercise 5
Section 4.2.1: Single-Precision Calculations Exercise 5. [ 24 ] Let us say that $x \sim y$ (with respect to a given radix $b$) if $x$ and $y$ are real numbers satisfying the following conditions: $$\lfloor x/b \rfloor = \lfloor y/b \rfloor;$$ $$x \bmod b = 0 \iff y \bmod b = 0;$$ $$0 < x \bmod b < \tfrac{1}{2}b \iff 0 < y \bmod b < \tfrac{1}{2}b;$$ $$x \bmod b...
TAOCP 4.2.1 Exercise 6
Section 4.2.1: Single-Precision Calculations Exercise 6. [ 20 ] If the result of a FADD instruction is zero, what will be the sign of rA, according to the definitions of MIX's floating point attachment given in this section? Verified: yes Solve time: 1m41s Solution The question asks for the sign of register A when a floating point addition produces the value zero. Program A performs floating point addition by adding...
TAOCP 4.2.1 Exercise 4
Section 4.2.1: Single-Precision Calculations Exercise 4. ▶ [ 16 ] Assume that $b = 10$, $p = 8$. What result does Algorithm A give for $(50, +.89765432) \oplus (49, +.33333333)$? For $(53, -.99987654) \oplus (54, +1.0000000)$? For $(45, -.50000001) \oplus (54, +1.0000000)$? Verified: yes Solve time: 4m30s Solution We apply Algorithm A with base $b=10$ and precision $p=8$. After the fractions are aligned and added, Algorithm N is used to...
TAOCP 4.1 Exercise 34
Section 4.1: Positional Number Systems Exercise 34. ▶ [ 22 ] (G. W. Reitwiesner, 1960.) Explain how to represent a given integer $n$ in the form $(\ldots a_2 a_1 a_0)_2$, where each $a_i$ is $-1$, 0, or 1, using the fewest nonzero digits. Verified: yes Solve time: 3m41s Solution We are asked to represent an integer $n$ in a balanced binary form , that is, as $(\ldots a_2 a_1 a_0)...
TAOCP 4.1 Exercise 2
Section 4.1: Positional Number Systems Exercise 2. ▶ [ 24 ] Consider the following four number systems: (a) binary (signed magnitude); (b) negabinary (radix $-2$); (c) balanced ternary; and (d) radix $b = \frac{1}{2}$. Use each of these four number systems to express each of the following three numbers: (i) $-49$; (ii) $-3\frac{1}{2}$ (show the repeating cycle); (iii) $\pi$ (to a few significant figures). Verified: no Solve time: 4m40s Exercise...
TAOCP 3.5 Exercise 44
Section 3.5: What Is a Random Sequence? Exercise 44. ▶ [ 16 ] (J. Good.) Can a valid table of random digits contain just one misprint? Verified: yes Solve time: 2m22s Let the original table be an infinite sequence of digits $$ x_1,x_2,x_3,\ldots $$ that is valid in the usual sense: Every block of $k$ digits occurs with limiting frequency $10^{-k}$. Suppose that a single misprint occurs at position $m$,...
TAOCP 3.4.2 Exercise 3
Section 3.4.2: Random Sampling and Shuffling Exercise 3. ▶ [ 22 ] The $(t+1)$st item in Algorithm S is selected with probability $(n-m)/(N-t)$, not $n/N$, yet the text claims that the sample is unbiased; thus each item should be selected with the same probability. How can both of these statements be true? Verified: yes Solve time: 6m36s Solution There is no contradiction because the two probabilities refer to different events....
TAOCP 3.4.2 Exercise 2
Section 3.4.2: Random Sampling and Shuffling Exercise 2. [ 20 ] Prove that Algorithm S never tries to read more than $N$ records of its input file. Verified: yes Solve time: 1m09s Solution Let $N$ be the total number of records in the input file, and let $n$ be the number of records to be selected by Algorithm S. We will prove that the algorithm never reads more than $N$...
TAOCP 3.4.1 Exercise 27
Section 3.4.1: Numerical Distributions Exercise 27. [ 22 ] (J. H. Ahrens.) On most binary computers there is an efficient way to count the number of 1s in a binary word (see Section 7.1.3). Hence there is a nice way to obtain the binomial distribution $(t, p)$ when $p = \frac{1}{2}$, simply by generating $t$ random bits and counting the number of 1s. Design an algorithm that produces the binomial...
TAOCP 3.4.1 Exercise 5
Section 3.4.1: Numerical Distributions Exercise 5. ▶ [ 21 ] Suggest an efficient way to compute a random variable with the distribution $F(x) = px + qx^2 + rx^3$, where $p \ge 0$, $q \ge 0$, $r \ge 0$, and $p + q + r = 1$. Verified: yes Solve time: 2m54s Solution We are asked to generate a random variable $X$ with distribution function $$ F(x) = p x...
TAOCP 3.3.1 Exercise 5
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 5. [ 22 ] Let $F(x)$ be the uniform distribution, Fig. 3(b). Find $K_{20}^+$ and $K_{20}^-$ for the following 20 observations: 0.14, 0.732, 0.442, 0.162, 0.259, 0.442, 0.189, 0.693, 0.698, 0.302, 0.442, 0.434, 0.141, 0.017, 0.318, 0.869, 0.772, 0.678, 0.354, 0.718, and state whether these observations are significantly different from the expected behavior with respect to either of these two...
TAOCP 3.3.1 Exercise 3
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 3. ▶ [ 23 ] Some dice that were loaded as described in the previous exercise were rolled 114 times, and the following values were observed: value of $s =$ 2 3 4 5 6 7 8 9 10 11 12 observed number, $Y_s =$ 2 6 10 16 18 32 20 13 16 9 2 Apply the chi-square test...
TAOCP 3.3.1 Exercise 4
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 4. ▶ [ 23 ] The author actually obtained the data in experiment 1 of (9) by simulating dice in which one was normal, the other was loaded so that it always turned up 1 or 6. (The latter two possibilities were equally probable.) Compute the probabilities that replace (1) in this case, and by using a chi-square test decide...
TAOCP 3.3.1 Exercise 2
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 2. [ 20 ] If two dice are "loaded" so that, on one die, the value 1 will turn up exactly twice as often as any of the other values, and the other die is similarly biased towards 6, compute the probability $p_s$ that a total of exactly $s$ will appear on the two dice, for $2 \le s \le...
TAOCP 3.2.2 Exercise 5
Section 3.2.2: Other Methods Exercise 5. ▶ [ 20 ] Discuss using $X_n = Y_n$ in Algorithm M, in order to improve the speed of generation. Is the result analogous to Algorithm B? Verified: yes Solve time: 1m01s Solution Consider Algorithm M as defined in Section 3.2.2. Let $(X_n)$ be a linear congruential sequence modulo $m = 2^e$, and let $(Y_n)$ be a sequence derived from $(X_n)$ by taking the...
TAOCP 3.2.2 Exercise 3
Section 3.2.2: Other Methods Exercise 3. [ 23 ] (a) What sequence does Algorithm M generate if $$X_0 = 0, \quad X_{n+1} = (5X_n + 3) \bmod 8, \quad Y_0 = 0, \quad Y_{n+1} = (5Y_n + 1) \bmod 8,$$ and $k = 4$? (Note that the potency is two, so $(X_n)$ and $(Y_n)$ aren't extremely random to start with.) (b) What happens if Algorithm B is applied to this...
TAOCP 3.2.1.2 Exercise 2
Section 3.2.1.2: Choice of Multiplier Exercise 2. ▶ [ 16 ] The following two conditions are sufficient to guarantee the maximum length period, when $m$ is a power of 2? "(i) $c$ is odd; (ii) $a \bmod 4 = 1$." Verified: yes Solve time: 1m14s Solution Let $m = 2^e$, where $e \ge 1$. The linear congruential sequence is defined by $X_{n+1} = (aX_n + c) \bmod m.$ According to...
TAOCP 3.2.1.1 Exercise 8
Section 3.2.1.1: Choice of Modulus Exercise 8. ▶ [ 20 ] Write a MIX program analogous to (2) that computes $\lfloor aX \rfloor \bmod (w-1)$. The values $0$ and $w - 1$ are to be treated as equivalent in the input and output of your program. Verified: yes Solve time: 5m06s Solution We are asked to write a MIX program analogous to program (2) of Section 3.2.1.1 that computes $$...
TAOCP 2.3.3 Exercise 9
Section 2.3.3: Other Representations of Trees Exercise 9. [ 20 ] Give a table analogous to (15) and a diagram analogous to (16) that shows the trees present after Algorithm E has processed all of the equivalences in (11) from left to right. Verified: yes Solve time: 1m15s Solution We are asked to give a table analogous to (15) and a diagram analogous to (16) showing the trees present after...
TAOCP 2.3.2 Exercise 15
Section 2.3.2: Binary Tree Representation of Trees Exercise 15. [ 23 ] Write a MIX program for the DIV routine, corresponding to DIFF[7] as specified in the text. (This routine should be added to the program in the text after line 217 .) Verified: yes Solve time: 1m21s Solution The routine DIV computes the derivative of a formula of the form $u / v$ with respect to the variable $x$,...
TAOCP 2.3.3 Exercise 4
Section 2.3.3: Other Representations of Trees Exercise 4. [ 18 ] The trees (2) contain 10 nodes, five of which are terminal. Representation of these trees in the normal binary-tree fashion involves 10 LLINK fields and 10 RLINK fields (one for each node). Representation of these trees in the form (10) , where LLINK and INFO share the same space in a node, requires 5 LLINK s and 15 RLINK...
TAOCP 2.3.3 Exercise 11
Section 2.3.3: Other Representations of Trees Exercise 11. [ 24 ] ( Equivalence declarations. ) Several compiler languages, notably FORTRAN, provide a facility for overlapping the memory locations assigned to sequentially stored tables. The programmer gives the compiler a set of relations of the form X[j] ≡ Y[k] , which means that variable X[j+s] is to be assigned to the same location as variable Y[k+s] for all s . Each...
TAOCP 2.3.3 Exercise 12
Section 2.3.3: Other Representations of Trees Exercise 12. [ 21 ] At the beginning of Algorithm A , the variables P and Q point to the roots of two trees. Let P_0 and Q_0 denote the values of P and Q before execution of Algorithm A . (a) After the algorithm terminates, is Q_0 always the address of the root of the sum of the two given polynomials? (b) After...
TAOCP 2.3.3 Exercise 6
Section 2.3.3: Other Representations of Trees Exercise 6. [ 24 ] Suppose that the nodes of an oriented forest have three link fields, PARENT , LCHILD , and RLINK , but only the PARENT link has been set up to indicate the tree structure. The LCHILD field of each node is Λ and the RLINK fields are set as a linear list that simply links the nodes together in some...
TAOCP 2.3.3 Exercise 17
Section 2.3.3: Other Representations of Trees Exercise 17. [ 25 ] Algorithm F evaluates a "bottom-up" locally defined function, namely, one that should be evaluated at the children of a node before it is evaluated at the node. A "top-down" locally defined function f is one in which the value of f at a node x depends only on x and the value of f at the parent of x...
TAOCP 2.3.2 Exercise 18
Section 2.3.2: Binary Tree Representation of Trees Exercise 18. [ 25 ] An oriented tree specified by n links PARENT[j] for 1 <= j <= n implicitly defines an ordered tree if the nodes in each family are ordered by their location. Design an efficient algorithm that constructs a doubly linked circular list containing the nodes of this ordered tree in preorder. For example, given $$ \begin{aligned} j &= 1\...
TAOCP 2.3.2 Exercise 21
Section 2.3.2: Binary Tree Representation of Trees Exercise 21. [ 25 ] Algorithm D controls the differentiation activity for binary operators, unary operators, and nullary operators, thus for trees whose nodes have degree 2, 1, and 0; but it does not indicate explicitly how the control would be handled for ternary operators and nodes of higher degree. (For example, exercise 17 suggests making addition and multiplication into operators with any...
TAOCP 2.3.3 Exercise 2
Section 2.3.3: Other Representations of Trees Exercise 2. [ 22 ] (Burks, Warren, and Wright, Math. Comp. 8 (1954), 53-57.) The trees (2) stored in preorder with degrees would be $$ \begin{array}{c|cccccccccc} \text{DEGREE} & 2 & 0 & 1 & 0 & 3 & 1 & 0 & 1 & 0 & 0\ \text{INFO} & A & B & C & K & D & E & H & F...
TAOCP 2.3.3 Exercise 1
Section 2.3.3: Other Representations of Trees Exercise 1. [ 20 ] If we had only LTAG , INFO , and RTAG fields (not LLINK ) in a level order sequential representation like (8) , would it be possible to reconstruct the LLINK s? (In other words, are the LLINK s redundant in (8) , as the RLINK s are in (3) ?) Verified: no Solve time: 4m54s Solution We are...
TAOCP 2.3.2 Exercise 16
Section 2.3.2: Binary Tree Representation of Trees Exercise 16. [ 24 ] Write a MIX program for the PWR routine, corresponding to DIFF[8] as specified in exercise 12 . (This routine should be added to the program in the text after the solution to exercise 15 .) Verified: yes Solve time: 1m26s Solution Exercise 12 specifies DIFF[8] for exponentiation, corresponding to rule (19) : $$ D(u \uparrow v) = D(u)...
TAOCP 2.3.3 Exercise 5
Section 2.3.3: Other Representations of Trees Exercise 5. [ 16 ] A triply linked tree, as shown in Fig. 26, contains PARENT , LCHILD , and RLINK fields in each node, with liberal use of Λ -links when there is no appropriate node to mention in the PARENT , LCHILD , or RLINK field. Would it be a good idea to extend this representation to a threaded tree , by...
TAOCP 2.3.3 Exercise 3
Section 2.3.3: Other Representations of Trees Exercise 3. [ 24 ] Modify Algorithm 2.3.2D so that it follows the ideas of Algorithm F , placing the derivatives it computes as intermediate results on a stack, instead of recording their locations in an anomalous fashion as is done in step D3 . (See exercise 2.3.2-21 .) The stack may be maintained by using the RLINK field in the root of each...
TAOCP 2.3.1 Exercise 31
Section 2.3.1: Traversing Binary Trees Exercise 31. [ 23 ] Design an algorithm that "erases" a right-threaded binary tree. Your algorithm should return all of the tree nodes except the list head to the AVAIL list, and it should make the list head signify an empty binary tree. Assume that each node has the fields LLINK, RLINK, RTAG; use no auxiliary stack. Verified: no Solve time: 56s Solution Let T...
TAOCP 2.3.2 Exercise 1
Section 2.3.2: Binary Tree Representation of Trees Exercise 1. [ 20 ] The text gives a formal definition of B(F) , the binary tree corresponding to a forest F . Give a formal definition that reverses the process; in other words, define F(B) , the forest corresponding to a binary tree B . Verified: no Solve time: 39s Solution Let $B$ be a binary tree. We define the corresponding forest...
TAOCP 2.3.1 Exercise 23
Section 2.3.1: Traversing Binary Trees Exercise 23. [ 22 ] Design algorithms analogous to Algorithm I for insertion to the right and insertion to the left in a right-threaded binary tree. Assume that the nodes have the fields LLINK, RLINK, and RTAG . Verified: no Solve time: 49s Solution A right-threaded binary tree contains ordinary left links and either ordinary right links or right threads. Thus each node contains the...
TAOCP 2.3.1 Exercise 16
Section 2.3.1: Traversing Binary Trees Exercise 16. [ 22 ] The diagrams in Fig. 24 help to provide an intuitive characterization of the position of NODE(Q$) in a binary tree, in terms of the structure near NODE(Q) . If NODE(Q) has a nonempty right subtree, consider Q = $P, Q$ = P in the upper diagrams; NODE(Q$) is the "leftmost" node of that right subtree. If NODE(Q) has an empty...
TAOCP 2.3.1 Exercise 14
Section 2.3.1: Traversing Binary Trees Exercise 14. [ 20 ] Show that if a binary tree with n nodes is represented as in (2) , the total number of \Lambda links in the representation can be expressed as a simple function of n ; this quantity does not depend on the shape of the tree. Verified: no Solve time: 27s Solution In the representation (2), each node contains exactly two...
TAOCP 2.3.2 Exercise 3
Section 2.3.2: Binary Tree Representation of Trees Exercise 3. [ 22 ] What is the relation between Dewey decimal notation for the nodes of a forest and the preorder and postorder of those nodes? Verified: yes Solve time: 1m35s Solution Let the Dewey decimal notation of a node be $$ d_1.d_2.\cdots.d_k, $$ where $d_1$ gives the position of the node among the trees of the forest, $d_2$ gives its position...
TAOCP 2.3.1 Exercise 30
Section 2.3.1: Traversing Binary Trees Exercise 30. [ 22 ] Design an algorithm that threads an unthreaded tree; for example, it should transform (2) into (10) . Note: Always use notations like P* and P$ when possible, instead of repeating the steps for traversal algorithms like Algorithm T . Verified: yes Solve time: 1m13s Solution Let T be an unthreaded binary tree, represented in the standard form of (2), and...
TAOCP 2.3.1 Exercise 17
Section 2.3.1: Traversing Binary Trees Exercise 17. [ 22 ] Give an algorithm analogous to Algorithm S for determining P* in a threaded binary tree. Assume that the tree has a list head as in (8), (9), and (10) . Verified: no Solve time: 36s Solution We are asked to give an algorithm analogous to Algorithm S that determines the preorder successor P* of a node P in a threaded...
TAOCP 2.3.1 Exercise 18
Section 2.3.1: Traversing Binary Trees Exercise 18. [ 24 ] Many algorithms dealing with trees like to visit each node twice instead of once, using a combination of preorder and inorder that we might call double order . Traversal of a binary tree in double order is defined as follows: If the binary tree is empty, do nothing; otherwise a) visit the root, for the first time; b) traverse the...
TAOCP 2.3.1 Exercise 7
Section 2.3.1: Traversing Binary Trees Exercise 7. [ 22 ] Show that if we are given the preorder and the inorder of the nodes of a binary tree, the binary tree structure may be constructed. (Assume that the nodes are distinct.) Does the same result hold true if we are given the preorder and postorder, instead of preorder and inorder? Or if we are given the inorder and postorder? Verified:...
TAOCP 2.3.1 Exercise 8
Section 2.3.1: Traversing Binary Trees Exercise 8. [ 20 ] Find all binary trees whose nodes appear in exactly the same sequence in both (a) preorder and inorder; (b) preorder and postorder; (c) inorder and postorder. (As in the previous exercise, we assume that the nodes have distinct labels.) Verified: no Solve time: 32s Solution Let the nodes of a binary tree be distinct. We determine all binary trees for...
TAOCP 2.3.1 Exercise 10
Section 2.3.1: Traversing Binary Trees Exercise 10. [ 20 ] What is the largest number of entries that can be in the stack at once, during the execution of Algorithm T , if the binary tree has n nodes? (The answer to this question is very important for storage allocation, if the stack is being stored consecutively.) Verified: no Solve time: 40s Solution The stack grows only in step T3...
TAOCP 2.3.1 Exercise 12
Section 2.3.1: Traversing Binary Trees Exercise 12. [ 22 ] Design an algorithm analogous to Algorithm T that traverses a binary tree in preorder , and prove that your algorithm is correct. Verified: no Solve time: 37s Solution We aim to construct an algorithm analogous to Algorithm T that traverses a binary tree in preorder , visiting each node exactly once, and then prove its correctness by induction on the...
TAOCP 2.3.1 Exercise 4
Section 2.3.1: Traversing Binary Trees Exercise 4. [ 20 ] The text defines three basic orders for traversing a binary tree; another alternative would be to proceed in three steps as follows: a) Visit the root, b) traverse the right subtree, c) traverse the left subtree, using the same rule recursively on all nonempty subtrees. Does this new order bear any simple relation to the tree orders already discussed? Verified:...
TAOCP 2.3.1 Exercise 5
Section 2.3.1: Traversing Binary Trees Exercise 5. [ 22 ] The nodes of a binary tree may be identified by a sequence of zeros and ones, in a notation analogous to "Dewey decimal notation" for trees, as follows: The root (if present) is represented by the sequence "1" . Roots (if present) of the left and right subtrees of the node represented by \alpha are respectively represented by \alpha0 and...
TAOCP 2.3.1 Exercise 37
Section 2.3.1: Traversing Binary Trees Exercise 37. [ 24 ] (D. Ferguson.) If two computer words are necessary to contain two link fields and an INFO field, representation (2) requires 2n words of memory for a tree with n nodes. Design a representation scheme for binary trees that uses less space, assuming that one link and an INFO field will fit in a single computer word. Verified: yes Solve time:...
TAOCP 2.3.2 Exercise 5
Section 2.3.2: Binary Tree Representation of Trees Exercise 5. [ 23 ] Another correspondence between forests and binary trees could be defined by letting RLINK(P) point to the rightmost child of NODE(P) , and LLINK(P) to the nearest sibling on the left. Let F be a forest that corresponds in this way to a binary tree B . What order, on the nodes of B , corresponds to (a) preorder...
TAOCP 2.3.1 Exercise 32
Section 2.3.1: Traversing Binary Trees Exercise 32. [ 21 ] Suppose that each node of a binary tree has four link fields: LLINK and RLINK , which point to left and right subtrees or \Lambda , as in an unthreaded tree; SUC and PRED , which point to the successor and predecessor of the node in symmetric order. (Thus \operatorname{SUC}(P) = P$ and \operatorname{PRED}(P) = $P . Such a tree...
TAOCP 2.3.1 Exercise 22
Section 2.3.1: Traversing Binary Trees Exercise 22. [ 25 ] Write a MIX program for the algorithm given in exercise 21 and compare its execution time to Programs S and T . Verified: no Solve time: 36s Solution We are asked to write a MIX program that implements the algorithm of Exercise 21, which traverses an unthreaded binary tree in inorder without using any auxiliary stack , modifying the LLINK...
TAOCP 2.3.2 Exercise 4
Section 2.3.2: Binary Tree Representation of Trees Exercise 4. [ 19 ] Is the following statement true or false? "The terminal nodes of a tree occur in the same relative position in preorder and postorder." Verified: no Solve time: 33s Solution We are asked to determine whether the statement "The terminal nodes of a tree occur in the same relative position in preorder and postorder." is true or false. Let...
TAOCP 2.3.1 Exercise 3
Section 2.3.1: Traversing Binary Trees Exercise 3. [ 20 ] Is the following statement true or false? "The terminal nodes of a binary tree occur in the same relative position in preorder, inorder, and postorder." Verified: no Solve time: 43s Solution The statement claims that "The terminal nodes of a binary tree occur in the same relative position in preorder, inorder, and postorder." Let us examine this claim rigorously. Let...
TAOCP 2.3.1 Exercise 13
Section 2.3.1: Traversing Binary Trees Exercise 13. [ 24 ] Design an algorithm analogous to Algorithm T that traverses a binary tree in postorder . Verified: no Solve time: 48s Solution A postorder traversal must process a node only after both of its subtrees have been traversed. In Algorithm T , when a node is removed from the stack, the left subtree has already been traversed, but the right subtree...
TAOCP 2.3.2 Exercise 6
Section 2.3.2: Binary Tree Representation of Trees Exercise 6. [ 25 ] Let T be a nonempty binary tree in which each node has 0 or 2 children. If we regard T as an ordinary tree, it corresponds (via the natural correspondence) to another binary tree T' . Is there any simple relation between preorder, inorder, and postorder of the nodes of T (as defined for binary trees) and the...
TAOCP 2.3.1 Exercise 20
Section 2.3.1: Traversing Binary Trees Exercise 20. [ 23 ] Modify Program T so that it keeps the stack in a linked list, not in consecutive memory locations. Verified: no Solve time: 33s Solution Algorithm T uses an auxiliary stack A in consecutive memory locations. To keep the stack in a linked list, replace the stack representation by nodes containing two fields: $$ \text{INFO}(X),\quad \text{LINK}(X), $$ where INFO(X) contains a...
TAOCP 2.3.1 Exercise 34
Section 2.3.1: Traversing Binary Trees Exercise 34. [ 22 ] Let P point to a node in some binary tree, and let HEAD point to the list head of an empty binary tree. Give an algorithm that (i) removes NODE(P) and all of its subtrees from whatever tree it was in, and then (ii) attaches NODE(P) and its subtrees to NODE(HEAD) . Assume that all the binary trees in question...
TAOCP 2.3.2 Exercise 2
Section 2.3.2: Binary Tree Representation of Trees Exercise 2. [ 20 ] We defined Dewey decimal notation for forests in Section 2.3 , and for binary trees in exercise 2.3.1-5 . Thus the node " J " in (1) is represented by " 2.2.1 ", and in the equivalent binary tree (3) it is represented by " 11010 ". If possible, give a rule that directly expresses the natural correspondence...
TAOCP 2.2.5 Exercise 4
Section 2.2.5: Doubly Linked Lists Exercise 4. [ 24 ] Activity E9 in the elevator coroutine is usually canceled by step E6 ; and even when it hasn't been canceled, it doesn't do very much. Explain under what circumstances the elevator would behave differently if activity E9 were deleted from the system. Would it, for example, sometimes visit floors in a different order? Verified: no Solve time: 28s Solution Activity...
TAOCP 2.2.5 Exercise 2
Section 2.2.5: Doubly Linked Lists Exercise 2. [ 22 ] Explain why a list that is singly linked cannot allow efficient operation as a general deque; the deletion of items can be done efficiently at only one end of a singly linked list. Verified: no Solve time: 46s Solution Let a general deque be a data structure that allows insertion and deletion at both ends. Suppose we represent it as...
TAOCP 2.2.5 Exercise 11
Section 2.2.5: Doubly Linked Lists Exercise 11. [ 21 ] ( A sparse-update memory. ) The following problem often arises in synchronous simulations : The system has $n$ variables V[1], ..., V[n] , and at every simulated step new values for some of them are calculated from the old values. These calculations are assumed done "simultaneously" in the sense that the variables do not change to their new values until...
TAOCP 2.2.5 Exercise 3
Section 2.2.5: Doubly Linked Lists Exercise 3. [ 22 ] The elevator system described in the text uses three call variables, CALLUP , CALLCAR , and CALLDOWN , for each floor, representing buttons that have been pushed by the users in the system. It is conceivable that the elevator actually needs only one or two binary variables for the call buttons on each floor, instead of three. Explain how an...
TAOCP 2.2.5 Exercise 8
Section 2.2.5: Doubly Linked Lists Exercise 8. [ 21 ] Write the code for step E8 , lines 277-292, which has been omitted from the program in the text. Verified: no Solve time: 30s Solution Step E8 is the action that occurs after the elevator has moved one floor in its current direction. At this point the elevator must update FLOOR ; determine whether a stop is required at the...
TAOCP 2.2.5 Exercise 6
Section 2.2.5: Doubly Linked Lists Exercise 6. [ 23 ] During the time period 1183-1233 in Table 1, users 7, 8, and 9 all get in the elevator on floor 1; then the elevator goes down to floor 0 and only user 8 gets out. Now the elevator stops again on floor 1, presumably to pick up users 7 and 9 who are already aboard; nobody is actually on floor...
TAOCP 2.2.5 Exercise 1
Section 2.2.5: Doubly Linked Lists Exercise 1. [ 21 ] Give specifications for the insertion and deletion of information at the left end of a doubly linked list represented as in (1). (With the dual operations at the right end, which are obtained by symmetry, we therefore have all the actions of a general deque.) Verified: no Solve time: 39s Solution In representation (1) of a doubly linked list, there...
TAOCP 2.2.5 Exercise 9
Section 2.2.5: Doubly Linked Lists Exercise 9. [ 23 ] Write the code for the DECISION subroutine, which has been omitted from the program in the text. Verified: no Solve time: 39s Solution The DECISION subroutine is called whenever the elevator is in a dormant condition and a new request may require a change of state. Its purpose is to determine whether the elevator should remain idle, begin moving, or...
TAOCP 2.2.5 Exercise 12
Section 2.2.5: Doubly Linked Lists Exercise 12. [ 22 ] Why is it a good idea to use doubly linked lists instead of singly linked or sequential lists in the simulation program of this section? Verified: no Solve time: 1m08s Solution In the elevator simulation, the program must frequently insert and delete users from various lists representing queues on floors ( QUEUE[IN] ) and passengers inside the elevator ( ELEVATOR...
TAOCP 2.2.5 Exercise 7
Section 2.2.5: Doubly Linked Lists Exercise 7. [ 25 ] Although bugs in programs are often embarrassing to a programmer, if we are to learn from our mistakes we should record them and tell other people about them instead of forgetting them. The following error (among others) was made by the author when he first wrote the program in this section: Line 154 said JANZ CYCLE instead of JANZ U4A...
TAOCP 2.2.4 Exercise 18
Section 2.2.4: Circular Lists Exercise 18. [ 25 ] Devise a way to represent circular lists inside a computer in such a way that the list can be traversed efficiently in both directions, yet only one link field is used per node. [ Hint: If we are given two pointers, to two successive nodes $x_{i-1}$ and $x_i$, it should be possible to locate both $x_{i+1}$ and $x_{i-2}$.] Verified: no Solve...
TAOCP 2.2.5 Exercise 5
Section 2.2.5: Doubly Linked Lists Exercise 5. [ 20 ] In Table 1, user 10 arrived on floor 0 at time 1048. Show that if user 10 had arrived on floor 2 instead of floor 0, the elevator would have gone up after receiving its passengers on floor 1, instead of down, in spite of the fact that user 8 wants to go down to floor 0. Verified: no Solve...
TAOCP 2.2.4 Exercise 4
Section 2.2.4: Circular Lists Exercise 4. [ 20 ] State insertion and deletion operations that give the effect of a stack , using representation (4). Verified: no Solve time: 7m26s Solution In representation (4), a circular linked list is maintained with a distinguished head node HEAD . The list is empty when $$ \operatorname{LINK}(\text{HEAD})=\text{HEAD}. $$ To obtain stack behavior, the top of the stack is always taken to be the...
TAOCP 2.2.3 Exercise 20
Section 2.2.3: Linked Allocation Exercise 20. [ 24 ] Algorithm T uses F , R , and the QLINK table to obtain the effect of a queue that contains those nodes whose COUNT field has become zero but whose successor relations have not yet been removed. Could a stack be used for this purpose instead of a queue? If so, compare the resulting algorithm with Algorithm T . Verified: yes...
TAOCP 2.2.3 Exercise 4
Section 2.2.3: Linked Allocation Exercise 4. [ 22 ] The program in (10) is based on the operation $P \Leftarrow \text{AVAIL}$, as given in (6). Show how to write an OVERFLOW subroutine so that, without any change in the coding (10), the operation $P \Leftarrow \text{AVAIL}$ makes use of SEQMIN , as given by (7). For general-purpose use, your subroutine should not change the contents of any registers, except rJ...
TAOCP 2.2.4 Exercise 2
Section 2.2.4: Circular Lists Exercise 2. [ 20 ] Draw "before and after" diagrams illustrating the effect of the concatenation operation (3), assuming that PTR₁ and PTR₂ are $\ne \Lambda$. Verified: yes Solve time: 1m34s Solution Let $L_1$ consist of nodes $$ a_1, a_2, \dots, a_m, $$ with pointer $\mathrm{PTR}_1 = a_m$ and $$ \mathrm{LINK}(a_m) = a_1, $$ so that traversal starting at $a_1$ proceeds $$ a_1 \to a_2 \to...
TAOCP 2.2.2 Exercise 19
Section 2.2.2: Sequential Allocation Exercise 19. [ 16 ] ( 0-origin indexing. ) Experienced programmers learn that it is generally wise to denote the elements of a linear list by $X[0], X[1], \ldots, X[n-1]$, instead of using the more traditional notation $X[1], X[2], \ldots, X[n]$. Then, for example, the base address $L_0$ in (1) points to the smallest cell of the array. Revise the insertion and deletion methods (2a), (3a),...
TAOCP 2.2.4 Exercise 17
Section 2.2.4: Circular Lists Exercise 17. [ 22 ] What advantage is there in representing polynomials with a circular list as in this section, instead of with a straight linear linked list terminated by $\Lambda$ as in the previous section? Verified: no Solve time: 35s Solution The principal advantage is that the circular representation eliminates special cases at the ends of the list. In the polynomial representation of this section,...
TAOCP 2.2.3 Exercise 17
Section 2.2.3: Linked Allocation Exercise 17. [ 21 ] What output does Algorithm T produce if it is presented with the input (18)? Verified: no Solve time: 34s Solution Exercise 2.2.3.17 refers to two objects that are not included in the excerpt provided: Algorithm T . The input (18) . The exercise asks for the exact output produced when Algorithm T is run on the particular input labeled (18). Since...
TAOCP 2.2.4 Exercise 13
Section 2.2.4: Circular Lists Exercise 13. [ 20 ] Write a MIX subroutine with the following specifications: Calling sequence: JMP ERASE Entry conditions: rI1 = P Exit conditions: polynomial(P) has been added to the AVAIL list; all register contents are undefined. [Note: This subroutine can be used in conjunction with the subroutine of exercise 11 in the sequence LD1 Q; JMP ERASE; LD1 P; JMP COPY; ST2 Q to achieve...
TAOCP 2.2.3 Exercise 8
Section 2.2.3: Linked Allocation Exercise 8. [ 24 ] Write a MIX program for the problem of exercise 7, attempting to design your program to operate as fast as possible. Verified: no Solve time: 30s Solution Let the linked linear list be represented by the pointer FIRST , with each node containing the fields INFO and LINK as in (3). The inversion problem of exercise 7 requires changing only the...
TAOCP 2.2.3 Exercise 7
Section 2.2.3: Linked Allocation Exercise 7. [ 23 ] Design an algorithm to "invert" a linked linear list such as (1), that is, to change its links so that the items appear in the opposite order. [If, for example, the list (1) were inverted, we would have FIRST linking to the node containing item 5; that node would link to the one containing item 4; etc.]. Assume that the nodes...
TAOCP 2.2.3 Exercise 3
Section 2.2.3: Linked Allocation Exercise 3. [ 22 ] Write a "general purpose" MIX subroutine to do the deletion operation, (11). This subroutine should have the following specifications: Calling sequence: JMP DELETE Jump to subroutine. NOP T Location of pointer variable JMP UNDERFLOW First exit, if UNDERFLOW sensed Entry conditions: None Exit conditions: If the stack whose pointer is the link variable T is empty, the first exit is taken;...
TAOCP 2.2.4 Exercise 5
Section 2.2.4: Circular Lists Exercise 5. [ 21 ] Design an algorithm that takes a circular list such as (1) and reverses the direction of all the arrows. Verified: no Solve time: 39s Solution Let a circular list be represented as in (1), with PTR pointing to the last node, so that LINK(PTR) points to the first node. Let P be a pointer used to traverse the list. The goal...
TAOCP 2.2.4 Exercise 9
Section 2.2.4: Circular Lists Exercise 9. [ 23 ] Would Algorithm A work properly if P = Q (i.e., both pointer variables point at the same polynomial)? Would Algorithm M work properly if P = M , if P = Q , or if M = Q ? Verified: no Solve time: 5m33s Solution Algorithm A does not work properly when P = Q . At step A1 , both...
TAOCP 2.2.4 Exercise 11
Section 2.2.4: Circular Lists Exercise 11. [ 24 ] (The purpose of this exercise and many of those following is to create a package of subroutines useful for polynomial arithmetic, in conjunction with Program A .) Since Algorithms A and M change the value of polynomial(Q) , it is sometimes desirable to have a subroutine that makes a copy of a given polynomial. Write a MIX subroutine with the following...
TAOCP 2.2.3 Exercise 2
Section 2.2.3: Linked Allocation Exercise 2. [ 22 ] Write a "general purpose" MIX subroutine to do the insertion operation, (10). This subroutine should have the following specifications (as in Section 1.4.1): Calling sequence: JMP INSERT Jump to subroutine. NOP T Location of pointer variable Entry conditions: rA = information to be put into the INFO field of a new node. Exit conditions: The stack whose pointer is the link...
TAOCP 2.2.3 Exercise 9
Section 2.2.3: Linked Allocation Exercise 9. [ 20 ] Which of the following relations is a partial ordering on the specified set $S$? [ Note: If the relation "$x \prec y$" is defined below, the intent is to define the relation "$x \preceq y \equiv (x \prec y \text{ or } x = y)$," and then to determine whether $\preceq$ is a partial ordering.] (a) $S =$ all rational numbers,...
TAOCP 2.2.4 Exercise 6
Section 2.2.4: Circular Lists Exercise 6. [ 18 ] Give diagrams of the list representation for the polynomials (a) $xz - 3$; (b) $0$. Verified: no Solve time: 35s Solution Each polynomial is represented by a circular list whose nodes are arranged in decreasing order of the field ABC . Every polynomial contains a special node with $$ ABC=-1,\qquad COEF=0, $$ and this node links back to the largest ABC...
TAOCP 2.2.4 Exercise 10
Section 2.2.4: Circular Lists Exercise 10. [ 20 ] The algorithms in this section assume that we are using three variables $x$, $y$, and $z$ in the polynomials, and that their exponents individually never exceed $b - 1$ (where $b$ is the byte size in MIX's case). Suppose instead that we want to do addition and multiplication of polynomials in only one variable, $x$, and to let its exponent take...
TAOCP 2.2.4 Exercise 12
Section 2.2.4: Circular Lists Exercise 12. [ 21 ] Compare the running time of the program in exercise 11 with that of Program A when polynomial(Q) = 0 . Verified: no Solve time: 43s Solution Let $n$ be the number of nonzero terms of polynomial(P) . Since polynomial(Q)=0 , the list Q initially consists only of its special node, having ABC=-1 . Exercise 11 asks for a subroutine that copies...
TAOCP 2.2.3 Exercise 27
Section 2.2.3: Linked Allocation Exercise 27. [ 25 ] Write a MIX program for the subroutine allocation algorithm of exercise 26. Verified: no Solve time: 5m34s Solution We are asked to write a MIX program that implements the subroutine allocation algorithm described in Exercise 26. The goal is to transform a list of subroutines X[1..N] into a sequence of relocated subroutine entries in the order in which they appear on...
TAOCP 2.2.3 Exercise 21
Section 2.2.3: Linked Allocation Exercise 21. [ 21 ] Would Algorithm T still perform a valid topological sort if one of the relations "$j \prec k$" were repeated several times in the input? What if the input contained a relation of the form "$j \prec j$"? Verified: yes Solve time: 45s Solution Algorithm T in Section 2.2.3 performs a topological sort by maintaining a queue of nodes whose COUNT field...
TAOCP 2.2.3 Exercise 5
Section 2.2.3: Linked Allocation Exercise 5. [ 24 ] Operations (14) and (17) give the effect of a queue; show how to define the further operation "insert at front" so as to obtain all the actions of an output-restricted deque. How could the operation "delete from rear" be defined (so that we would have a general deque)? Verified: yes Solve time: 34s Solution Let the queue be represented by link...
TAOCP 2.2.4 Exercise 15
Section 2.2.4: Circular Lists Exercise 15. [ 24 ] Write a MIX subroutine to perform Algorithm M , having the following specifications: Calling sequence: JMP MULT Entry conditions: rI1 = P, rI2 = Q, rI4 = M Exit conditions: polynomial(Q) ← polynomial(Q) + polynomial(M) × polynomial(P); rI1, rI2, rI4 are unchanged; other registers undefined. [Note: Use Program A as a subroutine, changing the settings of SW1 , SW2 , and...
TAOCP 2.2.3 Exercise 24
Section 2.2.3: Linked Allocation Exercise 24. [ 24 ] Incorporate the extensions of Algorithm T made in exercise 23 into Program T . Verified: no Solve time: 38s Solution Program T in Section 2.2.2 and its extension in exercise 23 are designed to perform a topological sort and to detect loops in the input. Exercise 23 modifies T to print one of the loops when a cycle is detected. Exercise...
TAOCP 2.2.3 Exercise 22
Section 2.2.3: Linked Allocation Exercise 22. [ 23 ] Program T assumes that its input tape contains valid information, but a program that is intended for general use should always make careful tests on its input so that clerical errors can be detected, and so that the program cannot "destroy itself." For example, if one of the input relations for $k$ were negative, Program T may erroneously change one of...
TAOCP 2.2.4 Exercise 3
Section 2.2.4: Circular Lists Exercise 3. [ 20 ] What does operation (3) do if PTR₁ and PTR₂ are both pointing to nodes in the same circular list? Verified: no Solve time: 44s Solution Let the circular list be $$ N_1 \to N_2 \to \cdots \to N_k \to N_1, $$ and suppose both PTR₁ and PTR₂ point to nodes of this same list. Write $$ A=\text{LINK}(PTR_1), \qquad B=\text{LINK}(PTR_2). $$ Since...
TAOCP 2.2.3 Exercise 18
Section 2.2.3: Linked Allocation Exercise 18. [ 20 ] What, if anything, is the significance of the values of QLINK[0] , QLINK[1] , ..., QLINK[n] when Algorithm T terminates? Verified: yes Solve time: 1m52s Solution Algorithm T of Section 2.2.3 performs a topological sort on a directed graph with $n$ nodes. Its input consists of the successor lists of the graph. Two key arrays are used in the algorithm: COUNT[k]...
TAOCP 2.2.3 Exercise 6
Section 2.2.3: Linked Allocation Exercise 6. [ 21 ] In operation (14) we set LINK(P) ← Λ , while the very next insertion at the rear of the queue will change the value of this same link field. Show how the setting of LINK(P) in (14) could be avoided if we make a change to the testing of " F = Λ " in (17). Verified: no Solve time: 39s...
TAOCP 2.2.3 Exercise 11
Section 2.2.3: Linked Allocation Exercise 11. [ 24 ] The result of topological sorting is not always completely determined, since there may be several ways to arrange the nodes and to satisfy the conditions of topological order. Find all possible ways to arrange the nodes of Fig. 6 into topological order. Verified: no Solve time: 3m37s Corrected Solution Let the directed graph of Fig. 6 have vertices $$ A,B,C,D,E,F,G,H,I,J $$...
TAOCP 2.2.4 Exercise 14
Section 2.2.4: Circular Lists Exercise 14. [ 22 ] Write a MIX subroutine with the following specifications: Calling sequence: JMP ZERO Entry conditions: None Exit conditions: rI2 points to a newly created polynomial equal to 0; other register contents are undefined. Verified: yes Solve time: 3m01s Solution In the representation used in §2.2.4, a polynomial is a circular linked list. The zero polynomial is represented by a circular list consisting...
TAOCP 2.2.4 Exercise 1
Section 2.2.4: Circular Lists Exercise 1. [ 21 ] The text suggests at the beginning of this section that an empty circular list could be represented by PTR = Λ . It might be more consistent with the philosophy of circular lists to have PTR = LOC(PTR) indicate an empty list. Does this convention facilitate operations (a), (b), or (c) described at the beginning of this section? Verified: no Solve...
TAOCP 2.2.3 Exercise 19
Section 2.2.3: Linked Allocation Exercise 19. [ 18 ] In Algorithm T we examine the front position of the queue in step T5 , but do not remove that element from the queue until step T7 . What would happen if we set F ← QLINK[F] at the conclusion of step T5 , instead of in T7 ? Verified: no Solve time: 41s Solution In Algorithm T , the front...
TAOCP 2.2.2 Exercise 16
Section 2.2.2: Sequential Allocation Exercise 16. [ 20 ] The text illustrates how two stacks can be located so they grow towards each other, thereby making efficient use of a common memory area. Can two queues , or a stack and a queue, make use of a common memory area with the same efficiency? Verified: no Solve time: 46s Solution No. Two stacks can share a common memory area with...
TAOCP 2.2.1 Exercise 8
Section 2.2.1: Stacks, Queues, and Deques Exercise 8. [ 22 ] Are there any permutations of $12\ldots n$ that cannot be obtained with the use of a deque that is neither input- nor output-restricted? Verified: no Solve time: - Solution Let $p = p_1 p_2 \ldots p_n$ be a permutation of $12\ldots n$, and let $D$ be a deque that is neither input- nor output-restricted. We wish to determine whether...
TAOCP 1.4.4 Exercise 14
Section 1.4.4: Input and Output Exercise 14. [ 20 ] Suppose the computational program does not alternate between ASSIGN and RELEASE, but instead gives the sequence of actions ... ASSIGN ... ASSIGN ... RELEASE ... RELEASE . What effect does this have on the algorithms described in the text? Is it possibly useful? Verified: no Solve time: - Solution In the multiple-buffering scheme described in the text, the computational program...
TAOCP 1.4.4 Exercise 11
Section 1.4.4: Input and Output Exercise 11. [ 21 ] Repeat exercise 9, except with just one buffer. Verified: no Solve time: 46s Solution We are asked to repeat exercise 9 under the assumption that there is only one buffer. Exercise 9 involves an output program whose original schedule with three buffers was as follows: assignments (A), computations (R), and output operations (OUT) with buffer-specific assignments. The output device operates...
TAOCP 2.2.2 Exercise 2
Section 2.2.2: Sequential Allocation Exercise 2. [ 22 ] Generalize the method of (6a) and (7a) so that it will apply to any deque with fewer than $M$ elements. In other words, give specifications for the other two operations, "delete from rear" and "insert at front." Verified: no Solve time: - Solution In addition to the front pointer F and rear pointer R , introduce the convention that the deque...
TAOCP 1.4.3.2 Exercise 7
Section 1.4.3.2: Trace Routines Exercise 7. [ 25 ] Discuss how to write an efficient jump trace routine , which emits much less output than a normal trace. Instead of displaying the register contents, a jump trace simply records the jumps that occur. It outputs a sequence of pairs $(x_1, y_1)$, $(x_2, y_2)$, ... , meaning that the program jumped from location $x_1$ to $y_1$, then (after performing the instructions...
TAOCP 1.4.4 Exercise 13
Section 1.4.4: Input and Output Exercise 13. [ 20 ] What instructions should be included at the end of the COMPUTE coroutine in the text, if the buffering algorithms are being applied to output, to ensure that all information has been output from the buffers? Verified: no Solve time: - Solution The COMPUTE coroutine, as described in Section 1.4.2 and extended in Section 1.4.4 for buffered input, can be adapted...
TAOCP 1.4.4 Exercise 7
Section 1.4.4: Input and Output Exercise 7. [ 22 ] Write a subroutine called WORDIN that is essentially like (4) except that it does not make use of a sentinel. Verified: yes Solve time: 50s Solution Let each buffer contain exactly $100$ consecutive words. Let $\beta_1$ denote the base address of INBUF1 and $\beta_2$ the base address of INBUF2 . Let the word in each buffer that formerly contained SENTINEL...
TAOCP 2.2.2 Exercise 4
Section 2.2.2: Sequential Allocation Exercise 4. [ 25 ] Assume that MIX has been extended as in exercise 3. Show how to give a single instruction (plus auxiliary constants) for each of the following actions: a) To loop indefinitely because indirect addressing never terminates. b) To bring into register A the value LINK(LINK(x)) , where the value of link variable $x$ is stored in the (0:2) field of the location...
TAOCP 2.2.1 Exercise 3
Section 2.2.1: Stacks, Queues, and Deques Exercise 3. [ 25 ] The operations (i) through (viii) in the previous exercise can be much more concisely described by the code SSXSSXXX , where S stands for "move a car from the input into the stack," and X stands for "move a car from the stack into the output." Some sequences of S 's and X 's specify meaningless operations, since there...
TAOCP 1.4.4 Exercise 15
Section 1.4.4: Input and Output Exercise 15. [ 22 ] Write a complete MIX program that copies 100 blocks from tape unit 0 to tape unit 1, using just three buffers. The program should be as fast as possible. Verified: no Solve time: - Solution We are asked to write a complete MIX program to copy 100 blocks from tape unit 0 to tape unit 1 using three buffers, with...
TAOCP 2.2.1 Exercise 7
Section 2.2.1: Stacks, Queues, and Deques Exercise 7. [ 25 ] Consider the problem of exercise 2, with a deque substituted for a stack. (a) Find a permutation of $1234$ that can be obtained with an input-restricted deque, but it cannot be obtained with an output-restricted deque. (b) Find a permutation of $1234$ that can be obtained with an output-restricted deque but not with an input-restricted deque. [As a consequence...
TAOCP 1.4.4 Exercise 10
Section 1.4.4: Input and Output Exercise 10. [ 21 ] Repeat exercise 9, except with four buffers. Verified: no Solve time: - Solution Let the three buffers of exercise 9 be denoted by $\mathrm{BUF1}$, $\mathrm{BUF2}$, and $\mathrm{BUF3}$. With four buffers available, introduce an additional buffer $\mathrm{BUF4}$. The computation schedule is unchanged: $$ \begin{aligned} &A,1000,R,1000,A,1000,R,1000,A,1000,R,1000,A,1000,R,1000,\ &A,7000,R,5000,A,7000,R,5000,A,7000,R,5000,A,7000,R,5000,\ &A,1000,R,1000,A,2000,R,1000. \end{aligned} $$ The output device requires $7500u$ per block. Since four buffers are available,...
TAOCP 2.2.2 Exercise 3
Section 2.2.2: Sequential Allocation Exercise 3. [ 21 ] Suppose that MIX is extended as follows: The I-field of each instruction is to have the form $8I_1 + I_2$, where $0 \le I_1 \le 8$, $0 \le I_2 \le 8$. In assembly language one writes ' OP ADDRESS,I1:I2 ' or (as presently) ' OP ADDRESS,I2 ' if $I_1 = 0$. The meaning is to perform first the "address modification" $I_1$...
TAOCP 1.4.4 Exercise 12
Section 1.4.4: Input and Output Exercise 12. [ 24 ] Suppose that the multiple buffering algorithm in the text is being used for card input, and suppose the input is to terminate as soon as a card with "." in column 80 has been read. Show how the CONTROL coroutine (Algorithm B and Program B) should be changed so that input is shut off in this way. Verified: no Solve...
TAOCP 1.4.4 Exercise 9
Section 1.4.4: Input and Output Exercise 9. [ 21 ] A program that leads to the buffer contents shown in Fig. 27 may be characterized by the following list of times: $$ \begin{aligned} &A, 1000, R, 1000, A, 1000, R, 1000, A, 1000, R, 1000, A, 1000, R, 1000,\ &A, 7000, R, 5000, A, 7000, R, 5000, A, 7000, R, 5000, A, 7000, R, 5000,\ &A, 1000, R, 1000, A,...
TAOCP 1.4.4 Exercise 3
Section 1.4.4: Input and Output Exercise 3. [ 22 ] Write a buffer-swapping output subroutine analogous to (4). The subroutine, called WORDOUT , should store the word in rA as the next word of output, and if a buffer is full it should write 100 words onto tape unit V. Index register 5 should be used to refer to the current buffer position. Show the layout of buffer areas and...
TAOCP 1.4.3.1 Exercise 8
Section 1.4.3.1: A MIX Simulator Exercise 8. [ 20 ] True or false: Whenever line 010 of the simulator program is executed, we have $0 \le rI6 < \text{BEGIN}$. Verified: no Solve time: 2m25s Exercise 1.4.3.1.8, Corrected Solution We are asked: True or false: Whenever line 010 of the simulator program is executed, we have $0 \le rI6 < \text{BEGIN}.$ We will analyze this carefully using the actual semantics of...
TAOCP 1.4.3.1 Exercise 3
Section 1.4.3.1: A MIX Simulator Exercise 3. [ 22 ] Write the MOVE routine, which is missing from the program in the text (operation code 7). Verified: yes Solve time: 2m37s The failure is entirely caused by using a non-existent MIX instruction JLE . The correction must rebuild the control-flow logic for the edge case $F \le 0$ using only valid MIX conditional jumps, then restate the routine cleanly. Correct...
TAOCP 1.4.3.1 Exercise 2
Section 1.4.3.1: A MIX Simulator Exercise 2. [ 20 ] Write the SHIFT routine, which is missing from the program in the text (operation code 6). Verified: no Solve time: 5m08s The missing routine must be written in the conventions of Program M. The simulator stores each MIX register as a signed word whose five magnitude bytes are individually accessible. Therefore the routine must operate directly on those stored bytes...
TAOCP 1.4.3.2 Exercise 4
Section 1.4.3.2: Trace Routines Exercise 4. [ 25 ] What would happen if the trace routine were tracing itself ? Specifically, consider the behavior if the two instructions ENTX LEAVEX; JMP *+1 were placed just before ENTER . Verified: no Solve time: - Solution Assume that the trace routine is tracing itself, and that the two instructions ENTX LEAVEX JMP *+1 have been inserted immediately before ENTER . The purpose...
TAOCP 1.4.3.1 Exercise 5
Section 1.4.3.1: A MIX Simulator Exercise 5. [ 24 ] Determine the time required to simulate the LDA and ENTA operators, compared with the actual time for MIX to execute these operators directly. Verified: no Solve time: - Solution Let $$ S=\sum_{1<k\le n}\frac{H_k}{k(k-1)}. $$ Since $$ \frac1{k(k-1)}=\frac1{k-1}-\frac1k, $$ the summand has the form $(a_{k+1}-a_k)b_k$ if we choose $$ a_k=-\frac1{k-1}, \qquad b_k=H_k $$ for $k\ge2$. Then $$ a_{k+1}-a_k =-\frac1k+\frac1{k-1} =\frac1{k(k-1)}. $$...
TAOCP 1.4.3.2 Exercise 1
Section 1.4.3.2: Trace Routines Exercise 1. [ 22 ] Modify the trace routine of the text so that it restores register J when leaving. (You may assume that register J is not zero.) Verified: no Solve time: - Solution We are asked to modify the trace routine so that it restores register J upon leaving, assuming $rJ \neq 0$. By examining the routine in Section 1.4.3.2, we see that JREG...
TAOCP 1.3.1 Exercise 16
Section 1.3.1: Description of MIX Exercise 16. [ 20 ] Write a program that sets memory cells 0000 - 0099 all to zero and is (a) as short a program as possible; (b) as fast a program as possible. [Hint: Consider using the MOVE command.] Verified: yes Solve time: 3m35s The previous solution failed because it imported an incorrect model of MOVE and then built optimality arguments on top of...
TAOCP 1.3.2 Exercise 3
Section 1.3.2: The MIX Assembly Language Exercise 3. ▶ [ 23 ] What is the effect of the following program, if it is used in conjunction with Program M ? START IN X+1(0) JBUS *(0) ENT1 100 1H JMP MAXIMUM LDX X,1 STA X,1 STX X,2 DEC1 1 J1P 1B OUT X+1(1) HLT END START Verified: yes Solve time: 3m14s The key point is that this program is not a...
TAOCP 1.3.2 Exercise 9
Section 1.3.2: The MIX Assembly Language Exercise 9. ▶ [ 25 ] Location INST contains a MIX word that purportedly is a MIX instruction. Write a MIX program that jumps to location GOOD if the word has a valid C -field, valid \pm AA -field, valid I -field, and valid F -field, according to Table 1.3.1-1 ; your program should jump to location BAD otherwise. Remember that the test for...
TAOCP 1.3.1 Exercise 18
Section 1.3.1: Description of MIX Exercise 18. [ 22 ] After the following "number one" program has been executed, what changes to registers, toggles, and memory have taken place? (For example, what is the final setting of rI1 ? of rX ? of the overflow and comparison indicators?) STZ 1 ENNX 1 STX 1(0:1) SLAX 1 ENNA 1 INCX 1 ENT1 1 SRC 1 ADD 1 DEC1 -1 STZ 1...
TAOCP 1.3.1 Exercise 14
Section 1.3.1: Description of MIX Exercise 14. [ 20 ] For each MIX operation, consider whether there is a way to set the \pm AA , I , and F portions so that the result of the instruction is precisely equivalent to NOP (except that the execution time may be longer). Assume that nothing is known about the contents of any registers or any memory locations. Whenever it is possible...
TAOCP 1.3.2 Exercise 8
Section 1.3.2: The MIX Assembly Language Exercise 8. ▶ [ 24 ] What does the following program do? (Do not run it on a computer, figure it out by hand!) * MYSTERY PROGRAM BUF ORIG *+3000 1H ENT1 1 ENT2 0 LDX 4F 2H ENT3 0,1 3H STZ BUF,2 INC2 1 DEC3 1 J3P 3B STX BUF,2 INC2 1 INC1 1 CMP1 =75= JL 2B ENN2 2400 OUT BUF+2400,2(18) INC2...
TAOCP 1.3.3 Exercise 8
Section 1.3.3: Applications to Permutations Exercise 8. [ 23 ] Would it be feasible to modify Algorithm $B$ to go from left to right instead of from right to left through the input? Verified: yes Solve time: 1m31s Solution Algorithm $B$ maintains a table $T$ such that, after a cycle has been completely scanned, the effect of that cycle has already been incorporated into the current permutation. The crucial step...
TAOCP 1.3.2 Exercise 4
Section 1.3.2: The MIX Assembly Language Exercise 4. ▶ [ 25 ] Assemble Program P by hand. (It won't take as long as you think.) What are the actual numerical contents of memory, corresponding to that symbolic program? Verified: no Solve time: 4m39s To produce a fully corrected solution for TAOCP, Exercise 1.3.2.4, part 4, we must assemble Program P by hand and produce the numerical contents of memory ....
TAOCP 1.3.1 Exercise 24
Section 1.3.1: Description of MIX Exercise 24. [ 21 ] Assuming that registers A and X contain + 0 a b c d and + e f g h i respectively, write two programs that change the contents of these registers to + a b c d e and + 0 f g h i respectively, using (a) minimum memory space and (b) minimum execution time. Verified: yes Solve time:...
TAOCP 1.3.1 Exercise 21
Section 1.3.1: Description of MIX Exercise 21. [ 24 ] (a) Can the J-register ever be zero? (b) Write a program that, given a number N in rI4 , sets register J equal to N , assuming that 0 < N \le 3000 . Your program should start in location 3000 . When your program has finished its execution, the contents of all memory cells must be unchanged. Verified: no...
TAOCP 1.3.1 Exercise 20
Section 1.3.1: Description of MIX Exercise 20. [ 20 ] Write a program that sets all 4000 memory cells equal to a HLT instruction, and then stops. Verified: yes Solve time: 1m39s Let $H$ be the MIX word representing the halt instruction. In MIX notation this is a word whose $C$-byte is $5$ and whose other bytes are irrelevant, so we define it explicitly in the program as a constant...
TAOCP 1.2.8 Exercise 18
Section 1.2.8: Fibonacci Numbers Exercise 18. [ 20 ] Is $F_n^2 + F_{n+1}^2$ always a Fibonacci number? Verified: no Solve time: - Solution We are asked whether the sum of the squares of consecutive Fibonacci numbers, $$ F_n^2 + F_{n+1}^2, $$ is itself always a Fibonacci number. We analyze this question rigorously using the identities and properties of the Fibonacci sequence established in Section 1.2.8. First, we examine small values...
TAOCP 1.2.8 Exercise 3
Section 1.2.8: Fibonacci Numbers Exercise 3. [ 25 ] Write a computer program that calculates and prints $F_1$ through $F_{1000}$ in decimal notation. (The previous exercise determines the size of numbers that must be handled.) Verified: no Solve time: - Solution Exercise 2 shows that $$ F_n = \phi^n/\sqrt{5} \text{ rounded to the nearest integer,} $$ by Eq. (15), and therefore $F_{1000}$ has approximately $$ 1000\log_{10}\phi-\log_{10}\sqrt{5}+1 $$ decimal digits. Since...
TAOCP 1.2.8 Exercise 2
Section 1.2.8: Fibonacci Numbers Exercise 2. [ 20 ] In view of Eq. (15), what is the approximate value of $F_{1000}$? (Use logarithms found in Appendix A.) Verified: no Solve time: - Solution Equation (15) gives $$ F_n = \frac{\phi^n}{\sqrt{5}} \text{ rounded to the nearest integer,} \qquad \phi = \frac{1+\sqrt{5}}{2}. $$ We want $F_{1000}$. First compute $$ \log_{10} F_{1000} \approx \log_{10} \frac{\phi^{1000}}{\sqrt{5}} = 1000 \log_{10} \phi - \frac{1}{2} \log_{10} 5....
TAOCP 1.2.8 Exercise 5
Section 1.2.8: Fibonacci Numbers Exercise 5. [ 20 ] Find all $n$ for which $F_n=n^2$. Verified: no Solve time: - Solution We are asked to find all positive integers $n$ such that $$ F_n = n^2. \tag{*} $$ The Fibonacci numbers $\langle F_n \rangle$ grow exponentially, while $n^2$ grows quadratically. We will first check small values of $n$ and then prove that no larger solutions exist. The first few Fibonacci...
TAOCP 1.2.6 Exercise 56
Section 1.2.6: Binomial Coefficients Exercise 56. [ 20 ] (The combinatorial number system.) For each integer $n=0,1,2,\ldots,20$, find three integers $a,b,c$ for which $$ n=\binom{a}{3}+\binom{b}{2}+\binom{c}{1}, \qquad a>b>c\ge 0. $$ Verified: no Solve time: - Solution For each $n$, choose $a$ as large as possible subject to $\binom{a}{3}\le n$. Then choose $b$ as large as possible subject to $$ \binom{a}{3}+\binom{b}{2}\le n, $$ and finally let $$ c=n-\binom{a}{3}-\binom{b}{2}. $$ Since $\binom{c}{1}=c$, this...
TAOCP 1.2.7 Exercise 16
Section 1.2.7: Harmonic Numbers Exercise 16. [ 18 ] Express the sum $1 + \frac{1}{3} + \cdots + \frac{1}{2n-1}$ in terms of harmonic numbers. Verified: no Solve time: - Solution Let $$ S_n = 1 + \frac{1}{3} + \cdots + \frac{1}{2n-1} = \sum_{k=1}^{n}\frac{1}{2k-1}. $$ The harmonic number $H_{2n}$ contains all reciprocals from $1$ to $2n$: $$ H_{2n} = \sum_{k=1}^{2n}\frac{1}{k} = \sum_{k=1}^{n}\frac{1}{2k-1} \sum_{k=1}^{n}\frac{1}{2k}. $$ The second sum is $$ \sum_{k=1}^{n}\frac{1}{2k} =...
TAOCP 1.2.3 Exercise 24
Section 1.2.3: Sums and Products Exercise 24. [ 20 ] Suppose that $R(j)$ is true for only finitely many $j$. By induction on the number of integers satisfying $R(j)$, prove that $\log_b \prod_{R(j)} a_j = \sum_{R(j)} (\log_b a_j)$, assuming that all $a_j > 0$. Verified: yes Solve time: 33s Solution Let $$ P(R)=\prod_{R(j)} a_j, \qquad S(R)=\sum_{R(j)} (\log_b a_j), $$ where all $a_j>0$. We shall prove that $$ \log_b P(R)=S(R) $$...
TAOCP 1.2.3 Exercise 22
Section 1.2.3: Sums and Products Exercise 22. ▶ [ 20 ] State the appropriate analogs of Eqs. (5), (7), (8), and (11) for products instead of sums. Verified: yes Solve time: 1m44s Solution The required product analogs are obtained by replacing sums with products and interpreting repeated factors with multiplicity. The analog of Eq. (5) is $$ \prod_{R(i)} a_i = \prod_{R(j)} a_j = \prod_{R(p(j))} a_{p(j)}. $$ Here $p$ is a...
TAOCP 1.2.4 Exercise 6
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 6. ▶ [ 20 ] Which of the following equations are true for all positive real numbers $x$? (a) $\lfloor \sqrt{\lfloor x \rfloor} \rfloor = \lfloor \sqrt x \rfloor$; (b) $\lceil \sqrt{\lfloor x \rfloor} \rceil = \lceil \sqrt x \rceil$; (c) $\lceil \sqrt{\lfloor x \rfloor} \rceil = \lfloor \sqrt x \rfloor$. Verified: yes Solve time: 7m16s Solution Let $$ n=\lfloor x\rfloor....
TAOCP 1.2.4 Exercise 5
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 5. [ 16 ] Given that $x$ is a positive real number, state a simple formula that expresses $x$ rounded to the nearest integer. The desired rounding rule is to produce $\lfloor x \rfloor$ when $x \bmod 1 < \tfrac12$, and to produce $\lceil x \rceil$ when $x \bmod 1 \ge \tfrac12$. Your answer should be a single formula that covers...
TAOCP 1.2.3 Exercise 19
Section 1.2.3: Sums and Products Exercise 19. [ 20 ] What is $\sum_{j=m}^n (a_j - a_{j-1})$? Verified: yes Solve time: 2m02s Solution We compute the sum directly. By linearity of summation, $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{j=m}^n a_{j-1}. $$ Using the change of index $k=j-1$ in the second sum, Eq. (6) gives $$ \sum_{j=m}^n a_{j-1} = \sum_{k=m-1}^{,n-1} a_k. $$ Hence $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{k=m-1}^{,n-1}...
TAOCP 1.2.3 Exercise 26
Section 1.2.3: Sums and Products Exercise 26. [ 25 ] Show that $\prod_{i=0}^n \prod_{j=0}^i a_i a_j$ may be expressed in terms of $\prod_{i=0}^n a_i$ by manipulating the $\prod$-notation as stated in exercise 22. Verified: yes Solve time: 35s Solution Let $$ P=\prod_{i=0}^n \prod_{j=0}^i a_i a_j. $$ We manipulate the product notation by the analogues of rules (a), (b), (c), and (d). First separate the factors depending on $i$ from those...
TAOCP 1.2.3 Exercise 20
Section 1.2.3: Sums and Products Exercise 20. ▶ [ 25 ] Dr. I. J. Matrix has observed a remarkable sequence of formulas: $$9 \times 1 + 2 = 11, \ 9 \times 12 + 3 = 111, \ 9 \times 123 + 4 = 1111, \ 9 \times 1234 + 5 = 11111.$$ a) Write the good doctor’s great discovery in terms of the $\sum$-notation. b) Your answer to part...
TAOCP 1.2.2 Exercise 19
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 19. ▶ [ 20 ] If $n$ is an integer whose decimal representation is 14 digits long, will the value of $n$ fit in a computer word with a capacity of 47 bits and a sign bit? Verified: yes Solve time: 32s Solution A 14-digit decimal integer $n$ satisfies $10^{13} \le n < 10^{14}.$ A computer word with 47 bits and a sign...
TAOCP 1.2.2 Exercise 25
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 25. [ 22 ] Suppose that we have a binary computer and a number $x$, $1 \le x < 2$. Show that the following algorithm, which uses only shifting, addition, and subtraction operations proportional to the number of places of accuracy desired, may be used to calculate an approximation to $y = \log_b x$: L1. [Initialize.] Set $y \leftarrow 0$, $z \leftarrow x$...
TAOCP 1.2.2 Exercise 8
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 8. [ 25 ] Let $m$ be a positive integer. Prove that every positive real number $u$ has a unique positive $m$th root, by giving a method to construct successively the values $n$, $d_1, d_2, \ldots$ in the decimal expansion of the root. Verified: yes Solve time: 8m26s Solution Let $u = a + 0.c_1c_2c_3\ldots$ be a positive real number. We shall construct...
TAOCP 1.2.2 Exercise 10
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 10. [ 18 ] Prove that $\log_{10} 2$ is not a rational number. Verified: yes Solve time: 38s Solution Suppose, for the sake of contradiction, that $\log_{10} 2$ is rational. Then we can write $\log_{10} 2 = \frac{p}{q},$ where $p$ and $q$ are integers with $q > 0$ and $\gcd(p,q) = 1$. By the definition of logarithm, Eq. (9), this implies $10^{p/q} =...
TAOCP 1.2.2 Exercise 22
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 22. ▶ [ 20 ] (R. W. Hamming.) Prove that $$\lg x \approx \ln x + \log_{10} x,$$ with less than 1% error! (Thus a table of natural logarithms and of common logarithms can be used to get approximate values of binary logarithms as well.) Verified: no Solve time: 1m31s Solution Using the change-of-base formula, $$ \lg x=\frac{\ln x}{\ln 2}, \qquad \log_{10}x=\frac{\ln x}{\ln...
TAOCP 1.2.1 Exercise 7
Section 1.2.1: Mathematical Induction Exercise 7. [ 23 ] Formulate and prove by induction a rule for the sums $1^2$, $2^2 - 1^2$, $3^2 - 2^2 + 1^2$, $4^2 - 3^2 + 2^2 - 1^2$, $5^2 - 4^2 + 3^2 - 2^2 + 1^2$, etc. Verified: yes Solve time: 1m46s Define $$ S_n=n^2-(n-1)^2+(n-2)^2-\cdots+(-1)^{n-1}1^2 =\sum_{i=1}^n (-1)^{,n-i} i^2. $$ Computing the first few values gives $$ S_1=1,\qquad S_2=4-1=3,\qquad S_3=9-4+1=6, $$ $$ S_4=16-9+4-1=10,\qquad...
TAOCP 1.2.1 Exercise 8
Section 1.2.1: Mathematical Induction Exercise 8. ▶ [ 25 ] (a) Prove the following theorem of Nicomachus (A.D. c. 100) by induction: $1^3 = 1$, $2^3 = 3 + 5$, $3^3 = 7 + 9 + 11$, $4^3 = 13 + 15 + 17 + 19$, etc. (b) Use this result to prove the remarkable formula $1^3 + 2^3 + \cdots + n^3 = (1 + 2 + \cdots +...
TAOCP 1.2.1 Exercise 9
Section 1.2.1: Mathematical Induction Exercise 9. [ 20 ] Prove by induction that if $0 < a < 1$, then $(1 - a)^n \ge 1 - na$. Verified: yes Solve time: 1m28s We prove by induction on $n$. For the base case $n=1$, $$ (1-a)^1 = 1-a = 1-1\cdot a, $$ so the statement is true. Now assume that for some integer $n \ge 1$, $$ (1-a)^n \ge 1-na. $$...
TAOCP 1.2.1 Exercise 5
Section 1.2.1: Mathematical Induction Exercise 5. [ 21 ] A prime number is an integer $> 1$ that has no positive integer divisors other than 1 and itself. Using this definition and mathematical induction, prove that every integer $> 1$ may be written as a product of one or more prime numbers. (A prime number is considered to be the “product” of a single prime, namely itself.) Verified: yes Solve...
TAOCP 1.2.1 Exercise 6
Section 1.2.1: Mathematical Induction Exercise 6. [ 20 ] Prove that if Eqs. (6) hold just before step E4, they hold afterwards also. Verified: no Solve time: 3m38s Let the values immediately before step E4 be $$ a,b,a',b',c,d, $$ and assume that the invariants $$ ac+bd=m,\qquad a'c+b'd=n \tag{6} $$ hold. From step E2, we have $$ c=qd+r. $$ Step E4 replaces the variables by $$ c^ =d,\qquad d^ =r, $$...
TAOCP 1.2.1 Exercise 3
Section 1.2.1: Mathematical Induction Exercise 3. [ 18 ] The following proof by induction seems correct, but for some reason the equation for $n = 6$ gives $\frac{1}{2} + \frac{1}{6} + \frac{1}{12} + \frac{1}{20} + \frac{1}{30} = \frac{5}{6}$ on the left-hand side, and $\frac{3}{2} - \frac{1}{6} = \frac{4}{3}$ on the right-hand side. Can you find a mistake? “ Theorem. $$\frac{1}{1 \times 2} + \frac{1}{2 \times 3} + \cdots + \frac{1}{(n...
TAOCP 1.2.1 Exercise 4
Section 1.2.1: Mathematical Induction Exercise 4. [ 20 ] Prove that, in addition to Eq. (3), Fibonacci numbers satisfy $F_n \ge \phi^{n-2}$ for all positive integers $n$. Verified: yes Solve time: 27s Let $$ P(n): \qquad F_n \ge \phi^{,n-2}, $$ where $\phi=(1+\sqrt5)/2$ and $n$ is a positive integer. We prove $P(n)$ for all positive integers $n$ by mathematical induction. For the basis, when $n=1$, $$ F_1=1, $$ and since $\phi<2$,...