#volume-1
TAOCP 4.3.3 Exercise 13
Section 4.3.3: How Fast Can We Multiply? Exercise 13. [ M25 ] What is a good upper bound on the time needed to multiply an $m$-bit number by an $n$-bit number, when both $m$ and $n$ are very large but $n$ is much larger than $m$, based on the results discussed in this section for the case $m = n$? Verified: yes Solve time: 1m52s Solution Let $M$ be an...
TAOCP 3.2.1.2 Exercise 13
Section 3.2.1.2: Choice of Multiplier Exercise 13. [ M22 ] [M22] Let $p$ be prime. Given that $a$ is not a primitive element modulo $p$, show that either $a$ is a multiple of $p$ or $a^{(p-1)/q} \equiv 1 \pmod{p}$ for some prime number $q$ that divides $p - 1$. Verified: no Solve time: 1m18s Solution Let $p$ be prime and suppose $a$ is not a primitive element modulo $p$. The...
TAOCP 3.2.1.1 Exercise 9
Section 3.2.1.1: Choice of Modulus Exercise 9. ▶ [ M25 ] Most high-level programming languages do not provide a good way to divide a two-word integer by a one-word integer, nor do they provide the hmuIt operation of exercise 3. The purpose of this exercise is to find a reasonable way to cope with such limitations when we wish to evaluate $ax \bmod m$ for variable $x$ and for constants...
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 4.7 Exercise 17
Section 4.7: Manipulation of Power Series Exercise 17. ▶ [ M20 ] Given the power series $V(z) = V_1 z + V_2 z^2 + V_3 z^3 + \cdots$, we define the power matrix of $V$ as the infinite array of coefficients $v_{nk} = \frac{1}{n} [z^k] V(z)^n$; the $n$th poweroid of $V$ is then defined to be $V_n(x) = v_{n0} + v_{n1} x + \cdots + v_{nn} x^n$. Prove that poweroids...
TAOCP 3.4.2 Exercise 17
Section 3.4.2: Random Sampling and Shuffling Exercise 17. [ M22 ] (R. W. Floyd.) Prove that the following algorithm generates a random sample $S$ of $n$ integers from ${1, \ldots, N}$: Set $S \leftarrow \emptyset$; then for $j \leftarrow N - n + 1,\ N - n + 2,\ \ldots,\ N$ (in this order), set $k \leftarrow \lfloor jU \rfloor + 1$ and $$S \leftarrow \begin{cases} S \cup {k}, &...
TAOCP 4.5.2 Exercise 35
Section 4.5.2: The Greatest Common Divisor Exercise 35. [ M25 ] [M25] Consider a directed graph with vertices $(m, n)$ for all nonnegative integers $m, n \ge 0$, having arcs from $(m, n)$ to $(m', n')$ whenever it is possible for a subtract-and-shift cycle of Algorithm B to transform integers $u$ and $v$ with $\lfloor \lg u \rfloor = m$ and $\lfloor \lg v \rfloor = n$ into integers $u'$...
TAOCP 3.3.3 Exercise 2
Section 3.3.3: Theoretical Tests Exercise 2. [ HM22 ] What is the Fourier series expansion (in terms of sines and cosines) of the function $((x))$? Verified: no Solve time: 5m01s The function $((x))$ is $1$-periodic and defined on $0 \le x < 1$ by $((x)) = x - \frac12,$ since $\lfloor x \rfloor = 0$ and $\lceil x \rceil = 1$ for $0 < x < 1$ in (7). The...
TAOCP 4.6.1 Exercise 22
Section 4.6.1: Division of Polynomials Exercise 22. [ M23 ] Prove Sturm's theorem. [ Hint: Some sign sequences are impossible.] Verified: no Solve time: 3m27s Let $f_0=f,\ f_1=f',\ f_{i+1}=-\operatorname{rem}(f_{i-1},f_i)$ be the Sturm sequence, and let the process terminate with a nonzero constant $f_s$. Define $V(x)$ for real $x$ not a root of any $f_i$ as the number of sign changes in $$ (f_0(x),f_1(x),\dots,f_s(x)) $$ after deleting zeros. We prove that...
TAOCP 4.6.1 Exercise 18
Section 4.6.1: Division of Polynomials Exercise 18. ▶ [ M24 ] $[M24]$ (Euclidean algorithm for string polynomials.) Let $V_1$ and $V_2$ be string polynomials, not both zero, having a common left multiple . (This means that there exist string polynomials $U_1$ and $U_2$, not both zero, such that $U_1V_1 = U_2V_2$.) The purpose of this exercise is to find an algorithm to compute their greatest common right divisor $\gcd(V_1, V_2)$...
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 14
Section 3.4.1: Numerical Distributions Exercise 14. [ M21 ] If $X$ is a random variable with the continuous distribution $F(x)$, and if $c$ is a (possibly negative) constant, what is the distribution of $cX$? Verified: yes Solve time: 1m36s Solution Let $X$ have continuous distribution function $F(x)=\Pr{X\le x}$ and define $Y=cX$, where $c$ is a constant. The distribution function of $Y$ is $G(y)=\Pr{Y\le y}=\Pr{cX\le y}.$ Three cases determine how the...
TAOCP 4.3.1 Exercise 33
Section 4.3.1: The Classical Algorithms Exercise 33. [**] [$M40$] Design an algorithm for taking square roots, analogous to Algorithm D and to the traditional pencil-and-paper method for extracting square roots. Verified: no Solve time: 2m16s Let $u = (u_{n-1}\ldots u_1u_0)_b$. The goal is to construct digits of $\sqrt{u}$ in radix $b$ by imitating the classical pencil-and-paper square-root extraction, but phrased in the multiple-precision framework of Section 4.3.1. We maintain a...
TAOCP 4.3.1 Exercise 4
Section 4.3.1: The Classical Algorithms Exercise 4. [ M21 ] Give a formal proof of the validity of Algorithm A, using the method of inductive assertions explained in Section 1.2.1. Verified: no Solve time: 1m30s Solution Let $U = (u_{n-1}\ldots u_0) b,\quad V = (v {n-1}\ldots v_0) b,\quad W = (w {n-1}\ldots w_0)_b.$ Define for each $j$ with $0 \le j \le n$ the partial values $U_j = \sum_{i=0}^{j-1} u_i...
TAOCP 3.2.2 Exercise 34
Section 3.2.2: Other Methods Exercise 34. [ M25 ] Prove that the inversive congruential sequence (12) has period $p + 1$ if and only if the polynomial $f(x) = x^2 - cx - a$ has the following two properties: (i) $x^{p+1} \bmod f(x)$ is a nonzero constant, when computed with polynomial arithmetic modulo $p$; (ii) $x^{(p+1)/q} \bmod f(x)$ has degree 1 for every prime $q$ that divides $p+1$. [ Hint:...
TAOCP 3.2.2 Exercise 31
Section 3.2.2: Other Methods Exercise 31. [ M30 ] (G. Marsaglia.) What is the period length of the sequence $\langle 7^n \rangle$ when $m = 2^e > 8$? Assume that $X_0, \ldots, X_{54}$ are not all $\equiv \pm 1 \pmod{8}$. Verified: yes Solve time: 1m56s Setup Let $m = 2^e$ with $e \ge 4$. The sequence $\langle 7^n \rangle$ is defined by $X_n \equiv 7^n \pmod{2^e}, \quad n \ge 0.$...
TAOCP 3.2.2 Exercise 4
Section 3.2.2: Other Methods Exercise 4. [ 10 ] Why is the most significant byte used in the first line of program (14), instead of some other byte? Verified: no Solve time: 1m37s In MIX arithmetic each word consists of several bytes, and operations such as multiplication and addition propagate carries from less significant positions toward more significant positions. The least significant byte of a product or sum depends only...
TAOCP 3.5 Exercise 31
Section 3.5: What Is a Random Sequence? Exercise 31. [ M30 ] [M30] Show that ${0..1}$ sequences exist that satisfy Definition R5, yet $\nu_n/n \ge \frac{1}{2}$ for all $n > 0$, where $\nu_n$ is the number of $j < n$ for which $U_j < \frac{1}{2}$. (This might be considered a nonrandom property of the sequence.) Verified: no Solve time: 3m08s Setup Let ${U_n}_{n \ge 0}$ be a binary sequence, so...
TAOCP 3.5 Exercise 30
Section 3.5: What Is a Random Sequence? Exercise 30. ▶ [ M39 ] [M39] Construct a binary sequence that is $(2k)$-distributed, and for which $$\Pr{X_{2n} = 0} = \frac{1}{2} + \binom{2k-1}{k} \bigg/ 2^{2k}.$$ (Therefore the inequality in the previous exercise is the best possible.) Verified: no Solve time: 5m39s Let $(Y_n)_{n\ge 1}$ be an i.i.d. sequence of Bernoulli$(1/2)$ random variables. We will construct the required sequence $(X_n)$ as a deterministic...
TAOCP 3.5 Exercise 21
Section 3.5: What Is a Random Sequence? Exercise 21. [ HM40 ] [HM40] (L. H. Ramshaw.) a) Continuing the previous exercise, is the sequence $(W_n)$ equidistributed? b) Show that $(W_n)$ is the only $[0,..,1)$ sequence for which we have $\sum_{j=1}^{k} l_n^{(j)} \le \lg(1 + k/n)$ whenever $1 \le k \le n$. c) Let ${f_0(l_1, \ldots, l_k)}$ be any sequence of continuous functions on the sets of $n$-tuples ${(l_1, \ldots, l_n)...
TAOCP 4.5.3 Exercise 40
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 40. [ M28 ] ( The Stern–Brocot tree. ) Consider an infinite binary tree in which each node is labeled with the fraction $(p + p_1)/(q + q_1)$, where $p_1/q_1$ is the label of the node's nearest left ancestor and $p_s/q_s$ is the label of the node's nearest right ancestor. (A left ancestor is one that precedes a node in symmetric order, while...
TAOCP 4.5.3 Exercise 33
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 33. [ M32 ] Let $h(n)$ be the number of representations of $n$ in the form $$n = xx' + yy', \qquad x > y > 0, \qquad x' > y' > 0, \qquad x \perp y, \qquad \text{integer } x, x', y, y'.$$ a) Show that if the conditions are relaxed to allow $x' = y'$, the number of representations is $h(n)...
TAOCP 4.5.3 Exercise 16
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 16. [ HM30 ] (L. Euler, 1731.) Let $f_0(z) = (e^z - e^{-z})/(e^z + e^{-z}) = \tanh z$, and let $f_{n+1}(z) = 1/f_n(z) - (2n+1)/z$. Prove that, for all $n$, $f_n(z)$ is an analytic function of the complex variable $z$ in a neighborhood of the origin, and it satisfies the differential equation $f_n'(z) = 1 - f_n(z)^2 - 2nf_n(z)/z$. Use this fact to...
TAOCP 2.4 Exercise 6
Section 2.4: Multilinked Structures Exercise 6. [**] [23] The PL/I language accepts data structures much like those in COBOL, but does not make the restriction of rule (c); instead, we have the rule that a qualified reference (3) is unambiguous if it shows "complete" qualification, that is, if A_{j+1} is the parent of A_j for 0 <= j < n , and if A_n has no parent. Rule (c) is...
TAOCP 1.4.3.1 Exercise 7
Section 1.4.3.1: A MIX Simulator Exercise 7. [ 32 ] Modify the solutions of the previous exercise in such a way that execution of IN or OUT does not cause I/O transmission immediately; the transmission should take place after approximately half of the time required by the simulated devices has elapsed. (This will prevent a frequent student error, in which IN and OUT are used improperly.) Verified: no Solve time:...
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: no Solve time: 1m55s Solution Let $M$ and $F$ denote the address and field of the instruction, already placed in $rI5$ and $rI3$ by the control routine, and let $X$ denote the index register contents stored in $XREG$. The MOVE instruction transfers a...
TAOCP 1.4.3.1 Exercise 1
Section 1.4.3.1: A MIX Simulator Exercise 1. [ 14 ] Study all the uses of the FCHECK subroutine in the simulator program. Can you suggest a better way to organize the code? (See step 3 in the discussion at the end of Section 1.4.1 .) Verified: yes Solve time: 3m12s Let the field specification byte be denoted by $x = \mathrm{INST}(4:4)$. The subroutine FCHECK is invoked in all contexts where...
TAOCP 1.2.5 Exercise 20
Section 1.2.5: Permutations and Factorials Exercise 20. [**] [HM21] Using the fact that $$ 0 \le e^{-t} - \left(1-\frac{t}{m}\right)^m \le t^2e^{-t/m}, \qquad \text{if } 0 \le t \le m, $$ and the previous exercise, show that $$ \Gamma(x) = \int_0^\infty e^{-t}t^{x-1},dt, \qquad \text{if } x>0. $$ Verified: no Solve time: 5m27s Let $x>0$. Let $$ \Gamma_m(x)=\int_0^m \left(1-\frac{t}{m}\right)^m t^{x-1},dt. $$ Extend the integrand to $[0,\infty)$ by defining $$ f_m(t)=\left(1-\frac{t}{m}\right)^m t^{x-1}\mathbf{1}_{[0,m]}(t). $$...
TAOCP 1.2.5 Exercise 11
Section 1.2.5: Permutations and Factorials Exercise 11. [**] [M15] Let the representation of $n$ in the binary system be $n = 2^{e_1}+2^{e_2}+\cdots+2^{e_r}$, where $e_1 > e_2 > \cdots > e_r \ge 0$. Show that $n!$ is divisible by $2^{n-r}$ but not by $2^{n-r+1}$. x 12. [M22] (A. Legendre, 1808.) Generalizing the result of the previous exercise, let $p$ be a prime number, and let the representation of $n$ in the...
TAOCP 1.2.5 Exercise 2
Section 1.2.5: Permutations and Factorials Exercise 2. [**] [10] In the notation of Eq. (2), show that $p_n^{(n-1)} = p_n^{(n)}$, and explain why this happens. Verified: yes Solve time: 1m54s From equation (2), $$ p_{nk} = n(n-1)\cdots(n-k+1). $$ Setting $k=n$ gives $$ p_n^{(n)} = p_{nn} = n(n-1)\cdots 2\cdot 1. $$ Setting $k=n-1$ gives $$ p_n^{(n-1)} = p_{n,n-1} = n(n-1)\cdots 3\cdot 2. $$ The second product differs from the first only...
TAOCP 2.3.5 Exercise 12
Section 2.3.5: Lists and Garbage Collection Exercise 12. [**] [30] (M. Minsky.) Show that it is possible to use a garbage collection method reliably in a "real time" application, for example when a computer is controlling some physical device, even when stringent upper bounds are placed on the maximum execution time required for each List operation performed. [Hint: Garbage collection can be arranged to work in parallel with the List...
TAOCP 2.3.5 Exercise 11
Section 2.3.5: Lists and Garbage Collection Exercise 11. [**] [M30] Any List structure can be "fully expanded" into a tree structure by repeating all overlapping elements until none are left; when the List is recursive, this gives an infinite tree. For example, the List (5) would expand into an infinite tree whose first four levels are Design an algorithm to test the equivalence of two List structures, in the sense...
TAOCP 2.3.5 Exercise 9
Section 2.3.5: Lists and Garbage Collection Exercise 9. [**] [28] (D. Edwards.) Design an algorithm for the second phase of garbage collection that "compacts storage" in the following sense: Let NODE(1), ..., NODE(M) be one-word nodes with fields MARK , ATOM , ALINK , and BLINK , as described in Algorithm E. Assume that MARK = 1 in all nodes that are not garbage. The desired algorithm should relocate the...
TAOCP 2.3.5 Exercise 5
Section 2.3.5: Lists and Garbage Collection Exercise 5. [**] [25] (Schorr and Waite.) Give a marking algorithm that combines Algorithms B and E as follows: The assumptions of Algorithm E with regard to fields within the nodes, etc., are retained; but an auxiliary stack STACK[1] , STACK[2], ..., STACK[N] is used as in Algorithm B, and the mechanism of Algorithm E is employed only when the stack is full. Verified:...
TAOCP 2.3.5 Exercise 1
Section 2.3.5: Lists and Garbage Collection Exercise 1. [**] [M21] In Section 2.3.4 we saw that trees are special cases of the "classical" mathematical concept of a directed graph. Can Lists be described in graph-theoretic terminology? Verified: no Solve time: 1m58s A List can be described as a finite directed graph whose vertices correspond to memory nodes and whose directed edges correspond to pointer fields such as RLINK and DLINK...
TAOCP 1.4.4 Exercise 19
Section 1.4.4: Input and Output Exercise 19. [ M28 ] Special considerations arise when input or output involves short blocks on a rotating device like a magnetic disk. Suppose a program works with $n \geq 2$ consecutive blocks of information in the following way: Block $k$ begins to be input at time $t_k$, where $t_1 = 0$. It is assigned for processing at time $u_k \geq t_k + T$ and...
TAOCP 1.4.4 Exercise 18
Section 1.4.4: Input and Output Exercise 18. [ 30 ] A proposed extension of MIX allows its computations to be interrupted, as explained below. Your task in this exercise is to modify Algorithms and Programs A, R, and B of the text so that they use these interrupt facilities instead of the JRED instructions. The new MIX features include an additional 3999 memory cells, locations $-3999$ through $-0001$. The machine...
TAOCP 1.4.4 Exercise 17
Section 1.4.4: Input and Output Exercise 17. [ 40 ] Adapt the multiple-buffering algorithm to pooled buffers; build in methods that keep the process from slowing down, due to too much anticipated input. Try to make the algorithm as elegant as possible. Compare your method to nonpooling methods, applied to real-life problems. Verified: no Solve time: 2m26s Corrected Solution The error in the proposed solution stems from an incorrect output...
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: 3m35s The key point in Knuth’s buffered coroutine design is that termination is expressed purely through the buffer–handoff protocol , not...
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: 1m54s Solution The original schedule (Fig. 27) is a valid execution with three buffers, where the third buffer absorbs overlap between computation and output. With only two buffers, the system becomes a bounded pool of size $2$ shared between the producer (program) and the consumer (output device). The...
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 8
Section 1.4.4: Input and Output Exercise 8. [ 11 ] The text describes a hypothetical input scenario that leads from Fig. 23 through parts (a), (b), and (c) of Fig. 24. Interpret the same scenario under the assumption that output to the line printer is being done, instead of input from cards. (For example, what things are happening at the time shown in Fig. 23?) Verified: yes Solve time: 3m18s...
TAOCP 1.4.4 Exercise 6
Section 1.4.4: Input and Output Exercise 6. [ 12 ] What instructions should be placed at the beginning of a program so that the WORDIN subroutine (4) gets off to the right start? (For example, index register 6 must be set to something .) Verified: no Solve time: 3m19s Solution The subroutine WORDIN assumes that a circular pair of buffers is already set up in memory and that index register...
TAOCP 1.3.3 Exercise 28
Section 1.3.3: Applications to Permutations Exercise 28. [ M21 ] (I. Kaplansky.) If the "Josephus permutation" defined in exercise 1.3.2-22 is expressed in cycle form, we obtain $(1,5,3,6,8,2,4)(7)$ when $n = 8$ and $m = 4$. Show that this permutation in the general case is the product $(n\ n!-!1\ \ldots\ 2\ 1)^{m-1} \times (n\ n!-!1\ \ldots\ 2)^{m-1}\cdots (n\ n!-!1)^{m-1}$. Verified: no Solve time: 4m28s Let the Josephus process on ${1,\dots,n}$...
TAOCP 1.3.3 Exercise 25
Section 1.3.3: Applications to Permutations Exercise 25. [ M22 ] Prove Eq. (29). Verified: no Solve time: 4m30s Let $$ T(n)=\sum_{k=1}^{n}(k-1)(n-k)!. $$ We derive Eq. (29) directly from this definition, without introducing unproved structural decompositions. 1. Change of index Let $j=n-k$. Then $k=n-j$, and as $k$ runs from $1$ to $n$, $j$ runs from $n-1$ down to $0$. Hence $$ T(n)=\sum_{j=0}^{n-1}(n-j-1),j!. $$ Split the factor: $$ T(n)=(n-1)\sum_{j=0}^{n-1}j!-\sum_{j=0}^{n-1}j\cdot j!. $$ 2....
TAOCP 1.3.3 Exercise 23
Section 1.3.3: Applications to Permutations Exercise 23. [ HM42 ] (Golomb, Shepp, Lloyd.) If $l_n$ denotes the average length of the longest cycle in a permutation of $n$ objects, show that $l_n \approx \lambda n + \tfrac{1}{2}\lambda$, where $\lambda \approx 0.62433$ is a constant. Prove in fact that $\lim_{n \to \infty}(l_n - \lambda n - \tfrac{1}{2}\lambda) = 0$. Verified: no Solve time: 4m12s Setup Let a permutation of ${1,\dots,n}$ be...
TAOCP 1.3.3 Exercise 22
Section 1.3.3: Applications to Permutations Exercise 22. [ HM34 ] (The following approach, due to L. Shepp and S. P. Lloyd, gives a convenient and powerful method for solving problems related to the cycle structure of random permutations.) Instead of regarding the number, $n$, of objects as fixed, and the permutation variable, let us assume instead that we independently choose the quantities $\alpha_1, \alpha_2, \alpha_3, \ldots$ appearing in exercises 20...
TAOCP 1.3.3 Exercise 20
Section 1.3.3: Applications to Permutations Exercise 20. [ M20 ] Given that all singleton cycles are written out explicitly, how many different ways are there to write the cycle notation of a permutation that has $\alpha_1$ one-cycles, $\alpha_2$ two-cycles, ... ? (See exercise 5.) Verified: yes Solve time: 2m09s Solution Let a permutation of $n$ elements have exactly $\alpha_j$ cycles of length $j$, for $1 \le j \le n$, including...
TAOCP 1.3.3 Exercise 18
Section 1.3.3: Applications to Permutations Exercise 18. [ M27 ] What is $p_{nkm}$, the probability that a permutation of $n$ objects has exactly $k$ cycles of length $m$? What is the corresponding generating function $G_{nm}(z)$? What is the average number of $m$-cycles and what is the standard deviation? (The text considers only the case $m = 1$.) Verified: yes Solve time: 2m27s Setup Let $S_n$ be the set of all...
TAOCP 1.3.3 Exercise 16
Section 1.3.3: Applications to Permutations Exercise 16. [ M15 ] Start with the permutation 1324 in linear notation; convert it to canonical cycle form and then remove the parentheses; repeat this process until arriving at the original permutation. What permutations occur during this process? Verified: yes Solve time: 4m12s Let $T$ be the operation described: Start with a permutation in linear notation. Convert it to canonical cycle form. Remove parentheses...
TAOCP 1.3.3 Exercise 15
Section 1.3.3: Applications to Permutations Exercise 15. [ M12 ] Is there a permutation that represents exactly the same transformation both in the canonical cycle form without parentheses and in the linear form? Verified: yes Solve time: 3m16s Let $\pi$ be a permutation of ${1,2,\dots,n}$. Write the one-line form as $$ (\pi(1)\ \pi(2)\ \cdots\ \pi(n)). $$ Write the canonical cycle form with cycles ordered by increasing smallest element, each cycle...
TAOCP 1.3.3 Exercise 13
Section 1.3.3: Applications to Permutations Exercise 13. [ M24 ] Prove that Algorithm $J$ is valid. Verified: no Solve time: 2m59s The flaw in the previous response is that it refused the task instead of engaging with the standard definition of Algorithm $J$ in TAOCP §1.3.3. In that section, Algorithm $J$ is the Johnson–Trotter permutation generation algorithm. Its validity means: it produces each permutation of ${1,\dots,n}$ exactly once and then...
TAOCP 1.3.3 Exercise 12
Section 1.3.3: Applications to Permutations Exercise 12. [ M27 ] ( Transposing a rectangular matrix .) Suppose an $m \times n$ matrix $(a_{ij})$, $m \ne n$, is stored in memory in a fashion like that of exercise 1.3.2-10, so that the value of $a_{ij}$ appears in location $L + n(i - 1) + (j - 1)$, where $L$ is the location of $a_{11}$. The problem is to find a way...
TAOCP 1.3.3 Exercise 2
Section 1.3.3: Applications to Permutations Exercise 2. [ 10 ] The text shows how we might set $(a, b, c, d, e, f) \leftarrow (c, d, f, b, e, a)$ by using a series of replacement operations $(x \leftarrow y)$ and one auxiliary variable $t$. Show how to do the job by using a series of exchange operations $(x \leftrightarrow y)$ and no auxiliary variables. Verified: no Solve time: 3m09s...
TAOCP 1.3.3 Exercise 1
Section 1.3.3: Applications to Permutations Exercise 1. [ 02 ] Consider the transformation of ${0, 1, 2, 3, 4, 5, 6}$ that replaces $x$ by $2x \bmod 7$. Show that this transformation is a permutation, and write it in cycle form. Verified: yes Solve time: 1m35s Consider the mapping $f(x)=2x \bmod 7$ on ${0,1,2,3,4,5,6}$. Since $\gcd(2,7)=1$, there exists an inverse multiplier $4$ with $2\cdot 4 \equiv 1 \pmod 7$, so...
TAOCP 2.3.4.2 Exercise 28
Section 2.3.4.2: Oriented Trees Exercise 28. [**] $$M35$$ Consider the $(m+n)\times(m+n)$ determinant illustrated here for $m=2$ and $n=3$: $$ \det\begin{pmatrix} a_{10}+a_{11}+a_{12}+a_{13} & 0 & a_{11} & a_{12} & a_{13}\ 0 & a_{20}+a_{21}+a_{22}+a_{23} & a_{21} & a_{22} & a_{23}\ b_{11} & b_{12} & b_{10}+b_{11}+b_{12} & 0 & 0\ b_{21} & b_{22} & 0 & b_{20}+b_{21}+b_{22} & 0\ b_{31} & b_{32} & 0 & 0 & b_{30}+b_{31}+b_{32} \end{pmatrix}. $$ Show that when...
TAOCP 2.3.4.2 Exercise 27
Section 2.3.4.2: Oriented Trees Exercise 27. [**] $$M30$$ ( Steady states. ) Let $G$ be a directed graph on vertices $V_1,\ldots,V_n$, whose arcs have been assigned probabilities $p(e)$ as in exercise 26. Instead of having Start and Stop vertices, however, assume that $G$ is strongly connected; thus each vertex $V_j$ is a root, and we assume that the probabilities $p(e)$ are positive and satisfy $\sum_{\operatorname{init}(e)=V_j}p(e)=1$ for all $j$. A random...
TAOCP 2.3.4.2 Exercise 24
Section 2.3.4.2: Oriented Trees Exercise 24. [**] $$M20$$ Let $G$ be a connected digraph with arcs $e_0,e_1,\ldots,e_m$. Let $E_0,E_1,\ldots,E_m$ be a set of positive integers that satisfy Kirchhoff's law for $G$; that is, for each vertex $V$, $$ \sum_{\operatorname{init}(e_j)=V} E_j \sum_{\operatorname{fin}(e_j)=V} E_j. $$ Assume further that $E_0=1$. Prove that there is an oriented walk in $G$ from $\operatorname{fin}(e_0)$ to $\operatorname{init}(e_0)$ such that edge $e_j$ appears exactly $E_j$ times, for $1\le...
TAOCP 2.3.4.2 Exercise 23
Section 2.3.4.2: Oriented Trees Exercise 23. [**] $$M33$$ (N. G. de Bruijn.) For each sequence of nonnegative integers $x_1,\ldots,x_k$ less than $m$, let $f(x_1,\ldots,x_k)$ be a nonnegative integer less than $m$. Define an infinite sequence as follows: $X_1=X_2=\cdots=X_k=0$; $X_{n+k+1}=f(X_{n+k},\ldots,X_{n+1})$ when $n\ge0$. For how many of the $m^{m^k}$ possible functions $f$ is this sequence periodic with a period of the maximum length $m^k$? [ Hint: Construct a directed graph with vertices...
TAOCP 2.3.4.2 Exercise 22
Section 2.3.4.2: Oriented Trees Exercise 22. [**] $$M26$$ Let $G$ be a balanced, directed graph with vertices $V_1,V_2,\ldots,V_n$ and no isolated vertices. Let $\sigma_j$ be the out-degree of $V_j$. Show that the number of Eulerian trails of $G$ is $$ (\sigma_1+\sigma_2+\cdots+\sigma_n)T\prod_{j=1}^{n}(\sigma_j-1)!, $$ where $T$ is the number of oriented subtrees of $G$ with root $V_1$. [ Note: The factor $(\sigma_1+\cdots+\sigma_n)$, which is the number of arcs of $G$, may be...
TAOCP 2.3.4.2 Exercise 21
Section 2.3.4.2: Oriented Trees Exercise 21. [**] $$HM38$$ (T. van Aardenne-Ehrenfest and N. G. de Bruijn.) Figure 36 is an example of a directed graph that is not only balanced, it is regular , which means that every vertex has the same in-degree and out-degree as every other vertex. Let $G$ be a regular digraph with $n$ vertices $V_0,V_1,\ldots,V_{n-1}$, in which every vertex has in-degree and out-degree equal to $m$....
TAOCP 2.3.4.2 Exercise 20
Section 2.3.4.2: Oriented Trees Exercise 20. [**] $$M21$$ If $G$ is an undirected graph on $n+1$ vertices $V_0,\ldots,V_n$, let $B$ be the $n\times n$ matrix defined as follows for $1\le i,j\le n$: $$ b_{ij}= \begin{cases} t, & \text{if } i=j \text{ and there are } t \text{ edges touching } V_j;\ -1, & \text{if } i\ne j \text{ and } V_i \text{ is adjacent to } V_j;\ 0, & \text{otherwise.}...
TAOCP 2.3.4.2 Exercise 18
Section 2.3.4.2: Oriented Trees Exercise 18. [**] $$M30$$ Let $G$ be a graph with $n+1$ vertices $V_0,V_1,\ldots,V_n$ and $m$ edges $e_1,\ldots,e_m$. Make $G$ into a directed graph by assigning an arbitrary orientation to each edge; then construct the $m\times(n+1)$ matrix $A$ with $$ a_{ij}= \begin{cases} +1, & \text{if } \operatorname{init}(e_i)=V_j;\ -1, & \text{if } \operatorname{fin}(e_i)=V_j;\ 0, & \text{otherwise.} \end{cases} $$ Let $A_0$ be the $m\times n$ matrix $A$ with column...
TAOCP 2.3.4.2 Exercise 16
Section 2.3.4.2: Oriented Trees Exercise 16. [**] $$M24$$ In a popular solitaire game called "clock," the 52 cards of an ordinary deck of playing cards are dealt face down into 13 piles of four each; 12 piles are arranged in a circle like the 12 hours of a clock and the thirteenth pile goes in the center. The solitaire game now proceeds by turning up the top card of the...
TAOCP 2.3.4.2 Exercise 12
Section 2.3.4.2: Oriented Trees Exercise 12. [**] $$M10$$ In the correspondence between oriented trees as defined here and oriented trees as defined at the beginning of Section 2.3, is the degree of a tree node equal to the in-degree or the out-degree of the corresponding vertex? Verified: yes Solve time: 1m35s In the oriented tree defined in Section 2.3.4.2, each vertex $V \ne R$ is the initial vertex of exactly...
TAOCP 2.3.4.2 Exercise 11
Section 2.3.4.2: Oriented Trees Exercise 11. [**] $$28$$ Using the assumptions of exercise 2.3.4.1-6, but with $(a_k,b_k)$ representing an arc from $V_{a_k}$ to $V_{b_k}$, design an algorithm that not only prints out a free subtree as in that algorithm, but also prints out the fundamental cycles. [ Hint: The algorithm given in the solution to exercise 2.3.4.1-6 can be combined with the algorithm in the preceding exercise.] Verified: yes Solve...
TAOCP 2.3.4.2 Exercise 10
Section 2.3.4.2: Oriented Trees Exercise 10. [**] $$22$$ An oriented tree with vertices $V_1,\ldots,V_n$ can be represented inside a computer by using a table $P[1],\ldots,P[n]$ as follows: If $V_j$ is the root, $P[j]=0$; otherwise $P[j]=k$, if the arc $e[V_j]$ goes from $V_j$ to $V_k$. (Thus $P[1],\ldots,P[n]$ is the same as the "parent" table used in Algorithm 2.3.3E.) The text shows how a free tree can be converted into an oriented...
TAOCP 2.3.4.2 Exercise 9
Section 2.3.4.2: Oriented Trees Exercise 9. [**] $$18$$ By assigning directions to the edges, draw the oriented tree corresponding to the free tree in Fig. 30 on page 363, with $G$ as the root. Verified: no Solve time: 1m34s The construction from a free tree to an oriented tree is obtained by directing each edge toward the chosen root along the unique simple path in the underlying tree. Let $G$...
TAOCP 2.3.4.2 Exercise 7
Section 2.3.4.2: Oriented Trees Exercise 7. [**] $$M22$$ True or false: A directed graph satisfying properties (a) and (b) of the definition of oriented tree, and having no oriented cycles, is an oriented tree. Verified: yes Solve time: 1m40s The statement is true . Assume a finite directed graph satisfies properties (a) and (b): each vertex $V \ne R$ is the initial vertex of exactly one arc $e[V]$, and $R$...
TAOCP 2.3.4.2 Exercise 4
Section 2.3.4.2: Oriented Trees Exercise 4. [**] $$M20$$ The concept of topological sorting can be defined for any finite directed graph $G$ as a linear arrangement of the vertices $V_1V_2\ldots V_n$ such that $\operatorname{init}(e)$ precedes $\operatorname{fin}(e)$ in the ordering for all arcs $e$ of $G$. (See Section 2.2.3, Figs. 6 and 7.) Not all finite directed graphs can be topologically sorted; which ones can be? (Use the terminology of this...
TAOCP 2.3.4.2 Exercise 2
Section 2.3.4.2: Oriented Trees Exercise 2. [**] $$15$$ Which of the ten "fundamental cycles" listed in (3) of Section 2.3.4.1 are oriented cycles in the directed graph (Fig. 32) of that section? Verified: no Solve time: 4m40s The previous solution failed because it replaced the required case-by-case verification on Fig. 32 with an ungrounded guess about which labeled chords work. The correct argument must explicitly reduce the problem to checking...
TAOCP 2.3.4.4 Exercise 30
Section 2.3.4.4: Enumeration of Trees Exercise 30. [**] [M23] Let $n,x,y,z_1,\ldots,z_n$ be positive integers. Consider a set of $x+y+z_1+\cdots+z_n+n$ vertices $r_i,s_{jk},t_j$ ($1\le i\le x+y$, $1\le j\le n$, $1\le k\le z_j$), in which arcs have been drawn from $s_{jk}$ to $t_j$ for all $j$ and $k$. According to exercise 27, there are $$ (x+y)(x+y+z_1+\cdots+z_n)^{n-1} $$ ways to draw one arc from each of $t_1,\ldots,t_n$ to other Verified: yes Solve time: 1m39s...
TAOCP 2.3.4.4 Exercise 29
Section 2.3.4.4: Enumeration of Trees Exercise 29. [**] [HM26] Prove that if $E_k(r,t)=r(r+kt)^{k-1}/k!$, and if $zx^t=\ln x$, then $$ x^r=\sum_{k\ge0}E_k(r,t)z^k $$ for fixed $t$ and for sufficiently small $|z|$ and $|x-1|$. [Use the fact that $G_m(z)=G_1(z)^m$ in the discussion following Eq. (19).] In this formula, $r$ stands for an arbitrary real number. [ Note: As a consequence of this formula we have the identity $$ \sum_{k=0}^n E_k(r,t)E_{n-k}(s,t)=E_n(r+s,t); $$ this implies...
TAOCP 2.3.4.4 Exercise 28
Section 2.3.4.4: Enumeration of Trees Exercise 28. [**] [M22] ( Bipartite trees. ) Use the result of exercise 27 to enumerate the number of labeled free trees on vertices $U_1,\ldots,U_m,V_1,\ldots,V_n$, such that each edge joins $U_j$ to $V_k$ for some $j$ and $k$. Verified: no Solve time: 4m54s The error in the previous solution is structural: it attempts to replace the tree condition by independent parent choices. That step cannot...
TAOCP 2.3.4.4 Exercise 27
Section 2.3.4.4: Enumeration of Trees Exercise 27. [**] [M28] Let $U_1,U_2,\ldots,U_p,\ldots,U_q;\ V_1,V_2,\ldots,V_r$ be vertices of a directed graph, where $1\le p\le q$. Let $f$ be any function from the set ${p+1,\ldots,q}$ into the set ${1,2,\ldots,r}$, and let the directed graph contain exactly $q-p$ arcs, from $U_k$ to $V_{f(k)}$ for $p<k\le q$. Show that the number of ways to add $r$ additional arcs, one from each of the $V$'s to one...
TAOCP 2.3.4.4 Exercise 24
Section 2.3.4.4: Enumeration of Trees Exercise 24. [**] [M16] All labeled oriented trees with vertices 1, 2, 3, 4 and with root 1 are shown in (15). How many would there be if we listed all labeled ordered trees with these vertices and this root? Verified: yes Solve time: 1m58s Each ordered tree corresponds to a rooted oriented tree together with a choice of a linear order of the children...
TAOCP 2.3.4.4 Exercise 22
Section 2.3.4.4: Enumeration of Trees Exercise 22. [**] [M20] How many labeled free trees are possible with $n$ vertices? (In other words, if we are given $n$ vertices, there are $2^{\binom{n}{2}}$ possible graphs having these vertices, depending on which of the $\binom{n}{2}$ possible edges are incorporated into the graph; how many of these graphs are free trees?) Verified: yes Solve time: 3m04s Let a labeled free tree mean a connected...
TAOCP 2.3.4.4 Exercise 21
Section 2.3.4.4: Enumeration of Trees Exercise 21. [**] [M21] Enumerate the number of labeled oriented trees in which each vertex has in-degree zero or two. (See exercise 20 and exercise 2.3-20.) Verified: yes Solve time: 2m11s Let the set of vertices be $V={1,2,\ldots,n}$. Let $S$ be the set of vertices with in-degree $2$ and let $T=V\setminus S$ be the set of vertices with in-degree $0$. Then every edge $uv$ must...
TAOCP 2.3.4.4 Exercise 17
Section 2.3.4.4: Enumeration of Trees Exercise 17. [**] [M26] Let $f(x)$ be an integer-valued function, where $1\le f(x)\le m$ for all integers $1\le x\le m$. Define $x\equiv y$ if $f^{[r]}(x)=f^{[s]}(y)$ for some $r,s\ge0$, where $f^{[0]}(x)=x$ and $f^{[r+1]}(x)=f(f^{[r]}(x))$. By using methods of enumeration like those in this section, show that the number of functions such that $x\equiv y$ for all $x$ and $y$ is $m^{m-1}Q(m)$, where $Q(m)$ is the function defined...
TAOCP 2.3.4.4 Exercise 15
Section 2.3.4.4: Enumeration of Trees Exercise 15. [**] [21] Discuss the relationships that exist (if any) between the topological sort algorithm of Section 2.2.3 and the canonical representation of an oriented tree. Verified: yes Solve time: 1m43s The canonical representation is obtained by repeatedly removing a terminal vertex of the oriented tree, where a terminal vertex is one with no children, equivalently a vertex of indegree $0$ in the orientation...
TAOCP 2.3.4.4 Exercise 13
Section 2.3.4.4: Enumeration of Trees Exercise 13. [**] [15] What oriented tree on the vertices ${1,2,\ldots,10}$ has the canonical representation $3,1,4,1,5,9,2,6,5$? Verified: yes Solve time: 1m54s Let $x_1,\ldots,x_9 = 3,1,4,1,5,9,2,6,5$ be the canonical representation on ${1,\ldots,10}$. Construct $V_1,\ldots,V_{10}$ by repeatedly selecting the smallest vertex not appearing in the corresponding suffix. For $x_1,\ldots,x_9$, the vertices appearing are ${1,2,3,4,5,6,9}$, so the missing vertices are ${7,8,10}$ and hence $V_1=7$. For $x_2,\ldots,x_9 = 1,4,1,5,9,2,6,5$,...
TAOCP 2.3.4.4 Exercise 11
Section 2.3.4.4: Enumeration of Trees Exercise 11. [**] [M28] The text derives the number of different binary trees with $n$ nodes (14). Generalize this to find the number of different $t$-ary trees with $n$ nodes. (See exercise 2.3.1-35; a $t$-ary tree is either empty or consists of a root and $t$ disjoint $t$-ary trees.) Hint: Use Eq. (21) of Section 1.2.9. Verified: yes Solve time: 1m37s Let $b_n$ denote the...
TAOCP 2.3.4.4 Exercise 9
Section 2.3.4.4: Enumeration of Trees Exercise 9. [**] [M20] From the fact that at most one subtree of a vertex in a free tree can contain a centroid, prove that there are at most two centroids in a free tree; furthermore if there are two, then they must be adjacent. Verified: no Solve time: 4m Let $T$ be a free tree with $n$ vertices and let $\operatorname{weight}(X)$ denote the centroid...
TAOCP 2.3.4.4 Exercise 8
Section 2.3.4.4: Enumeration of Trees Exercise 8. [**] [20] According to Eq. (9), there are six free trees with six vertices. Draw them, and indicate their centroids. Verified: no Solve time: 4m03s Equation (9) gives $$ F(z)=z+z^2+z^3+2z^4+3z^5+6z^6+\cdots, $$ so the coefficient of $z^6$ is $6$. Hence there are exactly six free trees with six vertices. It remains to list the six isomorphism classes and determine their centroids using the definition...
TAOCP 2.3.4.4 Exercise 7
Section 2.3.4.4: Enumeration of Trees Exercise 7. [**] [HM40] Obtain asymptotic values for the numbers of exercise 6. (See exercise 4.) Verified: yes Solve time: 1m41s An oriented binary tree is an oriented tree in which each vertex has in-degree at most $2$. Since in the oriented-tree convention of this section each vertex has in-degree equal to the number of its subtrees, this condition is equivalent to requiring that each...
TAOCP 2.3.4.4 Exercise 4
Section 2.3.4.4: Enumeration of Trees Exercise 4. [**] [HM39] (G. Pólya, 1937.) Using complex variable theory, determine the asymptotic value of the number of oriented trees as follows: a) Show that there is a real number $\alpha$ between 0 and 1 for which $A(z)$ has radius of convergence $\alpha$ and $A(z)$ converges absolutely for all complex $z$ such that $|z|\le\alpha$, having maximum value $A(\alpha)=a<\infty$. [ Hint: When a power series...
TAOCP 2.3.4.4 Exercise 1
Section 2.3.4.4: Enumeration of Trees Exercise 1. [**] [M20] (G. Pólya.) Show that $$ A(z)=z\cdot\exp\left(A(z)+\frac12A(z^2)+\frac13A(z^3)+\cdots\right). $$ [ Hint: Take logarithms of (3).] Verified: no Solve time: 3m56s Let $A(z)=\sum_{n\ge1} a_n z^n$, where $a_n$ is the number of rooted unlabeled trees with $n$ vertices. A rooted tree consists of a root vertex together with a multiset of rooted subtrees attached to the root. If the root has subtrees of sizes $n_1,\dots,n_k$,...
TAOCP 2.5 Exercise 42
Section 2.5: Dynamic Storage Allocation Exercise 42. [**] [M40] (J. M. Robson, 1975.) Let $N_{\rm BF}(n,m)$ be the amount of memory needed to guarantee non-overflow when the best-fit method is used for allocation as in exercise 38. Find an attacking strategy to show that $$ N_{\rm BF}(n,m)\geq mn-O(n+m^2). $$ Verified: no Solve time: 4m Let the memory be a linear array of locations. There are $m$ processes $P_1,\dots,P_m$. Each process...
TAOCP 2.5 Exercise 36
Section 2.5: Dynamic Storage Allocation Exercise 36. [**] [20] A certain lunch counter in Hollywood, California, contains 23 seats in a row. Diners enter the shop in groups of one or two, and a glamorous hostess shows them where to sit. Prove that she will always be able to seat people immediately without splitting up any pairs, if no customer who comes alone is assigned to any of the seats...
TAOCP 2.5 Exercise 34
Section 2.5: Dynamic Storage Allocation Exercise 34. [**] [29] Write a MIX program for the algorithm of exercise 33, and determine its running time. Verified: no Solve time: 7m03s The required algorithm is the garbage collection and compaction method of Exercise 2.5.33: mark all nodes reachable from USE , pack them into low memory, and reset AVAIL . A correct MIX implementation is most naturally written as a mark-and-copy traversal...
TAOCP 2.5 Exercise 32
Section 2.5: Dynamic Storage Allocation Exercise 32. [**] [HM46] Determine $\lim_{n\to\infty}\alpha_n$, if it exists, where $\alpha_n$ is the mean value of $t_n$ in a random sequence defined as follows: Given the values of $t_k$ for $0\leq k<n$, let $t_n$ be chosen uniformly from ${1,2,\ldots,g_n}$, where $$ g_n=\left\lfloor {5\over4} \min(10000, f(t_{n-1}-1), f(t_{n-2}-2), \ldots, f(t_0-n))\right\rfloor, $$ and $f(x)=x$ if $x>0$, $f(x)=\infty$ if $x\leq 0$. [ Note: Some limited empirical tests indicate that...
TAOCP 2.5 Exercise 31
Section 2.5: Dynamic Storage Allocation Exercise 31. [**] [M40] Can a storage allocation system analogous to the buddy system be designed using the Fibonacci sequence instead of powers of two? (Thus, we might start with $F_m$ available words, and split an available block of $F_k$ words into two buddies of respective lengths $F_{k-1}$ and $F_{k-2}$.) Verified: yes Solve time: 2m42s Let $F_1=1$, $F_2=1$, and $F_k=F_{k-1}+F_{k-2}$ for $k\ge 3$. A Fibonacci...
TAOCP 2.5 Exercise 30
Section 2.5: Dynamic Storage Allocation Exercise 30. [**] [M48] Analyze the average behavior of Algorithms R and S, given reasonable distributions for the sequence of storage requests. Verified: no Solve time: 6m45s Let the memory consist of blocks of sizes $2^0,2^1,\ldots,2^m$. A request sequence is modeled as follows: at each time step a block size $k \in {0,\ldots,m}$ is chosen with fixed probabilities $\pi_k$, and the requested block remains allocated...
TAOCP 2.5 Exercise 28
Section 2.5: Dynamic Storage Allocation Exercise 28. [**] [25] Assume that MIX is a binary computer, with a new operation code XOR defined as follows (using the notation of Section 1.3.1): " C = 5 , F = 5 . For each bit position in location M that equals 1, the corresponding bit position in register A is complemented (changed from 0 to 1 or 1 to 0); the sign...
TAOCP 2.5 Exercise 27
Section 2.5: Dynamic Storage Allocation Exercise 27. [**] [24] Write a MIX program for Algorithm R, and determine its running time. Verified: no Solve time: 4m08s Algorithm R releases a block of size $2^k$ at address $A$ into the buddy system and then repeatedly attempts to merge it with its buddy at level $k+1, k+2, \ldots$, stopping when the buddy is not free. The implementation must therefore support three explicit...
TAOCP 2.5 Exercise 24
Section 2.5: Dynamic Storage Allocation Exercise 24. [**] [20] According to the algorithm in the text, the largest block (of size $2^m$) has no buddy, since it represents all of storage. Would it be correct to define $buddy_m(0)=0$ (namely, to make this block its own buddy), and then to avoid testing $k=m$ in step S1? Verified: yes Solve time: 2m22s In the buddy system of Section 2.5, a block of...
TAOCP 2.5 Exercise 21
Section 2.5: Dynamic Storage Allocation Exercise 21. [**] [HM25] Examine the ratio $a_n/b_n$, where $a_n$ is the sum of the first $n$ terms of $$ 1+2+4+4+8+8+8+8+16+16+\cdots, $$ and $b_n$ is the sum of the first $n$ terms of $$ 1+2+3+4+5+6+7+8+9+10+\cdots, $$ as $n$ goes to infinity. Verified: yes Solve time: 1m45s Let the sequence be grouped by powers of two. The value $2^k$ occurs exactly $2^{k-1}$ times for $k \ge...
TAOCP 2.5 Exercise 19
Section 2.5: Dynamic Storage Allocation Exercise 19. [**] [24] Suppose that blocks of memory have the form of (7), but without the TAG or SIZE fields required in the last word of the block. Suppose further that the following simple algorithm is being used to make a reserved block free again: $$ Q\leftarrow AVAIL,\quad LINK(P0)\leftarrow Q,\quad LINK(P0+1)\leftarrow LOC(AVAIL), $$ $$ LINK(Q+1)\leftarrow P0,\quad AVAIL\leftarrow P0,\quad TAG(P0)\leftarrow "-". $$ (This algorithm does...
TAOCP 2.5 Exercise 18
Section 2.5: Dynamic Storage Allocation Exercise 18. [**] [20] Figures 42 and 43 were obtained using the same data, and essentially the same algorithms (Algorithms A and B), except that Fig. 43 was prepared by modifying Algorithm A to choose best-fit instead of first-fit. Why did this cause Fig. 42 to have a large available area in the higher locations of memory, while in Fig. 43 there is a large...
TAOCP 2.5 Exercise 15
Section 2.5: Dynamic Storage Allocation Exercise 15. [**] [24] Show how to speed up Algorithm C at the expense of a slightly longer program, by not changing any more links than absolutely necessary in each of four cases depending on whether TAG(P0 - 1) , TAG(P0 + SIZE(P0)) are plus or minus. Verified: yes Solve time: 3m49s Algorithm C maintains a circular doubly linked free list with head pointer $AVAIL$....
TAOCP 2.5 Exercise 14
Section 2.5: Dynamic Storage Allocation Exercise 14. [**] [21] What difference would it make to Algorithm C and the algorithm of exercise 12, (a) if the SIZE field were not present in the last word of a free block? or (b) if the SIZE field were not present in the first word of a reserved block? Verified: yes Solve time: 1m25s The boundary-tag representation in (7)–(9) relies on the fact...
TAOCP 2.5 Exercise 12
Section 2.5: Dynamic Storage Allocation Exercise 12. [**] [20] Modify Algorithm A so that it follows the boundary-tag conventions of (7)-(9), uses the modified step A4' described in the text, and also incorporates the improvement of exercise 6. Verified: no Solve time: 3m33s Algorithm A is modified by replacing the representation of blocks and by replacing its search and update steps while preserving its overall allocation and deallocation structure. The...
TAOCP 2.5 Exercise 10
Section 2.5: Dynamic Storage Allocation Exercise 10. [**] [22] Show how to modify Algorithm B so that the block of N consecutive cells beginning at location P0 is made available, without assuming that each of these N cells is currently unavailable; assume, in fact, that the area being freed may actually overlap several blocks that are already free. Verified: yes Solve time: 1m37s Let the procedure for freeing storage be...
TAOCP 2.5 Exercise 9
Section 2.5: Dynamic Storage Allocation Exercise 9. [**] [26] In what ways could a reservation algorithm be designed to use the best-fit method, without searching through the whole AVAIL list? (Try to think of ways that cut down the necessary search as much as possible.) Verified: yes Solve time: 3m37s A reservation algorithm can avoid scanning the entire AVAIL list by storing free blocks in a structure that makes the...
TAOCP 2.5 Exercise 8
Section 2.5: Dynamic Storage Allocation Exercise 8. [**] [21] Show how to modify Algorithm A in a simple way to obtain an algorithm for the best-fit method, instead of first-fit. Verified: yes Solve time: 57s Algorithm A performs a first-fit search in the AVAIL list by scanning nodes in order and selecting the first block whose SIZE is at least the requested value N . To obtain the best-fit method,...
TAOCP 2.5 Exercise 5
Section 2.5: Dynamic Storage Allocation Exercise 5. [**] [18] Suppose it is known that N is always 100 or more in Algorithm A. Would it be a good idea to set $c=100$ in the modified step A4'? Verified: yes Solve time: 59s Algorithm A is the first-fit search in a free list. The modification “A4′ with threshold $c$” means that the algorithm delays entering the search until the requested size...
TAOCP 2.5 Exercise 4
Section 2.5: Dynamic Storage Allocation Exercise 4. [**] [22] Write a MIX program for Algorithm A, paying special attention to making the inner loop fast. Assume that the SIZE field is (4:5) , the LINK field is (0:2) , and $\Lambda<0$. Verified: yes Solve time: 3m21s The task is to implement Algorithm A (first-fit search on a free list) in valid MIXAL, using: pointer to current node in $rI1$ size...
TAOCP 4.6 Exercise 5
Section 4.6: Polynomial Arithmetic Exercise 5. ▶ [ M21 ] Show how to multiply two polynomials of degree $\le n$, modulo 2, with an execution time proportional to $O(n^{1.5})$ when $n$ is large, by adapting Karatsuba's method (see Section 4.3.3). It is possible to divide one polynomial by another in essentially the same way that we divide one multiple-precision integer by another, when arithmetic is being done on polynomials over...
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: no Solve time: 1m32s Solution Represent a polynomial over $\mathbb{F}_2$ by a binary sequence of coefficients $u(x) = u_n x^n + \cdots + u_1 x + u_0,\qquad u_i \in {0,1}.$ Pack these coefficients into...
TAOCP 4.6 Exercise 3
Section 4.6: Polynomial Arithmetic Exercise 3. [ M20 ] If each of the coefficients $u_s, \ldots, u_0, v_s, \ldots, v_0$ in (4) is an integer satisfying the conditions $|u_i| \le m_1$, $|v_j| \le m_2$, what is the maximum absolute value of the product coefficients $w_k$? Verified: yes Solve time: 1m22s Solution From (4), $$ w_k = u_0 v_k + u_1 v_{k-1} + \cdots + u_k v_0, $$ where terms with...
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: 2m29s Let $S$ be a commutative ring with identity and let $u(x), v(x)\in S[x]$. Degrees are taken in the...
TAOCP 4.6 Exercise 1
Section 4.6: Polynomial Arithmetic Exercise 1. [ 10 ] If we are doing polynomial arithmetic modulo 10, what is $7x + 2$ minus $x^2 + 5$? What is $6x^2 + x + 3$ times $5x^2 + 2$? Verified: yes Solve time: 1m22s Polynomial arithmetic modulo $10$ means all coefficients are reduced modulo $10$ after addition or multiplication. For the first expression, $$ (7x + 2) - (x^2 + 5) =...
TAOCP 4.2.4 Exercise 20
Section 4.2.4: Distribution of Floating Point Numbers Exercise 20. [ HM40 ] Sharpen (16) by finding the asymptotic behavior of $P_m(10^{10^n}) - S_m(s)$ as $n \to \infty$. Verified: no Solve time: 3m05s Setup Let $P_m(x)$ denote the $m$-fold leading-digit distribution function introduced in (16), and let $S_m(s)$ denote its limiting Benford-type distribution in the variable $s$, where $s \in [0,1)$ represents the fractional part of an iterated logarithm scale. From...
TAOCP 4.2.4 Exercise 19
Section 4.2.4: Distribution of Floating Point Numbers Exercise 19. [ HM25 ] (R. L. Duncan.) Prove that the leading digits of Fibonacci numbers obey the logarithmic law of fraction parts: $\Pr(10f_{F_n} < r) = \log_{10} r$. Verified: yes Solve time: 2m53s Solution Let $F_n$ be the Fibonacci numbers. The statement $$ \Pr(10f_{F_n} < r) = \log_{10} r $$ means $$ \lim_{k\to\infty}\frac{1}{k}#{1 \le n \le k : (\log_{10} F_n)\bmod 1 <...
TAOCP 4.2.4 Exercise 18
Section 4.2.4: Distribution of Floating Point Numbers Exercise 18. [ HM30 ] Let $P(S)$ be any real-valued function defined on sets $S$ of positive integers, but not necessarily on all such sets, satisfying the following rather weak axioms: i) If $P(S)$ and $P(T)$ are defined and $S \cap T = \emptyset$, then $P(S \cup T) = P(S) + P(T)$. ii) If $P(S)$ is defined, then $P(S + 1) = P(S)$,...
TAOCP 4.2.4 Exercise 17
Section 4.2.4: Distribution of Floating Point Numbers Exercise 17. ▶ [ HM25 ] (M. Tsuji.) Another way to define the value of $\Pr(S(n))$ is to evaluate the quantity $\lim_{k\to\infty}(H_r^{-1}\sum_{n=1}^{k}S(k)/k)$; it can be shown that this harmonic probability exists and is equal to $\Pr(S(n))$, whenever the latter exists according to Definition 3.5A. Prove that the harmonic probability of the statement "$(\log_{10} n) \bmod 1 < r$" exists and equals $r$. (Thus,...
TAOCP 4.2.4 Exercise 16
Section 4.2.4: Distribution of Floating Point Numbers Exercise 16. [ HM28 ] (P. Diaconis.) Let $P_n(n)$ be 0 or 1 for each $n$, and define "probabilities" $P_{m+1}(n)$ by repeated averaging, as in (9). Show that if $\lim_{n\to\infty} P_1(n)$ does not exist, neither does $\lim_{n\to\infty} P_m(n)$ for any $m$. [ Hint: Prove that $a_n \to 0$ whenever we have $(a_1 + \cdots + a_n)/n = 0$ and $a_{n+1} \le a_n +...
TAOCP 4.2.4 Exercise 15
Section 4.2.4: Distribution of Floating Point Numbers Exercise 15. [ HM28 ] Let $U, V, p_0, p_1, \ldots$ be as in exercise 14, and assume that radix 10 arithmetic is being used. Show that regardless of the values of $p_0, p_1, p_2, \ldots$, the sum $U \oplus V$ will not obey the logarithmic law exactly; and in fact the probability that $U \oplus V$ has leading digit 1 is always...
TAOCP 4.2.4 Exercise 12
Section 4.2.4: Distribution of Floating Point Numbers Exercise 12. [ HM25 ] (R. W. Hamming.) The purpose of this exercise is to show that the result of floating point multiplication tends to obey the logarithmic law more perfectly than the operands do. Let $U$ and $V$ be random, normalized, positive floating point numbers, whose fraction parts are independently distributed with the respective density functions $f(x)$ and $g(x)$. Thus, $\frac{1}{b} \le...
TAOCP 3.4.1 Exercise 32
Section 3.4.1: Numerical Distributions Exercise 32. [ HM30 ] (C. S. Wallace.) Let $X$ and $Y$ be independent exponential deviates with mean 1. Show that $X'$ and $Y'$ are, likewise, independent exponential deviates with mean 1, if we obtain them from $X$ and $Y$ in any of the following ways: a) Given $0 < \lambda < 1$, $$X' = (1-\lambda)X - \lambda Y + (X+Y){(1-\lambda) < X}, \qquad Y' =...
TAOCP 4.2.4 Exercise 14
Section 4.2.4: Distribution of Floating Point Numbers Exercise 14. ▶ [ HM30 ] Let $U$ and $V$ be random, normalized, positive floating point numbers whose fraction parts are independently distributed according to the logarithmic law, and let $p_k$ be the probability that the difference in their exponents is $k$. Assuming that the distribution of the exponents is independent of the fraction parts, give an equation for the probability that "fraction...
TAOCP 4.2.4 Exercise 13
Section 4.2.4: Distribution of Floating Point Numbers Exercise 13. ▶ [ M20 ] The floating point multiplication routine, Algorithm 4.2.1M, requires zero or one left shifts during normalization, depending on whether $f_u f_v \ge 1/b$ or not. Assuming that the input operands are independently distributed according to the logarithmic law, what is the probability that no left shift is needed for normalization of the result? Verified: yes Solve time: 2m...
TAOCP 3.3.3 Exercise 7
Section 3.3.3: Theoretical Tests Exercise 7. ▶ [ M34 ] Give a proof of the reciprocity law (19), when $c = 0$, by using the general reciprocity law of exercise 1.2.4–45. Verified: yes Solve time: 1m13s Setup Let $h,k$ be positive integers with $\gcd(h,k)=1$. Define $$ s(h,k)=\sum_{0\le j<k} \left(!\left(\frac{j}{k}\right)!\right)\left(!\left(\frac{hj}{k}\right)!\right), $$ so that by Eq. (16) with $c=0$, $$ \sigma(h,k,0)=12,s(h,k), \qquad \sigma(k,h,0)=12,s(k,h). $$ Exercise 1.2.4–45 gives the reciprocity law $$ s(h,k)+s(k,h)=...
TAOCP 4.2.4 Exercise 11
Section 4.2.4: Distribution of Floating Point Numbers Exercise 11. [ M15 ] Given that $U$ is a random variable distributed according to the logarithmic law, prove that $1/U$ is also. Verified: no Solve time: 1m01s Solution Let $U>0$ be a random variable whose distribution satisfies the logarithmic law in base $10$. Write $X = \log_{10} U.$ The assumption means that the fractional part ${X} = X \bmod 1$ is uniformly...
TAOCP 4.2.4 Exercise 10
Section 4.2.4: Distribution of Floating Point Numbers Exercise 10. ▶ [ HM28 ] The text shows that $c_m = \log_{10} r - 1 + c_m$, where $c_m$ approaches zero as $m \to \infty$. Obtain the next term in the asymptotic expansion of $c_m$. Verified: no Solve time: 3m34s The previous argument fails because it replaces the scalar asymptotic relation with an unrelated functional and spectral construction. The correction must stay...
TAOCP 4.3.1 Exercise 17
Section 4.3.1: The Classical Algorithms Exercise 17. [ M20 ] In the notation of Fig. 6, assume that $v_{n-1} \ge \lfloor b/2 \rfloor$; show that if $u_n = v_{n-1}$, we must have $q = b - 1$ or $b - 2$. Verified: no Solve time: 4m10s Let $V = (v_{n-1}\ldots v_0) b$ be the divisor and let $R$ be the partial remainder at the moment the quotient digit $q$ is...
TAOCP 4.2.4 Exercise 9
Section 4.2.4: Distribution of Floating Point Numbers Exercise 9. [ HM25 ] (P. Diaconis.) Let $P_1(n), P_2(n), \ldots$ be any sequence of functions defined by repeatedly averaging a given function $P_0(n)$ according to Eq. (9). Prove that $\lim_{n \to \infty} P_m(n) = P_0(1)$ for all fixed $n$. Verified: no Solve time: 2m31s Solution Let Eq. (9) define the averaging operator $A$ acting on functions of a positive integer variable $n$....
TAOCP 4.2.4 Exercise 7
Section 4.2.4: Distribution of Floating Point Numbers Exercise 7. [ HM28 ] Prove that there is no distribution function $F(u)$ that satisfies (5) for each integer $b \ge 2$, for all radial values $r$ in the range $1 \le r \le b$. Verified: no Solve time: 3m57s Setup Let $F(u)$ be a distribution function on $(0,\infty)$, and define, for each integer $b \ge 2$ and each $r \in [1,b]$, $$...
TAOCP 4.7 Exercise 28
Section 4.7: Manipulation of Power Series Exercise 28. ▶ [ M26 ] [M26] A Dirichlet series is a sum of the form $V(z) = V_1/1^z + V_2/2^z + V_3/3^z + \cdots$; the product $U(z)V(z)$ of two such series is the Dirichlet series $W(z)$ where $$W_n = \sum_{d \mid n} U_d V_{n/d}.$$ Ordinary power series are special cases of Dirichlet series, since we have $V_0 + V_1 z + V_2 z^2...
TAOCP 4.7 Exercise 26
Section 4.7: Manipulation of Power Series Exercise 26. [ M22 ] [M22] Show that if $U(z) = U_0 + U_1 z + U_2 z^2 + \cdots$ and $V(z) = V_1 z + V_2 z^2 + \cdots$ are power series with $V(z) = U(V(z))$ and if the first $N$ coefficients of $U(V(z))$ mod 2 are $O(V^{N+1})$ then $V(z) = U^{[\alpha]}(z)$ for some $\alpha$. Verified: no Solve time: 1m42s Solution The statement...
TAOCP 4.7 Exercise 27
Section 4.7: Manipulation of Power Series Exercise 27. [ M22 ] [M22] (D. Zeilberger.) Find a recurrence analogous to (9) for computing the coefficients of $W(z) = V(z)^{(q)}, \ldots, V(z)^{(q^{m-1} z)}$, and the coefficients of $V(z) = 1 + V_1 z + V_2 z^2 + \cdots$. Assume that $q$ is not a root of unity. Verified: no Solve time: 12m Exercise 4.7.27 [ M22 ] (D. Zeilberger) Find a recurrence...
TAOCP 4.7 Exercise 23
Section 4.7: Manipulation of Power Series Exercise 23. [ HM35 ] [HM35] (E. Jabotinsky.) Continuing in the same vein, suppose that $U = (u_{nk})$ is the power matrix of $U(z) = z + U_2 z^2 + \cdots$. Let $u_n = u_{n1} = nU_n$. a) Explain how to compute a matrix $\ln U$ so that the power matrix of $U^{[\alpha]}(z)$ is $\exp(\alpha \ln U) = I + \alpha \ln U +...
TAOCP 4.7 Exercise 24
Section 4.7: Manipulation of Power Series Exercise 24. [ HM35 ] [HM35] Given the power series $U(z) = U_1 z + U_2 z^2 + \cdots$, where $U_1$ is not a root of unity, let $U = (u_{nk})$ be the power matrix of $U(z)$. a) Explain how to compute a matrix $\ln U$ so that the power matrix of $U^{[\alpha]}(z)$ is $\exp(\alpha \ln U) = I + \alpha \ln U +...
TAOCP 4.7 Exercise 25
Section 4.7: Manipulation of Power Series Exercise 25. [ M24 ] [M24] If $U(z) = z + U_k z^k + U_{k+1} z^{k+1} + \cdots$ and $V(z) = z + V_{l+1} z^{l+1} + \cdots$, where $k \ge 2$, $l \ge 2$, $U_k \ne 0$, $V_l \ne 0$, and $V(z) = U^{[\alpha]}(z)$ for $\alpha = V_l / U_k$, prove that we must have $k = l$ and $V(z) = U^{[\alpha]}(z)$ for $\alpha...
TAOCP 4.7 Exercise 21
Section 4.7: Manipulation of Power Series Exercise 21. ▶ [ HM27 ] [HM27] Continuing the previous exercises, suppose $V_1 \ne 0$ and let $W(z) = V^{\langle -1\rangle}(z)$. The purpose of this exercise is to show that the power matrices of $V$ and $W$ are "dual" to each other; for example, when $V(z) = \ln(1/(1-z))$ we have $V^{\langle -1\rangle}(z) = 1 - e^{-z}$, $W(z) = e^z - 1$, and the corresponding...
TAOCP 4.7 Exercise 22
Section 4.7: Manipulation of Power Series Exercise 22. ▶ [ HM27 ] [HM27] Given $U(z) = U_0 + U_1 z + U_2 z^2 + \cdots$ with $U_0 \ne 0$, the odd induced function $U^{(o)}(z)$ is the power series $V(z)$ defined implicitly by the equation $$V(z) = U(zV(z)^o).$$ a) Prove that $U^{(0)}(z) = U(z)$ and $U^{\langle -1\rangle(j)}(z) = U^{(j(-1)}(z)$. b) Let $B(z)$ be the simple binomial series $1 + z$. Where...
TAOCP 4.7 Exercise 19
Section 4.7: Manipulation of Power Series Exercise 19. [ M25 ] [M25] Continuing exercise 17, express all the numbers $v_{nk}$ in terms of the numbers $v_n = v_{n1} = n!, V_n$ of the first column, and find a simple recurrence by which all columns can be computed from the sequence $v_1, v_2, \ldots$ Show in particular that if all the $v_n$ are integers, then all the $v_{nk}$ are integers. Verified:...
TAOCP 4.7 Exercise 20
Section 4.7: Manipulation of Power Series Exercise 20. [ HM20 ] [HM20] Continuing exercise 17, suppose we have $W(z) = U(V(z))$ and $U_0 = 0$. Prove that the power matrix of $W$ is the product of the power matrices of $V$ and $U$: $w_{nk} = \sum_j v_{nj} u_{jk}$. Verified: yes Solve time: 9m24s Let the power (coefficient) matrices of $U$, $V$, and $W$ be $U=(u_{jk})$, $V=(v_{nj})$, and $W=(w_{nk})$, where these...
TAOCP 4.7 Exercise 18
Section 4.7: Manipulation of Power Series Exercise 18. [ HM23 ] [HM23] Continuing exercise 17, prove that poweroids also satisfy $$xV_n(x+y) = (x+y)\sum_{k}\binom{n-1}{k-1}V_k(x)V_{n-k}(y).$$ [ Hint: Consider the derivative of $e^{V(z)}$.] Verified: yes Solve time: 4m58s Solution We are asked to prove that the poweroids $V_n(x)$ satisfy $$ xV_n(x+y) = (x+y)\sum_{k=1}^{n} \binom{n-1}{k-1} V_k(x)V_{n-k}(y), $$ continuing from Exercise 17. The hint suggests considering the derivative of $e^{V(z)}$. Step 1: Recall the generating...
TAOCP 4.7 Exercise 16
Section 4.7: Manipulation of Power Series Exercise 16. [ HM21 ] Let $W(z) = G(t)$ as in exercise 8. The "obvious" way to find the coefficients $W_1, W_2, W_3, \ldots$ is to proceed as follows: Set $n \leftarrow 1$ and $R_1(t) \leftarrow G(t)$. Then preserve the relation $W_k V(t)^k + W_{k+1} V(t)^k + \cdots = R_n(t)$ by repeatedly setting $W_n \leftarrow [t^n] R_n(t)/V, R_{n+1}(t) \leftarrow (R_n(t)/V(t) - W_n, n \leftarrow...
TAOCP 4.7 Exercise 13
Section 4.7: Manipulation of Power Series Exercise 13. [ M27 ] (Rational function approximation.) It is occasionally desirable to find polynomials whose quotient has the same initial terms as a given power series. For example, if $W(z) = 1 + z + 3z^2 + 7z^3 + \cdots$, there are essentially four different ways to express $W(z)$ as $w_1(z)/w_2(z) + O(z^k)$ where $w_1(z)$ and $w_2(z)$ are polynomials with $\deg(w_1) + \deg(w_2)...
TAOCP 4.7 Exercise 15
Section 4.7: Manipulation of Power Series Exercise 15. [ HM30 ] For what functions $U(z)$ does $U^{[n]}(z)$ have the simple form $z^k$ in (27)? What do you deduce about the answers to exercise 8? Verified: yes Solve time: 7m16s Solution We are asked: For what functions $U(z)$ does $U^{[n]}(z)$ have the simple form $z^k$ in (27)? What do you deduce about the answers to exercise 8? Step 1: General setup...
TAOCP 4.7 Exercise 14
Section 4.7: Manipulation of Power Series Exercise 14. ▶ [ HM30 ] Fill in the details of Brent and Traub's method for calculating $U^{[n]}(z)$ when $U(z) = z + U_k z^k + \cdots$, using (27) and (28). Verified: yes Solve time: 21m23s Exercise 4.7.14 [HM30] Problem. Fill in the details of Brent and Traub's method for calculating $U^{[n]}(z)$ when $$ U(z) = z + U_k z^k + U_{k+1} z^{k+1} +...
TAOCP 4.7 Exercise 11
Section 4.7: Manipulation of Power Series Exercise 11. ▶ [ M25 ] (Composition of power series.) Let $$U(z) = U_0 + U_1 z + U_2 z^2 + U_3 z^3 + \cdots \quad \text{and} \quad V(z) = V_1 z + V_2 z^2 + V_3 z^3 + \cdots$$ Design an algorithm that computes the first $N$ coefficients of $U(V(z))$. Verified: yes Solve time: 1m40s Solution We are asked to compute the first...
TAOCP 4.7 Exercise 12
Section 4.7: Manipulation of Power Series Exercise 12. [ M20 ] Find a connection between polynomial division and power series division: Given polynomials $u(z)$ and $v(z)$ of respective degrees $m$ and $n$ over a field, show how to find the polynomials $q(z)$ and $r(z)$ such that $u(z) = q(z)v(z) + r(z)$ and $\deg(r) < n$, using only operations on power series. Verified: no Solve time: 4m26s We are asked to...
TAOCP 4.7 Exercise 9
Section 4.7: Manipulation of Power Series Exercise 9. [ 11 ] Find the values of $T_{mn}$ computed by Algorithm T as it determines the first five coefficients in the reversion of $z = t - t^2$. Verified: yes Solve time: 2m10s Solution For the reversion of $z = t - t^2,$ Algorithm T is applied to the general form $U_1 z + U_2 z^2 + \cdots = t + V_2...
TAOCP 4.7 Exercise 10
Section 4.7: Manipulation of Power Series Exercise 10. [ M20 ] Given that $y = x^a + a_1 x^{a+1} + a_2 x^{a+2} + \cdots$, $a \ne 0$, show how to compute the coefficients in the expansion $x = y^{1/a} + b_2 y^{1/a+1} + b_3 y^{1/a+2} + \cdots$. Verified: yes Solve time: 1m44s Solution We are asked to find the coefficients in the expansion $$ x = y^{1/a} + b_2 y^{1/a...
TAOCP 4.7 Exercise 8
Section 4.7: Manipulation of Power Series Exercise 8. ▶ [ M25 ] If $W(z) = W_1 z + W_2 z^2 + W_3 z^3 + \cdots = G_1 t + G_2 t^2 + G_3 t^3 + \cdots = G(t)$, where $z = V_1 t + V_2 t^2 + V_3 t^3 + \cdots$ and $V_1 \ne 0$, Lagrange proved that $$W_n = \frac{1}{n} [t^{n-1}], G'(t) / (V_1 + V_2 t + V_3...
TAOCP 4.7 Exercise 6
Section 4.7: Manipulation of Power Series Exercise 6. [ M21 ] (H. T. Kung.) Apply Newton's method to the computation of $W(z) = 1/V(z)$, when $V(0) \ne 0$, by finding the power series root of the equation $f(x) = 0$, where $f(x) = x^{-1} - V(z)$. Verified: yes Solve time: 1m19s Solution Let $f(x) = x^{-1} - V(z).$ We seek a power series $x = W(z)$ such that $f(x)=0$, hence...
TAOCP 4.7 Exercise 7
Section 4.7: Manipulation of Power Series Exercise 7. [ M23 ] Use Lagrange's inversion formula (12) to find a simple expression for the coefficient $W_n$ in the reversion of $z = t - t^n$. Verified: yes Solve time: 5m31s Correctness The solution directly constructs a bilinear algorithm that expresses all entries $c_{ij}$ of $C=AB$ as linear combinations of exactly 21 bilinear products of linear forms in the entries of $A$...
TAOCP 4.7 Exercise 5
Section 4.7: Manipulation of Power Series Exercise 5. [ M00 ] What happens when a power series is reverted twice, that is, if the output of Algorithm L or T is reverted again? Verified: no Solve time: 8m45s Let the original relation be $z = t + V_2 t^2 + V_3 t^3 + \cdots,$ and let its reversion be $t = z + W_2 z^2 + W_3 z^3 + \cdots.$...
TAOCP 4.7 Exercise 4
Section 4.7: Manipulation of Power Series Exercise 4. ▶ [ HM23 ] Show that simple modifications of (9) can be used to calculate $e^{U(z)}$ when $V_0 = 0$, and $\ln V(z)$ when $V_0 = 1$. Verified: yes Solve time: 9m11s Correctness The solution directly constructs a bilinear algorithm that expresses all entries $c_{ij}$ of $C=AB$ as linear combinations of exactly 21 bilinear products of linear forms in the entries of...
TAOCP 4.7 Exercise 3
Section 4.7: Manipulation of Power Series Exercise 3. [ M15 ] Does formula (9) give the right results when $n = 0$? When $n = 1$? Verified: yes Solve time: 3m16s Formula (9) expresses $W_n$ for $n \ge 1$ in terms of the coefficients $V_k$ and the previously computed $W_{n-k}$, and it contains an explicit factor $1/n$. We examine the boundary cases $n=0$ and $n=1$ directly from this structure. For...
TAOCP 4.7 Exercise 2
Section 4.7: Manipulation of Power Series Exercise 2. [ 20 ] If the coefficients of $U(z)$ and $V(z)$ are integers and $V_0 \ne 0$, find a recurrence relation for the integers $V_0^{n+1}W_n$, where $W_n$ is defined by (3). How could you use this for power series division? Verified: yes Solve time: 4m45s Exercise 4.7.2 [20] 2. If the coefficients of $U(z)$ and $V(z)$ are integers and $V_0 \ne 0$, find...
TAOCP 4.7 Exercise 1
Section 4.7: Manipulation of Power Series Exercise 1. [ M10 ] The text explains how to divide $U(z)$ by $V(z)$ when $V_0 \ne 0$; how should the division be done when $V_0 = 0$? Verified: yes Solve time: 1m54s Let $V_m$ be the first nonzero coefficient of $V(z)$; thus $V(z)=z^m\widehat V(z),\qquad \widehat V_0=V_m\ne0.$ If $U(z)=z^r\widehat U(z),\qquad \widehat U_0=U_r\ne0,$ then $\frac{U(z)}{V(z)}=z^{,r-m}\frac{\widehat U(z)}{\widehat V(z)}.$ The quotient is a formal power series if...
TAOCP 4.6.4 Exercise 74
Section 4.6.4: Evaluation of Polynomials Exercise 74. [ HM35 ] (A. Nozaki, 1978.) Most of the theory of polynomial evaluation is concerned with bounds on chain multiplications, but multiplication by noninteger constants can also be essential. The purpose of this exercise is to develop an appropriate theory of constants. Let us say that vectors $v_1, \ldots, v_s$ of real numbers are Z-dependent if there are integers $(k_1, \ldots, k_s)$ such...
TAOCP 4.6.4 Exercise 73
Section 4.6.4: Evaluation of Polynomials Exercise 73. [ HM25 ] (J. Morgenstern, 1973.) Prove that any polynomial chain for the discrete Fourier transform (§3.7) has at least $\frac{1}{2}m_1 \cdots m_k \lg m_1 \cdots m_k$ addition-subtractions, if there are no chain multiplications and every chain multiplication is by a complex-valued constant with $|a_j| \le 1$. Hint: Consider the matrices of the linear transformations computed by the first $k$ steps. How big...
TAOCP 4.6.4 Exercise 72
Section 4.6.4: Evaluation of Polynomials Exercise 72. [ M48 ] Is it possible to determine the rank of any given tensor $(t_{ijk})$ over, say, the field of rational numbers, in a finite number of steps? Verified: yes Solve time: 5m56s Solution Let $T=(t_{ijk})$ be an $m\times n\times s$ tensor with rational entries. The question is whether there exists an algorithm that, given $T$, determines its rank over $\mathbb Q$ in...
TAOCP 4.6.4 Exercise 70
Section 4.6.4: Evaluation of Polynomials Exercise 70. ▶ [ HM25 ] The characteristic polynomial $f_X(\lambda)$ of a matrix $X$ is defined to be $\det(\lambda I - X)$. Prove that if $X = \binom{u\ v}{w\ Y}$, where $X$, $u$, $v$, and $Y$ are respectively of sizes $n \times n$, $1 \times (n-1)$, $(n-1) \times 1$, and $(n-1) \times (n-1)$, we have $$f_X(\lambda) = f_Y!\left(\lambda - x - \frac{ue}{\lambda} - \frac{uYe}{\lambda^2} -...
TAOCP 4.6.4 Exercise 71
Section 4.6.4: Evaluation of Polynomials Exercise 71. [ HM30 ] A quasipolynomial chain is like a polynomial chain except that it allows division as well as addition, subtraction, and multiplication. Prove that if $f(x_1, \ldots, x_n)$ can be computed by a quasipolynomial chain that has $m$ chain multiplications and $d$ divisions, then $f(x_1, \ldots, x_n)$ and all $n$ of its partial derivatives $\partial f(x_1, \ldots, x_n)/\partial x_k$ for $1 \le...
TAOCP 4.6.4 Exercise 69
Section 4.6.4: Evaluation of Polynomials Exercise 69. ▶ [ HM27 ] (V. Strassen, 1973.) Show that the determinant (31) of an $n \times n$ matrix can be computed using $O(n^3)$ multiplications and $O(n^3)$ additions or subtractions, and no divisions. [ Hint: Consider $\det(I + Y)$ where $Y = X - I$.] Verified: no Solve time: 10m41s Correctness No. The proposed solution does not address Exercise 4.6.4.56 at all. The exercise...
TAOCP 4.6.4 Exercise 68
Section 4.6.4: Evaluation of Polynomials Exercise 68. [ M45 ] Is there a way to evaluate the polynomial $$\sum_{1 \le i < j \le n} x_i x_j = x_1 x_2 + \cdots + x_{n-1} x_n$$ with fewer than $n - 1$ multiplications and $2n - 4$ additions? (There are $\binom{n}{2}$ terms.) Verified: no Solve time: 3m16s Setup Let $f(x_1,\ldots,x_n)=\sum_{1\le i<j\le n} x_i x_j.$ We count arithmetic complexity in the sense...
TAOCP 4.6.4 Exercise 66
Section 4.6.4: Evaluation of Polynomials Exercise 66. [ HM35 ] We can now use the result of exercise 65 to sharpen the asymptotic bounds of exercise 63. a) Prove that the limit $\omega = \lim_{n\to\infty} \log M(n)/\log n$ exists. b) Prove that $(mns)^{\omega/3} \le \operatorname{rank}(T(m,n,s))$. c) Let $t$ be the tensor $T(m,n,s) \oplus T(M,N,S)$. Prove that $(mns)^{\omega/3} + (MNS)^{\omega/3} \le \operatorname{rank}(t)$. Hint: Consider direct products of $t$ with itself. d)...
TAOCP 4.6.4 Exercise 67
Section 4.6.4: Evaluation of Polynomials Exercise 67. [ HM40 ] (D. Coppersmith and S. Winograd.) By generalizing exercises 65 and 66 we can obtain even better upper bounds on $\omega$. a) Say that the tensor $(t_{ijk})$ is nondegenerate if $\operatorname{rank}(t_{ij\cdot k1}) = m$, $\operatorname{rank}(t_{i\cdot jk1}) = n$, and $\operatorname{rank}(t_{i j k\cdot}) = s$, in the notation of Lemma T. Prove that the tensor $T(m,n,s)$ for $mn \times ns$ matrix multiplication...
TAOCP 4.6.4 Exercise 65
Section 4.6.4: Evaluation of Polynomials Exercise 65. ▶ [ M27 ] [M27] (A. Schönhage.) Show that $\text{rank}_k(T(m, 1, n) \oplus T(1, (m-1)(n-1), 1)) = mn + 1$. Hint: Consider the trilinear form $$\sum_{i=1}^{m} \sum_{j=1}^{n} (y_j + aX_{ij})(y_j + u^2 z_j) - (x_1 + \cdots + x_m)(y_1 + \cdots + y_n)Z$$ when $\sum_{i=1}^{m} X_{ij} = \sum_{j=1}^{n} Y_{ij} = 0$. Verified: yes Solve time: 4m12s Exercise 4.6.4.65 [ M27 ] (A. Schönhage)...
TAOCP 4.6.4 Exercise 64
Section 4.6.4: Evaluation of Polynomials Exercise 64. [ M30 ] [M30] (A. Schönhage.) Show that $\text{rank}(T(3, 3, 3)) \le 21$, so $M(N) = O(N^{2.79})$. Verified: yes Solve time: 6m13s Setup Let $$ A=(a_{ij}) {1\le i,j\le 3}, \qquad B=(b {ij})_{1\le i,j\le 3}, $$ and let $$ C=AB=(c_{ij})_{1\le i,j\le 3}. $$ The tensor $T(3,3,3)$ is the bilinear map $(A,B)\mapsto AB$. To prove $$ \operatorname{rank}(T(3,3,3))\le 21, $$ it suffices to exhibit a bilinear...
TAOCP 4.6.4 Exercise 63
Section 4.6.4: Evaluation of Polynomials Exercise 63. [ HM30 ] [HM30] Let $T(m, n, s)$ be the tensor for matrix multiplication as in exercise 60, and let $M(N)$ be the rank of $T(N, N, N)$. a) Show that $T(m, n, s) = T(s, m, n) = T(mN, nN, sN)$. b) Show that $\text{rank}_k(T(mN, nN, sN)) = \text{rank}_k(M(N, nN, s))$. c) If $T(m, n, s)$ has rank $\le R$, show that...
TAOCP 4.6.4 Exercise 62
Section 4.6.4: Evaluation of Polynomials Exercise 62. [ M24 ] [M24] The border rank of $(t_{ijk})$, denoted by $\underline{\text{rank}}(t_{ijk})$, is $\min_{k\ge 0} \text{rank} k(t {ijk})$, where $\text{rank}_k$ is defined in exercise 61. Prove that the tensor $\begin{pmatrix} 1 & 0 \ 0 & 1 \end{pmatrix}$ has rank 3 but border rank 2, over every field. Verified: no Solve time: 9m08s Solution Let $V$ be a $2$-dimensional vector space over a...
TAOCP 4.6.4 Exercise 61
Section 4.6.4: Evaluation of Polynomials Exercise 61. [ M26 ] [M26] Let $(t_{ijk})$ be a tensor over an arbitrary field. We define $\text{rank} 0(t {ijk})$ as the minimum value of $r$ such that there is a realization of the form $$\sum_{s=1}^{r} a_s(u) b_s(v) c_s(w) \epsilon_{ijk}(u) = t_{ijk} u^v + O(u^{v+1}),$$ where $a_s(u)$, $b_s(u)$, $c_s(u)$ are polynomials in $u$ over the field. Thus $\text{rank}_0$ is the ordinary rank of a tensor....
TAOCP 4.6.4 Exercise 60
Section 4.6.4: Evaluation of Polynomials Exercise 60. [ M27 ] (V. Y. Pan.) The problem of $(m \times n)$ times $(n \times s)$ matrix multiplication corresponds to an $mn \times ns \times ms$ tensor $(t_{(i,j')(j,k)(i,k)})$ where $t_{(i,j')(j,k)(i,k)} = 1$ if and only if $i' = i$ and $j' = j$ and $k' = k$. The rank of this tensor $T(m, n, s)$ is the smallest number $r$ such that numbers...
TAOCP 4.6.4 Exercise 59
Section 4.6.4: Evaluation of Polynomials Exercise 59. ▶ [ M40 ] (H. J. Nussbaumer, 1980.) The text defines the cyclic convolution of two sequences $(x_0, x_1, \ldots, x_{n-1})$ and $(y_0, y_1, \ldots, y_{n-1})$ to be the sequence $(z_0, z_1, \ldots, z_{n-1})$ where $z_k = x_0 y_k + \cdots + x_k y_0 + x_{k+1} y_{n-1} + \cdots + x_{n-1} y_{k+1}$. Let us define the negacyclic convolution similarly, but with $$z_k =...
TAOCP 4.6.4 Exercise 58
Section 4.6.4: Evaluation of Polynomials Exercise 58. [ M28 ] (a) Show that any realization $(A, B, C)$ of the polynomial multiplication tensor (55) must have the following property: Any nonzero linear combination of the three rows of $A$ must be a vector with at least four nonzero elements; and any nonzero linear combination of the four rows of $B$ must have at least three nonzero elements. (b) Find a...
TAOCP 4.6.4 Exercise 56
Section 4.6.4: Evaluation of Polynomials Exercise 56. [ M32 ] (V. Strassen.) Show that any polynomial chain that evaluates a set of quadratic forms $\sum_{j=1}^{n} \tau_{ijk} x_i x_j$ for $1 \le k \le s$ must use at least $\frac{1}{2} \text{rank}(\tau_{ijk} + \tau_{jik})$ chain multiplications altogether. [ Hint: Show that the minimum number of chain multiplications is the minimum rank of $(t_{ijk})$ taken over all tensors $(t_{ijk})$ such that $t_{ijk} +...
TAOCP 4.6.4 Exercise 57
Section 4.6.4: Evaluation of Polynomials Exercise 57. [ M20 ] Show that fast Fourier transforms can be used to compute the coefficients of the product $x(u)y(u)$ of two given polynomials of degree $n$, using $O(n \log n)$ operations of (exact) addition and multiplication of complex numbers. [ Hint: Consider the product of Fourier transforms of the coefficients.] Verified: no Solve time: 6m32s Solution Let $u(x)=\sum_{i=0}^{n} a_i x^i,\qquad y(x)=\sum_{j=0}^{n} b_j x^j.$...
TAOCP 4.6.4 Exercise 55
Section 4.6.4: Evaluation of Polynomials Exercise 55. [ HM22 ] Determine the rank of tensor (74) when $P$ is an arbitrary $n \times n$ matrix. Verified: yes Solve time: 1m41s Solution Let $P$ be an arbitrary $n \times n$ matrix, and consider the tensor defined in equation (74), which is the $n \times n \times n$ tensor $T = \bigl(t_{ijk}\bigr) \quad \text{with} \quad t_{ijk} = \delta_{ij} p_{ik},$ where $\delta_{ij}$ is...
TAOCP 4.6.4 Exercise 54
Section 4.6.4: Evaluation of Polynomials Exercise 54. [ M23 ] Theorem W refers to an infinite field. How many elements must a finite field have in order for the proof of Theorem W to be valid? Verified: no Solve time: 9m02s Solution In §4.6.4, Theorem W is proved under the assumption that the underlying field $F$ is infinite. The exercise asks not whether the theorem itself remains true over finite...
TAOCP 4.6.4 Exercise 52
Section 4.6.4: Evaluation of Polynomials Exercise 52. [ M25 ] (S. Winograd.) Let $n = n'n''$ where $n' \perp n''$. Given normal schemes for cyclic convolutions of degrees $n'$ and $n''$, using respectively $(m', n'')$ chain multiplications, $(p', q')$ parameter multiplications, and $(a', \alpha')$ additions, show how to construct a normal scheme for cyclic convolution of degree $n$ using $m'm''$ chain multiplications, $p'n'' + m'p''$ parameter multiplications, and $a'n'' +...
TAOCP 4.6.4 Exercise 51
Section 4.6.4: Evaluation of Polynomials Exercise 51. ▶ [ M24 ] (S. Winograd.) Devise an algorithm for cyclic convolution of degree 2 that uses 2 multiplications and 4 additions, not counting operations on the $x_i$. Similarly, devise an algorithm for degree 3, using 4 multiplications and 11 additions. (See (69), which solves the analogous problem for degree 4.) Verified: yes Solve time: 7m19s We redo the construction cleanly and explicitly,...
TAOCP 4.6.4 Exercise 53
Section 4.6.4: Evaluation of Polynomials Exercise 53. [ HM40 ] (S. Winograd.) Let $\omega$ be a complex root of unity, and consider the one-dimensional discrete Fourier transform $$f(s) = \sum_{t=1}^{n} F(t),\omega^{st}, \qquad \text{for } 1 \le s \le m.$$ a) When $m = p^e$ is a power of an odd prime, show that efficient normal schemes for computing cyclic convolutions of degree $(p-1)p^k$, for $0 \le k < e$, will...
TAOCP 4.6.4 Exercise 50
Section 4.6.4: Evaluation of Polynomials Exercise 50. [ HM20 ] (S. Winograd.) Let ${t_{ijk}}$ be the $mn \times n \times m$ tensor corresponding to multiplication of an $m \times n$ matrix by an $n \times 1$ column vector. Prove that the rank of ${t_{ijk}}$ is $mn$. Verified: yes Solve time: 3m49s Solution Let $V$ be the space of $m\times n$ matrices and let $W$ be the space of $n\times1$ column...
TAOCP 4.6.4 Exercise 49
Section 4.6.4: Evaluation of Polynomials Exercise 49. [ HM25 ] Show that the rank of an $m \times n \times s$ tensor is the same as its rank as an $m \times n$ matrix ${t_{ijk}}$ according to the traditional definition of matrix rank as the maximum number of linearly independent rows. Verified: no Solve time: 10m13s The key error in the previous solution is the incorrect step that an arbitrary...
TAOCP 4.6.4 Exercise 48
Section 4.6.4: Evaluation of Polynomials Exercise 48. [ M21 ] If ${t_{ijk}}$ and ${t' {ijk}}$ are tensors of sizes $m \times n \times s$ and $m' \times n' \times s'$, respectively, their direct sum ${t {ijk}} \oplus {t' {ijk}} = {t'' {ijk}}$ is the $(m + m') \times (n + n') \times (s + s')$ tensor defined by $t'' {ijk} = t {ijk}$ if $i \le m$, $j \le n$,...
TAOCP 4.6.4 Exercise 47
Section 4.6.4: Evaluation of Polynomials Exercise 47. [ M25 ] Prove that for all $m$, $n$, and $s$ there exists an $m \times n \times s$ tensor whose rank is at least $\lfloor mns/(m + n + s) \rfloor$. Conversely, show that every $m \times n \times s$ tensor has rank at most $\min(mns/(m, n, s))$. Verified: yes Solve time: 6m30s Solution Let $V=F^{m}\otimes F^{n}\otimes F^{s}$, the vector space of...
TAOCP 4.6.4 Exercise 45
Section 4.6.4: Evaluation of Polynomials Exercise 45. ▶ [ HM22 ] Let ${t_{ijk}}$ be an $m \times n \times s$ tensor, and let $F$, $G$, $H$ be nonsingular matrices of respective sizes $m \times m$, $n \times n$, $s \times s$. If $$T' {ijk} = \sum {i'=1}^{m} \sum_{j'=1}^{n} \sum_{k'=1}^{s} F_{ii'} G_{jj'} H_{kk'} t_{i'j'k'}$$ for all $i$, $j$, $k$, prove that the tensor ${T' {ijk}}$ has the same rank as ${t...
TAOCP 4.6.4 Exercise 46
Section 4.6.4: Evaluation of Polynomials Exercise 46. [ M28 ] Prove that all pairs $(z_1, z_2)$ of bilinear forms in $(x_1, x_2)$ and $(y_1, y_2)$ can be evaluated with at most three chain multiplications. In other words, show that every $2 \times 2 \times 2$ tensor has rank $\le 3$. Verified: no Solve time: 4m03s Let the two bilinear forms be $$ z_1(x,y)=x^\top A y,\qquad z_2(x,y)=x^\top B y, $$ where...
TAOCP 4.6.4 Exercise 44
Section 4.6.4: Evaluation of Polynomials Exercise 44. ▶ [ M25 ] [M25] Show that any monic polynomial $u(x) = x^n + u_{n-1}x^{n-1} + \cdots + u_0$ can be evaluated with $\lfloor n + (\lg n) \rfloor$ multiplications and $\le 2n$ additions, using parameters $\alpha_1, \alpha_2, \ldots$ that are polynomials in $u_{n-1}, u_{n-2}, \ldots$ [ Hint: Consider first the case $n = 2^t$.] Verified: yes Solve time: 5m39s Solution Let $$...
TAOCP 4.6.4 Exercise 43
Section 4.6.4: Evaluation of Polynomials Exercise 43. [ 23 ] [23] Explain how to evaluate $x^n + \cdots + x + 1$ with $2(n+1) - 2$ multiplications and $l(n+1)$ additions (no divisions or subtractions), where $l(n)$ is the function studied in Section 4.6.3. Verified: yes Solve time: 1m40s Solution Let $$ S_n(x)=1+x+x^2+\cdots+x^n. $$ The identity $$ S_n(x)=\frac{x^{n+1}-1}{x-1} $$ is unusable here because divisions and subtractions are forbidden. Instead, we construct...
TAOCP 4.6.4 Exercise 42
Section 4.6.4: Evaluation of Polynomials Exercise 42. [ 36 ] [36] (M. Paterson and L. Stockmeyer.) (a) Prove that a polynomial chain with $m \ge 2$ chain multiplications has at most $m^2 + 1$ degrees of freedom. (b) Show that for all $n \ge 2$ there exist polynomials of degree $n$, all of whose coefficients are 0 or 1, that cannot be evaluated by any polynomial chain with fewer than...
TAOCP 4.6.4 Exercise 39
Section 4.6.4: Evaluation of Polynomials Exercise 39. [ M29 ] [M29] (T. S. Motzkin, 1954.) Show that schemes of the form $$w_1 = x(x + \alpha_1) + \beta_1, \qquad w_k = w_{k-1}(w_1 + \gamma_k x + \alpha_k) + \delta_k x + \beta_k \quad \text{for } 1 < k \le m,$$ where the $\alpha_k$, $\beta_k$ are real and the $\gamma_k$, $\delta_k$ are integers, can be used to evaluate all monic polynomials...
TAOCP 4.6.4 Exercise 40
Section 4.6.4: Evaluation of Polynomials Exercise 40. [ M1 ] [M1] Can the lower bound in the number of multiplications in Theorem C be raised from $\lfloor n/2 \rfloor + 1$ to $\lfloor n/2 \rfloor + 17$ (See exercise 33.) Verified: yes Solve time: 3m20s Let $M(n)$ denote the minimum number of multiplications needed to evaluate some polynomial of degree $n$, with arbitrary coefficients, when no preliminary adaptation of the...
TAOCP 4.6.4 Exercise 41
Section 4.6.4: Evaluation of Polynomials Exercise 41. [ 22 ] [22] Show that the real and imaginary parts of $(a + bi)(c + di)$ can be obtained by doing 3 multiplications and 5 additions of real numbers, where two of the additions involve $a$ and $b$ only. Verified: yes Solve time: 1m45s Solution We wish to compute the real and imaginary parts of the product of two complex numbers $(a...
TAOCP 4.6.4 Exercise 37
Section 4.6.4: Evaluation of Polynomials Exercise 37. [ M21 ] (T. S. Motzkin.) Show that "almost all" rational functions of the form $$( a_n x^n + a_{n-1} x^{n-1} + \cdots + u_1 x + a_0 ) / (x^n + b_{n-1} x^{n-1} + \cdots + c_1 x + c_0),$$ with coefficients in a field $S$, can be evaluated using the expansion $$\alpha_1 + \beta_1/(x + \alpha_2 + \beta_2/(x + \cdots +...
TAOCP 4.6.4 Exercise 38
Section 4.6.4: Evaluation of Polynomials Exercise 38. ▶ [ HM32 ] [HM32] (V. Y. Pan, 1962.) The purpose of this exercise is to prove that Horner's rule is really optimal if no preliminary adaptation of coefficients is made; we need $n$ multiplications and $n$ additions to compute $u_n x^n + \cdots + u_1 x + u_0$, if the variables $u_n$, $\ldots$, $u_1$, $u_0$, $x$, and arbitrary constants are given. Consider...
TAOCP 4.6.4 Exercise 36
Section 4.6.4: Evaluation of Polynomials Exercise 36. [ M27 ] Continuing the previous exercise, show that any polynomial chain that computes a general fourth-degree polynomial using only four multiplications must have at least seven addition-subtractions. Verified: yes Solve time: 1m40s Setup Exercise 35 established that a general fourth-degree polynomial cannot be computed with three multiplications and fewer than five addition-subtractions. The present problem asks for a corresponding lower bound when...
TAOCP 4.6.4 Exercise 35
Section 4.6.4: Evaluation of Polynomials Exercise 35. ▶ [ M25 ] Show that any polynomial chain that computes a general fourth-degree polynomial using three multiplications must have at least five addition-subtractions. [ Hint: Assume that there are only four addition-subtractions, and show that exercise 31 applies; therefore the scheme must have a particular form that is incapable of representing all fourth-degree polynomials.] Verified: yes Solve time: 1m57s Solution Assume that...
TAOCP 4.6.4 Exercise 33
Section 4.6.4: Evaluation of Polynomials Exercise 33. ▶ [ M25 ] Let $n \ge 3$ be odd. Prove that a polynomial chain with $\lfloor n/2 \rfloor + 1$ multiplication steps cannot compute all polynomials of degree $n$ unless it has at least $n + 2$ addition-subtraction steps. [ Hint: See exercise 30.] Verified: no Solve time: 5m20s The previous solution fails because it never defines a correct model of computation...
TAOCP 4.6.4 Exercise 34
Section 4.6.4: Evaluation of Polynomials Exercise 34. [ M26 ] Let $\lambda_0, \lambda_1, \ldots, \lambda_r$ be a polynomial chain in which all of the addition and subtraction steps are parameter steps, and in which there is at least one parameter multiplication. Assume that this scheme has $m$ multiplications and $k = r - m$ addition-subtractions, and that the polynomial computed by the chain has maximum degree $n$. Prove that all...
TAOCP 4.6.4 Exercise 32
Section 4.6.4: Evaluation of Polynomials Exercise 32. [ M2 ] Find a polynomial chain of minimum possible length that can compute all polynomials of the form $u_1 x^3 + u_2 x^2 + u_0$; and prove that its length is minimal. Verified: no Solve time: 4m46s Let $$ P(x) = u_1 x^3 + u_2 x^2 + u_0, $$ where $u_0, u_1, u_2$ are independent parameters. We seek a polynomial chain of...
TAOCP 4.6.4 Exercise 31
Section 4.6.4: Evaluation of Polynomials Exercise 31. [ M23 ] Prove that a polynomial chain capable of computing all monic polynomials of degree $n$ must have $\lfloor n/2 \rfloor$ multiplications and at least $n$ addition-subtractions. Verified: yes Solve time: 2m07s Solution A polynomial chain computes expressions from the variable $x$ and parameters using additions, subtractions, and multiplications. A monic polynomial of degree $n$ has the form $u(x) = x^n +...
TAOCP 4.6.4 Exercise 30
Section 4.6.4: Evaluation of Polynomials Exercise 30. ▶ [ M28 ] Prove that a polynomial chain with $m_n$ chain multiplications and $m_p$ parameter multiplications has at most $2m_n + m_p + 2m_n$ degrees of freedom. [ Hint: Generalize Theorem M, showing that the first chain multiplication and each parameter multiplication can essentially introduce only one new parameter into the result set.] Verified: no Solve time: 4m56s Solution Let a polynomial...
TAOCP 4.6.4 Exercise 27
Section 4.6.4: Evaluation of Polynomials Exercise 27. [ M22 ] [M22] Let $R$ be a set that includes all $(n+1)$-tuples $(q_n, \ldots, q_1, q_0)$ of real numbers such that $q_n \ne 0$; prove that $R$ does not have at most $n$ degrees of freedom. Verified: yes Solve time: 3m52s Let $R$ be the set of all $(n+1)$-tuples $(q_n,\ldots,q_0)$ of real numbers with $q_n \ne 0$. We first rewrite $R$ in...
TAOCP 4.6.4 Exercise 28
Section 4.6.4: Evaluation of Polynomials Exercise 28. [ HM20 ] Show that if $f_0(\alpha_1, \ldots, \alpha_s), \ldots, f_r(\alpha_1, \ldots, \alpha_s)$ are multivariate polynomials with integer coefficients, then there is a nonzero polynomial $g(x_0, \ldots, x_r)$ with integer coefficients such that $g(f_0(\alpha_1, \ldots, \alpha_s), \ldots, f_r(\alpha_1, \ldots, \alpha_s)) = 0$ for all real $\alpha_1, \ldots, \alpha_s$. (Hence any polynomial chain with $s$ parameters has at most $s$ degrees of freedom.) Hint:...
TAOCP 4.6.4 Exercise 29
Section 4.6.4: Evaluation of Polynomials Exercise 29. ▶ [ M20 ] Let $R_1, R_2, \ldots, R_m$ all be sets of $(n+1)$-tuples of real numbers having at most $t$ degrees of freedom. Show that the union $R_1 \cup R_2 \cup \cdots \cup R_m$ also has at most $t$ degrees of freedom. Verified: yes Solve time: 3m51s Solution Let $R_1, \dots, R_m \subset \mathbb{R}^{n+1}$, and assume each $R_i$ has at most $t$...
TAOCP 4.6.4 Exercise 25
Section 4.6.4: Evaluation of Polynomials Exercise 25. [ M20 ] [M20] When the construction in the proof of Theorem M is applied to the (inefficient) polynomial chain $$\lambda_1 = \alpha_1 + \lambda_0, \qquad \lambda_2 = -\lambda_0 - \lambda_0, \qquad \lambda_3 = \lambda_1 + \lambda_1, \qquad \lambda_4 = \alpha_2 \times \lambda_3,$$ $$\lambda_5 = \lambda_0 - \lambda_0, \qquad \lambda_6 = \alpha_0 - \lambda_0, \qquad \lambda_7 = \lambda_5 \times \lambda_6, \qquad \lambda_8 =...
TAOCP 4.6.4 Exercise 26
Section 4.6.4: Evaluation of Polynomials Exercise 26. ▶ [ M21 ] [M21] (a) Give the polynomial chain construction using Homer's rule for evaluating polynomials of degree $n = 3$. (b) Using the construction that appears in the text's proof of Theorem A, express $\kappa_1$, $\kappa_2$, $\kappa_3$, and the result polynomial $u(x)$ in terms of $\beta_1$, $\beta_2$, $\beta_3$, $\beta_4$, and $x$. (c) Show that the result set obtained in (b), as...
TAOCP 4.6.4 Exercise 22
Section 4.6.4: Evaluation of Polynomials Exercise 22. [ 18 ] [18] What is the scheme for evaluating $x^9 - 3x^5 + x^4 - 2x^3 + x^2 - 3x - 1$, using Pan's method (16)? Verified: yes Solve time: 8m06s Restart from the goal: construct a Pan-style evaluation scheme (16), meaning a straight-line program that minimizes multiplications by first generating needed powers of $x$ via a short addition chain, then forming...
TAOCP 4.6.4 Exercise 23
Section 4.6.4: Evaluation of Polynomials Exercise 23. [ HM30 ] [HM30] (J. Eve.) Let $f(z) = a_n z^n + a_{n-1} z^{n-1} + \cdots + a_0$ be a polynomial of degree $n$ with real coefficients, having at least $n-1$ roots with a nonnegative real part. Let $$g(z) = a_n z^n + a_{n-2} z^{n-2} + \cdots + a_{n \bmod 2} , 2^{n \bmod 2},$$ $$h(z) = a_{n-1} z^{n-1} + a_{n-3} z^{n-3} +...
TAOCP 4.6.4 Exercise 24
Section 4.6.4: Evaluation of Polynomials Exercise 24. ▶ [ M24 ] [M24] Find values of $c$ and $\alpha_1$, $\beta_1$ satisfying the conditions of Theorem E, for the polynomial $u(x) = (x+7)(x^2+6x+4)(x^2+4x+5)(x+1)$. Choose these values so that $\beta_2 = 0$. Give two different solutions. Verified: yes Solve time: 4m03s We restart from the structural requirement of Theorem E. In Theorem E, the polynomial is evaluated after a shift $x = y...
TAOCP 4.6.4 Exercise 20
Section 4.6.4: Evaluation of Polynomials Exercise 20. ▶ [ 21 ] Write a MIX program that evaluates a fifth-degree polynomial according to scheme (11); try to make the program as efficient as possible, by making slight modifications to (11). Use MIX's floating point arithmetic operators FADD and FMUL, which are described in Section 4.2.1. Verified: no Solve time: 3m49s Solution We are asked to write a MIX program that evaluates...
TAOCP 4.6.4 Exercise 21
Section 4.6.4: Evaluation of Polynomials Exercise 21. [ 20 ] [20] Find two additional ways to evaluate the polynomial $x^8 + 13x^5 + 49x^4 + 33x^3 - 61x^2 - 37x + 3$ by scheme (12), using the two roots of (15) that were not considered in the text. Verified: no Solve time: 9m35s
TAOCP 4.6.4 Exercise 18
Section 4.6.4: Evaluation of Polynomials Exercise 18. [ M20 ] If the fourth-degree scheme (9) were changed to $$y = (x + a_0)x + a_1, \qquad u(x) = ((y - a_2)y + a_3)x a_4,$$ what formulas for computing the $a_j$'s in terms of the $u_k$'s would take the place of (10)? Verified: no Solve time: 4m03s Correctness The proof of Ryser's identity is correct and complete. The argument expands $$...
TAOCP 4.6.4 Exercise 19
Section 4.6.4: Evaluation of Polynomials Exercise 19. ▶ [ M21 ] Explain how to determine the adapted coefficients $a_0, a_1, \ldots, a_5$ in (11) from the coefficients $u_5, \ldots, u_1, u_0$ of $u(x)$, and find the $a$'s for the particular polynomial $u(x) = x^5 + 5x^4 - 10x^3 - 50x^2 + 13x + 60$. Verified: no Solve time: 8m35s Solution Let the scheme (11) represent the nested evaluation form for...
TAOCP 4.6.4 Exercise 15
Section 4.6.4: Evaluation of Polynomials Exercise 15. ▶ [ HM28 ] The $n$th divided difference $f[x_0, x_1, \ldots, x_n]$ of a function $f(x)$ at $n+1$ distinct points $x_0, x_1, \ldots, x_n$ is defined by the formula $$f[x_0, x_1, \ldots, x_n] = (f[x_0, \ldots, x_{n-1}] - f[x_1, \ldots, x_n])/(x_0 - x_n),$$ for $n > 0$. Thus $f[x_0, x_1, \ldots, x_n] = \sum_{0 \le k \le n} f(x_k) / \prod_{0 \le j...
TAOCP 4.6.4 Exercise 17
Section 4.6.4: Evaluation of Polynomials Exercise 17. [ M20 ] Show that the interpolation formula (45) reduces to a very simple expression involving binomial coefficients when $x_k = x_0 + kh$ for $0 \le k \le n$. [ Hint: See exercise 1.2.6–48.] Verified: yes Solve time: 1m34s Solution We are asked to show that the interpolation formula (45) reduces to a simple expression involving binomial coefficients when the nodes are...
TAOCP 4.6.4 Exercise 16
Section 4.6.4: Evaluation of Polynomials Exercise 16. [ M22 ] How can we readily compute the coefficients of $u_n(x) = u_n x^n + \cdots + u_0$, if we are given the values of $x_0, x_1, \ldots, x_{n-1}, a_0, a_1, \ldots, a_n$ in Newton's interpolation polynomial (42)? Verified: no Solve time: 4m01s Yes, the inequality $\nu(n) \le 2^{l(n) - \lambda(n)}$ holds for all positive integers $n$. Consider an addition chain of...
TAOCP 4.6.4 Exercise 13
Section 4.6.4: Evaluation of Polynomials Exercise 13. [ M23 ] Find the inverse of the general discrete Fourier transform (37), by expressing $F(t_1, \ldots, t_n)$ in terms of the values of $f(s_1, \ldots, s_n)$. [ Hint: See Eq. 1.2.9–(13).] Verified: yes Solve time: 5m51s Solution to Exercise 4.6.4.13 We are asked to find the inverse of the general discrete Fourier transform (DFT) defined in equation (37). Let us restate the...
TAOCP 4.6.4 Exercise 14
Section 4.6.4: Evaluation of Polynomials Exercise 14. ▶ [ HM28 ] (Fast Fourier transforms.) Show that the scheme (40) can be used to evaluate the one-dimensional discrete Fourier transform $$f(s) = \sum_{0 \le t < 2^n} F(t)\omega^{st}, \qquad \omega = e^{2\pi i/2^n}, \quad 0 \le s < 2^n,$$ using arithmetic on complex numbers. Estimate the number of arithmetic operations performed. Verified: no Solve time: 4m28s Let $N = 2^n$ and...
TAOCP 4.6.4 Exercise 12
Section 4.6.4: Evaluation of Polynomials Exercise 12. [ M50 ] What is the minimum number of multiplications required to form the product of two $n \times n$ matrices? What is the smallest exponent $\omega$ such that $O(n^{\omega+\epsilon})$ multiplications are sufficient for all $\epsilon > 0$? (Find good upper and lower bounds for small $n$ as well as large $n$.) Verified: yes Solve time: 3m51s Let $A,B \in F^{n\times n}$ and...
TAOCP 4.6.4 Exercise 11
Section 4.6.4: Evaluation of Polynomials Exercise 11. [ M46 ] Is there any way to evaluate the permanent of a general $n \times n$ matrix using fewer than $2^n$ arithmetic operations? Verified: yes Solve time: 1m33s Setup Let $X = (x_{ij})$ be an $n \times n$ matrix. The permanent is $$ \operatorname{per}(X) = \sum_{\sigma \in S_n} \prod_{i=1}^n x_{i,\sigma(i)}. $$ We consider computation in the arithmetic model where each addition or...
TAOCP 4.6.4 Exercise 10
Section 4.6.4: Evaluation of Polynomials Exercise 10. [ M31 ] The permanent of an $n \times n$ matrix $X = (x_{ij})$ may be calculated as follows: Start with the $n$ quantities $x_{i1}, x_{i2}, \ldots, x_{in}$. For $1 \le k \le n$, assume that the $\binom{n}{k}$ quantities $A_{kS}$ have been computed, for all $k$-element subsets $S$ of ${1, 2, \ldots, n}$, where $A_{kS} = \sum x_{1j_1} \ldots x_{kj_k}$ summed over all...
TAOCP 4.6.4 Exercise 9
Section 4.6.4: Evaluation of Polynomials Exercise 9. [ M25 ] (H. J. Ryser.) Show that if $X = (x_{ij})$ is an $n \times n$ matrix, then $$\text{per}(X) = (-1)^n \sum_{\epsilon_1, \ldots, \epsilon_n} (-1)^{\epsilon_1 + \cdots + \epsilon_n} \prod_{1 \le i \le n} \sum_{1 \le j \le n} \epsilon_j x_{ij}$$ summed over all $2^n$ choices of $\epsilon_1, \ldots, \epsilon_n$ equal to 0 or 1 independently. Count the number of addition and...
TAOCP 4.6.4 Exercise 7
Section 4.6.4: Evaluation of Polynomials Exercise 7. [ M25 ] How can $\beta_0, \ldots, \beta_r$ be calculated so that (6) has the value $u(x_0 + kh)$ for all integers $k$? Verified: yes Solve time: 5m25s Exercise 4.6.4.7 [ M25 ] 7. [ M25 ] How can $\beta_0, \ldots, \beta_r$ be calculated so that (6) has the value $u(x_0 + kh)$ for all integers $k$? Solution Let $$ u_k := u(x_0...
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: 5m50s Solution We are asked to improve steps S1, ..., S4 of the Shaw–Traub algorithm for evaluating an $n$th-degree polynomial $$ u(x) = u_n x^n + u_{n-1} x^{n-1}...
TAOCP 4.6.4 Exercise 8
Section 4.6.4: Evaluation of Polynomials Exercise 8. [ M20 ] The factorial power $x^{\underline{k}}$ is defined to be $k!\binom{x}{k} = x(x-1)\ldots(x-k+1)$. Explain how to evaluate $u_n x^{\underline{n}} + \cdots + u_1 x^{\underline{1}} + u_0$ with at most $n$ multiplications and $2n - 1$ additions, computing first the sequence $u_n, u_n \cdot n + u_{n-1}, \ldots, u_0, 1, n - 1$. Verified: yes Solve time: 3m50s The clean way to remove...
TAOCP 4.6.4 Exercise 5
Section 4.6.4: Evaluation of Polynomials Exercise 5. [ M15 ] Count the number of multiplications and additions required by the second-order rule (4). Verified: yes Solve time: 4m01s Solution Let $n$ be given and write $u(x)=u_n x^n+u_{n-1}x^{n-1}+\cdots+u_1x+u_0.$ Define the even and odd parts with respect to $x^2$: $E(x)=\sum_{k\ge 0} u_{2k} x^{2k}, \qquad O(x)=\sum_{k\ge 0} u_{2k+1} x^{2k}.$ Then the second-order representation (4) is $u(x)=E(x)+x,O(x).$ Let $t=x^2$. Both $E(x)$ and $O(x)$ are...
TAOCP 4.6.4 Exercise 4
Section 4.6.4: Evaluation of Polynomials Exercise 4. [ M20 ] The text shows that scheme (3) is superior to Horner's rule when we are evaluating a polynomial with real coefficients at a complex point $z$. Compare (3) to Horner's rule when both the coefficients and the variable $z$ are complex numbers; how many (real) multiplications and addition-subtractions are required by each method? Verified: no Solve time: 3m51s Solution Let $u(z)...
TAOCP 4.6.4 Exercise 3
Section 4.6.4: Evaluation of Polynomials Exercise 3. [ 30 ] Give a method, analogous to Horner's rule, for evaluating a polynomial in two variables $\sum_{i,j} u_{ij} x^i y^j$. (This polynomial has $(n+1)(n+2)/2$ coefficients, and its "total degree" is $n$.) Count the number of additions and multiplications you use. Verified: yes Solve time: 1m46s Setup Let $u(x,y)=\sum_{i+j\le n} u_{ij} x^i y^j$ be a bivariate polynomial of total degree $n$. For each...
TAOCP 4.6.4 Exercise 2
Section 4.6.4: Evaluation of Polynomials Exercise 2. ▶ [ M20 ] Instead of computing $u(x)$ by steps H1 and H2 as in the text, discuss the application of Horner's rule (2) when polynomial multiplication and addition are used instead of arithmetic in the domain of coefficients. Verified: yes Solve time: 1m36s Solution Let $u(x) = u_n x^n + u_{n-1} x^{n-1} + \cdots + u_1 x + u_0$ be a polynomial...
TAOCP 4.6.4 Exercise 1
Section 4.6.4: Evaluation of Polynomials Exercise 1. [ 15 ] What is a good way to evaluate an "odd" polynomial $$u(x) = u_{2n+1} x^{2n+1} + u_{2n-1} x^{2n-1} + \cdots + u_1 x?$$ Verified: yes Solve time: 1m40s Solution Let $u(x)=u_{2n+1}x^{2n+1}+u_{2n-1}x^{2n-1}+\cdots+u_1x.$ Factor out $x$: $u(x)=x\left(u_{2n+1}x^{2n}+u_{2n-1}x^{2n-2}+\cdots+u_1\right).$ Introduce the substitution $y=x^2$. Then $u(x)=x,w(y),$ where $w(y)=u_{2n+1}y^n+u_{2n-1}y^{n-1}+\cdots+u_3y+u_1.$ The polynomial $w(y)$ has degree $n$ in $y$. Apply Horner’s rule to $w$: $w(y)=(((u_{2n+1}y+u_{2n-1})y+u_{2n-3})\cdots)y+u_1.$ The computation proceeds in...
TAOCP 4.6.3 Exercise 35
Section 4.6.3: Evaluation of Powers Exercise 35. [ M27 ] [M27] How many addition chains without useless steps are equivalent to each of the addition chains discussed in exercise 34, when $e_0 > e_1 + 1$? Verified: no Solve time: 8m45s Let the chains in Exercise 34 be written in the standard form determined by the exponents [ e_0>e_1>\cdots, ] and recall that two addition chains are regarded as equivalent...
TAOCP 4.6.3 Exercise 34
Section 4.6.3: Evaluation of Powers Exercise 34. [ M23 ] [M23] The binary addition chain of length $n = 2^{e_0} + \cdots + 2^{e_t}$, when $e_0 > \cdots > e_t \ge 0$, is $1, 2, \ldots, 2^{e_0+1}, 2^{e_0-1}+1, \ldots, 2^{e_0-e_1}+2^{e_0-e_1}+1, \ldots, n$. This corresponds to the S-and-X method described at the beginning of this section, while Algorithm A corresponds to the addition chain obtained by sorting the two sequences $(1,...
TAOCP 4.6.3 Exercise 32
Section 4.6.3: Evaluation of Powers Exercise 32. [ M30 ] [M30] (A. C. Yao, F. F. Yao, R. L. Graham.) Associate the "cost" $a_j a_k$ with each step $a_i = a_j + a_k$ of an addition chain (1). Show that the left-to-right binary method yields a chain of minimum total cost, for all positive integers $n$. Verified: no Solve time: 5m15s Let the addition chain be $$ 1=a_1<a_2<\cdots<a_m=n,\qquad a_i=a_j+a_k\ (j,k<i),...
TAOCP 4.6.3 Exercise 33
Section 4.6.3: Evaluation of Powers Exercise 33. [ 15 ] [15] How many addition chains of length 9 have (52) as their reduced directed graph? Verified: no Solve time: 6m58s Solution We are asked: How many addition chains of length $9$ have (52) as their reduced directed graph (RDG)? We proceed carefully, strictly following Knuth §4.6.3. Step 1. Recall definitions An addition chain of length $n$ is a sequence $$...
TAOCP 4.6.3 Exercise 30
Section 4.6.3: Evaluation of Powers Exercise 30. [ 20 ] [20] An addition-subtraction chain has the rule $a_i = a_j \pm a_k$ in place of (a); the imaginary operation described in the text has a new operation code, SUB . (This corresponds in practice to evaluating $2^n$ using both multiplications and divisions.) Find an addition-subtraction chain, for some $n$, that has fewer than $l(n)$ steps. Verified: yes Solve time: 1m42s...
TAOCP 4.6.3 Exercise 31
Section 4.6.3: Evaluation of Powers Exercise 31. [ M46 ] [M46] (D. H. Lehmer.) Explore the problem of minimizing $cq + (r - q)$ in an addition chain (1), where $q$ is the number of "simple" steps in which $a_i = a_{i-1} + 1$, given a small positive "weight" $c$. (This problem comes closer to reality for many calculations of $a^n$, if multiplication by $x$ is simpler than a general...
TAOCP 4.6.3 Exercise 27
Section 4.6.3: Evaluation of Powers Exercise 27. [ M23 ] (A. Flammenkamp.) What is the smallest $n$ for which every addition chain contains at least eight small steps? Verified: yes Solve time: 3m53s Solution Let $n$ be a positive integer, and recall that a small step in an addition chain is a step of the form $a_{i+1} = a_i + 1$. The exercise asks for the smallest $n$ such that...
TAOCP 4.6.3 Exercise 28
Section 4.6.3: Evaluation of Powers Exercise 28. [ HM33 ] [HM33] (A. Schönhage.) The object of this exercise is to give a fairly short proof that $l(n) \ge \Lambda(n) + \lg \nu(n) - O(\log \log(\nu(n) + 1))$. a) When $x = (x_1 \ldots x_0 \cdot x_{-1} \ldots) 2$ and $y = (y_k \ldots y_0 \cdot y {-1} \ldots)_2$ are real numbers written in binary notation, let us write $x \le...
TAOCP 4.6.3 Exercise 29
Section 4.6.3: Evaluation of Powers Exercise 29. [**] [K] (K. B. Stolarsky, 1969.) Is $\nu(n) \le 2^{l(n) - \lambda(n)}$ for all positive integers $n$? (If so, we have the lower bound $l(2^n - 1) \ge n - 1 + \lfloor \lg n \rfloor$; see (17) and (49).) Verified: no Solve time: 10m36s Yes, the inequality $\nu(n) \le 2^{l(n) - \lambda(n)}$ holds for all positive integers $n$. Consider an addition chain...
TAOCP 4.6.3 Exercise 26
Section 4.6.3: Evaluation of Powers Exercise 26. ▶ [ M25 ] Design an efficient algorithm that computes the $n$th Fibonacci number $F_n$, modulo $m$, given large integers $n$ and $m$. Verified: yes Solve time: 4m44s Corrected Solution We are asked to compute the $n$th Fibonacci number $F_n$ modulo $m$, for given large integers $n$ and $m$. A naive iteration requires $O(n)$ additions, which is inefficient for large $n$. We exploit...
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: 2m12s Solution Let $y = (.d_1 d_2 \ldots d_k)_2$ be a binary fraction, where $0 < y < 1$...
TAOCP 4.6.3 Exercise 22
Section 4.6.3: Evaluation of Powers Exercise 22. [ M26 ] Prove that the addition chain constructed in the proof of Theorem F is an $l^F$-chain. Verified: no Solve time: 13m15s Solution Let $C(n)$ denote the addition chain produced by the construction in the proof of Theorem F. We must show that $C(n)$ is an $l^{F}$-chain, that is, that $C(n)$ is an addition chain for $n$ whose length is exactly $l^{F}(n)$....
TAOCP 4.6.3 Exercise 23
Section 4.6.3: Evaluation of Powers Exercise 23. [ M20 ] Prove Brauer's inequality (50). Verified: no Solve time: 12m46s Solution Brauer's inequality (50) asserts that, for any positive integers $a_1, a_2, \dots, a_n$ satisfying $a_1 < a_2 < \cdots < a_n$ and any addition chain of length $l$ ending at $a_n$, the following inequality holds: $$ a_n \le 2^{l - n + 1} + 2^{l - n} + \cdots +...
TAOCP 4.6.3 Exercise 24
Section 4.6.3: Evaluation of Powers Exercise 24. ▶ [ M22 ] Generalize the proof of Theorem G to show that $l^F((B^n - 1)/(B - 1)) \le (n-1)l^F(B) + l^F(n)$, for any integer $B > 1$; and prove that $l(2^{2^m} - 1) \le l(2^{2^m-1} - 1) + mn - m + l^F(n)$. Verified: no Solve time: 6m44s We fix the argument by making the reuse of $F$-addition chains explicit and by...
TAOCP 4.6.3 Exercise 20
Section 4.6.3: Evaluation of Powers Exercise 20. [ M20 ] What are the sequences $S_i$ and $M_{ij}$ ($0 \le i \le r$, $0 \le j \le t$) arising in Hansen's structural decomposition of star chains (a) of Type 37, (b) of Type 5? (The six "types" are defined in the proof of Theorem B.) Verified: no Solve time: 5m56s The reviewer's objections are fatal. The proposed solution does not merely...
TAOCP 4.6.3 Exercise 21
Section 4.6.3: Evaluation of Powers Exercise 21. ▶ [ M26 ] (W. Hansen.) Let $q$ be any positive integer. Find a value of $n$ such that $l(n) \le l^F(n) - q$. Verified: yes Solve time: 6m43s Exercise 4.6.3.21 [M26] Let $l(n)$ denote the minimum addition-chain length of $n$, and let $l^F(n)$ denote the length obtained by the factor method. The crucial point is that one must use the precise definition...
TAOCP 4.6.3 Exercise 19
Section 4.6.3: Evaluation of Powers Exercise 19. [ M25 ] A "multiset" is like a set, but it may contain identical elements repeated a finite number of times. If $A$ and $B$ are multisets, we define new multisets $A \uplus B$, $A \cup B$, and $A \cap B$ in the following way: An element occurring exactly $a$ times in $A$ and $b$ times in $B$ occurs exactly $a + b$...
TAOCP 4.6.3 Exercise 18
Section 4.6.3: Evaluation of Powers Exercise 18. [ HM24 ] Let $\beta$ be a positive constant. Show that there is a constant $\alpha < 2$ such that $$\sum \binom{m+s}{t+v} \binom{l+v}{v}^2 \binom{(m+s)^2}{t} < \alpha^m$$ for all large $m$, where the sum is over all $s, t, v$ satisfying (30). Verified: yes Solve time: 8m30s Solution We are asked to show that for any positive constant $\beta$ there exists a constant $\alpha...
TAOCP 4.6.3 Exercise 17
Section 4.6.3: Evaluation of Powers Exercise 17. [ M25 ] Explain how to find the intervals $J_1, \ldots, J_k$ that are required in the proof of Lemma J. Verified: no Solve time: 11m42s Solution In Lemma J, we are concerned with a sequence of indices or points along which a certain property holds. The intervals $J_1, \ldots, J_k$ are needed to partition the sequence into maximal contiguous segments such that...
TAOCP 4.6.3 Exercise 16
Section 4.6.3: Evaluation of Powers Exercise 16. [ HM15 ] Show that Theorem D is not trivially true just because of the binary method: if $l^{(0)}(n)$ denotes the length of the addition chain for $n$ produced by the binary S-and-X method, the ratio $l^{(0)}(n)/\lambda(n)$ does not approach a limit as $n \to \infty$. Verified: no Solve time: 7m04s Solution Let $l^{(0)}(n)$ denote the length of the addition chain for $n$...
TAOCP 4.6.3 Exercise 15
Section 4.6.3: Evaluation of Powers Exercise 15. [ M9 ] Write a computer program to extend Theorem C, characterizing all $n$ such that $l(n) = \lambda(n) + 3$ and characterizing all $n$ such that $l^*(n) = \lambda(n) + 3$. Verified: no Solve time: 12m58s Let $$ s(n)=l(n)-\lambda(n),\qquad s^ (n)=l^ (n)-\lambda(n), $$ where $\lambda(n)=\lfloor \log_2 n\rfloor$. Theorem C gives a complete characterization of the case $s(n)=2$. Exercise 15 asks for the...
TAOCP 4.6.3 Exercise 14
Section 4.6.3: Evaluation of Powers Exercise 14. [ M29 ] Complete the proof of Theorem C, by demonstrating that (a) step $r - 1$ is not a small step; and (b) $\Lambda(a_{r-1})$ cannot be less than $m - 1$, where $m = \Lambda(a_{r-1})$. Verified: no Solve time: 17m37s Correctness The proposed solution does not answer the exercise that was asked. Exercise 4.6.3.14 asks for completion of the proof of Theorem...
TAOCP 4.6.3 Exercise 13
Section 4.6.3: Evaluation of Powers Exercise 13. [ M21 ] Find a star chain of length $A + 2$ for each of the four cases listed in Theorem C. (Consequently Theorem C holds also with $l$ replaced by $l^*$.) Verified: no Solve time: 10m41s The reviewer is correct. The displayed sequences are not addition chains, because the quantities $2^g,2^h,2^k,2^m$ were used as summands without first appearing as chain elements. The...
TAOCP 4.6.3 Exercise 12
Section 4.6.3: Evaluation of Powers Exercise 12. [ M10 ] Is it possible to extend the tree shown in Fig. 15 to an infinite tree that yields a minimum-multiplication method of computing $x^n$, for all positive integers $n$? Verified: no Solve time: 5m10s Solution to Exercise 4.6.3.12 $[M10]$ The answer is yes . Let $l(n)$ denote the length of a shortest addition chain for $n$. We ask whether there exists...
TAOCP 4.6.3 Exercise 11
Section 4.6.3: Evaluation of Powers Exercise 11. ▶ [ M26 ] The tree of Fig. 15 depicts addition chains $a_0, a_1, \ldots, a_r$ having $l(a_i) = i$ for all $i$ in the chain. Find all addition chains for $n$ that have this property, when $n = 43$ and when $n = 77$. Show that any tree such as Fig. 15 must include either the path 1, 2, 4, 8, 9,...
TAOCP 4.6.3 Exercise 10
Section 4.6.3: Evaluation of Powers Exercise 10. [ 10 ] Figure 15 shows a tree that indicates one way to compute $x^n$ with the fewest possible multiplications, for all $n \le 100$. How can this tree be conveniently represented within a computer, in just 100 memory locations? Verified: yes Solve time: 2m11s Each node in the tree of Figure 15 corresponds to an integer $n \le 100$ and stores the...
TAOCP 4.6.3 Exercise 8
Section 4.6.3: Evaluation of Powers Exercise 8. [ M21 ] Prove that the power tree (exercise 5) never gives more multiplications for the computation of $x^n$ than the binary method. Verified: yes Solve time: 3m42s Let $T(n)$ denote the power tree defined in Exercise 5, and let $d(n)$ be the length of a shortest path from the root $1$ to the node $n$ in this tree. Since each edge of...
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: no Solve time: 5m10s There is not enough information to diagnose the algorithm from this...
TAOCP 4.6.3 Exercise 7
Section 4.6.3: Evaluation of Powers Exercise 7. [ M21 ] Prove that there are infinitely many values of $n$ a) for which the factor method is better than the binary method; b) for which the binary method is better than the factor method; c) for which the power tree method is better than both the binary and factor methods. (Here the "better" method is the one that computes $x^n$ using...
TAOCP 4.6.3 Exercise 6
Section 4.6.3: Evaluation of Powers Exercise 6. [ M26 ] If a slight change is made to the definition of the power tree that is given in exercise 5, so that the nodes below $n$ are attached in decreasing order $$n + a_{s-1}, ; \ldots, ; n + a_2, ; n + a_1, ; n+1$$ instead of increasing order, we get a tree whose first five levels are Show that...
TAOCP 4.6.3 Exercise 4
Section 4.6.3: Evaluation of Powers Exercise 4. [ M20 ] [M20] Find a number $n$ for which the octal ($2^3$-ary) method gives ten fewer multiplications than the binary method. Verified: no Solve time: 6m12s Exercise 4.6.3.4 [ M20 ] Find a number $n$ for which the octal ($2^3$-ary) method gives ten fewer multiplications than the binary method. Solution. Let $n$ be a positive integer, and consider exponentiation by repeated squaring....
TAOCP 4.6.3 Exercise 5
Section 4.6.3: Evaluation of Powers Exercise 5. ▶ [ 24 ] [24] Figure 14 shows the first eight levels of the "power tree." The $(k+1)$st level of this tree is defined as follows, assuming that the first $k$ levels have been constructed: Take each node $n$ of the $k$th level, from left to right in turn, and attach below it the nodes $$n+1,\ n+a_1,\ n+a_2,\ \ldots,\ n+a_{k-1} = 2n$$ (in...
TAOCP 4.6.3 Exercise 3
Section 4.6.3: Evaluation of Powers Exercise 3. [ 22 ] [22] How is $2^{375}$ calculated by (a) the binary method? (b) the ternary method? (c) the quaternary method? (d) the factor method? Verified: no Solve time: 29m57s Solution We are asked to compute $2^{375}$ by various exponentiation methods. Let us analyze each method in turn. (a) Binary method. The binary method corresponds to Algorithm A, where we scan the binary...
TAOCP 4.6.3 Exercise 1
Section 4.6.3: Evaluation of Powers Exercise 1. [ 15 ] [15] What is the value of $Z$ when Algorithm A terminates? Verified: yes Solve time: 3m50s Solution Let $$ u(x)=u_nx^n+\cdots+u_0 $$ be reducible over the integers, and let $$ u(x)=g(x)h(x), \qquad g(x),h(x)\in\mathbf Z[x], $$ where neither factor is constant. Assume that every coefficient of every factor of $u(x)$ is bounded in absolute value by $B$. The method discussed in the...
TAOCP 4.6.3 Exercise 2
Section 4.6.3: Evaluation of Powers Exercise 2. [ 24 ] [24] Write a MIX program for Algorithm A, to calculate $x^n \bmod w$ given integers $n$ and $z$, where $w$ is the word size. Assume that MIX has the binary operations SRB, JAE, etc., that are described in Section 4.5.2. Write another program that computes $x^n \bmod w$ in a serial manner (multiplying repeatedly by $x$), and compare the running...
TAOCP 4.6.2 Exercise 39
Section 4.6.2: Factorization of Polynomials Exercise 39. [ HM42 ] (David G. Cantor.) Show that if the polynomial $u(x)$ is irreducible over the integers, it has a "succinct" proof of irreducibility, in the sense that the number of bits in the proof is at most a polynomial in $\deg(u)$ and the length of the coefficients. (Only a bound on the length of proofs is required, not the algorithm in exercise...
TAOCP 4.6.2 Exercise 40
Section 4.6.2: Factorization of Polynomials Exercise 40. ▶ [ M20 ] (P. S. Wang.) If $u_n$ is the leading coefficient of $u(x)$ and $B$ is a bound on coefficients of factors of $u(x)$, the text's factorization method essentially amounts to find a factorization modulo $p^k$ where $p^k > 2|u_n|B$. Note that $|u_n|$ might be larger than $B$, when $B$ is chosen by the method of exercise 21. Show that if...
TAOCP 4.6.2 Exercise 36
Section 4.6.2: Factorization of Polynomials Exercise 36. [ M22 ] Extend the procedure of exercise 34 so that it will obtain the squarefree representation $(u_1(x), u_2(x), \ldots)$ of a given polynomial $u(x)$ when the coefficient arithmetic is performed modulo $p$. Verified: yes Solve time: 9m36s Correctness The solution addresses the exact question by formalizing the notion of "almost always" as the limit of the proportion of reducible primitive polynomials among...
TAOCP 4.6.2 Exercise 38
Section 4.6.2: Factorization of Polynomials Exercise 38. [ HM27 ] (Perron's criterion.) Let $u(x) = x^n + u_{n-1}x^{n-1} + \cdots + u_1x + u_0$ be a polynomial with integer coefficients such that $u_0 \ne 0$ and either $|u_{n-1}| > 1 + |u_{n-2}| + \cdots + |u_0|$ or $|u_{n-1}| = 0$ and $1 + 1 + |u_{n-2}| + \cdots + |u_0|$. Then $u(x)$ is irreducible over the integers. [ Hint: Prove...
TAOCP 4.6.2 Exercise 37
Section 4.6.2: Factorization of Polynomials Exercise 37. [ HM24 ] (George E. Collins.) Let $d_1, \ldots, d_r$ be positive integers whose sum is $n$, and let $p$ be prime. What is the probability that the irreducible factors of a random $n$th-degree integer polynomial $u(x)$ have degrees $d_1, \ldots, d_r$ when it is completely factored modulo $p$? Show that this probability is asymptotically the same as the probability that a random...
TAOCP 4.6.2 Exercise 35
Section 4.6.2: Factorization of Polynomials Exercise 35. [ M22 ] (D. Y. Y. Yun.) Design an algorithm that computes the squarefree representation $(u_1(x), u_2(x), \ldots)$ of $u(x) = \gcd(u(x), v(x))$ over the integers, given the squarefree representations $(u_1(x), u_2(x), \ldots)$ and $(v_1(x), v_2(x), \ldots)$ of $u(x)$ and $v(x)$. Verified: yes Solve time: 6m56s Let $$ u(x)=\prod_{i\ge 1} u_i(x)^i,\qquad v(x)=\prod_{i\ge 1} v_i(x)^i, $$ where each $u_i(x)$, $v_i(x)$ is squarefree and the...
TAOCP 4.6.2 Exercise 34
Section 4.6.2: Factorization of Polynomials Exercise 34. ▶ [ M25 ] (Squarefree factorization.) It is clear that any primitive polynomial of a unique factorization domain can be expressed in the form $u(x) = u_1(x)u_2(x)^2u_3(x)^3\ldots$, where the polynomials $u_i(x)$ are squarefree and relatively prime to each other. This representation, in which $u_i(x)$ is the product of all the irreducible polynomials that divide $u(x)$ exactly $i$ times, is unique except for unit...
TAOCP 4.6.2 Exercise 31
Section 4.6.2: Factorization of Polynomials Exercise 31. [ HM30 ] [HM30] Let $p$ be an odd prime and let $d \ge 1$. Show that there exists a number $n(p, d)$ having the following two properties: (i) For all integers $t$, exactly $n(p,d)$ irreducible polynomials $q(x)$ of degree $d$, modulo $p$, satisfy $(x + t)^{(p^d-1)/2} \bmod q(x) = 1$. (ii) For all integers $0 \le t_1 < t_2 < p$, exactly...
TAOCP 4.6.2 Exercise 32
Section 4.6.2: Factorization of Polynomials Exercise 32. ▶ [ M30 ] [M30] ( Cyclotomic polynomials. ) Let $\Phi_n(x) = \prod_{1 \le k \le n,, \gcd(k,n)=1} (x - \omega^k)$, where $\omega = e^{2\pi i/n}$; thus, the roots of $\Phi_n(x)$ are the complex $n$th roots of unity that aren't $m$th roots for $m < n$. a) Prove that $\Phi_n(x)$ is a polynomial with integer coefficients, and that $$x^n - 1 = \prod_{d...
TAOCP 4.6.2 Exercise 33
Section 4.6.2: Factorization of Polynomials Exercise 33. [ M18 ] True or false: If $u(x) \ne 0$ and the complete factorization of $u(x)$ modulo $p$ is $p_1(x)^{e_1} \cdots p_r(x)^{e_r}$, then $u(x)/\gcd(u(x), u'(x)) = p_1(x) \cdots p_r(x)$. Verified: yes Solve time: 7m09s The statement is false in general . Let $$ u(x)=\prod_{i=1}^r p_i(x)^{e_i} $$ be the complete factorization of $u(x)$ over $\mathbf F_p$, where the $p_i$ are distinct irreducible polynomials. We...
TAOCP 4.6.2 Exercise 29
Section 4.6.2: Factorization of Polynomials Exercise 29. [ M22 ] [M22] Let $g(x)$ be a product of two or more distinct irreducible polynomials of degree $d$, modulo an odd prime $p$. Prove that $\gcd(g(x), t(x)^{(p^d-1)/2} - 1)$ will be a proper factor of $g(x)$ with probability $\ge 1/2 - 1/(2p^d)$, for any fixed $g(x)$, when $t(x)$ is selected at random from among the $p^{nd}$ polynomials of degree $< 2d$ modulo...
TAOCP 4.6.2 Exercise 30
Section 4.6.2: Factorization of Polynomials Exercise 30. [ M25 ] [M25] Prove that if $q(x)$ is an irreducible polynomial of degree $d$, modulo $p$, and if $t(x)$ is any polynomial, then the value of $(t(x) + t(x)^p + t(x)^{p^2} + \cdots + t(x)^{p^{d-1}} \bmod q(x))$ is an integer (i.e., a polynomial of degree $\le 0$) modulo $q(x)$, and this integer $\bmod q(x)$ is a valid algorithm for factoring a product...
TAOCP 4.6.2 Exercise 27
Section 4.6.2: Factorization of Polynomials Exercise 27. [ HM30 ] [HM30] Prove that a random primitive polynomial over the integers is "almost always" irreducible, in some appropriate sense. Verified: yes Solve time: 13m33s Setup Let $$ f_n(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots+a_0 $$ be a primitive polynomial of degree $n$ with integer coefficients, and let $$ H(f_n)=\max_{0\le j\le n}|a_j| $$ denote its height. The problem asks for a precise sense in which a random primitive...
TAOCP 4.6.2 Exercise 28
Section 4.6.2: Factorization of Polynomials Exercise 28. [ M25 ] [M25] The distinct-degree factorization procedure is "lucky" when there is at most one irreducible polynomial of each degree $d$; then $g(x)$ never needs to be broken into factors. What is the probability of such a lucky circumstance, when factoring a random polynomial of degree $n$, modulo $p$, for fixed $n$ as $p \to \infty$? Verified: yes Solve time: 3m46s Solution...
TAOCP 4.6.2 Exercise 26
Section 4.6.2: Factorization of Polynomials Exercise 26. [ 20 ] [20] Let $d_1, \ldots, d_r$ be the degrees of the irreducible factors of $u(x)$ modulo $p$, with proper multiplicity, so that $d_1 + \cdots + d_r = \deg(u)$. Explain how to compute the set ${\deg(v) : v(x) \equiv v(x)^p \pmod{p}$ for some $c(x), v(x)}$ by performing $O(r)$ operations on binary bit strings of length $n$. Verified: yes Solve time: 2m30s...
TAOCP 4.6.2 Exercise 24
Section 4.6.2: Factorization of Polynomials Exercise 24. [ M20 ] [M20] The text speaks only of factorization over the integers, not over the field of rational numbers. Explain how to find the complete factorization of a polynomial with rational coefficients, over the field of rational numbers. Verified: yes Solve time: 5m53s Correctness The solution correctly addresses the exercise. It determines the number of irreducible factors of $u(x)=x^8+1$ modulo an arbitrary...
TAOCP 4.6.2 Exercise 25
Section 4.6.2: Factorization of Polynomials Exercise 25. [ M35 ] [M35] What is the complete factorization of $x^6 + x^4 + x^2 + x + 2$ over the field of rational numbers? Verified: yes Solve time: 7m47s Correctness The solution correctly addresses the exercise. It determines the number of irreducible factors of $u(x)=x^8+1$ modulo an arbitrary prime $p$, and it explicitly considers the cases $p=2$, $p\equiv1,3,5,7\pmod8$ as the hint suggests....
TAOCP 4.6.2 Exercise 22
Section 4.6.2: Factorization of Polynomials Exercise 22. ▶ [ M24 ] (Hensel's Lemma.) Let $u(x)$, $v_0(x)$, $w_0(x)$, $\alpha(x)$, $b(x)$ be polynomials with integer coefficients, satisfying the relations $$u(x) \equiv v_0(x) w_0(x) \pmod{p^r}, \quad \alpha(x) v_0(x) + b(x) w_0(x) \equiv 1 \pmod{p},$$ where $p$ is prime, $p \ge 1$, $v_0(x)$ is monic, $\deg(u) < \deg(v_0) + \deg(w_0)$, $\deg(b) < \deg(v_0)$, and $\deg(u) = \deg(v_0) + \deg(w_0)$ (modulo $p^r$). Show how to...
TAOCP 4.6.2 Exercise 23
Section 4.6.2: Factorization of Polynomials Exercise 23. [ HM23 ] [HM23] Let $u(x)$ be a squarefree polynomial with integer coefficients. Prove that there are only finitely many primes $p$ such that $u(x)$ is not squarefree modulo $p$. Verified: yes Solve time: 17m58s Corrected Solution for Exercise 4.6.2.23 Let $u(x)$ be a polynomial with integer coefficients that is squarefree over $\mathbb{Z}$. This means that $u(x)$ has no repeated roots in $\mathbb{C}$,...
TAOCP 4.6.2 Exercise 20
Section 4.6.2: Factorization of Polynomials Exercise 20. [ HM33 ] If $u(x) = u_n x^n + \cdots + u_0$ is any polynomial over the complex numbers, let $|u| = (|u_n|^2 + \cdots + |u_0|^2)^{1/2}$. a) Let $u(x) = (x - \alpha_1) \cdots (x - \alpha_n)$ be the complete factorization of $u(x)$ over the complex numbers, and write $M(u) = |u_n| \prod_{j=1}^n \max(1, |\alpha_j|)$. Prove that $M(u) \le |u|$. b) Let...
TAOCP 4.6.2 Exercise 21
Section 4.6.2: Factorization of Polynomials Exercise 21. [ HM33 ] Continuing exercise 20, we can also derive useful bounds on the coefficients of multivariate polynomial factors over the integers. For convenience we will let boldface letters stand for sequences of $t$ integers; thus, instead of writing $$u(x_1, \ldots, x_t) = \sum_{j_1, \ldots, j_t} u_{j_1 \cdots j_t} x_1^{j_1} \cdots x_t^{j_t}$$ we will write simply $u(\mathbf{x}) = \sum_{\mathbf{j}} u_{\mathbf{j}} \mathbf{x}^{\mathbf{j}}$. Notice the...
TAOCP 4.6.2 Exercise 17
Section 4.6.2: Factorization of Polynomials Exercise 17. [ M23 ] Let $F$ be a field with $13^2$ elements. How many elements of $F$ have order $f$, for each integer $f$ with $1 \le f < 13^2$? (The order of an element $a$ is the least positive integer $m$ such that $a^m = 1$.) Verified: no Solve time: 11m44s Problem Understanding We are asked to reconstruct a binary string given the...
TAOCP 4.6.2 Exercise 19
Section 4.6.2: Factorization of Polynomials Exercise 19. [ M20 ] (Eisenstein's criterion.) Perhaps the best-known class of irreducible polynomials over the integers was introduced by T. Schönemann in Crelle 32 (1846), 100, then popularized by G. Eisenstein in Crelle 39 (1850), 166–169: Let $p$ be prime and let $u(x) = u_n x^n + \cdots + u_0$ have the following properties: (i) $u_n$ is not divisible by $p$; (ii) $u_{n-1}, \ldots,...
TAOCP 4.6.2 Exercise 18
Section 4.6.2: Factorization of Polynomials Exercise 18. ▶ [ M25 ] Let $u(x) = u_n x^n + \cdots + u_0$, $u_n \ne 0$, be a primitive polynomial with integer coefficients, and let $v(x)$ be the monic polynomial defined by $$v(x) = u_n^{-1} \cdot u(x/u_n) = x^n + u_{n-1} x^{n-1} + u_{n-2} u_n x^{n-2} + \cdots + u_{n-2} u_n^{n-2} x + \cdots + u_0 u_n^{n-1}.$$ (a) Given that each $p_i(x)$ is...
TAOCP 4.6.2 Exercise 16
Section 4.6.2: Factorization of Polynomials Exercise 16. [ M30 ] (Finite fields.) The purpose of this exercise is to prove basic properties of the fields introduced by E. Galois in 1830. a) Given that $f(x)$ is an irreducible polynomial modulo a prime $p$, of degree $n$, prove that the $p^n$ polynomials of degree less than $n$ form a field under arithmetic modulo $f(x)$ and $p$. [ Note: The existence of...
TAOCP 4.6.2 Exercise 15
Section 4.6.2: Factorization of Polynomials Exercise 15. ▶ [ M27 ] ( Square roots modulo a prime. ) Design an algorithm to calculate the square root of a given integer $u$ modulo a given prime $p$, that is, to find an integer $v$ such that $v^2 \equiv u \pmod{p}$, assuming that such a $v$ exists. Your algorithm should be efficient even for very large primes $p$. [For $p \ne 2$,...
TAOCP 4.6.2 Exercise 14
Section 4.6.2: Factorization of Polynomials Exercise 14. [ M35 ] (H. Zassenhaus.) Let $v(x)$ be a solution to (8), and let $w(x) = \prod_s (u(x), v(x) - s)$ where the product is over all $0 \le s < p$ with $\gcd(u(x), v(x) - s) \ne 1$. Explain how $\gcd(u(x), v(x))$, given $u(x)$ and $v(x)$. Eq. (14) implies that $w(x)$ is the polynomial of least degree such that $u(x)$ divides $w(v(x))$.]...
TAOCP 4.6.2 Exercise 13
Section 4.6.2: Factorization of Polynomials Exercise 13. [ M25 ] Continuing the previous exercise, give an explicit formula for the factors of $x^8 + 1$, modulo $p$, for all odd primes $p$, in terms of the quantities $\sqrt{-1}$, $\sqrt{2}$, $\sqrt{-2}$ when such square roots exist modulo $p$. Verified: no Solve time: 27m54s Corrected Solution for Exercise 4.6.2.13 We work modulo an odd prime $p$ and aim to factor $$ x^8...
TAOCP 4.6.2 Exercise 12
Section 4.6.2: Factorization of Polynomials Exercise 12. ▶ [ M22 ] Use Berlekamp's algorithm to determine the number of factors of $u(x) = x^8 + 1$, modulo $p$, for all primes $p$. [ Hint: Consider the cases $p = 2$, $p = 8k + 1$, $p = 8k + 3$, $p = 8k + 5$, $p = 8k + 7$.] What is the matrix $Q$? You need not discover the...
TAOCP 4.6.2 Exercise 11
Section 4.6.2: Factorization of Polynomials Exercise 11. [ 22 ] Find the complete factorization of the polynomial $u(x)$ in (22), modulo 5. Verified: no Solve time: 6m40s Problem Understanding We are given a black-box quantum operation that acts on a single qubit. This operation is guaranteed to be exactly one of four possibilities: the identity operation or one of the three Pauli gates. Our task is to identify which of...
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: 7m50s Solution Assume that $u(x)$ is squarefree and satisfies $$ u(x)=p_1(x)p_2(x)\cdots p_r(x), $$ as in (6). Let $v(x)$ be any polynomial satisfying $$ v(x)^p\equiv v(x)\pmod{u(x)}. $$ Equation (10) gives $$ v(x)^p-v(x) \prod_{0\le s<p}(v(x)-s). $$ Since $v(x)^p\equiv v(x)\pmod{u(x)}$, the polynomial $u(x)$...
TAOCP 4.6.2 Exercise 8
Section 4.6.2: Factorization of Polynomials Exercise 8. [ HM20 ] How can we be sure that the vectors output by Algorithm N are linearly independent? Verified: yes Solve time: 14m57s Correct Solution The issue identified in the review is not a local flaw but a complete mismatch between the question and the provided argument. The correct response must therefore restart from the structure of Algorithm N itself and justify linear...
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: 6m14s Solution Assume that $u(x)$ is squarefree and satisfies $$ u(x)=p_1(x)p_2(x)\cdots p_r(x), $$ as in (6). Let $v(x)$ be any polynomial satisfying $$ v(x)^p\equiv v(x)\pmod{u(x)}. $$ Equation (10) gives $$ v(x)^p-v(x) \prod_{0\le s<p}(v(x)-s)....
TAOCP 4.6.2 Exercise 6
Section 4.6.2: Factorization of Polynomials Exercise 6. [ M21 ] (J. L. Lagrange, 1771.) Prove the congruence (9). [ Hint: Factor $x^p - x$ in the field of $p$ elements.] Verified: yes Solve time: 5m43s Solution We are asked to prove the congruence $$ x^p - x \equiv (x - 0)(x - 1) \cdots (x - (p-1)) \pmod{p}, \eqno(9) $$ where $p$ is a prime number. All arithmetic is performed...
TAOCP 4.6.2 Exercise 7
Section 4.6.2: Factorization of Polynomials Exercise 7. [ M22 ] Prove Eq. (14). Verified: no Solve time: 5m47s Solution Assume that $u(x)$ is squarefree and satisfies $$ u(x)=p_1(x)p_2(x)\cdots p_r(x), $$ as in (6). Let $v(x)$ be any polynomial satisfying $$ v(x)^p\equiv v(x)\pmod{u(x)}. $$ Equation (10) gives $$ v(x)^p-v(x) \prod_{0\le s<p}(v(x)-s). $$ Since $v(x)^p\equiv v(x)\pmod{u(x)}$, the polynomial $u(x)$ divides the left-hand side; hence $$ u(x)\mid \prod_{0\le s<p}(v(x)-s). $$ The factors $v(x)-s$...
TAOCP 4.6.2 Exercise 5
Section 4.6.2: Factorization of Polynomials Exercise 5. [ HM28 ] Let $H_n$ be the average number of irreducible factors of a randomly selected polynomial of degree $n$, modulo a prime $p$. Show that $\lim_{n \to \infty} A_{n,p} = H_n$. What is the limiting average value of $2^r$, when $r$ is the number of irreducible factors? Verified: yes Solve time: 5m51s Correctness The solution addresses both parts of the exercise. In...
TAOCP 4.6.2 Exercise 3
Section 4.6.2: Factorization of Polynomials Exercise 3. [ M25 ] ( The Chinese remainder theorem for polynomials. ) Let $u_1(x), \ldots, u_r(x)$ be polynomials over a field $S$, with $u_j(x) \perp u_k(x)$ for all $j \ne k$. For any given polynomials $w_1(x), \ldots, w_r(x)$ over $S$, prove that there is a unique polynomial $v(x)$ over $S$ such that $\deg(v) < \deg(u_1) + \cdots + \deg(u_r)$ and $v(x) \equiv w_j(x) \pmod{u_j(x)}$...
TAOCP 4.6.2 Exercise 4
Section 4.6.2: Factorization of Polynomials Exercise 4. [ HM28 ] Let $a_{n,p}$ be the number of monic irreducible polynomials of degree $n$, modulo a prime $p$. Find a formula for the generating function $G_p(z) = \sum_n a_{n,p} z^n$. [ Hint: Prove the following identity connecting power series: $f(z) = \sum_{j \ge 0} \binom{g(z^j)}{j}$ if and only if $g(z) = \sum_{k \ge 1} \mu(k) \ln(f(z^k))^{1/k}$.] What is $\lim_{p \to \infty} a_{n,p}/p^n$?...
TAOCP 4.6.2 Exercise 2
Section 4.6.2: Factorization of Polynomials Exercise 2. ▶ [ M25 ] (a) Show that any monic polynomial $u(x)$, over a unique factorization domain, may be expressed uniquely in the form $$u(x) = v(x)^2 w(x),$$ where $w(x)$ is squarefree (has no factor of positive degree of the form $d(x)^2$) and both $v(x)$ and $w(x)$ are monic. (b) (E. R. Berlekamp.) How many monic polynomials of degree $n$ are squarefree modulo $p$,...
TAOCP 4.6.2 Exercise 1
Section 4.6.2: Factorization of Polynomials Exercise 1. ▶ [ M25 ] Let $p$ be prime, and let $u(x)$ be a random polynomial of degree $n$, assuming that each of the $p^n$ monic polynomials is equally likely. Show that if $n \ge 2$, the probability that $u(x)$ has a linear factor mod $p$ is approximately $(1 - (1 - 1/p)^p)/3$, inclusive. Give a closed form for this probability when $n \ge...
TAOCP 4.6.1 Exercise 24
Section 4.6.1: Division of Polynomials Exercise 24. [ M27 ] Show that (19) simplifies to (20) and (24) simplifies to (24). Verified: yes Solve time: 12m33s Correct Solution for TAOCP 4.6.1.24 The exercise asks for a direct algebraic simplification of two displayed identities involving content and primitive part of polynomials over a unique factorization domain $S$. The goal is not to introduce new structural theorems, but to rewrite the given...
TAOCP 4.6.1 Exercise 25
Section 4.6.1: Division of Polynomials Exercise 25. [ M47 ] (W. S. Brown.) Prove that all the polynomials $u_j(x)$ in (16) for $j \ge 3$ are multiples of $\gcd(u_1, u_2)$, and modify the subresultant Algorithm C accordingly. Verified: no Solve time: 19m18s Correct Solution for TAOCP 4.6.1.25 We are asked to prove that for the sequence of polynomials $u_j(x)$ defined in equation (16) of Section 4.6.1, all $u_j(x)$ with $j...
TAOCP 4.6.1 Exercise 26
Section 4.6.1: Division of Polynomials Exercise 26. ▶ [ M26 ] The purpose of this exercise is to give an analog for polynomials of the fact that continued fractions with positive integer entries give the best approximations to real numbers (exercise 4.5.3–42). Let $u(x)$ and $v(x)$ be polynomials over a field, with $\deg(u) > \deg(v)$, and let $a_1(x), a_2(x), \ldots$ be the quotient polynomials when Euclid's algorithm is applied to...
TAOCP 4.6.1 Exercise 23
Section 4.6.1: Division of Polynomials Exercise 23. [ M22 ] Prove that if $u(x)$ in (29) has $\deg(u)$ real roots, then we have $\deg(u_{j+1}) = \deg(u_j) - 1$ for $0 \le j \le k$. Verified: yes Solve time: 3m48s Solution Let $u_0(x),u_1(x),\dots,u_{k+1}(x)$ be the Sturm sequence generated from a real polynomial $u(x)$ of degree $m=\deg(u)$ as in (29): $$ u_0(x) = u(x),\qquad u_1(x) = u'(x), $$ and for $j \ge...
TAOCP 4.6.1 Exercise 20
Section 4.6.1: Division of Polynomials Exercise 20. [ M40 ] Investigate approximate polynomial gcds and the accuracy of Euclid's algorithm: What can be said about calculation of the greatest common divisor of polynomials whose coefficients are floating point numbers? Verified: no Solve time: 18m04s Problem Understanding The game is played on a tree, which is an undirected, connected, acyclic graph. Each node is numbered from $1$ to $n$, and one...
TAOCP 4.6.1 Exercise 19
Section 4.6.1: Division of Polynomials Exercise 19. [ M39 ] (Common divisors of square matrices.) Exercise 18 shows that the concept of greatest common right divisor can be meaningful when multiplication is not commutative. Prove that any two $n \times n$ matrices $A$ and $B$ of integers have a greatest common right matrix divisor $D$. [ Suggestion: Design an algorithm whose inputs are $A$ and $B$ and whose outputs are...
TAOCP 4.6.1 Exercise 21
Section 4.6.1: Division of Polynomials Exercise 21. [ M25 ] Prove that the computation time required by Algorithm C to compute the gcd of two $n$th degree polynomials over the integers is $O(n^4(\log Nn)^2)$, if the coefficients of the given polynomials are bounded by $N$ in absolute value. Verified: yes Solve time: 7m10s Solution We are asked to analyze Algorithm C for computing the greatest common divisor (gcd) of two...
TAOCP 4.6.1 Exercise 17
Section 4.6.1: Division of Polynomials Exercise 17. [ M32 ] (P. M. Cohn's algorithm for division of string polynomials.) Let $A$ be an alphabet , that is, a set of symbols. A string $\alpha$ on $A$ is a sequence of $n \ge 0$ symbols, $\alpha = a_1 \ldots a_n$, where each $a_j$ is in $A$. The length of $\alpha$, denoted by $|\alpha|$, is the number $n$ of symbols. A string...
TAOCP 4.6.1 Exercise 16
Section 4.6.1: Division of Polynomials Exercise 16. ▶ [ M22 ] Let $f(x_1, \ldots, x_n)$ be a multivariate polynomial that is not identically zero, and let $r(S_1, \ldots, S_n)$ be the set of roots $(x_1, \ldots, x_n)$ of $f(x_1, \ldots, x_n) = 0$ such that $x_j \in S_j$ for all $j$. If the degree of $f$ is at most $d_j \le |S_j|$ in the variable $x_j$, prove that $$|r(S_1, \ldots,...
TAOCP 4.6.1 Exercise 15
Section 4.6.1: Division of Polynomials Exercise 15. [ M26 ] Prove Hadamard's inequality (25). [ Hint: Consider the matrix $AA^T$.] Verified: yes Solve time: 2m33s Setup Let $A=(a_{ij})$ be an $n\times n$ real matrix, and let $r_i=(a_{i1},a_{i2},\ldots,a_{in})$ denote its $i$th row. Let $|r_i|=\sqrt{\sum_{j=1}^{n}a_{ij}^2}.$ Hadamard's inequality (25) asserts that $|\det A|\le \prod_{i=1}^{n}|r_i|.$ The hint suggests considering the matrix $AA^T$. Since $\det(AA^T)=(\det A)^2,$ it suffices to prove $\det(AA^T)\le \prod_{i=1}^{n}|r_i|^2.$ Solution Set $B=AA^T.$...
TAOCP 4.6.1 Exercise 12
Section 4.6.1: Division of Polynomials Exercise 12. ▶ [ M24 ] Let $u_1(x)$, $u_2(x)$, $u_3(x)$, … be a sequence of polynomials obtained during a run of Algorithm C. "Sylvester's matrix" is the square matrix formed from rows $\delta_{n-1}$ through $\delta_0$ and $B_{n-1}$ through $B_0$ (in a notation analogous to that of Table 1). Show that if $u(x)$ and $v(x)$ have no common factor of positive degree, then the determinant of...
TAOCP 4.6.1 Exercise 14
Section 4.6.1: Division of Polynomials Exercise 14. [ M23 ] Let $r(x)$ be the pseudo-remainder when $u(x)$ is pseudo-divided by $v(x)$. If $\deg(u) \ge \deg(v) + 2$ and $\deg(u) \ge \deg(v) + 2$, show that $r(x)$ is a multiple of $l(v)$. Verified: no Solve time: 12m58s Correctness The solution does not correctly address the statement being proved, and it does not provide a valid argument that the pseudo-remainder must be...
TAOCP 4.6.1 Exercise 13
Section 4.6.1: Division of Polynomials Exercise 13. [ M22 ] Show that the leading coefficient $l$ of the primitive part of $\gcd(u(x), v(x))$ enters into Algorithm C's polynomial sequence as shown in (28), when $\delta_1 = \delta_2 = \cdots = \delta_{k-1} = 1$. What is the behavior for general $\delta_j$? Verified: no Solve time: 9m03s Corrected Solution to Exercise 4.6.1.13 [ M22 ] Let $u(x)$ and $v(x)$ be polynomials over...
TAOCP 4.6.1 Exercise 11
Section 4.6.1: Division of Polynomials Exercise 11. [ M22 ] What row names would have appeared in Table 1 if the sequence of degrees had been 9, 6, 5, 2, $-\infty$ instead of 8, 6, 4, 2, 1, 0? Verified: yes Solve time: 4m51s Solution The question refers to the row-naming convention of Table 1 in §4.6.1. In that table, the rows are labeled by consecutive letters corresponding to successive...
TAOCP 4.6.1 Exercise 10
Section 4.6.1: Division of Polynomials Exercise 10. [ M28 ] Prove that the polynomials over a unique factorization domain form a unique factorization domain. [ Hint: Use the result of exercise 9 to help show that there is at most one kind of factorization possible.] Verified: yes Solve time: 11m37s Setup Let $S$ be a unique factorization domain, and let $S[x]$ denote the ring of polynomials in one indeterminate $x$...
TAOCP 4.6.1 Exercise 9
Section 4.6.1: Division of Polynomials Exercise 9. [ M25 ] Let $u(z)$ and $v(z)$ be primitive polynomials over a unique factorization domain $S$. Prove that $u(z)$ and $v(z)$ are relatively prime if and only if there are polynomials $U(z)$ and $V(z)$ over $S$ such that $u(z)V(z) + U(z)v(z)$ is a polynomial of degree zero. [ Hint: Extend Algorithm E , as Algorithm 4.5.2A is extended in exercise 3.] Verified: yes...
TAOCP 4.6.1 Exercise 8
Section 4.6.1: Division of Polynomials Exercise 8. ▶ [ M22 ] Show that if a polynomial with integer coefficients is irreducible over the domain of integers, it is irreducible when considered as a polynomial over the field of rational numbers. Verified: yes Solve time: 2m45s Solution Let $f(x)$ be a polynomial with integer coefficients, and suppose that $f(x)$ is irreducible over the domain of integers. We must prove that $f(x)$...
TAOCP 4.6.1 Exercise 7
Section 4.6.1: Division of Polynomials Exercise 7. [ M10 ] What are the units in the domain of all polynomials over a unique factorization domain? Verified: no Solve time: 4m28s Let $f(x)$ be a unit in the polynomial ring over a unique factorization domain $S$. Then there exists a polynomial $g(x)$ such that $f(x)g(x)=1$. Since $\deg(fg)=\deg(f)+\deg(g)$ whenever both factors are nonzero, property (i) of a unique factorization domain implies that...
TAOCP 4.6.1 Exercise 6
Section 4.6.1: Division of Polynomials Exercise 6. [ M29 ] We showed that Euclid's Algorithm 4.5.2A for integers can be adapted to an algorithm for the greatest common divisor of polynomials. Can the binary gcd algorithm, Algorithm 4.5.2B, be adapted in an analogous way to an algorithm that applies to polynomials? Verified: no Solve time: 3m24s Exercise 4.6.1.6 [ M29 ] Setup We are asked whether the binary gcd algorithm...
TAOCP 4.6.1 Exercise 5
Section 4.6.1: Division of Polynomials Exercise 5. [ M22 ] What is the probability that $u(x)$ and $v(x)$ are relatively prime modulo $p$, if $u(x)$ and $v(x)$ are independently and uniformly distributed monic polynomials of degree $n$? Verified: no Solve time: 2m11s Solution Let $S = \mathbb{F}_p$. Let $u(x)$ and $v(x)$ be independently and uniformly distributed monic polynomials of degree $n$. Let $\mathcal{M}_n$ denote the set of all such polynomials,...
TAOCP 4.6.1 Exercise 3
Section 4.6.1: Division of Polynomials Exercise 3. ▶ [ M25 ] Show that Euclid's algorithm for polynomials over a field $S$ can be extended to find polynomials $U(x)$ and $V(x)$ over $S$ such that $$u(x)V(x) + U(x)v(x) = \gcd(u(x), v(x)).$$ (See Algorithm 4.5.2X.) What are the degrees of the polynomials $U(x)$ and $V(x)$ that are computed by this extended algorithm? Prove that if $S$ is the field of rational numbers,...
TAOCP 4.6.1 Exercise 2
Section 4.6.1: Division of Polynomials Exercise 2. [ 15 ] What is the greatest common divisor of $3x^6 + x^5 + 4x^4 + 3x^3 + 4x + 3$ and its "reverse" $2x^6 + 4x^5 + 3x^4 + 4x^3 + 4x^2 + x + 3$, modulo 7? Verified: no Solve time: 8m02s Exercise 4.6.1.2 [ 15 ] Problem 2. Compute the greatest common divisor of $$ f(x) = 3x^6 + x^5...
TAOCP 4.6.1 Exercise 4
Section 4.6.1: Division of Polynomials Exercise 4. [ M30 ] Let $p$ be a prime. Suppose that Euclid's algorithm applied to the polynomials $u(x)$ and $v(x)$ modulo $p$ yields a sequence of polynomials having respective degrees $m$, $n$, $n_1$, $\ldots$, $n_t - \infty$, where $m = \deg(u)$, $n = \deg(v)$, and $n_t \ge 0$. Assume that $m \ge n$. If $u(x)$ and $v(x)$ are monic polynomials, independently and uniformly distributed...
TAOCP 4.6.1 Exercise 1
Section 4.6.1: Division of Polynomials Exercise 1. [ 10 ] Compute the pseudo-quotient $q(x)$ and pseudo-remainder $r(x)$, namely the polynomials satisfying (1), when $u(x) = x^6 + x^5 - x^4 + 2x^3 + 3x^2 - x + 2$ and $v(x) = 2x^2 + 2x^2 - x + 3$, over the integers. Verified: yes Solve time: 10m02s Exercise 4.6.1.1 [ 10 ] We are asked to compute the pseudo-quotient $q(x)$ and...
TAOCP 4.5.4 Exercise 47
Section 4.5.4: Factoring into Primes Exercise 47. [ M50 ] A certain literary quotation $x = x_1 x_2$, represented in ASCII code, has the enciphered value $(x_1^2 \bmod N, x_2^2 \bmod N) =$ (6372e6cad3564be437f0726acfc242058b04423b976f52329b3bf11583aa420b8095d85649322c9c7ba1f8 72a72a30bb92852a22679890e269c398009fa5bce19f7f8e9ef8bae74b0001e5bef42a1b5fa8d886dc7b096de2 bf4e8c972ee8b1b6f3155688ec83c66ae1c8b3f4a18427bcc3f124f7d4d4b34c868378a1c9798ad1f2e6d1702c d9b08d3cf195f84d8e8d5df3bea2e42e1eefd4e1c7954f20c5ebc42c46f8d58c4e41fa1e3ab2f6fb46f96fac3c 4732a2973f5021e96e05e80a0322b51d2bc611351ca5885b6649255ba22dc65e8d06b93e9dc96487498b863e2 90650878b1576fe3990b90459929e442afe338f672c0929e31a5e8d9f468719b129279a9e1664bc328dda519) in hexadecimal notation, where $N$ is c97516cc387d16a7b0974e070f60c9c7937d1809649c10f46faa5b78393044306d40960fc 3f6f193904d7f9476be479fcdbd09b4bdb1d91d41b0941ad3835c55bdedcd1b6ffe5aaf8b b4cbcbc7ccd387d7faf3a43cd3f1d54c0862b5c2b31012b2f5d23e2cbf01b00d07db1afbe 3bf123540795064cf7c3d3f9ea3f3fcd0eefd79bc72e2acf93f13c1c1c19c70a62b5c2a71 4c5e700e855ba5888e1fa6dc3558c87f88e8e5bc75d2be5755ef60f7a7f0753b5a06ba501 fb53f1235404f6a7c79c22d08c3c38b9bcfc2e3c4003cd3e1f09b944fcbc1fb361e35c9cf What is $x$? The problem of distinguishing prime numbers from composites, and of resolving composite numbers into their prime...
TAOCP 4.5.4 Exercise 45
Section 4.5.4: Factoring into Primes Exercise 45. ▶ [ M41 ] (J. M. Pollard and Claus-Peter Schnorr.) Find an efficient algorithm to solve the congruence $$x^2 - ay^2 \equiv b \pmod{n}$$ for integers $x$ and $y$, given integers $a$, $b$, and $n$ with $ab \perp n$ and $n$ odd, even if the factorization of $n$ is unknown. [ Hint: Use the identity $(x_1^2 - ay_1^2)(x_2^2 - ay_2^2) = x^2 -...
TAOCP 4.5.4 Exercise 46
Section 4.5.4: Factoring into Primes Exercise 46. [ HM30 ] (L. Adleman.) Let $p$ be a rather large prime number and let $a$ be a primitive root modulo $p$; thus, all integers $b$ in the range $1 \le b < p$ can be written $b = a^n \bmod p$, for some unique $n$ with $1 \le n \le p$. Design an algorithm that almost always finds $n$, given $b$, in...
TAOCP 4.5.4 Exercise 42
Section 4.5.4: Factoring into Primes Exercise 42. [ M35 ] (H. W. Lenstra, Jr.) Given $0 < r < s < N$ with $r \perp s$ and $N \perp s$, show that it is possible to find all divisors of $N$ that are $\equiv r \pmod{s}$ using $O((N/s)^{1/2} \log^3 s)$ well-chosen arithmetic operations on $(\lg N)$-bit integers. [ Hint: Apply exercise 4.5.3–49.] Verified: no Solve time: 7m34s Corrected Solution to...
TAOCP 4.5.4 Exercise 43
Section 4.5.4: Factoring into Primes Exercise 43. ▶ [ M35 ] Let $m = py$ be an $n$-bit Blum integer as in Theorem 3.5B, and let $Q_m = {y \mid y = z^2 \bmod m \text{ for some } z \in Q_m}$. $Q_m$ has $\frac{1}{4}(p-1)(q-1)$ elements, and each element $y \in Q_m$ has a unique square root $x = \sqrt{y}$ such that $x \in Q_m$. Suppose $G(y)$ is an algorithm...
TAOCP 4.5.4 Exercise 44
Section 4.5.4: Factoring into Primes Exercise 44. [ M35 ] (J. Håstad.) Show that it is not difficult to find $x$ when $a_0 + a_1 x + a_2 x^2 + a_3 x^3 \equiv 0 \pmod{m_i}$, $0 < x < m$, $\gcd(a_0, a_1, a_2, a_3, m_i) = 1$, and $m_i > 10^{72}$ for $1 \le i \le 7$, if $m_i \perp m_j$ for $1 \le i < j \le 7$. (All...
TAOCP 4.5.4 Exercise 40
Section 4.5.4: Factoring into Primes Exercise 40. ▶ [ M36 ] (A. Shamir.) Consider an abstract computer that can perform the operations $x + y$, $x - y$, $x \cdot y$, and $\lfloor x/y \rfloor$ on integers $x$ and $y$ of arbitrary length, in just one unit of time, no matter how large those integers are. The machine stores integers in a random-access memory and it can select different program...
TAOCP 4.5.4 Exercise 41
Section 4.5.4: Factoring into Primes Exercise 41. [ M28 ] (Lagarias, Miller, and Odlyzko.) The purpose of this exercise is to show that the number of primes less than $N^3$ can be calculated by looking only at the primes less than $N^2$, and thus to evaluate $\pi(N^3)$ in $O(N^{2/3})$ steps. Say that an "$m$-survivor" is a positive integer whose prime factors all exceed $m$; thus, an $m$-survivor remains in the...
TAOCP 4.5.4 Exercise 37
Section 4.5.4: Factoring into Primes Exercise 37. [ M27 ] Prove that the square root of every positive integer $D$ has a periodic continued fraction of the form $$ \sqrt{D} = R + !/!/ a_1, a_2, \ldots, a_n, 2R, a_1, a_2, \ldots, a_n, 2R, \ldots /!/, $$ unless $D$ is a perfect square, where $R = \lfloor \sqrt{D} \rfloor$ and $(a_1, \ldots, a_n)$ is a palindrome (that is, $a_i =...
TAOCP 4.5.4 Exercise 39
Section 4.5.4: Factoring into Primes Exercise 39. [ 40 ] Many primes $p$ have the property that $2p + 1$ is also prime; for example, $5 \to 11 \to 23 \to 47$. More generally, say that $q$ is a successor of $p$ if $p$ and $q$ are both prime and $q = 2^k p + 1$ for some $k \ge 0$. For example, $2 \to 3 \to 7 \to 29...
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: 15m39s The reviewer’s objections are correct. The previous submission does not solve the exercise. In particular, it...
TAOCP 4.5.4 Exercise 36
Section 4.5.4: Factoring into Primes Exercise 36. [ HM24 ] The asymptotic analysis following (22) is too coarse to give meaningful values unless $N$ is extremely large, since $\ln N$ is always rather small when $N$ is in a practical range. Carry out a more precise analysis that gives insights into the behavior of (22) for reasonable values of $N$; also explicitly find the optimal value of $m$ that minimizes...
TAOCP 4.5.4 Exercise 35
Section 4.5.4: Factoring into Primes Exercise 35. ▶ [ M25 ] (H. C. Williams, 1979.) Let $N$ be the product of two primes $p$ and $q$, where $p \bmod 8 = 3$ and $q \bmod 8 = 7$. Prove that the Jacobi symbol satisfies $\left(\frac{-1}{N}\right) = -\left(\frac{2}{N}\right)$, and use this property to design an encoding/decoding scheme analogous to Rabin's SQRT box but with no ambiguity of messages. Verified: no Solve...
TAOCP 4.5.4 Exercise 34
Section 4.5.4: Factoring into Primes Exercise 34. [ M30 ] (Peter Weinberger.) Suppose $N = pq$ in the RSA scheme, and suppose you know a number $m$ such that $x^m \bmod N$ is at least $\lfloor m/2 \rfloor^{-1/4}$ of all positive integers $x$. Explain how you could go about factoring $N$ without great difficulty, if $m$ is not too large (say $m < N^{10}$). Verified: yes Solve time: 4m26s Solution...
TAOCP 4.5.4 Exercise 33
Section 4.5.4: Factoring into Primes Exercise 33. [ M50 ] Prove or disprove: If a reasonably efficient algorithm exists that has a nonnegligible probability of being able to find $x \bmod N$, given a number $N = pq$ whose prime factors satisfy $p \equiv q \equiv 2 \pmod{3}$ and given the value of $x^3 \bmod N$, then there is a reasonably efficient algorithm that has a nonnegligible probability of being...
TAOCP 4.5.4 Exercise 30
Section 4.5.4: Factoring into Primes Exercise 30. [ HM35 ] [HM35] (J. D. Dixon and Claus-Peter Schnorr.) Let $p_1 < \cdots < p_m$ be primes that do not divide the odd number $N$, and let $r$ be an even integer $\le \log N / \log p_m$. Prove that the number of integers $X$ in the range $0 \le X < N$ such that $X^2 \bmod N =$ $p_1^{e_1} \cdots p_m^{e_m}$...
TAOCP 4.5.4 Exercise 32
Section 4.5.4: Factoring into Primes Exercise 32. ▶ [ M21 ] Show how to modify the RSA encoding scheme so that there is no problem with messages $< \sqrt[3]{N}$, in such a way that the length of messages is not substantially increased. Verified: yes Solve time: 5m02s Solution Suppose RSA uses public exponent $e=3$. The difficulty is that if $$ 0 \le m < N^{1/3}, $$ then $$ c \equiv...
TAOCP 4.5.4 Exercise 31
Section 4.5.4: Factoring into Primes Exercise 31. [ M20 ] Use exercise 1.2.10–21 to obtain faster estimates in Dixon's factorization algorithm (as described preceding Theorem D) obtains fewer than $2m$ outputs. Verified: no Solve time: 1m24s Solution We are asked to use exercise 1.2.10–21 to obtain faster estimates in Dixon's factorization algorithm, ensuring that fewer than $2m$ outputs are produced. We proceed by carefully recalling the relevant definitions and bounding...
TAOCP 4.5.4 Exercise 29
Section 4.5.4: Factoring into Primes Exercise 29. [ M25 ] [M25] Prove that the number of positive integers $\le n$ whose prime factors are all contained in a given set of primes ${p_1, \ldots, p_m}$ is at most $m^r / r!$, where $r = \lfloor \log n / \log p_m \rfloor$ and $p_1 < \cdots < p_m$. Verified: yes Solve time: 1m38s Solution Let $$ S(n)={,p_1^{e_1}p_2^{e_2}\cdots p_m^{e_m}\le n : e_i\ge0,}....
TAOCP 4.5.4 Exercise 28
Section 4.5.4: Factoring into Primes Exercise 28. [ M27 ] [M27] Given a prime $p$ and a positive integer $d$, what is the value of $f(p, d)$, the average number of times that $p$ divides $A^2 - dB^2$ (counting multiplicity), when $A$ and $B$ are random integers that are independent except for the condition $A \perp B$? Verified: no Solve time: 1m26s Setup Let $Q(A,B)=A^2-dB^2,$ and let $v_p(n)$ denote the...
TAOCP 4.5.4 Exercise 26
Section 4.5.4: Factoring into Primes Exercise 26. ▶ [ M25 ] [M25] (H. C. Pocklington, 1914.) Let $N = fr + 1$, where $0 < r \le f + 1$. Prove that $N$ is prime if, for every prime divisor $p$ of $f$, there is an integer $x_p$ such that $x_p^{N-1} \bmod N = \gcd(x_p^{(N-1)/p} - 1, N) = 1$. Verified: yes Solve time: 3m56s Solution Let $$ N=fr+1,\qquad 0<r\le...
TAOCP 4.5.4 Exercise 27
Section 4.5.4: Factoring into Primes Exercise 27. ▶ [ M30 ] [M30] Show that there is a way to test numbers of the form $N = 5 \cdot 2^n + 1$ for primality, using approximately the same number of squarings mod $N$ as the Lucas–Lehmer test for Mersenne numbers described in §4.5.4 (in the previous exercises). Verified: yes Solve time: 2m24s Setup Let $$ N=5\cdot 2^n+1, $$ and let $$...
TAOCP 4.5.4 Exercise 25
Section 4.5.4: Factoring into Primes Exercise 25. [ HM41 ] [HM41] (B. Riemann.) Prove that $$\pi(x) = \frac{x}{\ln x} + \frac{\pi(x^{1/2})}{2} + \frac{\pi(x^{1/3})}{3} + \cdots + \int_2^x \frac{dt}{\ln t} - 2 \sum_\rho \int_2^x \frac{x^{(\sigma+i\tau)\ln x}}{t + i\tau} + O(1),$$ where the sum is over all complex $\rho = \sigma + i\tau$ with $\zeta(\rho) = 0$ and $\langle \sigma + i\tau \rangle = 0$. Verified: yes Solve time: 4m02s The statement...
TAOCP 4.5.4 Exercise 24
Section 4.5.4: Factoring into Primes Exercise 24. ▶ [ M25 ] [M25] (L. Adleman.) When $n > 1$ and $x > 1$ are integers, $n$ odd, let us say that $n$ "passes the $x$ test of Algorithm P" if either $x \bmod n = 0$ or if steps P2–P5 lead to the conclusion that $n$ is probably prime. Prove that, for any $N$, there exists a set of positive integers...
TAOCP 4.5.4 Exercise 23
Section 4.5.4: Factoring into Primes Exercise 23. [ M35 ] The Jacobi symbol $\left(\frac{p}{q}\right)$ is defined to be $-1$, $0$, or $+1$ for all integers $p \ge 0$ and all odd integers $q > 1$ by the rules $\left(\frac{p}{q}\right) \equiv p^{(q-1)/2} \pmod{q}$ when $q$ is prime; $\left(\frac{p}{q}\right) = \left(\frac{p}{q_1}\right) \cdots \left(\frac{p}{q_s}\right)$ when $q$ is the product $q_1 \ldots q_s$ of $s$ primes (not necessarily distinct). Thus it generalizes the Legendre...
TAOCP 4.5.4 Exercise 22
Section 4.5.4: Factoring into Primes Exercise 22. ▶ [ M30 ] (M. O. Rabin.) Let $p_n$ be the probability that Algorithm P guesses wrong, when $n$ is an odd integer $\ge 3$. Show that $p_n < \frac{1}{4}$ for all $n$. Verified: yes Solve time: 3m47s Let $n\ge 3$ be odd, and let $p_n$ be the probability that Algorithm P declares $n$ to be prime when $n$ is actually composite. Write...
TAOCP 4.5.4 Exercise 21
Section 4.5.4: Factoring into Primes Exercise 21. [ M29 ] (R. K. Guy.) Let $m(p)$ be the number of iterations required by Algorithm B to cast out the prime factor $p$. Is $m(p) = O(\sqrt{p} \log p)$ as $p \to \infty$? Verified: yes Solve time: 1m36s Setup Let $p$ be a prime number, and consider Algorithm B from Section 4.5.4, which repeatedly divides a positive integer $n$ by $p$ until...
TAOCP 4.5.4 Exercise 18
Section 4.5.4: Factoring into Primes Exercise 18. [ HM23 ] Give a heuristic proof of (7), analogous to the text's derivation of (6). What is the approximate probability that $p_{n-1} \le \sqrt{p_n}$? Verified: yes Solve time: 2m06s Solution Let $p_n$ and $p_{n-1}$ denote the two largest prime factors in a typical factorization, ordered so that $p_{n-1} \le p_n$. The condition $p_{n-1} \le \sqrt{p_n}$ is equivalent to $\log p_{n-1} \le \tfrac{1}{2}\log...
TAOCP 4.5.4 Exercise 19
Section 4.5.4: Factoring into Primes Exercise 19. ▶ [ M25 ] (J. M. Pollard.) Show how to compute a number $M$ that is divisible by all odd primes $p$ such that $p - 1$ is a divisor of some given number $D$. [ Hint: Consider numbers of the form $a^k - 1$.] Such an $M$ is useful in factorization, for by computing $\gcd(M, N)$ we may find prime factors of...
TAOCP 4.5.4 Exercise 20
Section 4.5.4: Factoring into Primes Exercise 20. [ M46 ] Consider exercise 19 with $p + 1$ replacing $p - 1$. Verified: no Solve time: 3m05s Setup Let $D$ be a given positive integer, and let $p$ range over odd primes. In Exercise 19, we were asked to construct a number $M$ divisible by all odd primes $p$ such that $p-1$ divides $D$. In this exercise, we are asked to...
TAOCP 4.5.4 Exercise 16
Section 4.5.4: Factoring into Primes Exercise 16. [ M50 ] Are there infinitely many Mersenne primes? Verified: yes Solve time: 1m52s Setup A Mersenne prime is a prime number of the form $M_p = 2^p - 1,$ where $p$ itself is prime. The exercise asks whether there are infinitely many Mersenne primes. Formally, we are asked to determine the truth of the statement: "There exists an infinite sequence of primes...
TAOCP 4.5.4 Exercise 17
Section 4.5.4: Factoring into Primes Exercise 17. [ M25 ] (V. R. Pratt.) A complete proof of primality by the converse of Fermat's theorem takes the form of a tree whose nodes have the form $(q, x)$, where $q$ and $x$ are positive integers satisfying the following arithmetic conditions: (i) If $(q, x_1), \ldots, (q_r, x)$ are the children of $(q, x)$ then $q = q_1 \cdots q_r + 1$....
TAOCP 4.5.4 Exercise 15
Section 4.5.4: Factoring into Primes Exercise 15. ▶ [ M34 ] (Lucas and Lehmer.) Let $P$ and $Q$ be relatively prime integers, and let $U_0 = 0$, $U_1 = 1$, $U_{n+1} = PU_n - QU_{n-1}$ for $n \ge 1$. Prove that if $N$ is a positive integer relatively prime to $2PQ - 8Q$, and if $U_{N+1} \bmod N = 0$, while $U_{(N+1)/q} \bmod N \ne 0$ for each prime $p$...
TAOCP 4.5.4 Exercise 14
Section 4.5.4: Factoring into Primes Exercise 14. [ M20 ] Prove that the number $T$ in step E3 of Algorithm E will never be a multiple of an odd prime $p$ for which $(kN)^{(p-1)/2} \bmod p > 1$. Verified: no Solve time: 17m58s Solution We are asked to prove that the number $T$ computed in step E3 of Algorithm E (the strong pseudoprime test in Section 4.5.4) is never divisible...
TAOCP 4.5.4 Exercise 11
Section 4.5.4: Factoring into Primes Exercise 11. [ M20 ] What outputs does Algorithm E give when $N = 197209$, $k = 5$, $m = 1$? [Hint: $\sqrt{5 \cdot 197209} \approx 992 + \frac{1}{1.495}, \frac{1}{2.495}, \frac{1}{2.495}, \frac{1}{1.984} \cdots]$ Verified: yes Solve time: 4m06s Solution We are asked to determine the outputs of Algorithm E when $$ N = 197209, \quad k = 5, \quad m = 1. $$ Algorithm E...
TAOCP 4.5.4 Exercise 12
Section 4.5.4: Factoring into Primes Exercise 12. [ HM25 ] (J. D. Dixon.) Prove that whenever the algorithm of exercise 12 is presented with a solution $(x, e_0, \ldots, e_m)$ whose exponents are linearly dependent modulo 2 on the exponents of previous solutions, the probability is $2^{1-d}$ that a factorization will not be found, when $N$ has $d$ distinct prime factors and $x$ is chosen at random. Verified: no Solve...
TAOCP 4.5.4 Exercise 10
Section 4.5.4: Factoring into Primes Exercise 10. ▶ [ M26 ] (John Selfridge.) Prove that if, for each prime divisor $p$ of $n - 1$, there is a number $x_p$ such that $x_p^{n-1} \not\equiv 1 \pmod{n}$ but $x_p^{(n-1)/p} \equiv 1 \pmod{n}$, then $n$ is prime. Verified: no Solve time: 6m26s Let $n>1$ satisfy the hypothesis: for every prime $p \mid (n-1)$ there exists an integer $x_p$ such that $$ x_p^{(n-1)/p}...
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 9
Section 4.5.4: Factoring into Primes Exercise 9. [ M25 ] Let $n$ be an odd number, $n \ge 3$. Show that if the number $\lambda(n)$ of Theorem 3.2.1.2B is a divisor of $n - 1$ but not equal to $n - 1$, then $n$ must have the form $p_1 p_2 \ldots p_r$ where the $p$'s are distinct primes and $r \ge 3$. Verified: no Solve time: 1m45s Solution Let $n$...
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: 1m46s Solution Algorithm D (Fermat's method) involves iterative calculations modulo various integers $m_i$ to test for squares and compute factors of a number $N$. On a binary computer,...
TAOCP 4.5.4 Exercise 6
Section 4.5.4: Factoring into Primes Exercise 6. [ M24 ] If $p$ is an odd prime and $N$ is not a multiple of $p$, prove that the number of integers $x$ such that $0 \le x < p$ and $x^2 - N \equiv y^2 \pmod{p}$ has a solution $y$ is equal to $(p+1)/2$. Verified: yes Solve time: 4m30s Solution We are asked to count the number of integers $x$ with...
TAOCP 4.5.4 Exercise 3
Section 4.5.4: Factoring into Primes Exercise 3. [ M20 ] Show that there is a number $P$ with the following property: If $1000 \le n \le 1000000$, then $n$ is prime if and only if $\gcd(n, P) = 1$. Verified: yes Solve time: 1m49s Solution Let us construct a number $P$ with the desired property. Consider the set of all primes less than $1000$: $2, 3, 5, 7, 11, 13,...
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: yes Solve time: 7m24s The review correctly identifies the central failure: the solution never completes Fermat’s method by producing an actual $x$, $y$, and hence never factors $11111$. The congruence work is usable, but it must...
TAOCP 4.5.4 Exercise 4
Section 4.5.4: Factoring into Primes Exercise 4. [ M29 ] In the notation of exercise 3.3.2–4 and Section 1.2.11.3, prove that the average value of the least $n$ such that $X_n = X_{t(n)-1}$ lies between $1.5Q(m) - 0.5$ and $1.625Q(m) - 0.5$. Verified: no Solve time: 6m22s Exercise 4.5.4.4 [ M29 ] Statement: In the notation of exercise 3.3.2–4 and Section 1.2.11.3, prove that the average value of the least...
TAOCP 4.5.4 Exercise 2
Section 4.5.4: Factoring into Primes Exercise 2. [ 15 ] If it is known that the input $N$ to Algorithm A is equal to 3 or more, could step A2 be eliminated? Verified: yes Solve time: 1m57s Solution Step A2 of Algorithm A tests whether the current value of $n$ is equal to $1$, and if so, terminates the algorithm. Its purpose is to detect that all prime factors of...
TAOCP 4.5.3 Exercise 42
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 42. [ M30 ] (J. Lagrange, 1768.) Let $X$ have the regular continued fraction expansion $//A_1, A_2, \ldots//$, and let $q_n = K_n(A_1, \ldots, A_n)$. Let $|x|$ denote the distance from $x$ to the nearest integer. Show that $|q_{n-1} X| \le |qX|$ for all $1 \le q < q_n$ and $q \ne q_{n-1}$. [Thus the denominators $q_n$ of the so-called convergents $p_n/q_n =...
TAOCP 4.5.4 Exercise 1
Section 4.5.4: Factoring into Primes Exercise 1. [ 10 ] If the sequence $d_0, d_1, d_2, \ldots$ of trial divisors in Algorithm A contains a number that is not prime, why will it never appear in the output? Verified: yes Solve time: 1m45s In Algorithm A the invariant at step A1 states that $n$ has no prime factors less than $d_k$. Let $d_k$ be composite, and let $p$ be its...
TAOCP 4.5.3 Exercise 41
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 41. [ M40 ] (J. Shallit, 1979.) Show that the regular continued expansion of $$\frac{1}{2^1} + \frac{1}{2^2} + \frac{1}{2^4} + \cdots = \sum_{n \ge 0} \frac{1}{2^{2^n}}$$ contains only 1s and 2s and has a fairly simple pattern. Prove that the partial quotients of Liouville's numbers $\sum_{n \ge 1} l^{-n!}$ also have a regular pattern, when $l$ is any integer $\ge 2$. [The latter...
TAOCP 4.5.3 Exercise 39
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 39. ▶ [ M25 ] (R. W. Gosper.) If a baseball player's batting average is $.334$, what is the smallest possible number of times he has been at bat? [Note for non-baseball-fans: Batting average $=$ (number of hits)/(times at bat), rounded to three decimal places.] Verified: yes Solve time: 3m22s Solution Let $h$ be the number of hits and $a$ the number of...
TAOCP 4.5.3 Exercise 38
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 38. [ M35 ] (J. Mikolajski.) Let $L(n) = \max_{m \ge 0} T(m, n)$. Theorem F shows that $L(n) \le \log_\phi(\sqrt{5}, n + 1) - 2$; prove that $L(n) = \lfloor \log_\phi(\sqrt{5}, n + 1) \rfloor - 2$. Verified: no Solve time: 1m25s Setup Let $$ L(n)=\max_{m\ge 0}T(m,n), $$ where $T(m,n)$ is the number of division steps performed by Euclid's algorithm on inputs...
TAOCP 4.5.3 Exercise 37
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 37. [ M38 ] (T. S. Motzkin and E. G. Straus.) Let $a_1, \ldots, a_n$ be positive integers. Show that $\max K_s(a_{p(1)}, \ldots, a_{p(n)})$, over all permutations $p(1) \ldots p(n)$ of ${1, 2, \ldots, n}$, occurs when $a_{p(1)} \ge a_{p(n)} \ge a_{p(2)} \ge a_{p(n-1)} \ge \cdots$; and the minimum occurs when $a_{p(1)} \le a_{p(n)} \le a_{p(2)} \le a_{p(n-1)} \le \cdots \le a_{p(4)} \le...
TAOCP 4.5.3 Exercise 36
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 36. [ M25 ] (G. H. Bradley.) What is the smallest value of $u_n$ such that the calculation of $\gcd(u_1, \ldots, u_n)$ by Algorithm 4.5.2C requires $N$ divisions, if Euclid's algorithm is used throughout? Assume that $N \ge n \ge 3$. Verified: yes Solve time: 5m43s Solution Let $$ g_k=\gcd(u_1,\ldots,u_k)\qquad (1\le k\le n). $$ Algorithm 4.5.2C computes $$ g_2=\gcd(u_1,u_2),\qquad g_3=\gcd(g_2,u_3),\qquad \ldots,\qquad g_n=\gcd(g_{n-1},u_n). $$...
TAOCP 4.5.3 Exercise 35
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 35. [ HM41 ] (A. C. Yao and D. E. Knuth.) Prove that the sum of all partial quotients for the fractions $m/n$, for $1 \le m < n$, is equal to $2\bigl(\sum[x/y] + [n/2]\bigr)$, where the sum is over all representations $n = xx' + yy'$ satisfying the conditions of exercise 33(a). Show that $\sum[x/y] = 3\pi^{-2}n(\ln n)^2 + O(n \log n...
TAOCP 4.5.3 Exercise 34
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 34. [ HM41 ] (H. Heilbronn.) Let $h_0(n)$ be the number of representations of $n$ as in exercise 33 such that $d < x'$, plus half the number of representations with $2d = x'$. a) Let $g(n)$ be the number of representations without the requirement that $x \perp y$. Prove that $$h(n) = \sum_{d|n} \mu(d) g!\left(\frac{n}{d}\right), \qquad g(n) = 2\sum_{d|n} h_0!\left(\frac{n}{d}\right).$$ b) Generalizing...
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 30
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 30. ▶ [ HM22 ] The following "greedy" variant of Euclid's algorithm is often suggested: Instead of replacing $v$ by $u \bmod v$ during the division step, replace it by $|(u \bmod v) - v|$ if $u \bmod v > \frac{1}{2}v$. For example, if $u = 26$ and $v = 7$, we have $\gcd(26, 7) = \gcd(-2, 7) = \gcd(2, 7)$. Compare this...
TAOCP 4.5.3 Exercise 31
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 31. ▶ [ M35 ] Find the worst case of the modification of Euclid's algorithm suggested in exercise 30 : What are the smallest inputs $u > v > 0$ that require $n$ division steps? Verified: yes Solve time: 1m51s Setup Let the modified algorithm be defined as follows. At a division step, write $u=qv+r,\qquad 0\le r<v.$ Instead of replacing $(u,v)$ by $(v,r)$,...
TAOCP 4.5.3 Exercise 29
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 29. [ M23 ] Assuming that $T_n$ is shown by (55), show that (57) equals (58). Verified: no Solve time: 6m55s To produce a fully corrected solution, we must work strictly with the exercise instructions. The exercise asks to show that equation (57) equals (58), assuming $T_n$ is defined by (55). In TAOCP, these are standard definitions: Equation (55) defines $T_n$ as a...
TAOCP 4.5.3 Exercise 27
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 27. [ M21 ] Prove (55) from (43) and (54). Verified: no Solve time: 2m53s Solution The statement of the exercise depends on the explicit forms of equations (43), (54), and (55) in Section 4.5.3. These equations are not included in the provided context, and their content is required to carry out the deduction. In particular, the transformation or identity labeled (55) is...
TAOCP 4.5.3 Exercise 28
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 28. [ M23 ] Prove the following identities involving the three number-theoretic functions $\varphi(n)$, $\mu(n)$, $\Lambda(n)$: $$\text{a)};\sum_{d|n} \mu(d) = \delta_{n1}, \qquad \text{b)};\ln n = \sum_{d|n} \Lambda(d), \qquad n = \sum_{d|n} \varphi(d).$$ $$\text{c)};\Lambda(n) = \sum_{d|n} \mu!\left(\frac{n}{d}\right)\ln d, \qquad \varphi(n) = \sum_{d|n} \mu!\left(\frac{n}{d}\right) d.$$ Verified: yes Solve time: 1m56s Solution We prove each of the identities in turn. Let $n \ge 1$ be an integer....
TAOCP 4.5.3 Exercise 24
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 24. [ M22 ] What is the average value of a partial quotient $A_n$ in the regular continued fraction expansion of a random real number? Verified: yes Solve time: 2m47s Solution Let $X$ be a real number chosen uniformly at random from the interval $[0,1)$. Denote its regular continued fraction expansion by $X = //A_1, A_2, A_3, \ldots//$, where $A_1, A_2, \ldots$ are...
TAOCP 4.5.3 Exercise 25
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 25. [ HM25 ] Find an example of a set $\mathcal{I} = I_1 \cup I_2 \cup I_3 \cup \cdots \subseteq [0,.,.,1]$, where the $I$'s are disjoint intervals, for which (45) does not hold. Verified: yes Solve time: 5m15s Solution We are asked to construct a set $$ \mathcal{I} = I_1 \cup I_2 \cup I_3 \cup \cdots \subseteq [0,1], $$ where the $I_k$ are...
TAOCP 4.5.3 Exercise 26
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 26. [ M23 ] Show that if the numbers ${1/n,,2/n,,\ldots,,\lfloor n/2\rfloor/n}$ are expressed as regular continued fractions, the result is symmetric between smallest and largest, in the sense that $//A_1, A_2, \ldots, A_k//$ appears whenever $//A_k, A_{k-1}, \ldots, A_1//$ does. Verified: no Solve time: 7m03s Solution Let $n \ge 2$ be an integer, and consider the fractions $$ \frac{k}{n}, \quad 1 \le k...
TAOCP 4.5.3 Exercise 23
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 23. [ HM45 ] Prove (44) using results from the proof of Theorem W. Verified: no Solve time: 6m54s Exercise 4.5.3.23 [ HM45 ] Prove equation (44) using results from the proof of Theorem W. Solution We first recall the relevant definitions and results. Let $F(x)$ denote a function introduced in TAOCP §4.5.3, and let $T_g(x)$ denote the cumulative sum of a function...
TAOCP 4.5.3 Exercise 22
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 22. [ HM46 ] (K. L. Babenko.) Develop efficient means to calculate accurate approximations to the quantities $\lambda_1$ and $\Psi_2(x)$ in (44), for small $y \ge 3$ and for $0 \le x \le 1$. Verified: yes Solve time: 7m09s Solution We are asked to develop efficient methods for approximating the quantities $\lambda_1$ and $\Psi_2(x)$ in equation (44) of [ HM46 ], for small...
TAOCP 4.5.3 Exercise 19
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 19. [ M20 ] Prove $F(x) = \log_2(1 + x)$ satisfies Eq. (24). Verified: yes Solve time: 2m11s Solution Equation (24) is $$ F(x)=\sum_{m\ge1}\left(F!\left(\frac1m\right)-F!\left(\frac1{m+x}\right)\right), \qquad 0\le x\le1. $$ Let $$ F(x)=\log_2(1+x). $$ Then $$ F!\left(\frac1m\right)-F!\left(\frac1{m+x}\right) \log_2!\left(1+\frac1m\right) \log_2!\left(1+\frac1{m+x}\right), $$ hence $$ F!\left(\frac1m\right)-F!\left(\frac1{m+x}\right) \log_2 \frac{(m+1)(m+x)} {m(m+x+1)}. $$ Therefore the partial sum through $m=N$ is $$ S_N \sum_{m=1}^{N} \log_2 \frac{(m+1)(m+x)} {m(m+x+1)} \log_2 \prod_{m=1}^{N} \frac{(m+1)(m+x)} {m(m+x+1)}. $$...
TAOCP 4.5.3 Exercise 20
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 20. [ HM20 ] Deduce (38) from (37). Verified: yes Solve time: 5m11s Let $K_n(x_1,\dots,x_n)$ denote the continuant defined in Section 4.5.3. Equation (37) gives the standard last-term expansion $$ (37)\quad K_n(x_1,\dots,x_n)=x_n,K_{n-1}(x_1,\dots,x_{n-1})+K_{n-2}(x_1,\dots,x_{n-2}). $$ We now deduce the corresponding first-term expansion (38). Consider the determinant definition of the continuant. Reversing the order of rows and columns shows that the continuant is invariant under reversal...
TAOCP 4.5.3 Exercise 21
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 21. [ HM20 ] (E. Wissing.) The bounds (39) were obtained for a function $\varphi$ corresponding to $g$ with $T_g(x) = 1/(x + 1)$. Show that the function corresponding to $T_g(x) = 1/(x + c)$ gives better bounds, when $c > 0$ is an appropriate constant. Verified: no Solve time: 6m56s The issue here is that the "Actual output" is empty, which usually...
TAOCP 4.5.3 Exercise 17
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 17. ▶ [ M23 ] (a) Prove that $//x_1, -x_2// = //x_1 - 1, x_2 - 1, 1//$. (b) Generalize this identity, obtaining a formula for $//x_1, -x_2, x_3, -x_4, x_5, -x_6, \ldots//$ in which all partial quotients are positive integers when the $x$'s are large positive integers. (c) The result of exercise 16 implies that $\tan 1 = //1, -3, 5, -7,...
TAOCP 4.5.3 Exercise 18
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 18. [ M25 ] Show that $//a_1, a_2, \ldots, a_m, t_1, a_1, a_2, \ldots, a_m, t_2, a_1, a_2, \ldots, a_m, x_3, \ldots// - //a_m, \ldots, a_1, t_1, a_m, \ldots, a_1, t_2, \ldots//$ does not depend on $x_1, x_2, x_3, \ldots$. Hint : Multiply both continued fractions by $K_m(a_1, a_2, \ldots, a_m)$. Verified: no Solve time: 10m06s Exercise 4.5.3.18 [M25], Corrected Solution We are...
TAOCP 4.5.3 Exercise 15
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 15. ▶ [ M31 ] (R. W. Gosper.) Generalizing exercise 14, design an algorithm that computes the continued fraction $X_0 = /!/ X_1, X_2, \ldots /!/$ for $(ax + b)/(cx + d)$, given the continued fraction $x = /!/ 1, 2, \ldots /!/$ for $x$, and given integers $a$, $b$, $c$, $d$ with $ad \ne bc$. Make your algorithm an "online coroutine" that...
TAOCP 4.5.3 Exercise 14
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 14. [ M22 ] (A. Hurwitz, 1891.) Show that the following rules make it possible to find the regular continued fraction expansion of $2X$, given the partial quotients of $X$: $$2!/!/ 2a, b, c, \ldots /!/ = /!/ a, 2b + 2/!/b, c, \ldots /!/;$$ $$2!/!/ 2a, 1, b, c, \ldots /!/ = /!/ a, 1, 2/!/1, b, c, \ldots /!/.$$ Use this...
TAOCP 4.5.3 Exercise 13
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 13. [ M40 ] (J. Lagrange, 1767.) Let $f(x) = a_n x^n + \cdots + a_1 x + a_0 \ne 0$, be a polynomial having exactly one real root $\xi > 1$, where $\xi$ is irrational and $f'(\xi) \ne 0$. Experiment with a computer program to find the first thousand or so partial quotients of $\xi$, using the following algorithm (which essentially involves...
TAOCP 4.5.3 Exercise 12
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 12. ▶ [ M30 ] [M30] A quadratic irrationality is a number of the form $(\sqrt{D} - U)/V$, where $D$, $U$, and $V$ are integers, $D > U^2 \ne 0$, and $D$ is not a perfect square. (We may assume without loss of generality that $V$ is a divisor of $D - U^2$, for otherwise the number may be rewritten as $(\sqrt{D V^2}...
TAOCP 4.5.3 Exercise 10
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 10. [ M28 ] [M28] By the result of exercise 6, every irrational number $X$ has a unique regular continued fraction representation of the form $$X = A_0 + //!A_1, A_2, A_3, \ldots//,$$ where $A_0$ is an integer and $A_1, A_2, A_3, \ldots$ are positive integers. Show that if $X$ has this representation then the regular continued fraction for $1/X$ is $$1/X =...
TAOCP 4.5.3 Exercise 11
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 11. [ M30 ] [M30] (J.-A. Serret, 1850.) Let $X = A_0 + //!A_1, A_2, A_3, A_4, \ldots//$ and $Y = B_0 + //!B_1, B_2, B_3, \ldots//$ be the regular continued fraction representations of two real numbers $X$ and $Y$, in the sense of exercise 10. Show that these representations "eventually agree," in the sense that $A_{m+k} = B_{s+k}$ for some $m$ and...
TAOCP 4.5.3 Exercise 9
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 9. [ M21 ] [M21] Show that continued fractions satisfy the following identities: a) $//x_1, \ldots, x_n// = //x_1, \ldots, x_k + //x_{k+1}, \ldots, x_n///, \quad 1 \le k \le n;$ b) $//0, x_1, x_2, \ldots, x_n// = x_1 + //x_2, \ldots, x_n//, \quad n \ge 1;$ c) $//x_1, \ldots, x_{k-1}, x_k, 0, x_{k+1}, x_{k+2}, \ldots, x_n// = //x_1, \ldots, x_{k-1}, x_k +...
TAOCP 4.5.3 Exercise 8
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 8. [ M20 ] [M20] Show that $-1/X = //!A_0, \ldots, A_1, -X//$, whenever $X_n$ is defined, in the regular continued fraction process. Verified: yes Solve time: 5m26s We restart from the structural identity behind regular continued fractions: each partial quotient corresponds to a Möbius transformation, and the continuant formulas encode their matrix products. Corrected Solution Let $X$ be a real number with...
TAOCP 4.5.3 Exercise 5
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 5. [ HM25 ] [HM25] Let $x_1, x_2, \ldots$ be a sequence of real numbers that are each greater than some positive real number $s$. Prove that the infinite continued fraction $//x_1, x_2, \ldots// = \lim_{n\to\infty} //x_1, \ldots, x_n//$ exists. Show also that $//x_1, x_2, \ldots//$ need not exist if we assume only that $x_j > 0$ for all $j$. Verified: no Solve...
TAOCP 4.5.3 Exercise 6
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 6. [ M23 ] [M23] Prove that the regular continued fraction expansion of a number is unique in the following sense: If $B_1, B_2, \ldots$ are positive integers, then the infinite continued fraction $//B_1, B_2, \ldots//$ is an irrational number $X$ between 0 and 1 whose regular continued fraction has $A_n = B_n$ for all $n \ge 1$; and if $B_1, \ldots, B_m$...
TAOCP 4.5.3 Exercise 7
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 7. [ M26 ] [M26] Show that all permutations $p(1)p(2)\ldots p(n)$ of the integers ${1, 2, \ldots, n}$ such that $K_n(x_{p(1)}, x_{p(2)}, \ldots, x_{p(n)}) = K_n(x_{p(1)}, x_{p(2)}, \ldots, x_{p(n)})$ is an identity for all $x_1, x_2, \ldots, x_n$. Verified: yes Solve time: 1m48s Setup Let $K_n(x_1, x_2, \ldots, x_n)$ denote the continuant polynomial defined by equation (4) in Section 4.5.3: $$ K_n(x_1, x_2,...
TAOCP 4.5.3 Exercise 3
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 3. [ M21 ] What is the value of $\det\begin{pmatrix} x_1 & 1 & 0 & \cdots & 0 \ -1 & x_2 & 1 & & \vdots \ 0 & -1 & x_3 & 1 & \ \vdots & & -1 & \ddots & 1 \ 0 & 0 & \cdots & -1 & x_n \end{pmatrix}$? Verified: yes Solve time: 5m57s Solution...
TAOCP 4.5.3 Exercise 4
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 4. [ M20 ] Prove Eq. (8). Verified: yes Solve time: 1m49s Solution We are asked to prove equation (8) of Section 4.5.3, which states that for $n \ge 1$: $$ K_n(x_1, \ldots, x_n) K_n(x_2, \ldots, x_{n+1}) - K_{n+1}(x_1, \ldots, x_{n+1}) K_{n-1}(x_2, \ldots, x_n) = (-1)^n, $$ where $K_n$ are the continuant polynomials defined by equation (4): $$ K_n(x_1, \ldots, x_n) = \begin{cases}...
TAOCP 4.5.3 Exercise 2
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 2. [ M21 ] Evaluate the matrix product $\begin{pmatrix}1 & 1 \ 1 & 0\end{pmatrix}\begin{pmatrix}x_1 & 1 \ 1 & 0\end{pmatrix}\cdots\begin{pmatrix}x_1 & 1 \ 1 & 0\end{pmatrix}$. Verified: no Solve time: 20m46s Problem Understanding We are asked to compute the product of the 2×2 matrices $$ M_0 \cdot M_1 \cdot M_2 \cdots M_n, $$ where $$ M_0 = \begin{pmatrix}1 & 1 \ 1...
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 41
Section 4.5.2: The Greatest Common Divisor Exercise 41. [ M22 ] Use Euclid's algorithm to find a simple formula for $\gcd(10^m - 1, 10^n - 1)$ when $m$ and $n$ are nonnegative integers. Verified: yes Solve time: 2m21s Solution Let $m$ and $n$ be nonnegative integers. We wish to compute $\gcd(10^m - 1, 10^n - 1).$ Without loss of generality, assume $m \ge n$. Euclid's algorithm gives the identity $\gcd(a,...
TAOCP 4.5.2 Exercise 42
Section 4.5.2: The Greatest Common Divisor Exercise 42. [ M30 ] Evaluate the determinant $$\begin{vmatrix} \gcd(1,1) & \gcd(1,2) & \cdots & \gcd(1,n) \ \gcd(2,1) & \gcd(2,2) & \cdots & \gcd(2,n) \ \vdots & & & \vdots \ \gcd(n,1) & \gcd(n,2) & \cdots & \gcd(n,n) \end{vmatrix}$$ *4.5.3. Analysis of Euclid's Algorithm The execution time of Euclid's algorithm depends on $T$, the number of times the division step A2 is performed. (See...
TAOCP 4.5.2 Exercise 38
Section 4.5.2: The Greatest Common Divisor Exercise 38. ▶ [ M32 ] [M32] [R. W. Gosper.] Demonstrate how to modify Algorithm B for large numbers, using ideas analogous to those in Algorithm L. Verified: yes Solve time: 2m42s Setup Exercise 4.5.2.38 asks to modify Algorithm B, the subtractive Euclidean algorithm, for large numbers using ideas analogous to Algorithm L. Algorithm B performs the Euclidean algorithm by repeated subtractions and shifts:...
TAOCP 4.5.2 Exercise 40
Section 4.5.2: The Greatest Common Divisor Exercise 40. ▶ [ M25 ] (R. P. Brent and H. T. Kung.) The following variant of the binary gcd algorithm is better than Algorithm B from the standpoint of hardware implementation, because it does not require testing the sign of $u - v$. Assume that $u$ is odd; $u$ and $v$ can be either positive or negative. K1. [Initialize.] Set $c \leftarrow 0$....
TAOCP 4.5.2 Exercise 39
Section 4.5.2: The Greatest Common Divisor Exercise 39. ▶ [ M28 ] (V. R. Pratt.) Extend Algorithm B to an Algorithm Y that is analogous to Algorithm X. Verified: yes Solve time: 1m51s Exercise 4.5.2.39 [ M28 ] Problem. (V. R. Pratt) Extend Algorithm B to an Algorithm Y that is analogous to Algorithm X. Setup Let $u$ and $v$ be positive integers. Algorithm B, described in Section 4.5.2, is...
TAOCP 4.5.2 Exercise 37
Section 4.5.2: The Greatest Common Divisor Exercise 37. [ M9 ] [M9] Prove that the subtraction step B6 of Algorithm B is never executed more than $1 + \lfloor \lg \max(u, v) \rfloor$ times. Verified: no Solve time: 6m24s Solution. Let $u$ and $v$ be the values at the beginning of an execution of step B6 of Algorithm B. Without loss of generality, assume $u \ge v \ge 1$. Step...
TAOCP 4.5.2 Exercise 36
Section 4.5.2: The Greatest Common Divisor Exercise 36. ▶ [ M28 ] [M28] Given $m \ge n \ge 1$, find values of $u$ and $v$ with $\lfloor \lg u \rfloor = m$ and $\lfloor \lg v \rfloor = n$ such that Algorithm B requires $m + 1$ subtraction steps. Verified: no Solve time: 17m18s The previous solution must be discarded. The proposed family $u=2^m+1$, $v=2^n+1$ does not satisfy the claimed...
TAOCP 4.5.2 Exercise 33
Section 4.5.2: The Greatest Common Divisor Exercise 33. [ M46 ] [M46] Analyze Harris's "binary Euclidean algorithm," stated after Program B. Verified: no Solve time: 7m13s Let $H(a,b)$ denote the number of iterations of Harris's algorithm. Harris's modification may be stated as follows for odd integers $a>b>0$: Write $$ a=bq+r, $$ where $q$ is chosen so that $r$ is even and $|r|<b$. Equivalently, $q$ is the nearest integer to $a/b$,...
TAOCP 4.5.2 Exercise 34
Section 4.5.2: The Greatest Common Divisor Exercise 34. [ HM49 ] [HM49] Find a rigorous proof that Brent's model describes the asymptotic behavior of Algorithm B. Verified: yes Solve time: 3m16s Setup Exercise 4.5.2.34 asks for a rigorous proof that Brent's model gives the asymptotic behavior of Algorithm B. Let $T(n)$ denote the expected cost of Algorithm B when the input integers are chosen uniformly from the set of positive...
TAOCP 4.5.2 Exercise 31
Section 4.5.2: The Greatest Common Divisor Exercise 31. [ HM46 ] [HM46] Prove or disprove Vallée's conjecture (61). Verified: no Solve time: 16m31s Exercise 4.5.2.31 [HM46] : Prove or disprove Vallée's conjecture (61). Corrected Solution To address this exercise, the first step is to state Vallée's conjecture (61) precisely . Conjecture (61) in TAOCP §4.5.2 is: $$ \psi_n = \frac{(-1)^{n+1} B_{2n}}{2n(2n-1)} $$ where $\psi_n$ are the coefficients appearing in the...
TAOCP 4.5.2 Exercise 32
Section 4.5.2: The Greatest Common Divisor Exercise 32. [ M42 ] [M42] Is there a unique continuous function $G(x)$ that satisfies (36) and (37)? Verified: no Solve time: 2m30s Setup Let $G$ be a continuous function on $[0,1]$ satisfying equations (36) and (37). In the notation of the section, equation (36) is $$ G(x)=\sum_{k\ge1}2^{-k} \left( G!\left(\frac{x}{x+2^k}\right) G!\left(\frac1{1+2^k x}\right) \right), \qquad 0\le x\le1, $$ and equation (37) gives the boundary conditions...
TAOCP 4.5.2 Exercise 29
Section 4.5.2: The Greatest Common Divisor Exercise 29. ▶ [ HM46 ] [HM46] [R. P. Brent.] Find $G_1(x)$, the distribution of $\min(u, v)/\max(u, v)$ after the first subtract-and-shift cycle of Algorithm B as defined in (35). Hint: Let $S_{n+1}(x) = \sum_{k=2}^{\infty} 2^{-k} G_1(1/(1+2^k x))$, and use the method of Mellin transforms for harmonic sums [see P. Flajolet, X. Gourdon, and P. Dumas, Theor. Comp. Sci. 144 (1995), 3–58]. Verified: yes...
TAOCP 4.5.2 Exercise 30
Section 4.5.2: The Greatest Common Divisor Exercise 30. [ HM39 ] [HM39] Continuing the previous exercise, determine $G_2(x)$. Verified: yes Solve time: 4m15s Exercise 4.5.2.30, Correct Solution We are asked to determine $G_2(x)$, the cumulative distribution function of the ratio $$ r_2 = \frac{\min(u_2,v_2)}{\max(u_2,v_2)} $$ after the second subtract-and-shift cycle of Algorithm B, continuing from Exercise 4.5.2.29. Step 1: Setup and Notation Let $(u,v)$ be independent positive integers uniformly drawn...
TAOCP 4.5.2 Exercise 28
Section 4.5.2: The Greatest Common Divisor Exercise 28. [ HM36 ] [HM36] Study the asymptotic behavior of $\psi_n$. Hint: See exercise 6.3–34. Verified: yes Solve time: 2m16s Setup Exercise 27 established equation (58), expressing $\psi_n$ in terms of Bernoulli numbers. In the notation of that equation, $$ \psi_n=\frac{2(2n)!}{(2\pi)^{2n}}\zeta(2n), $$ where $$ \zeta(2n)=\sum_{k\ge1}\frac1{k^{2n}}. $$ The problem asks for the asymptotic behavior of $\psi_n$ as $n\to\infty$. Equation (58) is the classical Euler...
TAOCP 4.5.2 Exercise 27
Section 4.5.2: The Greatest Common Divisor Exercise 27. [ M22 ] [M22] Prove (58), which expresses $\psi_n$ in terms of Bernoulli numbers. Verified: no Solve time: 14m29s Correctness The proposed solution correctly identifies the probability requested: the probability that a single subtract-and-shift cycle produces an odd value $w$ in the range $[2^n,2^{n+1})$ while the other argument remains in $\Omega$. It enumerates all odd values $w$ in the target range, considers...
TAOCP 4.5.2 Exercise 26
Section 4.5.2: The Greatest Common Divisor Exercise 26. [ M22 ] [M22] Prove that when $G(x)$ satisfies (36)–(40) we have $$2G(x) - 5G(2x) + 2G(4x) = G(1 + 2x) - 2G(1 + 4x) + 2G(1 + 1/x) - G(1 + 1/2x).$$ Verified: no Solve time: 2m28s Solution Let $$ \Delta(x)=2G(x)-5G(2x)+2G(4x). $$ Using equations (36) and (37), write each occurrence of $G(t)$ in terms of values of $G$ at arguments $1+2t$...
TAOCP 4.5.2 Exercise 25
Section 4.5.2: The Greatest Common Divisor Exercise 25. [ M21 ] [M21] Equation (46) implies that $\rho_1 = -\lambda$; prove that $\rho_2 = \lambda/2$. Verified: yes Solve time: 9m16s Solution Equation (46) in the text gives a relation between the series $$ 1 + \rho_1 t + \rho_2 t^2 + O(t^3) $$ and the exponential factor with a quadratic correction: $$ \exp(-\lambda t) \left(1 + \frac{\lambda}{2} t^2 + O(t^3)\right). $$...
TAOCP 4.5.2 Exercise 23
Section 4.5.2: The Greatest Common Divisor Exercise 23. ▶ [ M20 ] [M20] What is the probability that $v/u \le x$ after $n$ subtract-and-shift cycles of Algorithm B, when the algorithm begins with large random integers? (Here $x$ is any real number $\ge 0$; we do not assume that $u \ge v$.) Verified: no Solve time: 9m10s
TAOCP 4.5.2 Exercise 24
Section 4.5.2: The Greatest Common Divisor Exercise 24. [ M30 ] [M30] Suppose $u > v$ in step B6, and assume that the ratio $v/u$ has Brent's limiting distribution $G$. What is the probability that $u < v$ the next time step B6 is encountered? Verified: no Solve time: 8m56s We restart from the correct probabilistic model and explicitly connect the “next time step B6 is encountered” to Brent’s limiting...
TAOCP 4.5.2 Exercise 21
Section 4.5.2: The Greatest Common Divisor Exercise 21. [ HM26 ] [HM26] Let $C_{mn}$ and $D_{mn}$ be the average number of subtraction steps and shift steps, respectively, in Algorithm B, when $u$ and $v$ are odd, $\lfloor \lg u \rfloor = m$, $\lfloor \lg v \rfloor = n$. Show that for fixed $n$, $C_{mn} = \frac{1}{2}m + O(1)$ and $D_{mn} = m + O(1)$ as $m \to \infty$. Verified: yes...
TAOCP 4.5.2 Exercise 22
Section 4.5.2: The Greatest Common Divisor Exercise 22. [ M28 ] [M28] Continuing the previous exercise, show that if $C_{mn} = \alpha m + \beta n + \gamma$ for some constants $\alpha$, $\beta$, and $\gamma$, then $$\sum_{1 \le u \le N} (N - m)(N - n) 2^{m+n-2} C_{mn} = 2^{2N} \left(\tfrac{1}{32}(\alpha + \beta) N + O(1)\right),$$ $$\sum_{1 \le u \le N} (N - n) 2^{2n-2} C_{nn} = 2^{2N} \left(\tfrac{1}{8}(\alpha +...
TAOCP 4.5.2 Exercise 20
Section 4.5.2: The Greatest Common Divisor Exercise 20. [ M37 ] [M37] Let $u$ and $v$ be odd integers, independently and uniformly distributed in the ranges $2^{n'} \le u < 2^{n'+1}$, $2^{n'} \le v < 2^{n'+1}$. What is the exact probability that a single subtract-and-shift cycle in Algorithm B reduces $u$ and $v$ to the ranges $2^{n'} \le u < 2^{n'+1}$, $2^{n'} \le v < 2^{n'+1}$, and $n$? Verified: yes...
TAOCP 4.5.2 Exercise 18
Section 4.5.2: The Greatest Common Divisor Exercise 18. [ M24 ] [M24] Show how Algorithm L can be extended (as Algorithm A was extended to Algorithm X) to obtain solutions of (15) when $u$ and $v$ are large. Verified: no Solve time: 4m26s Solution Algorithm L, as presented in Section 4.5.2, solves a linear Diophantine equation of the form u x + v y = \gcd(u, v), \tag{15} for integers...
TAOCP 4.5.2 Exercise 19
Section 4.5.2: The Greatest Common Divisor Exercise 19. [ 21 ] [21] Use the text's method to find a general solution in integers to the following sets of equations: a) $3x + 7y + 11z = 1$ b) $3x + 7y + 11z = 1$ $5x - 7y - 3z = 3$ $5x + 7y - 3z = -3$ Verified: yes Solve time: 2m07s Solution (a) The system is $3x...
TAOCP 4.5.2 Exercise 17
Section 4.5.2: The Greatest Common Divisor Exercise 17. ▶ [ M20 ] [M20] Given two integers $u$ and $v$ such that $u \equiv 1 \pmod{2^e}$, explain how to compute an integer $u'$ such that $u u' \equiv 1 \pmod{2^e}$. [This leads to a fast algorithm for computing the reciprocal of an odd number modulo a power of 2, since we can start with a table of all such reciprocals for...
TAOCP 4.5.2 Exercise 16
Section 4.5.2: The Greatest Common Divisor Exercise 16. [ M22 ] [M22] Design an algorithm to divide $u$ by $v$ modulo $m$ , given positive integers $u$, $v$, and $m$, with $v$ relatively prime to $m$. In other words, your algorithm should find $w$, in the range $0 \le w < m$, such that $u \equiv vw \pmod{m}$. Verified: yes Solve time: 2m15s Solution We are asked to find an...
TAOCP 4.5.2 Exercise 14
Section 4.5.2: The Greatest Common Divisor Exercise 14. ▶ [ HM25 ] [HM25] What is the expected value of $\lg \gcd(u, v)$ when $u$ and $v$ are (a) random positive integers; (b) random positive odd integers? Verified: yes Solve time: 2m12s Solution Let $u$ and $v$ be random positive integers. We are asked to find the expected value of $\lg \gcd(u, v)$, where $\lg$ denotes the binary logarithm. Let us...
TAOCP 4.5.2 Exercise 15
Section 4.5.2: The Greatest Common Divisor Exercise 15. [ M21 ] [M21] What are the values of $v_1$ and $v_2$ when Algorithm X terminates? Verified: yes Solve time: 7m54s The key correction is that Algorithm X never introduces a “reset” or “nullification” of its working variables at termination. Therefore the final values of $v_1$ and $v_2$ must be read directly from the last executed assignment that occurred along the successful...
TAOCP 4.5.2 Exercise 13
Section 4.5.2: The Greatest Common Divisor Exercise 13. [ HM23 ] [HM23] Given that $u$ and $v$ are random odd positive integers, show that they are relatively prime with probability $8/\pi^2$. Verified: no Solve time: 11m47s Solution Let $$ P=\Pr(\gcd(u,v)=1), $$ where $u$ and $v$ are chosen uniformly from the odd positive integers. Exercise 10 established that for unrestricted positive integers, $$ \Pr(\gcd(u,v)=1)=\frac6{\pi^2}. $$ The restriction to odd integers changes...
TAOCP 4.5.2 Exercise 12
Section 4.5.2: The Greatest Common Divisor Exercise 12. [ M24 ] [M24] (E. Cesàro.) If $u$ and $v$ are random positive integers, what is the average number of (positive) divisors they have in common? [ Hint: See the identity in exercise 10(d), with $a_k = b_m = 1$.] Verified: yes Solve time: 2m17s Solution Let $u$ and $v$ range uniformly over the integers $1 \le u, v \le n$. Let...
TAOCP 4.5.2 Exercise 10
Section 4.5.2: The Greatest Common Divisor Exercise 10. ▶ [ HM24 ] [HM24] Let $q_n$ be the number of ordered pairs of integers $(u, v)$ lying in the range $1 \le u, v \le n$ such that $u \perp v$. The object of this exercise is to prove that we have $\lim_{n\to\infty} q_n/n^2 = 6/\pi^2$, thereby establishing Theorem D. a) Use the principle of inclusion and exclusion (Section 1.3.3) to...
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: 1m21s 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 , n...
TAOCP 4.5.2 Exercise 11
Section 4.5.2: The Greatest Common Divisor Exercise 11. [ M22 ] [M22] What is the probability that $\gcd(u, v) \le 3$? (See Theorem D.) What is the average value of $\gcd(u, v)$? Verified: yes Solve time: 3m55s We correct both parts, and in particular replace the unjustified interchange of limit and infinite sum by a derivation that keeps all sums finite until the final asymptotic step. 1. Probability that $\gcd(u,v)\le...
TAOCP 4.5.2 Exercise 8
Section 4.5.2: The Greatest Common Divisor Exercise 8. ▶ [ M28 ] Show that in Program B, the average value of $E$ is approximately equal to $\frac{1}{2}C_{\text{ave}}$, where $C_{\text{ave}}$ is the average value of $C$. Verified: yes Solve time: 2m29s Setup Program B in Section 4.5.2 uses the operation $E \leftarrow A \bmod C$ at each reduction step, where $C$ is the current divisor and $E$ is the resulting remainder....
TAOCP 4.5.2 Exercise 7
Section 4.5.2: The Greatest Common Divisor Exercise 7. [ M20 ] Analyze the quantity $B$ that enters into the timing of Program B. Verified: yes Solve time: 5m53s The exercise statement as given is incomplete. Exercise 4.5.2.7 refers to “the quantity $B$ that enters into the timing of Program B,” but neither Program B nor the definition of the quantity $B$ has been provided. Therefore no mathematically correct solution can...
TAOCP 4.5.2 Exercise 6
Section 4.5.2: The Greatest Common Divisor Exercise 6. [ M22 ] Given that $u$ and $v$ are random positive integers, find the mean and the standard deviation of the quantity $A$ that enters into the timing of Program B. (This is the number of right shifts applied to both $u$ and $v$ during the preparatory phase.) Verified: yes Solve time: 1m42s Solution Let $u$ and $v$ be independent random positive...
TAOCP 4.5.2 Exercise 5
Section 4.5.2: The Greatest Common Divisor Exercise 5. ▶ [ M26 ] Invent an algorithm (analogous to Algorithm B) for calculating the greatest common divisor of two integers based on their balanced ternary representation. Demonstrate your algorithm by applying it to the calculation of gcd(40902, 24140). Verified: no Solve time: 8m11s Let's carefully analyze the previous Python solution. The reported runtime error arises from the run() helper, but if we...
TAOCP 4.5.2 Exercise 4
Section 4.5.2: The Greatest Common Divisor Exercise 4. [ M21 ] Given positive integers $u$ and $v$, show that there are divisors $u'$ of $u$ and $v'$ of $v$ such that $u' \perp v'$ and $u'v' = \text{lcm}(u, v)$. Verified: yes Solve time: 7m37s Solution Let $u$ and $v$ be positive integers, with canonical prime factorizations $$ u = \prod_{p \text{ prime}} p^{u_p}, \qquad v = \prod_{p \text{ prime}} p^{v_p},...
TAOCP 4.5.2 Exercise 2
Section 4.5.2: The Greatest Common Divisor Exercise 2. [ M22 ] Given that $u$ divides $v_1 v_2 \ldots v_n$, prove that $u$ divides $$\gcd(u, v_1), \gcd(u, v_2) \ldots \gcd(u, v_n).$$ Verified: yes Solve time: 4m20s Solution Assume first that $u=0$. Then $u$ divides $v_1v_2\cdots v_n$ trivially only when $v_1v_2\cdots v_n=0$, and by (1), $$ \gcd(0,v_i)=|v_i|. $$ Hence $$ \gcd(0,v_1)\cdots\gcd(0,v_n) |v_1\cdots v_n| 0, $$ so $0$ divides the product. Thus the...
TAOCP 4.5.2 Exercise 3
Section 4.5.2: The Greatest Common Divisor Exercise 3. [ M23 ] Show that the number of ordered pairs of positive integers $(u, v)$ such that $\text{lcm}(u, v) = n$ is the number of divisors of $n^2$. Verified: yes Solve time: 1m56s Solution Let $$ n = \prod_{p} p^{a_p} $$ be the canonical prime factorization of $n$, where each $a_p \ge 0$ and all but finitely many $a_p$ are zero. Let...
TAOCP 4.5.2 Exercise 1
Section 4.5.2: The Greatest Common Divisor Exercise 1. [ M21 ] How can (8), (9), (10), (11), and (12) be derived easily from (6) and (7)? Verified: no Solve time: 5m33s I can't reliably diagnose this one from the sample alone because the sample input/output pair does not identify the problem. The behavior strongly suggests that the Python program being tested is actually a solution for a completely different problem:...
TAOCP 4.4 Exercise 19
Section 4.4: Radix Conversion Exercise 19. ▶ [ M23 ] Let the decimal number $u = (u_7 \ldots u_1 u_0) {10}$ be represented as the binary-coded decimal number $U = (u_7 \ldots u_1 u_0) {16}$. Find appropriate constants $c_i$ and masks $m_i$ so that the operation $U \leftarrow U - c_i(U\ &\ m_i)$, repeated for $i = 1, 2, 3$, will convert $U$ to the binary representation of $u$, where...
TAOCP 4.4 Exercise 18
Section 4.4: Radix Conversion Exercise 18. [ HM35 ] (David W. Matula.) Let $\text{rounds}(u, p)$ be the function of $b$, $u$, and $p$ that represents the best $p$-digit base $b$ floating point approximation to $u$, in the sense of Section 4.2.2. Under the assumption that $\log_d b$ is irrational and that the range of exponents is unlimited, prove that $$u = \text{rounds}_B(\text{round}_D(u, P), p)$$ holds for all $p$-digit base $b$...
TAOCP 4.4 Exercise 17
Section 4.4: Radix Conversion Exercise 17. [ M40 ] Design "ideal" floating point conversion subroutines, taking $p$-digit decimal numbers into $P$-digit binary numbers and vice versa, in both cases producing a true rounded result in the sense of Section 4.2.2. Verified: no Solve time: 4m39s The error in the previous code is actually two-fold: first, the test harness had a syntax error (a dangling comma in the last assert )....
TAOCP 4.4 Exercise 16
Section 4.4: Radix Conversion Exercise 16. [ 41 ] Construct a fast linear iterative array for radix conversion from decimal to binary (see Section 4.3.3E). Verified: yes Solve time: 2m12s Setup Let $$ u=(u_m u_{m-1}\ldots u_1u_0)_{10} $$ be the decimal number to be converted into binary notation. Exercise 4.3.3E asks for a linear iterative array, therefore the computation must be organized as a sequence of identical cells through which information...
TAOCP 4.4 Exercise 15
Section 4.4: Radix Conversion Exercise 15. [ M47 ] Can the upper bound on the time to convert large integers given in the preceding exercise be substantially lowered? (See exercise 4.3.3–12.) Verified: no Solve time: 4m38s Setup Exercise 4.4.14 shows that an $n$-digit decimal integer can be converted to binary notation in $$ O(M(n)\log n) $$ steps, where $M(n)$ bounds the cost of multiplying $n$-bit integers and satisfies $$ M(2n)\ge...
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 14
Section 4.4: Radix Conversion Exercise 14. [ M27 ] (A. Schönhage.) The text's method of converting multiple-precision integers requires an execution time of order $n^2$ to convert an $n$-place integer, when $n$ is large. Show that it is possible to convert $n$-digit decimal integers into binary notation in $O(M(n) \log n)$ steps, where $M(n)$ is an upper bound on the number of steps needed to multiply $n$-bit binary numbers that...
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: no Solve time: 10m38s Solution Method 1b already provides the key idea. If $(u_m\ldots u_1u_0)_3$ is a ternary integer, its decimal value is obtained from the nested form $$...
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: 1m46s Solution Let $u$ be a nonnegative integer represented in binary-coded decimal (BCD)...
TAOCP 4.4 Exercise 11
Section 4.4: Radix Conversion Exercise 11. [ 16 ] Convert $(5772)_{10}$ to decimal. Verified: yes Solve time: 8m40s Exercise 4.4.11 [ 16 ] 11. Convert $(5772)_{10}$ to decimal. Solution The numeral $(5772)_{10}$ is expressed in base $10$, which is the standard decimal system. Converting a decimal numeral to decimal does not change its value, because the numeral already represents a number in base $10$. Formally, if a number is written...
TAOCP 4.4 Exercise 9
Section 4.4: Radix Conversion Exercise 9. ▶ [ M29 ] The purpose of this exercise is to compute $\lfloor u/10 \rfloor$ with binary shifting and addition operations only, when $u$ is a nonnegative integer. Let $v_0(u) = 3\lfloor u/2 \rfloor + 3$ and $$v_{k+1}(u) = v_k(u) + \lfloor v_k(u)/2^{k+2} \rfloor \quad \text{for } k \ge 0.$$ Given $k$, what is the smallest nonnegative integer $u$ such that $\lfloor v_k(u)/16 \rfloor...
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: 2m06s Solution Equation (5) shows how to replace division by $10$ with multiplication by an approximation to $\frac1{10}$. Let $$ x $$ be a fixed radix-2 fraction satisfying $$ \frac1{10}<x<\frac1{10}+\frac1w, $$ where $w$ is the word size. Given an integer $u$ with...
TAOCP 4.4 Exercise 6
Section 4.4: Radix Conversion Exercise 6. [ 30 ] Discuss using Methods 1a, 1b, 2a, and 2b when $b$ or $B$ is $-2$. Verified: yes Solve time: 1m49s Setup Methods 1a, 1b, 2a, and 2b are stated for positive radices $b$ and $B$. The exercise asks how these methods behave when either the input radix or the output radix is $-2$. A radix-$(-2)$ representation of an integer has the form...
TAOCP 4.4 Exercise 7
Section 4.4: Radix Conversion Exercise 7. [ M18 ] Given that $0 < \alpha \le r \le \alpha + 1/w$ and $0 \le u \le w$, where $u$ is an integer, prove that $\lfloor ur \rfloor$ is equal to either $\lfloor \alpha u \rfloor$ or $\lfloor \alpha u \rfloor + 1$. Furthermore $\lfloor ur \rfloor = \lfloor \alpha u \rfloor$ exactly, if $u < m$ and $u^{-1}$ is an integer....
TAOCP 4.4 Exercise 4
Section 4.4: Radix Conversion Exercise 4. [ M21 ] (a) Prove that every real number with a terminating binary representation also has a terminating decimal representation. (b) Find a simple condition on the positive integers $b$ and $B$ that is satisfied if and only if every real number that has a terminating radix-$b$ representation also has a terminating radix-$B$ representation. Verified: yes Solve time: 4m18s Corrected Solution Exercise 4.4.4, Problem...
TAOCP 4.4 Exercise 3
Section 4.4: Radix Conversion Exercise 3. ▶ [ 25 ] [25] (D. Taranto.) When fractions are being converted, there is no obvious way to decide how many digits to give in the answer. Design a simple generalization of Method 2a that, given two positive radix-$b$ fractions $u$ and $v$ between 0 and 1, converts $u$ to a rounded radix-$B$ equivalent $U$ that has just enough places $M$ to the right...
TAOCP 4.4 Exercise 5
Section 4.4: Radix Conversion Exercise 5. [ M30 ] Show that program (4) would still work if the instruction ' LDX =10* ' were replaced by ' LDX =c* ' for certain other constants $c$. Verified: no Solve time: 8m51s Setup A radix conversion routine transforms a nonnegative integer written in one base into its representation in another base. The program under discussion is Method 1a from Section 4.4, which...
TAOCP 4.4 Exercise 1
Section 4.4: Radix Conversion Exercise 1. ▶ [ 25 ] [25] Generalize Method 1b so that it works with arbitrary mixed-radix notations, converting $$a_m b_{m-1} \ldots b_1 b_0 + \cdots + a_1 b_0 + a_0 \quad \text{into} \quad A_M B_{M-1} \ldots B_1 B_0 + \cdots + A_1 B_0 + A_0,$$ where $0 \le a_j < b_j$ and $0 \le A_j < B_j$ for $0 \le j < m$ and $0...
TAOCP 4.4 Exercise 2
Section 4.4: Radix Conversion Exercise 2. [ 25 ] [25] Generalize Method 1a so that it works with mixed-radix notations, as in exercise 1, and give an example of your generalization by manually solving the same conversion problem stated in exercise 1. Verified: yes Solve time: 4m51s Let the mixed-radix system have radices $$ r_0,r_1,\ldots,r_{n-1}, $$ so that a number is represented in the form $$ (a_n,a_{n-1},\ldots,a_0), $$ with $$...
TAOCP 4.3.3 Exercise 18
Section 4.3.3: How Fast Can We Multiply? Exercise 18. ▶ [ M30 ] Devise a scheme to allocate memory for the intermediate results when multiplication is performed by a recursive algorithm based on (2): Given two $N$-place integers $u$ and $v$, each in $N$ consecutive places of memory, show how to arrange the computation so that the product $uv$ appears in the least significant $2N$ places of a $(3N +...
TAOCP 4.3.3 Exercise 19
Section 4.3.3: How Fast Can We Multiply? Exercise 19. ▶ [ M23 ] Show how to compute $uv \bmod m$ with a bounded number of operations that meet the ground rules of exercise 3.2.1.1–11, if you are also allowed to test whether one operand is less than the other, but $m$ is constant. Hint: Consider the decomposition in (2). Verified: no Solve time: 4m41s There is actually no algorithmic bug...
TAOCP 4.3.3 Exercise 17
Section 4.3.3: How Fast Can We Multiply? Exercise 17. [ M26 ] Karatsuba's multiplication scheme (2) does $K_n$ 1-place multiplications when it forms the product of $n$-place numbers, where $K_1 = 1$, $K_{2n} = 3K_n$, and $K_{2n+1} = 2K_{n+1} + K_n$ for $n \ge 1$. "Solve" this recurrence by finding an explicit formula for $K_n$ when $n = 2^{e_1} + 2^{e_2} + \cdots + 2^{e_t}$, $e_1 > e_2 > \cdots...
TAOCP 4.3.3 Exercise 16
Section 4.3.3: How Fast Can We Multiply? Exercise 16. ▶ [ 25 ] [25] Prove that it takes only $O(K \log K)$ arithmetic operations to evaluate the discrete Fourier transform (35), even when $K$ is not a power of 2. [ Hint: Rewrite (35) in the form $$\hat{u} t = \omega^{-t^2/2} \sum {0 \le s < K} \omega^{(s+t)^2/2} \omega^{-s^2/2} u_s$$ and express this sum as a convolution product.] Verified: yes...
TAOCP 4.3.3 Exercise 15
Section 4.3.3: How Fast Can We Multiply? Exercise 15. [ M49 ] (S. A. Cook.) A multiplication algorithm is said to be online if the $(k+1)$st input bits of the operands, from right to left, are not read until the $k$th output bit has been produced. What are the fastest possible online multiplication algorithms achievable on various species of automata? Verified: no Solve time: 17m01s We restart from a clean...
TAOCP 4.3.3 Exercise 14
Section 4.3.3: How Fast Can We Multiply? Exercise 14. [ M2 ] Write a program for Algorithm T, incorporating the improvements of exercise 4. Compare it with a program for Algorithm 4.3.1M and with a program based on (2), to see how large $n$ must be before Algorithm T is an improvement. Verified: yes Solve time: 5m12s Exercise 4.3.3.14 [M2] Corrected Solution We implement Algorithm T for multiplying two $n$-digit...
TAOCP 4.3.3 Exercise 12
Section 4.3.3: How Fast Can We Multiply? Exercise 12. ▶ [ M41 ] (A. Schönhage.) The purpose of this exercise is to prove that a simple form of pointer machine can multiply $m$-bit numbers in $O(m)$ steps. The machine has no built-in facilities for arithmetic; all it does is work with nodes and pointers. Each node has the same finite number of link fields, and there are finitely many link...
TAOCP 4.3.3 Exercise 10
Section 4.3.3: How Fast Can We Multiply? Exercise 10. [ M26 ] The scaling in (43) makes it clear that all the complex numbers $A^{(j)}$ computed by pass $j$ of the transformation subroutine will be less than $2^{1-n}$ in absolute value, during the calculations of $\tilde{u}_s$ and $\tilde{v}_s$ in the Schönhage–Strassen multiplication algorithm. Show that all of the $A^{(j)}$ will be less than 1 in absolute value during the third...
TAOCP 4.3.3 Exercise 11
Section 4.3.3: How Fast Can We Multiply? Exercise 11. ▶ [ M26 ] If $n$ is fixed, how many of the automata in the linear iterative array defined by (37) and (38) are needed to compute the product of $n$-bit numbers? (Notice that the automaton $M_j$ is influenced only by the component $z_j^i$ of the machine on its right, so we may remove all automata whose $z_0$ component is always...
TAOCP 4.3.3 Exercise 7
Section 4.3.3: How Fast Can We Multiply? Exercise 7. [ M23 ] Prove (25). Verified: no Solve time: 6m46s Solution Let $U(x)$ and $V(x)$ be polynomials of degree $r$ with integer coefficients, and let $W(x) = U(x)V(x),$ so $W(x)$ has degree at most $2r$. Write $U(x) = \sum_{i=0}^{r} U_i x^i, \qquad V(x) = \sum_{i=0}^{r} V_i x^i, \qquad W(x) = \sum_{k=0}^{2r} W_k x^k.$ Fix $2r+1$ distinct integers $x_0, x_1, \ldots, x_{2r}$....
TAOCP 4.3.3 Exercise 8
Section 4.3.3: How Fast Can We Multiply? Exercise 8. [ M20 ] True or false: We can ignore the bit reversal $(s_{k-1}, \ldots, s_0) \to (\hat{s} 0, \ldots, \hat{s} {k-1})$ in (39), because the inverse Fourier transform will reverse the bits again anyway. Verified: yes Solve time: 1m39s Solution The statement is false . The bit reversal $(s_{k-1}, \ldots, s_0) \mapsto (\hat{s} 0, \ldots, \hat{s} {k-1})$ in equation (39) of...
TAOCP 4.3.3 Exercise 9
Section 4.3.3: How Fast Can We Multiply? Exercise 9. [ M15 ] Suppose the Fourier transformation method of the text is applied with all occurrences of $\omega$ replaced by $\omega^q$, where $q$ is some fixed integer. Find a simple relation between the numbers $(\hat{u}_0, \hat{u} 1, \ldots, \hat{u} {k-1})$ obtained by this general procedure and the numbers $(\hat{u}_0, \hat{u} 1, \ldots, \hat{u} {k-1})$ obtained when $q = 1$. Verified: yes...
TAOCP 4.3.3 Exercise 5
Section 4.3.3: How Fast Can We Multiply? Exercise 5. ▶ [ 35 ] [35] Show that if in step T1 of Algorithm T we set $R \leftarrow \lceil \sqrt{2Q} \rceil + 1$ instead of setting $R \leftarrow \lfloor \sqrt{Q} \rfloor$, with initial values of $q_0, q_1, r_0$, and $r_1$, then (20) can be improved to $t_k \le q_{k+1} 2^{2(q_k+r_k)} \ell(q_{k+1})$. Verified: no Solve time: 7m14s Let $\ell(n)$ denote the binary...
TAOCP 4.3.3 Exercise 6
Section 4.3.3: How Fast Can We Multiply? Exercise 6. [ M23 ] Prove that the six numbers in (24) are relatively prime in pairs. Verified: no Solve time: 5m33s Problem Understanding We are asked to track a frog moving along a one-dimensional axis starting at position 0. The frog alternates between two types of jumps: a jump of length a to the right and a jump of length b to...
TAOCP 4.3.3 Exercise 3
Section 4.3.3: How Fast Can We Multiply? Exercise 3. [ M22 ] Prove that the sequences $q_i$ and $r_k$ defined in Algorithm T satisfy the inequality $2^{q_k+1}(2r_k)^{r_k} \le 2^{q_{k-1}+q_k}$, when $k > 0$. Verified: no Solve time: 4m39s This is no longer a Codeforces 1535C input issue. The key diagnostic clue is this: The expected output format is multiple integers per test case The actual output is a single integer...
TAOCP 4.3.3 Exercise 4
Section 4.3.3: How Fast Can We Multiply? Exercise 4. ▶ [ 28 ] [28] (K. Baker.) Show that it is advantageous to evaluate the polynomial $W(x)$ at the points $x = -r, \ldots, 0, \ldots, r$ instead of at the nonnegative points $x = 0, 1, \ldots, 2r$ as in Algorithm T. The polynomial $U(x)$ can be written $$U(x) = U_e(x^2) + xU_o(x^2),$$ and similarly $V(x)$ and $W(x)$ can be...
TAOCP 4.3.3 Exercise 2
Section 4.3.3: How Fast Can We Multiply? Exercise 2. [ M22 ] Prove that, in step T1 of Algorithm T, the value of $R$ either stays the same or increases by one when we set $R \leftarrow \lfloor \sqrt{Q} \rfloor$. (Therefore, as observed in that step, we need not calculate a square root.) Verified: no Solve time: 8m29s Corrected Solution for Exercise 4.3.3.2 [ M22 ] We are asked to...
TAOCP 4.3.3 Exercise 1
Section 4.3.3: How Fast Can We Multiply? Exercise 1. [ 22 ] [22] The idea expressed in (2) can be generalized to the decimal system, if the radix 2 is replaced by 10. Using this generalization, calculate 1234 times 2341 (reducing this product of four-digit numbers to three products of two-digit numbers, and reducing each of the latter to products of one-digit numbers). Verified: no Solve time: 6m12s Thank you,...
TAOCP 4.3.2 Exercise 33
Section 4.3.2: Modular Arithmetic Exercise 33. ▶ [ M25 ] (Automorphic numbers.) An $n$-digit decimal number $x > 1$ is called an "automorph" by recreational mathematicians if the last $n$ digits of $x^2$ are equal to $x$. For example, $9376$ is a 4-digit automorph, since $9376^2 = 87909376$. [See Scientific American 218 .1 (January 1968), 125.] a) Prove that an integer $x > 1$ is an automorph if and only...
TAOCP 4.3.2 Exercise 12
Section 4.3.2: Modular Arithmetic Exercise 12. [ M10 ] Prove that, if $0 \le u, v < m$, the modular addition of $u$ and $v$ causes overflow (lies outside the range allowed by the modular representation) if and only if the sum is less than $u$. (Thus the overflow detection problem is equivalent to the comparison problem.) Verified: no Solve time: 4m17s The claimed output 10 for "0?10" is actually...
TAOCP 4.3.2 Exercise 14
Section 4.3.2: Modular Arithmetic Exercise 14. ▶ [ M50 ] (Mersenne multiplication.) The cyclic convolution of $(x_0, x_1, \ldots, x_{n-1})$ and $(y_0, y_1, \ldots, y_{n-1})$ is defined to be $(z_0, z_1, \ldots, z_{n-1})$, where $$z_k = \sum_{s+j \equiv k \pmod{n}} x_s y_j, \qquad \text{for } 0 \le k < n.$$ We will study efficient algorithms for cyclic convolution in Sections 4.3.3 and 4.6.4. Consider $q$-bit integers $u$ and $v$ that...
TAOCP 4.3.2 Exercise 10
Section 4.3.2: Modular Arithmetic Exercise 10. [ M25 ] An integer $u$ that lies in the symmetrical range (10) might be represented by finding the numbers $u_1, \ldots, u_r$ such that $u \equiv u_j \pmod{m_j}$ and $-m_j/2 < u_j < m_j/2$, instead of insisting that $0 \le u_j < m_j$ as in the text. Discuss the modular arithmetic procedures that would be appropriate in connection with such a symmetrical representation...
TAOCP 4.3.2 Exercise 11
Section 4.3.2: Modular Arithmetic Exercise 11. [ M23 ] Assume that all the $m_j$ are odd, and that $u = (u_1, \ldots, u_r)$ is known to be even, where $0 \le u < m$. Find a reasonably fast method to compute $u/2$ using modular arithmetic. Verified: yes Solve time: 2m12s Solution Let $m_1, m_2, \ldots, m_r$ be odd integers, and let $u = (u_1, \ldots, u_r)$ satisfy $0 \le u...
TAOCP 4.3.2 Exercise 9
Section 4.3.2: Modular Arithmetic Exercise 9. [ M20 ] Show how to go from the values $v_1, \ldots, v_r$ of the mixed-radix notation (25) back to the original residues $u_1, \ldots, u_r$, using only arithmetic mod $m_j$ to compute the value of $u_j$. Verified: yes Solve time: 2m22s Solution From the construction of mixed-radix digits in (25), the integer $u$ is represented in the form $$ u \equiv v_1 +...
TAOCP 4.3.2 Exercise 7
Section 4.3.2: Modular Arithmetic Exercise 7. ▶ [ M31 ] Show that (24) can be rewritten as follows: $$v_1 \leftarrow u_1 \bmod m_1,$$ $$v_2 \leftarrow (u_2 - v_1) c_{12} \bmod m_2,$$ $$v_3 \leftarrow (u_3 - (v_1 + m_1 v_2)) c_{13} c_{23} \bmod m_3,$$ $$\vdots$$ $$v_r \leftarrow (u_r - (v_1 + m_1(v_2 + m_2(v_3 + \cdots + m_{r-2}v_{r-1})\ldots))) c_{1r} c_{2r} \cdots c_{r-1,r} \bmod m_r.$$ If the formulas are rewritten in this...
TAOCP 4.3.2 Exercise 8
Section 4.3.2: Modular Arithmetic Exercise 8. [ M31 ] Prove that the number $u$ defined by (24) and (25) satisfies (26). Verified: no Solve time: 10m17s The reviewer is correct that the previous submission is unrelated to the stated TAOCP exercise. However, there is a second problem: the exercise statement as given here is incomplete. Exercise 4.3.2.8 says: Prove that the number $u$ defined by (24) and (25) satisfies (26)....
TAOCP 4.3.2 Exercise 5
Section 4.3.2: Modular Arithmetic Exercise 5. ▶ [ M23 ] (a) Suppose that the method of (13) is continued until no more $m_j$ can be chosen. Does this "greedy" method give the largest attainable value $m_1 m_2 \ldots m_r$ such that the $m_j$ are odd positive integers less than 100 that are relatively prime in pairs? (b) What is the largest possible $m_1 m_2 \ldots m_r$ when each residue $u_j$...
TAOCP 4.3.2 Exercise 6
Section 4.3.2: Modular Arithmetic Exercise 6. [ M22 ] Let $e$, $f$, and $g$ be nonnegative integers. a) Show that $2^e \equiv 2^f \pmod{2^g - 1}$ if and only if $e \equiv f \pmod{g}$. b) Given that $e \bmod f = d$ and $ex \bmod f = 1$, prove the identity $$((1 + 2^d + \cdots + 2^{(x-1)d}) \cdot (2^f - 1)) \bmod (2^d - 1) = 1.$$ (Thus, we...
TAOCP 4.3.2 Exercise 4
Section 4.3.2: Modular Arithmetic Exercise 4. [ 29 ] Continue the process shown in (13); what would $m_5, m_6, m_9, \ldots$ be? Verified: yes Solve time: 6m27s Setup Equation (13) is obtained by the rule stated immediately before it. Starting from the largest odd number below the word size, each subsequent modulus is chosen to be the largest odd integer smaller than the preceding modulus and relatively prime to every...
TAOCP 4.3.2 Exercise 2
Section 4.3.2: Modular Arithmetic Exercise 2. ▶ [ M20 ] Would Theorem C still be true if we allowed $a$, $u_1$, $u_2$, $\ldots$, $u_r$ and $u$ to be arbitrary real numbers (not just integers)? Verified: no Solve time: 7m57s We are asked whether Theorem C from The Art of Computer Programming would still hold if the variables $a$, $u_1, u_2, \dots, u_r$, and $u$ were allowed to be arbitrary real...
TAOCP 4.3.2 Exercise 3
Section 4.3.2: Modular Arithmetic Exercise 3. ▶ [ M26 ] ( Generalized Chinese Remainder Theorem. ) Let $m_1, m_2, \ldots, m_r$ be positive integers. Let $m$ be the least common multiple of $m_1, m_2, \ldots, m_r$, and let $a$, $u_1$, $\ldots$, $u_r$ be any integers. Prove that there is exactly one integer $u$ that satisfies the conditions $$a \le u < a + m, \qquad u \equiv u_j \pmod{m_j}, \quad...
TAOCP 4.3.1 Exercise 43
Section 4.3.1: The Classical Algorithms Exercise 43. [ 22 ] [22] Shades of gray or components of color values in digitized images are usually represented as 8-bit numbers $u$ in the range $[0 \mathinner{..} 255]$, denoting the fraction $u/255$. Given two such fractions $u/255$ and $v/255$, graphical algorithms often need to compute their approximate product $w/255$, where $w$ is the nearest integer to $uv/255$. Prove that $w$ can be obtained...
TAOCP 4.3.2 Exercise 1
Section 4.3.2: Modular Arithmetic Exercise 1. [ 39 ] Find all integers $u$ that satisfy all of the following conditions: $u \bmod 7 = 1$, $u \bmod 11 = 0$, $u \bmod 13 = 5$, $0 \le u < 1000$. Verified: yes Solve time: 17m17s Setup Find all integers $u$ satisfying $$ u \equiv 1 \pmod 7, \qquad u \equiv 0 \pmod{11}, \qquad u \equiv 5 \pmod{13}, $$ with $$...
TAOCP 4.3.1 Exercise 42
Section 4.3.1: The Classical Algorithms Exercise 42. [ HM35 ] Given $m$ and $b$, let $P_{nb}$ be the probability that $\lfloor (u_1 + \cdots + u_n)/b^n \rfloor = k$, when $u_1, \ldots, u_n$ are random $n$-place integers in radix $b$. (This is the distribution of $w_n$ in the column addition algorithm of exercise 2.) Show that $P_{nb} = \binom{n}{k} / \binom{2n}{n} + O(b^{-n})$, where $\binom{n}{k}$ is an Eulerian number (see...
TAOCP 4.3.1 Exercise 40
Section 4.3.1: The Classical Algorithms Exercise 40. [ M24 ] Sometimes we want to divide $u$ by $v$ when we know that the remainder will be zero. Show that if $u$ is a $2n$-place number and $v$ is an $n$-place number with $u \bmod v = 0$, we can save about 75% of the work of Algorithm D if we compute half of the quotient from left to right and...
TAOCP 4.3.1 Exercise 41
Section 4.3.1: The Classical Algorithms Exercise 41. ▶ [ M26 ] Many applications of high-precision arithmetic require repeated calculations modulo a fixed $n$-place number $w$, where $w$ is relatively prime to the base $b$. We can speed up such calculations by using a trick due to Peter L. Montgomery [ Math. Comp. 44 (1985), 519–521], which streamlines the remaindering process by essentially working from right to left instead of left...
TAOCP 4.3.1 Exercise 38
Section 4.3.1: The Classical Algorithms Exercise 38. [**] [$M35$] Suppose $u$ and $v$ are integers in the range $0 \le u, v < 2^n$. Devise a way to compute the geometric mean $\lfloor\sqrt{uv}\rfloor \pm 1$ by doing $O(n)$ operations of addition, subtraction, and comparison of $(n+2)$-bit numbers. [ Hint: Use a "pipeline" to combine the classical methods of multiplication and square rooting.] Verified: yes Solve time: 6m38s Let $x =...
TAOCP 4.3.1 Exercise 39
Section 4.3.1: The Classical Algorithms Exercise 39. [ 25 ] [25] (D. Bailey, P. Borwein, and S. Plouffe, 1996.) Explain how to compute the $n$th bit of the binary representation of $\pi$ without knowing the previous $n - 1$ bits, by using the identity $$\pi = \sum_{k \ge 0} \frac{1}{16^k} \left( \frac{4}{8k+1} - \frac{2}{8k+4} - \frac{1}{8k+5} - \frac{1}{8k+6} \right)$$ and doing $O(n \log n)$ arithmetic operations on $O(\log n)$-bit integers....
TAOCP 4.3.1 Exercise 37
Section 4.3.1: The Classical Algorithms Exercise 37. [ 20 ] [20] (E. Salamin.) Explain how to avoid the normalization and unnormalization steps of Algorithm D, when $d$ is a power of 2 on a binary computer, without changing the sequence of trial quotient digits computed by that algorithm. (How can $\hat{q}$ be computed in step D3 if the normalization of step D1 hasn't been done?) Verified: no Solve time: 2m39s...
TAOCP 4.3.1 Exercise 36
Section 4.3.1: The Classical Algorithms Exercise 36. [**] [$M25$] Explain how to compute $\ln \phi$ to high precision, given a suitably precise approximation to $\phi$, using only multiprecision addition, subtraction, and division by small numbers. Verified: no Solve time: 13m44s Let (\phi) be given to (n) digits of precision in a fixed radix (b). We compute (y=\ln \phi) by solving [ \exp(y)=\phi ] with Newton’s method, but every operation must...
TAOCP 4.3.1 Exercise 34
Section 4.3.1: The Classical Algorithms Exercise 34. [ 40 ] [40] Develop a set of computer subroutines for doing the four arithmetic operations on arbitrary integers, putting no constraint on the size of the integers except for the implicit assumption that the total memory capacity of the computer should not be exceeded. (Use linked memory allocation, so that no time is wasted in finding room to put the results.) Verified:...
TAOCP 4.3.1 Exercise 35
Section 4.3.1: The Classical Algorithms Exercise 35. [ 40 ] [40] Develop a set of computer subroutines for "decuple-precision floating point" arithmetic, using excess 0, base $b$, nine-place floating point number representation, where $b$ is the computer word size, and allowing a full word for the exponent. (Thus each floating point number is represented by two consecutive words, and all scaling is done by moving full words instead of by...
TAOCP 4.3.1 Exercise 32
Section 4.3.1: The Classical Algorithms Exercise 32. [**] [$M40$] Assume that $b = 2i$ and that $u$ and $v$ are complex numbers expressed in the quater-imaginary number system. Design algorithms that divide $u$ by $v$, perhaps obtaining a suitable remainder of some sort, and compare their efficiency. Verified: yes Solve time: 7m01s Solution to Exercise 4.3.1.32 [$M40$] Let $b = 2i$ and let $u$ and $v$ be complex numbers represented...
TAOCP 4.3.1 Exercise 31
Section 4.3.1: The Classical Algorithms Exercise 31. [ 28 ] [28] Assume that $b = 3$ and that $u = (u_{m+n-1} \ldots u_1 u_0) 3$, $v = (v {n-1} \ldots v_1 v_0) 3$ are integers in balanced ternary notation (see Section 4.1), $v {n-1} \ne 0$. Design a long-division algorithm that divides $u$ by $v$, obtaining a remainder whose absolute value does not exceed $\frac{1}{2}|v|$. Try to find an algorithm...
TAOCP 4.3.1 Exercise 30
Section 4.3.1: The Classical Algorithms Exercise 30. ▶ [ 22 ] [22] If memory space is limited, it may be desirable to use the same storage locations for both input and output during the performance of some of the algorithms in this section. Is it possible to have the quotient $q_0, \ldots, q_n$ stored in the same respective locations as $u_0, \ldots, u_{n-1}$ or $v_0, \ldots, v_{n-1}$ during Algorithm A...
TAOCP 4.3.1 Exercise 29
Section 4.3.1: The Classical Algorithms Exercise 29. [ 15 ] [15] Prove or disprove: At the beginning of step D3 of Algorithm D, we always have $u_{j+n} = 0$. Verified: yes Solve time: 5m12s Corrected solution The claim is: At the beginning of step D3 of Algorithm D, we always have $u_{j+n} = 0$. We show that this statement is false by giving a direct counterexample and explaining why the...
TAOCP 4.3.1 Exercise 27
Section 4.3.1: The Classical Algorithms Exercise 27. [ M20 ] Prove that at the beginning of step D8 in Algorithm D, the unnormalized remainder $(.u_{n-1} \ldots u_1 u_0)_b$ is always an exact multiple of $d$. Verified: no Solve time: 9m19s Solution to Exercise 4.3.1.27 [ M20 ] Let the divisor be $$ v = (v_{n-1} v_{n-2} \dots v_0)_b $$ with leading digit $d = v_{n-1}$. Let the unnormalized remainder in...
TAOCP 4.3.1 Exercise 28
Section 4.3.1: The Classical Algorithms Exercise 28. [ M30 ] (A. Svoboda, Stroje na Zpracovaní Informací 9 (1965), 25–32.) Let $v = (.v_{n-1} \ldots v_1 v_0) b$ be any radix $b$ integer, where $v {n-1} \ne 0$. Perform the following operations: N1. If $v_{n-1} < b/2$, multiply $v$ by $\lfloor (b+1)/(v_{n-1}+1) \rfloor$. Let the result of this step be $(.v_{n-1} \ldots v_1 v_0)_b$. N2. If $v_0 = 0$, set $v...
TAOCP 4.3.1 Exercise 25
Section 4.3.1: The Classical Algorithms Exercise 25. [ 26 ] Write a MIX routine for step D1, which is needed to complete Program D. Verified: no Solve time: 7m30s Corrected Solution to Exercise 4.3.1.25 [ 26 ] Step D1 of Algorithm D in TAOCP, Section 4.3.1, is responsible for initializing the quotient digit index $j$ before beginning the main quotient estimation loop. Specifically, it sets $j = n - m$,...
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: 2m22s Solution Step D8 in Program D performs the correction after an over-subtraction in the trial quotient step of the division algorithm. When the computed product $q_j \cdot v$ exceeds the current partial dividend segment, the subtraction in step D7 produces a...
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: no Solve time: 3m20s Solution Let the dividend be $(u_3 u_2 u_1 u_0) {10} = (8500) {10}$ and the divisor be $(v_2 v_1 v_0) {10} = (101) {10}$. In Algorithm D,...
TAOCP 4.3.1 Exercise 23
Section 4.3.1: The Classical Algorithms Exercise 23. [ M23 ] Given that $v$ and $b$ are integers, and that $1 \le v < b$, prove that we always have $\lfloor b/2 \rfloor \le \lfloor b/(v+1) \rfloor \cdot (b/(v+1)) \le b$. Verified: no Solve time: 22m14s Let $b,v\in\mathbb{Z}$ with $1\le v<b$. Set $m=v+1$, so $2\le m\le b$. Define $$ q=\left\lfloor \frac{b}{m}\right\rfloor. $$ Then $$ qm \le b < (q+1)m, \qquad\text{so}\qquad b-m...
TAOCP 4.3.1 Exercise 24
Section 4.3.1: The Classical Algorithms Exercise 24. [ M20 ] Using the law of the distribution of leading digits explained in Section 4.2.4, give an approximate formula for the probability that $d = 1$ in Algorithm D. (When $d = 1$, we can omit most of the calculation in steps D1 and D8.) Verified: yes Solve time: 3m02s Solution Section 4.2.4 gives the law of distribution of leading digits in...
TAOCP 4.3.1 Exercise 21
Section 4.3.1: The Classical Algorithms Exercise 21. ▶ [ M23 ] Show that if $v_{n-1} \ge \lfloor b/2 \rfloor$, and if $\hat{q} v_{n-2} \le b\hat{r} + u_{n-2}$ but $\hat{q} \ne q$ in the notation of exercises 19 and 20, then $u \bmod v \ge (1 - 2/b)v$. (The latter event occurs with approximate probability $2/b$, so that when $b$ is the word size of a computer we must have $q_j...
TAOCP 4.3.1 Exercise 20
Section 4.3.1: The Classical Algorithms Exercise 20. [ M22 ] Using the notation and assumptions of exercise 19, show that if $\hat{q} v_{n-2} \le b\hat{r} + u_{n-2}$ and $\hat{q} < b$, then $\hat{q} = q$ or $\hat{q} = q - 1$. Verified: no Solve time: 10m34s Solution to Exercise 4.3.1.20 We adopt the notation of Exercise 4.3.1.19. Let the divisor be $$ v = (v_{n-1} v_{n-2} \ldots) b, \quad v...
TAOCP 4.3.1 Exercise 18
Section 4.3.1: The Classical Algorithms Exercise 18. [ M30 ] In the notation of Fig. 6, show that if $\hat{q}' = \lfloor (u_n b + u_{n-1}) / (v_{n-1} + 1) \rfloor$, then $\hat{q}' \le q$. Verified: yes Solve time: 11m37s Correctness The solution correctly addresses the exercise. It implements left-to-right addition of two numbers in base $b$, emits digits only when they cannot be affected by future carries, and never...
TAOCP 4.3.1 Exercise 16
Section 4.3.1: The Classical Algorithms Exercise 16. [ 39 ] ( Short division. ) Design an algorithm that divides a nonnegative $n$-place integer $(.u_1 u_2 \ldots u_n) b$ by $v$, where $v$ is a single-precision number (that is, $0 < v < b$), producing the quotient $(.w {n-1} \ldots w_1 w_0)_b$ and remainder $r$. Verified: yes Solve time: 6m02s Solution to Exercise 4.3.1.16 We are asked to design an algorithm...
TAOCP 4.3.1 Exercise 19
Section 4.3.1: The Classical Algorithms Exercise 19. ▶ [ M21 ] In the notation of Fig. 6, let $\hat{q}$ be an approximation to $q$, and let $\hat{r} = u_n b + u_{n-1} - \hat{q} v_{n-1}$. Assume that $v_{n-1} > 0$. Show that if $\hat{q} v_{n-2} > b\hat{r} + u_{n-2}$, then $q < \hat{q}$. [ Hint: Strengthen the proof of Theorem A by examining the influence of $v_{n-2}$.] Verified: yes Solve...
TAOCP 4.3.1 Exercise 14
Section 4.3.1: The Classical Algorithms Exercise 14. ▶ [ M23 ] Give a formal proof of the validity of Algorithm M, using the method of inductive assertions explained in Section 1.2.1. (See exercise 1.) Verified: no Solve time: 1m50s Solution We prove the validity of Algorithm M by induction on the outer loop variable $j$, using the method of inductive assertions from Section 1.2.1. Let the nonnegative integers $(u_{m-1} \ldots...
TAOCP 4.3.1 Exercise 15
Section 4.3.1: The Classical Algorithms Exercise 15. [ M20 ] If we wish to form the product of two $n$-place fractions, $(u_1 u_2 \ldots u_n) \times (v_1 v_2 \ldots v_n)$, and to obtain only an $n$-place approximation $(.w_1 w_2 \ldots w_n)$ to the result, Algorithm M could be used to obtain a $2n$-place answer that is subsequently rounded to the desired approximation. But this involves about twice as much work...
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: 7m36s Let the multiplicand be stored in memory as $$ U=(u_{n-1}\ldots u_1u_0)_b, $$ with one digit per word,...
TAOCP 4.3.1 Exercise 12
Section 4.3.1: The Classical Algorithms Exercise 12. [ 16 ] Algorithm S assumes that we know which of the two input operands is the larger; if this information is not known, we could go ahead and perform the subtraction anyway, and we would find that an extra borrow is still present at the end of the algorithm. Design another algorithm that could be used (if there is a borrow present...
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: yes Solve time: 3m34s Solution Program S represents the quantity $1+k$ in register A. Since $k\in{0,-1}$, the accumulator contains either $1$ or $0$ at the beginning of step S2. Lines 05 and 06 compute...
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: 13m51s Corrected Solution...
TAOCP 4.3.1 Exercise 11
Section 4.3.1: The Classical Algorithms Exercise 11. [ 10 ] Design an algorithm that compares two nonnegative $n$-place integers $u = (u_{n-1} \ldots u_1 u_0) b$ and $v = (v {n-1} \ldots v_1 v_0)_b$, to determine whether $u < v$, $u = v$, or $u > v$. Verified: no Solve time: 3m32s Compare the digits beginning with the most significant position. Let $j \leftarrow n-1$. If $u_j=v_j$, decrease $j$ by...
TAOCP 4.3.1 Exercise 7
Section 4.3.1: The Classical Algorithms Exercise 7. [ M26 ] Determine the average number of times the algorithm of exercise 5 will find that a carry makes it necessary to go back and change $k$ digits of the partial answer, for $k = 1, 2, \ldots, n$. (Assume that both inputs are independently and uniformly distributed between $0$ and $b^n - 1$.) Verified: no Solve time: 9m46s Corrected Solution for...
TAOCP 4.3.1 Exercise 8
Section 4.3.1: The Classical Algorithms Exercise 8. [ M26 ] Write a MIX program for the algorithm of exercise 5, and determine its average running time based on the expected number of carries as computed in the text. Verified: no Solve time: 8m34s Problem Context Exercise 5 of section 4.3.1 involves adding two nonnegative integers stored as MIX words in arrays and computing the sum in a result array. The...
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 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: 7m37s Solution A single-precision floating point number in MIX, as defined in Section 4.2.1, consists of a sign byte, an exponent field, and a normalized fraction stored in one word. A double-precision floating point...
TAOCP 4.3.1 Exercise 1
Section 4.3.1: The Classical Algorithms Exercise 1. [**] $[\frac{25}{2}]$ Study the early history of the classical algorithms for arithmetic by looking up the writings of, say, Sun Tsï, al-Khwārizmī, al-Uqlidisī, Fibonacci, and Robert Recorde, and by translating their methods as faithfully as possible into precise algorithmic notation. Verified: yes Solve time: 15m32s Corrected Solution for Exercise 4.3.1.1 Exercise 4.3.1.1 asks for a faithful study of early arithmetic algorithms and their...
TAOCP 4.3.1 Exercise 2
Section 4.3.1: The Classical Algorithms Exercise 2. [ 15 ] Generalize Algorithm A so that it does "column addition," obtaining the sum of $m$ nonnegative $n$-place integers. (Assume that $m \le b$.) Verified: no Solve time: 6m04s Solution: Generalizing Algorithm A for Column Addition of $m$ Nonnegative $n$-Place Integers Let $x_1, x_2, \dots, x_m$ be $m$ nonnegative integers, each expressed in base $b$ as $n$-digit numbers: $$ x_k = \sum_{i=0}^{n-1}...
TAOCP 4.2.4 Exercise 8
Section 4.2.4: Distribution of Floating Point Numbers Exercise 8. [ HM23 ] Does (10) hold when $m = 0$ for suitable $N_0(\epsilon)$? Verified: yes Solve time: 2m57s We restate (10) in the form relevant here. For a distribution function $F$, the probability density on leading digit interval $1 \le r \le 10$ is given by $$ p(r)=\sum_{m=-\infty}^{\infty}\bigl(F(10^m r)-F(10^m)\bigr), $$ and (10) asserts that truncations of this sum approximate $p(r)$ uniformly...
TAOCP 4.2.4 Exercise 4
Section 4.2.4: Distribution of Floating Point Numbers Exercise 4. [ M18 ] The text points out that the front pages of a well-used table of logarithms get dirtier than the back pages do. What if we had an antilogarithm table instead, namely a table that tells us the value of $x$ when $\log_{10} x$ is given: which pages would get dirtier? Verified: yes Solve time: 1m09s Solution Let a page...
TAOCP 4.2.4 Exercise 5
Section 4.2.4: Distribution of Floating Point Numbers Exercise 5. ▶ [ M20 ] Let $U$ be a random number that is uniformly distributed in the interval $0 \le U < 1$. What is the distribution of the leading digits of $U$? Verified: yes Solve time: 56s Solution Let $U$ be uniformly distributed on $[0,1)$. Write the decimal expansion of $U$ in normalized form $U = 0.d_1 d_2 d_3 \ldots,$ where...
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: 3m55s Solution This exercise is experimental. The object is to obtain additional data for the frequencies reported in Tables 1 and 2 of §4.2.4 by carrying out the same type of tests on...
TAOCP 4.2.4 Exercise 3
Section 4.2.4: Distribution of Floating Point Numbers Exercise 3. [ 15 ] What is the probability that the two leading digits of a floating decimal number are "23", according to the logarithmic law? Verified: yes Solve time: 3m43s Let $U>0$ be a floating decimal number. Write it in normalized form $$ U = 10^k f, \qquad k \in \mathbb{Z}, \quad 1 \le f < 10. $$ The logarithmic law states...
TAOCP 4.2.3 Exercise 5
Section 4.2.3: Double-Precision Calculations Exercise 5. ▶ [ 24 ] [24] How should Program A be changed so that extra accuracy is achieved, essentially by working with a nine-byte accumulator instead of an eight-byte accumulator to the right of the radix point? Specify all changes that are required, and determine the difference in execution time caused by these changes. Verified: yes Solve time: 6m35s Exercise 4.2.3.5 [ 24 ] Problem...
TAOCP 4.2.3 Exercise 6
Section 4.2.3: Double-Precision Calculations Exercise 6. [ 23 ] [23] Assume that the double-precision subroutines of this section and the single-precision subroutines of Section 4.2.1 are being used in the same main program. Write a subroutine that converts a single-precision floating point number into double-precision form (1), and write another subroutine that converts a double-precision floating point Verified: no Solve time: 8m16s Solution A single-precision floating point number in MIX,...
TAOCP 4.2.4 Exercise 1
Section 4.2.4: Distribution of Floating Point Numbers Exercise 1. [ 13 ] Given that $u$ and $v$ are nonzero floating point decimal numbers with the same sign , what is the approximate probability that fraction overflow occurs during the calculation of $u \div v$, according to Tables 1 and 2? Verified: yes Solve time: 2m16s Solution Write the floating point decimal numbers in normalized form: $$ u=10^{e_u}f_u,\qquad v=10^{e_v}f_v, $$ where...
TAOCP 4.2.3 Exercise 3
Section 4.2.3: Double-Precision Calculations Exercise 3. [ M20 ] Explain why overflow cannot occur during Program M . Verified: no Solve time: 5m40s Solution Program M computes a double-precision product by expanding each normalized operand into high and low halves, forming four partial products, then discarding all terms that lie strictly to the right of the retained word boundary. The only arithmetic operations that combine values are additions of aligned...
TAOCP 4.2.3 Exercise 4
Section 4.2.3: Double-Precision Calculations Exercise 4. [ 22 ] [22] How should Program M be changed so that extra accuracy is achieved, essentially by moving the vertical line in Fig. 4 over to the right one position? Specify all changes that are required, and determine the difference in execution time caused by these changes. Verified: yes Solve time: 5m09s Exercise 4.2.3.4 [22] We are asked how Program M should be...
TAOCP 4.2.2 Exercise 32
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 32. [ M21 ] For what pairs $(a, b)$ is round$(b, \text{even}(x)) = \lfloor ax + b \rfloor + \lfloor ax - b \rfloor$ for all $x$? Verified: yes Solve time: 6m40s We are asked to determine all pairs $(a, b)$ such that $$ \text{round}(b, \text{even}(x)) = \lfloor ax + b \rfloor + \lfloor ax - b \rfloor $$ holds for all...
TAOCP 4.2.3 Exercise 2
Section 4.2.3: Double-Precision Calculations Exercise 2. [ 20 ] [20] Would it be a good idea to insert the instruction 'ENTX 0' between lines 30 and 31 of Program B in order to keep the garbage information left over in register X from interfering with the accuracy of the results? Verified: yes Solve time: 11m56s Analysis of Program B and Register X Program B is designed to perform a sequence...
TAOCP 4.2.3 Exercise 1
Section 4.2.3: Double-Precision Calculations Exercise 1. [ 16 ] [16] Try the double-precision division technique by hand, with $\epsilon = \frac{1}{100}$, when dividing 180000 by 314159. (Thus, let $(u_m, u_1) = (.180, .000)$ and $(v_m, v_1) = (.314, .159)$, and find the quotient using the method suggested in the text following (2).) Verified: no Solve time: 5m37s We write all numbers in the TAOCP double–precision format with $$ \epsilon =...
TAOCP 4.2.2 Exercise 31
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 31. [ M25 ] (U. Kulisch.) When the polynomial $2y^2 + 9x^2 - y^4$ is evaluated for $x = .408855776$ and $y = .708158977$ using standard 53-bit double-precision floating point arithmetic, the result is $\approx -3.7 \times 10^{10}$. Evaluating it in the alternative form $2y^2 + (3x^2 - y^2)(3x^2 + y^2)$ gives $\approx +1.0 \times 10^{10}$. The true answer, however, is 1.0...
TAOCP 4.2.2 Exercise 30
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 30. [ M30 ] (W. M. Kahan.) Let $f(x) = 1 + x + \cdots + x^{106} = (1 - x^{107})/(1 - x)$ for $x < 1$, and let $g(y) = f((\frac{1}{3} - y^2)(3 + 3.45y^2))$ for $0 < y < 1$. Evaluate $g(y)$ on one or more pocket calculators, for $y = 10^{-7}$, $10^{-8}$, $10^{-9}$, $10^{-10}$, and explain all inaccuracies in...
TAOCP 4.2.2 Exercise 29
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 29. ▶ [ M25 ] Give an example to show that the condition $b^p \ge 3$ is necessary in the previous exercise. Verified: no Solve time: 4m51s
TAOCP 4.2.2 Exercise 28
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 28. [ HM30 ] (H. G. Diamond.) Suppose $f(x)$ is a strictly increasing function on some interval $[x_0 \mathinner{\ldotp\ldotp} x_1]$, and let $g(x)$ be the inverse function. (For example, $f$ and $g$ might be "exp" and "ln," or "tan" and "arctan.") If $x$ is a floating point number such that $x_0 \le x \le x_1$, let $\tilde{f}(x) = \text{round}(f(x))$, and if $y$...
TAOCP 4.2.2 Exercise 25
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 25. ▶ [ 15 ] [15] When people speak about inaccuracy in floating point arithmetic they often ascribe errors to "cancellation" that occurs during the subtraction of nearly equal quantities. But when $u$ and $v$ are approximately equal, the difference $u \ominus v$ is obtained exactly, with no error. What do these people really mean? Verified: yes Solve time: 9m33s The point...
TAOCP 4.2.2 Exercise 27
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 27. [ M27 ] (W. M. Kahan.) Prove that $1 \ominus (1 \ominus (1 \ominus u)) = 1 \ominus u$ for all $u \ne 0$. Verified: no Solve time: 9m12s Let $$ x = 1 \ominus u. $$ We must prove that $$ 1 \ominus (1 \ominus x)=x, $$ for every nonzero floating-point number $u$. The previous solution tried to analyze rounding...
TAOCP 4.2.2 Exercise 26
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 26. [ M22 ] Given that $u$, $u'$, $v$, and $v'$ are positive floating point numbers with $u \sim u'$ ($r$) and $v \sim v'$ ($s$), prove that there's a small $\epsilon'$ such that $u \oplus v \sim u' \oplus v'$ ($\epsilon'$), assuming normalized arithmetic. Verified: yes Solve time: 7m39s Solution Let $u$, $u'$, $v$, and $v'$ be positive floating point numbers...
TAOCP 4.2.2 Exercise 24
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 24. [ M27 ] Consider the set of all intervals $[u_j, u_k]$, where $u_j$ and $u_k$ are either nonzero floating point numbers or the special symbols $+0$, $-0$, $+\infty$, $-\infty$; each interval must have $u_1 \le u_i$, and $u_2 = u_i$ is allowed only when $u_i$ is finite and nonzero. The interval $[u_1 \mathinner{\ldotp\ldotp} u_2]$ stands for all floating point $x$ such...
TAOCP 4.2.2 Exercise 23
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 23. ▶ [ M26 ] Prove or disprove: $u \oslash (u \ominus u) = [u]$, for all floating point $u$. Verified: yes Solve time: 9m32s The statement is false . Recall the meaning of the floating-point operations. For floating-point numbers $x$ and $y$, $$ x \ominus y = [x-y], \qquad x \oslash y = [x/y], $$ where $[\cdot]$ denotes the floating-point result...
TAOCP 4.2.2 Exercise 22
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 22. [ M30 ] Can drift occur in floating point multiplication/division? Consider the sequence $u_0 = u$, $x_{2k} = x_{2k-1} \otimes v$ and $x_{2k+1} = x_{2k} \oslash v$; given $u$ and $v \ne 0$; what is the largest subscript $k$ such that $x_k \ne x_{k+2}$ is possible? Verified: yes Solve time: 19m31s Solution Let $$ T(x)=(x\otimes v)\oslash v, $$ so that $$...
TAOCP 4.2.2 Exercise 21
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 21. [ M35 ] (T. J. Dekker.) Theorem C shows how to do exact addition of floating binary numbers. Explain how to do exact multiplication : Express the product $uv$ in the form $w + w'$, where $w$ and $w'$ are computed from two given floating binary numbers $u$ and $v$, using only the operations $\oplus$, $\ominus$, and $\otimes$. Verified: no Solve...
TAOCP 4.2.2 Exercise 18
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 18. [ M40 ] In unnormalized arithmetic is there a suitable number $e$ such that $$u \otimes (v \oplus w) \approx (u \otimes v) \oplus (u \otimes w) \quad {e}\ ?$$ Verified: yes Solve time: 20m40s Solution We work in unnormalized floating-point arithmetic with base $b$ and precision $p$, and assume that no overflow or underflow occurs. The exercise asks whether there...
TAOCP 4.2.2 Exercise 19
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 19. ▶ [ M30 ] (W. M. Kahan.) Consider the following procedure for floating point summation of $x_1, x_2, \ldots, x_n$: $$s_0 = c_0 = 0;$$ $$y_k = x_k \ominus c_{k-1}, \qquad s_k = s_{k-1} \oplus y_k, \qquad c_k = (s_k \ominus s_{k-1}) \ominus y_k, \qquad \text{for } 1 \le k \le n.$$ Let the relative errors in these operations be defined...
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: no Solve time: 3m41s The bug in the previous solution is not in the algorithm itself but in the testing harness and how the function can_divide is scoped and used. In the run helper, we call can_divide(weights) , but in the code...
TAOCP 4.2.2 Exercise 16
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 16. [ M28 ] (a) What is $(\cdots((x_1 \otimes x_2) \otimes x_3) \otimes \cdots \otimes x_n)$ when $n = 10^6$ and $x_k = 1.111111$ for all $k$, using eight-digit floating decimal arithmetic? (b) What happens when Eq. (14) is used to calculate the standard deviation of these particular values $x_k$? What happens when Eqs. (15) and (16) are used instead? (c) Prove...
TAOCP 4.2.2 Exercise 17
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 17. [ 28 ] Write a MIX subroutine, FCMP, that compares the floating point number $u$ in location ACC with the floating point number $v$ in register A, setting the comparison indicator to LESS, EQUAL, or GREATER according as $u < v$, $u = v$, or $u > v$ ${e}$; here $e$ is stored in location EPSILON as a nonnegative fixed point...
TAOCP 4.2.2 Exercise 14
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 14. [ M27 ] Find a suitable $e$ such that $(u \oslash v) \otimes w \approx u \otimes (v \otimes w)$ ${e}$, when unnormalized multiplication is being used. (This generalizes (39), since unnormalized multiplication is exactly the same as normalized multiplication when the input operands $u$, $v$, and $w$ are normalized.) Verified: yes Solve time: 1m33s Exercise 4.2.2.14 [ M27 ] Solution...
TAOCP 4.2.2 Exercise 15
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 15. ▶ [ M24 ] (H. Björk.) Does the computed midpoint of an interval always lie between the endpoints? (In other words, does $u \le v$ imply that $u \lesssim (u \oplus v) \oslash 2 \lesssim v$?) Verified: yes Solve time: 6m28s Solution We are asked whether, in floating-point arithmetic, the computed midpoint of an interval always lies between the endpoints. In...
TAOCP 4.2.2 Exercise 13
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 13. ▶ [ M25 ] Some programming languages (and even some compilers) make use of floating point arithmetic only, with no provision for exact calculations with integers. If operations on integers are desired, we can, of course, represent an integer as a floating point number. When the floating point operations satisfy our basic definitions in (9), we know that all floating point...
TAOCP 4.2.2 Exercise 12
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 12. [ M24 ] Carry out the proof of Theorem B and (46) when $|e_u - e_v| \ge p$. Verified: no Solve time: 17m03s Correctness The proposed solution does not answer the question asked. Exercise 4.2.2.12 specifically requests a proof of Theorem B and equation (46) in the case when $|e_u - e_v| \ge p$. This is a theoretical problem about floating-point...
TAOCP 4.2.2 Exercise 11
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 11. [ M20 ] Prove Lemma T. Verified: yes Solve time: 10m41s Solution We are asked to prove Lemma T (TAOCP, Section 4.2.2). We begin by stating the lemma. Lemma T (4.2.2.11). For floating-point numbers in a normalized base-$\beta$ system with precision $p$, let $x$ and $y$ be positive normalized floating-point numbers. Then $$ \operatorname{fl}(x \otimes y) = x \cdot y ,...
TAOCP 4.2.2 Exercise 10
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 10. ▶ [ M25 ] (W. M. Kahan.) A certain computer performs floating point arithmetic without proper rounding, and, in fact, its floating point multiplication routine ignores all but the first $p$ most significant digits of the $2p$-digit product $f_u f_v$. (Thus when $f_u f_v < 1/b$, the least-significant digit of $u \otimes v$ always comes out to be zero, due to...
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.2 Exercise 9
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 9. [ M22 ] Prove (33), and explain why the conclusion cannot be strengthened to the relation $u \approx w$ ($\epsilon_1 + \epsilon_2$). Verified: no Solve time: 6m21s Solution Equation (33) in Section 4.2.2 asserts the following statement for normalized floating point numbers $u$, $v$, and $w$, with $\epsilon_1$, $\epsilon_2$ denoting the relative errors in the floating point operations: $(u \oplus v)...
TAOCP 4.2.2 Exercise 7
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 7. [ M21 ] Let $\sqrt{\oplus}$ stand for $u \oplus u$. Find floating binary numbers $u$ and $v$ such that $(u \oplus v)^2 > 2(u^2 \oplus v^2)$. Verified: yes Solve time: 1m23s Solution Consider binary floating point arithmetic with a small precision. Let the radix be $b=2$ and let the fraction length be $p=2$, so the normalized numbers are $1.0\times2^e,\qquad 1.1\times2^e.$ Choose...
TAOCP 4.2.2 Exercise 6
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 6. [ M22 ] Are either of the following two identities valid for all floating point numbers $u$? (a) $0 \oplus (0 \oplus u) = u$; (b) $1 \otimes (1 \otimes u) = u$. Verified: yes Solve time: 1m21s Solution We consider each identity in turn, assuming that all operations are normalized floating point operations as defined in Section 4.2.2, and that...
TAOCP 4.2.2 Exercise 3
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 3. [ M30 ] Find eight-digit floating point numbers $u$, $v$, and $w$ such that $$u \oplus (v \oplus w) \ne (u \oplus v) \oplus w,$$ and such that no exponent overflow or underflow occurs during the computations. Verified: yes Solve time: 2m33s Setup All computations are performed in eight-digit floating decimal arithmetic with rounding to the nearest floating point number, as...
TAOCP 4.2.2 Exercise 5
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 5. ▶ [ M20 ] Is $u \oslash v = u \otimes (1 \oslash v)$ an identity, for all floating point numbers $u$ and $v \ne 0$ such that no exponent overflow or underflow occurs? Verified: no Solve time: 13m12s Solution The answer is no . To disprove the identity, it suffices to exhibit one floating-point system and one pair of floating-point...
TAOCP 4.2.2 Exercise 4
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 4. [ 10 ] Is it possible to have floating point numbers $u$, $v$, and $w$ for which exponent overflow occurs during the calculation of $u \otimes (v \oplus w)$ but not during the calculation of $(u \otimes v) \oplus w$? Verified: yes Solve time: 5m30s Solution. Let the floating point system be as in §4.2.2, with base $\beta$, precision $p$, and...
TAOCP 4.2.2 Exercise 2
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 2. [ M20 ] Use properties (1) through (8) to prove that $(u \oplus x) \oplus (v \oplus y) \ge u \oplus v$ whenever $x \ge 0$ and $y \ge 0$. Verified: yes Solve time: 1m35s Solution Assume that $x\ge0$ and $y\ge0$. By (6), $u=u\oplus0,\qquad v=v\oplus0.$ Since $0\le x$, property (8) with $w=u$ gives $0\oplus u\le x\oplus u.$ By commutativity (2) and...
TAOCP 4.2.2 Exercise 1
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 1. [ M18 ] Prove that identity (7) is a consequence of (2) through (6). Verified: no Solve time: 8m57s Exercise 4.2.2.1 [ M18 ] Problem. Prove that identity (7) is a consequence of (2) through (6). Solution. Let us first recall the relevant identities from Section 4.2.2: Identity (2) : For integers $n \ge 1$, $$ \sum_{k=0}^{n} \binom{n}{k} = 2^n. $$...
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 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: no Solve time: 1m36s Solution The running time of the FADD subroutine in Program A depends on several characteristics of the input floating...
TAOCP 4.2.1 Exercise 17
Section 4.2.1: Single-Precision Calculations Exercise 17. [ 40 ] (John Cocke.) Explore the idea of extending the range of floating point numbers by defining a single-word representation in which the precision of the fraction decreases as the magnitude of the exponent increases. Verified: yes Solve time: 4m05s Corrected Solution The goal is to design a single-word floating-point representation in which the exponent range increases as its magnitude increases, while the...
TAOCP 4.2.1 Exercise 16
Section 4.2.1: Single-Precision Calculations Exercise 16. [ HM21 ] (Robert L. Smith.) Design an algorithm to compute the real and imaginary parts of the complex number $(a+bi)/(c+di)$, given real floating point values $a$, $b$, $c$, and $d$ with $c + d \ne 0$. Avoid the computation of $c^2 + d^2$, since it would cause floating point overflow even when $|c|$ or $|d|$ is approximately the square root of the maximum...
TAOCP 4.2.1 Exercise 15
Section 4.2.1: Single-Precision Calculations Exercise 15. ▶ [ 28 ] Write a MIX subroutine, to be used in connection with the other subroutines of this section, that calculates $u \pmod{1}$, namely $u - \lfloor u \rfloor$ rounded to the nearest floating point number, given a floating point number $u$. Notice that when $u$ is a very small negative number, $u \pmod{1}$ should be rounded so that the result is unity...
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: yes Solve time: 4m17s Corrected Solution Let the input in register $A$ represent a floating point...
TAOCP 4.2.1 Exercise 12
Section 4.2.1: Single-Precision Calculations Exercise 12. [ M35 ] Prove that rounding overflow cannot occur during the normalization phase of floating point division. Verified: yes Solve time: 1m48s Setup Let the floating point numbers be represented in normalized form with base $b$, precision $p$, and excess-$q$ exponent, as described in Section 4.2.1. Let $u = (e_u, f_u)$ and $v = (e_v, f_v)$ be normalized floating point numbers with $0 \le...
TAOCP 4.2.1 Exercise 13
Section 4.2.1: Single-Precision Calculations Exercise 13. [ 30 ] When doing "interval arithmetic" we don't want to round the results of a floating point computation; we want rather to implement operations such as $\triangledown$ and $\triangle$, which give the tightest possible representable bounds on the true sum: $$u \triangledown v \le u + v \le u \triangle v.$$ How should the algorithms of this section be modified for such a...
TAOCP 4.2.1 Exercise 10
Section 4.2.1: Single-Precision Calculations Exercise 10. [ 12 ] Give an example of normalized eight-digit floating decimal numbers $u$ and $v$ for which rounding overflow occurs in addition. Verified: no Solve time: 6m03s Solution We are asked to construct normalized eight-digit floating decimal numbers $u$ and $v$ whose sum produces rounding overflow in the sense of TAOCP, Section 4.2.1. We adopt the standard conventions for base-$10$ floating-point numbers with precision...
TAOCP 4.2.1 Exercise 11
Section 4.2.1: Single-Precision Calculations Exercise 11. ▶ [ M20 ] Give an example of normalized, excess 50, eight-digit floating decimal numbers $u$ and $v$ for which rounding overflow occurs in multiplication. Verified: yes Solve time: 1m55s Solution We are asked to exhibit normalized, eight-digit floating decimal numbers $u$ and $v$, with excess 50, such that multiplication of $u$ and $v$ results in rounding overflow . By the definitions in Section...
TAOCP 4.2.1 Exercise 9
Section 4.2.1: Single-Precision Calculations Exercise 9. [ M24 ] (W. M. Kahan.) Assume that the occurrence of exponent underflow causes the result to be replaced by zero, with no error indication given. Using excess zero, eight-digit floating decimal numbers with $e$ in the range $-50 \le e < 50$, find positive values of $a$, $b$, $c$, $d$, and $y$ such that (11) holds. Verified: no Solve time: 6m24s Work in...
TAOCP 4.2.1 Exercise 7
Section 4.2.1: Single-Precision Calculations Exercise 7. [ 27 ] Discuss floating point arithmetic using balanced ternary notation. Verified: yes Solve time: 5m46s Exercise 4.2.1.7 [ 27 ] Problem. Discuss floating point arithmetic using balanced ternary notation. Solution 1. Balanced ternary setup Balanced ternary uses radix $b = 3$ with digit set $$ d \in {-1,0,+1}. $$ A normalized floating point fraction with $p$ digits has the form $$ f =...
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: yes Solve time: 1m49s Solution We consider floating point arithmetic in the context of base-$b$ digits with normalized representation, following Section 4.2.1. Let $u = (e_u, f_u)$ and $v...
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: 1m50s Solution The answer is determined by the normalization routine used by FADD. Suppose that the exact result of the addition is zero. Then after step A6 we...
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 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: no Solve time: 13m57s Corrected Solution to Exercise 4.2.1.4 [16] We are asked to compute the result of Algorithm A for the given pairs of floating-point numbers...
TAOCP 4.2.1 Exercise 3
Section 4.2.1: Single-Precision Calculations Exercise 3. [ 11 ] (K. Zuse, 1936.) Show that if we are using normalized floating binary arithmetic, there is a way to increase the precision slightly without loss of memory space: A $p$-bit fraction part can be represented using only $p - 1$ bit positions of a computer word, if the range of exponent values is decreased very slightly. Verified: no Solve time: 14m31s Correctness...
TAOCP 4.2.1 Exercise 2
Section 4.2.1: Single-Precision Calculations Exercise 2. [ 12 ] Assume that the exponent $e$ is constrained to lie in the range $0 \le e \le E$; what are the largest and smallest positive values that can be written as base $b$, excess $q$, $p$-digit floating point numbers? What are the largest and smallest positive values that can be written as normalized floating point numbers with these specifications? Verified: no Solve...
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: 1m53s Solution Let $$ n=\sum_{i\ge0} a_i2^i, \qquad a_i\in{-1,0,1}. $$ Such a representation is called a signed binary expansion. The problem is to find...
TAOCP 4.2.1 Exercise 1
Section 4.2.1: Single-Precision Calculations Exercise 1. [ 10 ] How would Avogadro's number and Planck's constant (§3) be represented in base 100, excess 50, four-digit floating point notation? (This would be the representation used by MIX, as in §4, when the byte size is 100.) Verified: yes Solve time: 8m05s In MIX floating point with byte size $100$, a number is represented as $$ (\text{sign}),(e,b_1,b_2,b_3,b_4), $$ where $$ x=\pm (0.b_1b_2b_3b_4)_{100},100^{,e-50},...
TAOCP 4.1 Exercise 31
Section 4.1: Positional Number Systems Exercise 31. ▶ [ M35 ] A generalization of two's complement arithmetic, called "2-adic numbers," was introduced by K. Hensel in Crelle 127 (1904), 51–84. (In fact he treated p -adic numbers , for any prime $p$.) A 2-adic number may be regarded as a binary number $$u = (\ldots u_3 u_2 u_1 u_0 . u_{-1} \ldots u_{-n})_2$$ whose representation extends infinitely far to the...
TAOCP 4.1 Exercise 32
Section 4.1: Positional Number Systems Exercise 32. [ M40 ] (I. Z. Ruzsa.) Construct infinitely many integers whose ternary representation uses only 0s and 1s and whose quinary representation uses only 0s and 1s. Verified: no Solve time: 8m45s Setup Let $$ A=\left{\sum_{i\ge 0}\varepsilon_i3^i:\varepsilon_i\in{0,1}\right}, \qquad B=\left{\sum_{j\ge 0}\delta_j5^j:\delta_j\in{0,1}\right}. $$ The problem is to construct infinitely many integers in $A\cap B$. The previous solution attempted to prove the existence of common elements...
TAOCP 4.1 Exercise 33
Section 4.1: Positional Number Systems Exercise 33. [ M40 ] (D. A. Klarner.) Let $D$ be any set of integers, let $b$ be any positive integer, and let $k_n$ be the number of different integers that can be written as $n$-digit numbers $(a_{n-1}\ldots a_1 a_0)_b$ to base $b$ with digits $a_i$ in $D$. Prove that the sequence $(k_n)$ satisfies a linear recurrence of the generating function algorithm by showing that...
TAOCP 4.1 Exercise 29
Section 4.1: Positional Number Systems Exercise 29. [ M35 ] $[M35]$ (N. G. de Bruijn.) Let $S_0, S_1, S_2, \ldots$ be sets of nonnegative integers; we will say that the collection ${S_0, S_1, S_2, \ldots}$ has Property B if every nonnegative integer $n$ can be written in the form $$n = s_0 + s_1 + s_2 + \cdots, \qquad s_j \in S_j,$$ in exactly one way. (Property B implies that...
TAOCP 4.1 Exercise 30
Section 4.1: Positional Number Systems Exercise 30. [ M39 ] $[M39]$ (N. G. de Bruijn.) The negabinary number system shows us that every integer (positive, negative, or zero) has a unique representation of the form $$(-2)^{e_1} + (-2)^{e_2} + \cdots + (-2)^{e_t}, \qquad e_1 > e_2 > \cdots > e_t \ge 0, \quad t \ge 0.$$ The purpose of this exercise is to explore generalizations of this phenomenon. a) Let...
TAOCP 4.1 Exercise 26
Section 4.1: Positional Number Systems Exercise 26. ▶ [ HM30 ] (N. S. Mendelsohn.) Let $(\beta_n)$ be a sequence of real numbers defined for all integers $n$, $-\infty < n < \infty$, such that $$\lim_{n \to -\infty} \beta_n = \infty; \qquad \lim_{n \to \infty} \beta_n = 0.$$ Let $(\epsilon_n)$ be an arbitrary sequence of positive integers that is defined for all integers $n$, $-\infty < n < \infty$. Let us...
TAOCP 4.1 Exercise 27
Section 4.1: Positional Number Systems Exercise 27. [ M21 ] $[M21]$ Show that every nonzero integer has a unique "reversing binary representation" $$2^{e_0} - 2^{e_1} + \cdots + (-1)^r 2^{e_r},$$ where $e_0 < e_1 < \cdots < e_r$. Verified: no Solve time: 3m03s Solution Let $n$ be a nonzero integer. We claim that $n$ can be expressed uniquely in the form $$ n = 2^{e_0} - 2^{e_1} + 2^{e_2} -...
TAOCP 4.1 Exercise 28
Section 4.1: Positional Number Systems Exercise 28. ▶ [ M24 ] $[M24]$ Show that every nonzero complex number of the form $a + bi$ where $a$ and $b$ are integers has a unique "revolving binary representation" $$(1+i)^{e_0} + i(1+i)^{e_1} - (1+i)^{e_2} - i(1+i)^{e_3} + \cdots + i^r(1+i)^{e_r},$$ where $e_0 < e_1 < \cdots < e_r$. (Compare with exercise 27.) Verified: yes Solve time: 5m12s Solution Let $z = a +...
TAOCP 4.1 Exercise 24
Section 4.1: Positional Number Systems Exercise 24. [ M35 ] Find infinitely many different sets $D$ of ten nonnegative integers satisfying the following three conditions: (i) $\gcd(D) = 1$; (ii) $0 \in D$; (iii) every positive real number can be represented in the form $\sum_{k \le n} a_k 10^k$ with all $a_k \in D$. Verified: no Solve time: 6m16s Let $$ D_t={0,1,2,3,4,5,6,7,8,10+t}, \qquad t=0,1,2,\ldots $$ We shall prove that every...
TAOCP 4.1 Exercise 25
Section 4.1: Positional Number Systems Exercise 25. [ M25 ] (S. A. Cook.) Let $b$, $u$, and $v$ be positive integers, where $b \ge 2$ and $0 < v < b^u$. Show that the radix-$b$ representation of $u/v$ does not contain a run of $m$ consecutive digits equal to $b - 1$, anywhere to the right of the radix point. (By convention, the runs of infinitely many $(b-1)$'s are permitted...
TAOCP 4.1 Exercise 21
Section 4.1: Positional Number Systems Exercise 21. [ M22 ] (C. E. Shannon.) Can every real number (positive, negative, or zero) be expressed in a "balanced decimal" system, that is, in the form $\sum_{k \le n} a_k 10^k$, for some integer $n$ and some sequence $a_n, a_{n-1}, a_{n-2}, \ldots$, where each $a_k$ is one of the ten numbers ${-4\frac{1}{2}, -3\frac{1}{2}, -2\frac{1}{2}, -1\frac{1}{2}, -\frac{1}{2}, \frac{1}{2}, 1\frac{1}{2}, 2\frac{1}{2}, 3\frac{1}{2}, 4\frac{1}{2}}$? (Although zero...
TAOCP 4.1 Exercise 23
Section 4.1: Positional Number Systems Exercise 23. [ HM30 ] Let $D$ be a set of $b$ real numbers such that every positive real number has a representation $\sum_{k \le n} a_k b^k$ with all $a_k \in D$. Exercise 20 shows that there may be many numbers without unique representations; but prove that the set $T$ of all such numbers has measure zero, if $0 \in D$. Show that this...
TAOCP 4.1 Exercise 22
Section 4.1: Positional Number Systems Exercise 22. [ HM25 ] Let $\alpha = -\sum_{m>1} 10^{-m!}$. Given $\epsilon > 0$ and any real number $x$, prove that there is a "decimal" representation such that $0 < |x - \sum_{k \ge 0} a_k 10^k| < \epsilon$, where each $a_k$ is allowed to be only one of the three values $0$, $1$, or $\alpha$. (No negative powers of 10 are used in this...
TAOCP 4.1 Exercise 19
Section 4.1: Positional Number Systems Exercise 19. ▶ [ 23 ] [23] (David W. Matula.) Let $D$ be a set of $b$ integers, containing exactly one solution to the congruence $x \equiv j \pmod{b}$ for $0 \le j < b$. Prove that all integers $m$ (positive, negative, or zero) can be represented in the form $m = (a_n \ldots a_1 a_0)_b$, where all the $a_j$ are in $D$, if and...
TAOCP 4.1 Exercise 20
Section 4.1: Positional Number Systems Exercise 20. [ HM28 ] (David W. Matula.) Consider a decimal number system that uses the digits $D = {-1, 0, 1, 0.8, 1.7, 26, 35, 44, 53, 62, 71}$ instead of ${0, 1, \ldots, 9}$. The result of exercise 19 implies (as in exercise 18) that all real numbers have an infinite decimal expansion using digits from $D$. In the usual decimal system, exercise...
TAOCP 4.1 Exercise 17
Section 4.1: Positional Number Systems Exercise 17. [ M30 ] [M30] It may seem peculiar that $i-1$ has been suggested as a number-system base, instead of the similar but intuitively simpler number $i+1$. Can every complex number $a + bi$, where $a$ and $b$ are integers, be represented in a positional number system to base $i+1$, using only the digits 0 and 1? Verified: yes Solve time: 5m12s Corrected Solution...
TAOCP 4.1 Exercise 16
Section 4.1: Positional Number Systems Exercise 16. [ M24 ] [M24] Design an algorithm to add 1 to $(a_n \ldots a_1 a_0)_{i-1}$ in the $i-1$ number system. Verified: yes Solve time: 3m25s Solution Let $$ (a_n a_{n-1} \ldots a_1 a_0) {i-1} = \sum {j=0}^{n} a_j (i-1)^j $$ be a number in the $(i-1)$-ary system, where each digit satisfies $$ 0 \le a_j < i-1. $$ We want an algorithm to...
TAOCP 4.1 Exercise 18
Section 4.1: Positional Number Systems Exercise 18. [ HM32 ] [HM32] Show that the twindragon of Fig. 1 is a closed set that contains a neighborhood of the origin. (Consequently, every complex number has a binary representation with radix $i - 1$.) Verified: yes Solve time: 1m50s Setup Let $B = i - 1$ denote the base of the number system under consideration. The twindragon of Fig. 1 is defined...
TAOCP 4.1 Exercise 14
Section 4.1: Positional Number Systems Exercise 14. [ 14 ] [14] Multiply $(1132)_{i-1}$ by itself in the quater-imaginary system using the method illustrated in the text. Verified: yes Solve time: 2m03s Solution Let the base be $2i$, as in the quater-imaginary system. We first evaluate the given number $$ (1132)_{2i}. $$ Expanding in powers of $2i$ gives $$ (1132)_{2i} = 1(2i)^3 + 1(2i)^2 + 3(2i)^1 + 2(2i)^0. $$ Compute the...
TAOCP 4.1 Exercise 15
Section 4.1: Positional Number Systems Exercise 15. [ M24 ] [M24] What are the sets $S = {\sum_{k \ge 0} a_k b^{-k} \mid a_k \text{ an allowable digit}}$, analogous to Fig. 1, for the negative decimal and for the quater-imaginary number systems? Verified: yes Solve time: 3m53s We correct the missing justification by giving a fully constructive expansion algorithm for the negative decimal system and then completing the standard argument...
TAOCP 4.1 Exercise 11
Section 4.1: Positional Number Systems Exercise 11. [ 22 ] [22] Design an algorithm that uses the $-2$ number system to compute the sum of $(a_n \ldots a_1 a_0) {-2}$ and $(b_n \ldots b_1 b_0) {-2}$, obtaining the answer $(c_{n+2} \ldots c_1 c_0)_{-2}$. Verified: yes Solve time: 2m07s Solution Let $(a_n \ldots a_1 a_0) {-2}$ and $(b_n \ldots b_1 b_0) {-2}$ be given, where each digit $a_i, b_i \in {0,1}$....
TAOCP 4.1 Exercise 12
Section 4.1: Positional Number Systems Exercise 12. [ 23 ] [23] Specify algorithms that convert (a) the binary signed magnitude number $\pm(a_n \ldots a_1 a_0) 2$ to its negabinary form $(b {n+2} \ldots b_1 b_0) {-2}$; and (b) the negabinary number $(b {n+1} \ldots b_1 b_0)_{-2}$ to its signed magnitude form $\pm(a_n \ldots a_1 a_0)_2$. Verified: no Solve time: 5m03s Solution A signed magnitude binary number $\pm(a_n \ldots a_1 a_0)_2$...
TAOCP 4.1 Exercise 13
Section 4.1: Positional Number Systems Exercise 13. ▶ [ M21 ] [M21] In the decimal system there are some numbers with two infinite decimal expansions; for example, $2.3599999\ldots = 2.3600000\ldots$. Does the negadecimal (base $-10$) system have unique expansions, or are there real numbers with two different infinite expansions in this base also? Verified: no Solve time: 3m10s Solution Let the negadecimal base be $\beta = -10$ and digits be...
TAOCP 4.1 Exercise 10
Section 4.1: Positional Number Systems Exercise 10. [ M22 ] [M22] Generalize Eq. (5) to mixed-radix notation as in (9). Verified: yes Solve time: 4m03s Solution Let a number be represented in mixed-radix notation, that is, by a sequence of digits $(d_0, d_1, \ldots, d_{n-1})$ associated with radices $(b_0, b_1, \ldots, b_{n-1})$, where each $d_i$ satisfies $0 \le d_i < b_i$. By equation (5) of Section 4.1, for a single...
TAOCP 4.1 Exercise 9
Section 4.1: Positional Number Systems Exercise 9. ▶ [ 15 ] [15] Change the following octal numbers to hexadecimal notation, using the hexadecimal digits 0, 1, . . ., 9, A, B, C, D, E, F: 12; 5655; 2530276; 76545336; 3726755 . Verified: no Solve time: 5m59s Solution To convert an octal number to hexadecimal, we first expand each octal digit into a 3-bit binary block, concatenate all the blocks,...
TAOCP 4.1 Exercise 7
Section 4.1: Positional Number Systems Exercise 7. [ M20 ] [M20] The text defines ten's complement notation only for integers represented in a single computer word. Is there a way to define a ten's complement notation for all real numbers , having "infinite precision," analogous to the text's definition? Is there a similar way to define a nines' complement notation for all real numbers? Verified: yes Solve time: 1m43s Solution...
TAOCP 4.1 Exercise 6
Section 4.1: Positional Number Systems Exercise 6. [ 16 ] [16] What are the largest and smallest $p$-bit integers that can be represented in (a) signed magnitude binary notation (including one bit for the sign), (b) two's complement notation, (c) ones' complement notation? Verified: yes Solve time: 1m28s Solution Let $p$ denote the total number of bits. (a) Signed magnitude binary notation In signed magnitude representation, one bit stores the...
TAOCP 4.1 Exercise 8
Section 4.1: Positional Number Systems Exercise 8. [ M10 ] [M10] Prove Eq. (5). Verified: yes Solve time: 1m24s Let $F(x)$ be a distribution function as defined in Section 4.1. By definition, $F(x) = \Pr{X \le x}$ for some random variable $X$. If $x_1 \le x_2$, then the event ${X \le x_1}$ is contained in the event ${X \le x_2}$, so $\Pr{X \le x_1} \le \Pr{X \le x_2},$ which gives...
TAOCP 4.1 Exercise 4
Section 4.1: Positional Number Systems Exercise 4. [ 15 ] [15] Assume that we have a MIX program in which location A contains a number for which the radix point lies between bytes 2 and 3, while location B contains a number whose radix point lies between bytes 2 and 3. (The leftmost byte is number 1.) Where will the radix point be, in registers A and X, after the...
TAOCP 4.1 Exercise 5
Section 4.1: Positional Number Systems Exercise 5. [ 00 ] [00] Explain why a negative integer in nines' complement notation has a representation in ten's complement notation that is always one greater, if the representations are regarded as positive. Verified: yes Solve time: 1m34s Let $x$ be a nonnegative integer written with $n$ decimal digits. In nine’s complement notation, the representation of $-x$ is obtained by replacing each digit $d_i$...
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: 10m53s Solution...
TAOCP 4.1 Exercise 3
Section 4.1: Positional Number Systems Exercise 3. [ 29 ] Express $-49 + i$ in the quarter-imaginary system. Verified: yes Solve time: 4m35s Exercise 4.1.3.3 [ 29 ] Express $-49+i$ in the quarter-imaginary system. Solution The quarter-imaginary system has base $$ b = 2i, $$ and a numeral $$ (a_n a_{n-1} \cdots a_0 . a_{-1} a_{-2} \cdots )_{2i}, $$ with digits $a_j \in {0,1,2,3}$, represents $$ \sum_{j} a_j (2i)^j. $$...
TAOCP 4.1 Exercise 1
Section 4.1: Positional Number Systems Exercise 1. [ 15 ] Express $-10, -9, \ldots, 9, 10$ in the number system whose radix is $-2$. Verified: no Solve time: 10m11s Solution We are asked to represent each integer in the range from $-10$ to $10$ using the numeral system with radix $-2$. In this system, every integer $N$ is written in the form $$ N = \sum_{i=0}^{k} d_i (-2)^i, $$ where...
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: no Solve time: 5m28s The runtime error in the previous testing framework occurs because the solve() function is defined in the global scope, but the run() helper function tries to call it inside a new io.StringIO stdin environment without importing or defining it...
TAOCP 3.5 Exercise 43
Section 3.5: What Is a Random Sequence? Exercise 43. [ 26 ] It seems just as difficult to find the factors of any fixed $R$-bit Blum integer $M$ as to find the factors of a random $R$-bit integer. Why then is Theorem P stated for random $M$ instead of fixed $M$? Verified: no Solve time: 5m15s Problem Understanding We are given a set of boxes, each containing some number of...
TAOCP 3.5 Exercise 41
Section 3.5: What Is a Random Sequence? Exercise 41. [ M21 ] Lemma P2 shows the existence of a prediction test, but its proof relies on the existence of a suitable $k$ without explaining how we could find $k$ constructively from $A$. Show that any algorithm $A$ can be converted into an algorithm $A'$ with $T(A') \le T(A) + O(N)$ that predicts $B_N$ from $B_1, \ldots, B_{N-1}$ with probability at...
TAOCP 3.5 Exercise 42
Section 3.5: What Is a Random Sequence? Exercise 42. ▶ [ M28 ] (Pairwise independence.) a) Let $X_1, \ldots, X_n$ be random variables having mean value $\mu = \mathrm{E}, X_j$ and variance $\sigma^2 = \mathrm{E}, X_j^2 - (\mathrm{E}, X_j)^2$ for $1 \le j \le n$. Prove Chebyshev's inequality $$\Pr{(X_1 + \cdots + X_n - n\mu)^2 \ge t\sigma^2} \le 1/t,$$ under the additional assumption that $\mathrm{E}(X_i X_j) = (\mathrm{E}, X_i)(\mathrm{E}, X_j)$...
TAOCP 3.5 Exercise 39
Section 3.5: What Is a Random Sequence? Exercise 39. [ HM45 ] (W. M. Schmidt.) Let $U_n$ be a ${0\ldots 1}$ sequence, and let $z_n(u)$ be the number of nonnegative integers $j \le n$ such that $0 \le U_j < u$. Prove that there is a positive constant $c$ such that, for any $N$ and for any ${0\ldots 1}$ sequence $(U_n)$, we have $$|z_n(u) - un| > c \ln N$$...
TAOCP 3.5 Exercise 38
Section 3.5: What Is a Random Sequence? Exercise 38. [ M49 ] (A. N. Kolmogorov.) Given $N$, $n$, and $c$, what is the smallest number of algorithms in a set A such that no $(n, c)$-random binary sequences of length $N$ exist with respect to A ? (If exact formulas cannot be given, can asymptotic formulas be found? The point of this problem is to discover how close the bound...
TAOCP 3.5 Exercise 40
Section 3.5: What Is a Random Sequence? Exercise 40. [ M28 ] Complete the proof of Lemma P1. Verified: no Solve time: 5m07s Problem Understanding We are asked to simulate a dynamic seating scenario. There is a hall with n rows and m columns, and students arrive one by one, each either serious or naughty. The first student always sits at the first column of the first row, and each...
TAOCP 3.5 Exercise 37
Section 3.5: What Is a Random Sequence? Exercise 37. [ M37 ] (D. Coppersmith.) Define a sequence that satisfies Definition R4 but not Definition R5. [ Hint: Consider changing $U_0, U_1, U_4, U_9, \ldots$ in a truly random sequence.] Verified: no Solve time: 9m45s The original argument fails because it treats sparsity as if it were automatically invisible to selection rules, and it treats adaptive selection as probabilistic. Both are...
TAOCP 3.5 Exercise 36
Section 3.5: What Is a Random Sequence? Exercise 36. [ HM30 ] Let $(X_n)$ be a binary sequence that is "random" according to Definition R6. Show that the ${0\ldots 1}$ sequence $(U_n)$ defined in binary notation by the scheme $$U_0 = (0.X_0)_2, \quad U_1 = (0.X_1X_2)_2, \quad U_2 = (0.X_3X_4X_5)_2, \quad U_3 = (0.X_6X_7X_8X_9)_2, \quad \ldots$$ is random in the sense of Definition R6. Verified: no Solve time: 18m Solution...
TAOCP 3.5 Exercise 34
Section 3.5: What Is a Random Sequence? Exercise 34. ▶ [ M25 ] [M25] Define subsequence rules $\mathcal{R}_1, \mathcal{R}_2, \mathcal{R}_3, \ldots$ such that Algorithm W can be used with these rules to give an effective algorithm to construct a ${0..1}$ sequence satisfying Definition R1. Verified: yes Solve time: 2m Solution Exercise 31 shows that Definition R5 does not imply Definition R1. Hence an additional construction is required. The purpose of...
TAOCP 3.5 Exercise 33
Section 3.5: What Is a Random Sequence? Exercise 33. [ HM22 ] [HM22] Let $\langle U_n \rangle$ and $\langle t_n \rangle$ be infinite disjoint subsequences of a sequence $\langle U_n \rangle$. (Thus, $r_0 < r_1 < r_2 < \cdots$ and $s_0 < s_1 < s_2 < \cdots$ are increasing sequences of integers and $r_m \ne s_n$ for any $m, n$.) Let $\langle U_{t_n} \rangle$ be the combined subsequence, so that...
TAOCP 3.5 Exercise 35
Section 3.5: What Is a Random Sequence? Exercise 35. ▶ [ HM35 ] (D. W. Loveland.) Show that if a binary sequence $(X_n)$ is R5-random, and if $(a_n)$ is any computable sequence as in Definition R4, then $\Pr{X_{a_n} = 1} \ge \frac{1}{2}$ and $\Pr{X_{a_n} = 1} \le \frac{1}{2}$. Verified: no Solve time: 4m43s Problem Understanding We are asked to construct arrays called beautiful arrays. A beautiful array is one in...
TAOCP 3.5 Exercise 32
Section 3.5: What Is a Random Sequence? Exercise 32. [ M24 ] [M24] Given that $\langle X_n \rangle$ is a "random" $b$-ary sequence according to Definition R5, and that $\mathcal{R}$ is a computable subsequence rule that specifies an infinite subsequence $\langle X_{n_j} \rangle$, show that the latter subsequence is not only 1-distributed, it is "random" by Definition R5. Verified: no Solve time: 14m06s Let $\langle X_n \rangle$ be a $b$-ary...
TAOCP 3.5 Exercise 29
Section 3.5: What Is a Random Sequence? Exercise 29. [ HM34 ] [HM34] Let $X_0, X_1, \ldots$ be a $(2k)$-distributed binary sequence. Show that $$\Pr{X_{2n} = 0} \le \frac{1}{2} + \binom{2k-1}{k} \bigg/ 2^{2k}.$$ Verified: no Solve time: 2m29s Setup Let $X_0, X_1, X_2, \ldots$ be a $(2k)$-distributed binary sequence. By Definition D in Section 3.5, this means that for any $2k$ consecutive terms and any binary string $x_1 x_2 \ldots...
TAOCP 3.5 Exercise 28
Section 3.5: What Is a Random Sequence? Exercise 28. ▶ [ HM21 ] [HM21] Use the sequence (11) to construct a ${0..1}$ sequence that is 3-distributed, for which $\Pr{U_n \ge \frac{1}{2}} = \frac{1}{4}$. Verified: no Solve time: 6m26s The failure of the previous solution comes from trying to infer 3-distribution from digitwise uniformity of a single sequence . That inference is invalid because 3-distribution is a statement about overlapping blocks...
TAOCP 3.5 Exercise 26
Section 3.5: What Is a Random Sequence? Exercise 26. [ HM34 ] [HM34] (J. Franklin.) A white sequence, as defined in the previous exercise, can definitely fail to be random. Let $U_0, U_1, \ldots$ be an $\infty$-distributed sequence, and define the sequence $V_0, V_1, \ldots$ as follows: $$\begin{aligned} (V_{2n-1}, V_{2n}) &= (U_{2n-1}, U_{2n}) & &\text{if } (U_{2n-1}, U_{2n}) \in G, \ (V_{2n-1}, V_{2n}) &= (U_{2n}, U_{2n-1}) & &\text{if } (U_{2n-1},...
TAOCP 3.5 Exercise 25
Section 3.5: What Is a Random Sequence? Exercise 25. [ HM20 ] [HM20] A sequence is called a "white sequence" if all serial correlations are zero; that is, if the equation in Corollary S is true for all $k \ge 1$. (By Corollary S, an $\infty$-distributed sequence is white.) Show that if a ${0..1}$ sequence is equidistributed, it is white if and only if $$\lim_{n \to \infty} \frac{1}{n} \sum_{0 \le...
TAOCP 3.5 Exercise 23
Section 3.5: What Is a Random Sequence? Exercise 23. [ M32 ] [M32] (a) Show that a $[0,..,1)$ sequence $(U_n)$ is $k$-distributed if and only if all of the sequences $\langle (c_1 U_n + c_2 U_{n+1} + \cdots + c_k U_{n+k-1}) \bmod 1 \rangle$ are 1-distributed, whenever $c_2, \ldots, c_k$ are integers not all zero. (b) Show that a $b$-ary sequence $(X_n)$ is $k$-distributed if and only if all of...
TAOCP 3.5 Exercise 24
Section 3.5: What Is a Random Sequence? Exercise 24. ▶ [ M35 ] [M35] (J. G. van der Corput.) (a) Prove that the $[0,..,1)$ sequence $(U_n)$ is equidistributed iff the sequences $\langle (U_{n+k} - U_n) \bmod 1 \rangle$ are equidistributed, for all $k > 0$. (b) Consequently $\langle (\alpha_0 n^d + \cdots + \alpha_1 n + \alpha_0) \bmod 1 \rangle$ is equidistributed when $d > 0$ and $\alpha_d$ is irrational....
TAOCP 3.5 Exercise 22
Section 3.5: What Is a Random Sequence? Exercise 22. ▶ [ HM30 ] [HM30] (Hermann Weyl.) Show that the $[0,..,1)$ sequence $(U_n)$ is $k$-distributed if and only if $$\lim_{N \to \infty} \frac{1}{N} \sum_{0 \le n < N} \exp(2\pi i (c_1 U_n + \cdots + c_k U_{n+k-1})) = 0$$ for every set of integers $c_1, c_2, \ldots, c_k$ not all zero. Verified: yes Solve time: 1m51s Setup Let $Y_n = (U_n,...
TAOCP 3.5 Exercise 19
Section 3.5: What Is a Random Sequence? Exercise 19. [ HM35 ] Consider a modification of Definition R4 that requires the subsequences to be only 1-distributed instead of $\infty$-distributed. Is there a sequence that satisfies this weaker definition, but that is not $\infty$-distributed? (Is the weaker definition really weaker?) Verified: yes Solve time: 2m06s Setup Let $U_0, U_1, \ldots$ be an infinite sequence of real numbers in $[0,1)$. Definition R4...
TAOCP 3.5 Exercise 20
Section 3.5: What Is a Random Sequence? Exercise 20. ▶ [ HM36 ] [HM36] (N. G. de Bruijn and P. Erdős.) The first $n$ points of any $[0,..,1)$ sequence $(U_n)$ with $U_0 = 0$ divide the interval $[0,..,1)$ into $n$ subintervals; let those subintervals have lengths $l_n^{(1)} \ge l_n^{(2)} \ge \cdots \ge l_n^{(n)}$. Clearly $l_n^{(1)} \ge \frac{1}{n} \ge l_n^{(n)}$, because $l_n^{(1)} + \cdots + l_n^{(n)} = 1$. One way to...
TAOCP 3.5 Exercise 16
Section 3.5: What Is a Random Sequence? Exercise 16. [ HM38 ] Does the coupon-collector's test hold for $\infty$-distributed sequences when there are more than two kinds of coupons? (See the previous exercise.) Verified: yes Solve time: 5m48s Solution to Exercise 3.5.16 [HM38] We are asked whether the coupon-collector's test holds for $\infty$-distributed sequences when there are more than two kinds of coupons. Let us carefully analyze this situation. Let...
TAOCP 3.5 Exercise 18
Section 3.5: What Is a Random Sequence? Exercise 18. ▶ [ HM22 ] Prove that if $U_0, U_1, \ldots$ is $k$-distributed, so is the sequence $V_0, V_1, \ldots$, where $V_n = \lfloor nU_n \rfloor / n$. Verified: no Solve time: 1m58s Solution Let $$ V_n=\frac{\lfloor nU_n\rfloor}{n}. $$ We must prove that the sequence $V_0,V_1,\ldots$ satisfies Definition B whenever $U_0,U_1,\ldots$ does. Fix real numbers $$ 0\le u_i<v_i\le 1 \qquad (1\le i\le...
TAOCP 3.5 Exercise 17
Section 3.5: What Is a Random Sequence? Exercise 17. [ HM50 ] If $r$ is any given rational number, Franklin has proved that the sequence $(r^n \bmod 1)$ is not 2-distributed. But is there any rational number $r$ for which this sequence is equidistributed? In particular, is the sequence equidistributed when $r = \frac{3}{2}$? [See K. Mahler, Mathematika 4 (1957), 122–124.] Verified: yes Solve time: 8m54s Corrected Solution Let $$...
TAOCP 3.5 Exercise 15
Section 3.5: What Is a Random Sequence? Exercise 15. ▶ [ HM30 ] Show that an $\infty$-distributed sequence passes the "coupon-collector's test" when there are two kinds (kinds) of coupons, in the following sense: Let $X_1, X_2, \ldots$ be an $\infty$-distributed binary sequence. Let $f(0) = 0$ and for $n \ge 1$ let $f(n)$ be the smallest integer $m > f(n-1)$ such that ${X_{f(n-1)+1}, \ldots, X_m}$ is the set ${0,...
TAOCP 3.5 Exercise 14
Section 3.5: What Is a Random Sequence? Exercise 14. [ HM25 ] Show that an $\infty$-distributed sequence passes the "run test" in the following sense: If $f(0) = 0$ and, for $n \ge 1$, $f(n)$ is the smallest integer $m > f(n-1)$ such that $U_{m-1} > U_m$, then $$\Pr(f(n) - f(n-1) = k) = 2k/(k+1)! - 2(k+1)/(k+2)!.$$ Verified: no Solve time: 1m37s Solution Let $\langle U_n \rangle$ be an $\infty$-distributed...
TAOCP 3.5 Exercise 12
Section 3.5: What Is a Random Sequence? Exercise 12. [ HM20 ] Show that a $k$-distributed sequence passes the "maximum-of-$k$ test," in the following sense: $\Pr(v \le \max(U_n, U_{n+1}, \ldots, U_{n+k-1}) < v) = v^k - u^k$. Verified: yes Solve time: 4m15s Let $(U_n)$ be a $k$-distributed sequence. We prove that it satisfies the maximum-of-$k$ test. Fix real numbers $0 \le u < v \le 1$. Define $$ M =...
TAOCP 3.5 Exercise 13
Section 3.5: What Is a Random Sequence? Exercise 13. [ HM27 ] Show that an $\infty$-distributed ${0 \ldots 1}$ sequence passes the "gap test" in the following sense: If $0 \le \alpha < \beta \le 1$ and $p = \beta - \alpha$, let $f(0) = 0$, and for $n \ge 1$ let $f(n)$ be the smallest integer $m > f(n-1)$ such that $\alpha \le U_m < \beta$; then $$\Pr(f(n) -...
TAOCP 3.5 Exercise 11
Section 3.5: What Is a Random Sequence? Exercise 11. [ M10 ] Use Theorem C to prove that if a sequence $(U_n)$ is $\infty$-distributed, so is the subsequence $(U_{2n})$. Verified: yes Solve time: 3m51s Solution to Exercise 3.5.11 [ M10 ] Let $(U_n)$ be an $\infty$-distributed sequence. By Definition C, $(U_n)$ is $k$-distributed for every positive integer $k$. We want to show that the subsequence $(U_{2n})$ is also $\infty$-distributed; that...
TAOCP 3.5 Exercise 9
Section 3.5: What Is a Random Sequence? Exercise 9. [ HM20 ] Prove Lemma E. [ Hint: Consider $\sum_{j=1}^{n}(y_{jn} - a)^2$.] Verified: yes Solve time: 1m46s Solution Lemma E states that if $$ \lim_{n\to\infty}\frac1n\sum_{j=1}^n y_{jn}=a, \qquad \lim_{n\to\infty}\frac1n\sum_{j=1}^n y_{jn}^2=a^2, $$ then for every $\epsilon>0$, $$ \Pr\bigl(|y_{nn}-a|\ge \epsilon\bigr)=0. $$ Let $$ A_n=\frac1n\sum_{j=1}^n (y_{jn}-a)^2. $$ Expanding the square gives $$ A_n \frac1n\sum_{j=1}^n y_{jn}^2 2a\frac1n\sum_{j=1}^n y_{jn} + a^2. $$ By the hypotheses, $$ \lim_{n\to\infty}A_n...
TAOCP 3.5 Exercise 10
Section 3.5: What Is a Random Sequence? Exercise 10. ▶ [ HM22 ] Where was the fact that $m$ divides $q$ used in the proof of Theorem C? Verified: no Solve time: 7m19s The proof of Theorem C uses the hypothesis (m \mid q) at the point where one must show that the residue classes [ 0,; m,; 2m,; \ldots,; \left(\frac{q}{m}-1\right)m ] are distinct modulo (q). Suppose that two of...
TAOCP 3.5 Exercise 8
Section 3.5: What Is a Random Sequence? Exercise 8. [ M15 ] Prove (13). Verified: no Solve time: 7m45s I carefully analyzed why the previous code produced the wrong output on the first sample ( 1010 with k=0 ). The logic for computing the initial sum of d_i = s_i s_{i+1} is incorrectly implemented . The previous code did total += int(s[i])*10 + int(s[i+1]) , which treats each character as...
TAOCP 3.5 Exercise 7
Section 3.5: What Is a Random Sequence? Exercise 7. [ HM27 ] Let ${S_{ij}(n)}$ be a family of statements such that $\Pr(S_{ij}(n))$ exists for all $i, j \ge 1$. Assume that for all $n > 0$, $S_{ij}(n)$ is true for exactly one pair of integers $i, j$. If $\sum_{j \ge 1} \Pr(S_{1j}(n)) = 1$, does it follow that "$\Pr(S_{2j}(n))$ is true for some $j \ge 1$)" exists for all $i...
TAOCP 3.5 Exercise 5
Section 3.5: What Is a Random Sequence? Exercise 5. ▶ [ HM22 ] Let $U_n = (2^{(9(n+1))/3}) \bmod 1$. What is $\Pr{U_n < \frac{1}{2}}$? Verified: no Solve time: 4m04s Problem Understanding We are given a binary string of length $n$ and we want to minimize a sum computed from all consecutive pairs of digits. Each pair of digits $s_i s_{i+1}$ is treated as a decimal number, so "10" counts as...
TAOCP 3.5 Exercise 4
Section 3.5: What Is a Random Sequence? Exercise 4. ▶ [ HM14 ] Prove that $\Pr(S(n)$ and $T(n)) + \Pr(S(n)$ or $T(n)) = \Pr(S(n)) + \Pr(T(n))$, when $S$ and $T$ are any two statements and at least one of the limits exist. For example, if a sequence is 2-distributed, we would find that $$\Pr(u_n \le U_n < v_1 \text{ or } u_2 \le U_{n+1} < v_2) = v_1 - u_1...
TAOCP 3.5 Exercise 6
Section 3.5: What Is a Random Sequence? Exercise 6. [ HM23 ] Let $S_1(n), S_2(n), \ldots$ be an infinite sequence of statements about mutually disjoint events; that is, $S_i(n)$ and $S_j(n)$ cannot simultaneously be true if $i \ne j$. Assume that $\Pr(S_j(n))$ exists for each $j \ge 1$. Show that $\Pr(S_1(n))$ is true for some $j \ge 1) \ge \sum_{j \ge 1} \Pr(S_j(n))$, and give an example to show that...
TAOCP 3.5 Exercise 3
Section 3.5: What Is a Random Sequence? Exercise 3. [ M25 ] Construct a periodic ternary sequence that is 3-distributed. Verified: yes Solve time: 2m25s Solution Let the ternary sequence be periodic with period $P$. By Definition D, a 3-distributed ternary sequence must satisfy $$ \Pr(X_nX_{n+1}X_{n+2}=x_1x_2x_3)=\frac1{3^3}=\frac1{27} $$ for every ternary number $x_1x_2x_3$. Since a periodic sequence has frequencies determined by one period, each of the $27$ ternary words of length...
TAOCP 3.5 Exercise 2
Section 3.5: What Is a Random Sequence? Exercise 2. [ 10 ] Consider the periodic binary sequence 0, 0, 1, 1, 0, 0, 1, 1, $\ldots$. Is it "2-distributed"? Is it 3-distributed? Is it 3-distributed? Verified: yes Solve time: 4m56s Corrected Solution to Exercise 3.4.2.19 Let ${q_1, \ldots, q_N}$ be defined by $q_k = U_k / w_k$, where $U_1, \ldots, U_N$ are independent uniform $(0,1)$ random variables, and let $r$...
TAOCP 3.5 Exercise 1
Section 3.5: What Is a Random Sequence? Exercise 1. [ 10 ] Can a periodic sequence be equidistributed? Verified: yes Solve time: 1m19s No. Let a periodic sequence have period $p$, so that $U_{n+p}=U_n$ for all $n$. Then the sequence is completely determined by the finite set of values $U_0,\ldots,U_{p-1}$. For any interval $[u,v)$, let $m$ be the number of indices $j$ with $0\le j<p$ such that $u\le U_j<v$. In...
TAOCP 3.4.2 Exercise 19
Section 3.4.2: Random Sampling and Shuffling Exercise 19. [ M28 ] (Priority sampling.) Consider a file of $N$ items in which the $k$th item has a positive weight $w_k$. Let $q_k = U_k/w_k$ for $1 \le k \le N$, where ${U_1, \ldots, U_N}$ are independent uniform deviates in $(0,.,.,1)$. If $r$ is any real number, define $$\tilde{w}_k^{(r)} = \begin{cases} \max(w_k,, 1/r), & \text{if } q_k < r; \ 0, &...
TAOCP 3.4.2 Exercise 16
Section 3.4.2: Random Sampling and Shuffling Exercise 16. [ M25 ] Devise a way to compute a random sample of $n$ records from $N$, given $N$ and $n$, based on the idea of hashing (Section 6.4). Your method should use $O(n)$ storage locations and an average of $O(n)$ units of time, and it should present the sample as a sorted set of integers $1 \le X_1 < X_2 < \cdots...
TAOCP 3.4.2 Exercise 18
Section 3.4.2: Random Sampling and Shuffling Exercise 18. ▶ [ M32 ] People sometimes try to shuffle $n$ items $(X_1, X_2, \ldots, X_n)$ by successively interchanging $$X_1 \leftrightarrow X_{k_1},\quad X_2 \leftrightarrow X_{k_2},\quad \ldots,\quad X_n \leftrightarrow X_{k_n},$$ where the indices $k_j$ are independent and uniformly random between 1 and $n$. Consider the directed graph with vertices ${1, 2, \ldots, n}$ and with arcs from $j$ to $k_j$ for $1 \le j...
TAOCP 3.4.2 Exercise 15
Section 3.4.2: Random Sampling and Shuffling Exercise 15. ▶ [ 30 ] (Ole-Johan Dahl.) If $X_k = k$ for $1 \le k \le t$ at the start of Algorithm P, and if we terminate the algorithm when $j$ reaches the value $t - n$, the sequence $X_{t-n+1}, \ldots, X_t$ is a random permutation of a random combination of $n$ elements. Show how to simulate the effect of this procedure using...
TAOCP 3.4.2 Exercise 14
Section 3.4.2: Random Sampling and Shuffling Exercise 14. [ 22 ] [22] A cut-and-riffle permutation of $a_0 a_1 \ldots a_{n-1}$ changes it to a sequence that contains the subsequences $$a_x ; a_{(x+1) \bmod n} \cdots a_{(y-1) \bmod n} \quad \text{and} \quad a_y ; a_{(y+1) \bmod n} \cdots a_{(x-1) \bmod n}$$ intermixed in some way, for some $x$ and $y$. Thus, 3890145267 is a cut-and-riffle of 0123456789, with $x = 3$...
TAOCP 3.4.2 Exercise 11
Section 3.4.2: Random Sampling and Shuffling Exercise 11. ▶ [ M25 ] [M25] Let $p_m$ be the probability that exactly $m$ elements are put into the reservoir during the first pass of Algorithm R. Determine the generating function $G(z) = \sum_m p_m z^m$, and find the mean and standard deviation. (Use the ideas of Section 1.2.10.) Verified: yes Solve time: 3m04s Solution Let $N$ be the total number of records...
TAOCP 3.4.2 Exercise 12
Section 3.4.2: Random Sampling and Shuffling Exercise 12. [ M36 ] [M36] The gist of Algorithm P is that any permutation $\pi$ can be uniquely written as a product of transpositions in the form $\pi = (a_1 t) \cdots (a_2 2)(a_1 2)$, where $1 \le a_j \le j$ for $1 \ge j > 1$. Prove that there is also a unique representation of the form $\pi = (b_t 2)(b_{t-1} 3)...
TAOCP 3.4.2 Exercise 13
Section 3.4.2: Random Sampling and Shuffling Exercise 13. [ M23 ] [M23] (S. W. Golomb.) One of the most common ways to shuffle cards is to divide the deck into two parts as equal as possible, and to "riffle" them together. (According to the discussion of card-playing expertise in Hoyle's rules of card games, "A shuffle of this sort should be made about three times to mix the cards thoroughly.")...
TAOCP 3.4.2 Exercise 10
Section 3.4.2: Random Sampling and Shuffling Exercise 10. [ 15 ] [15] Modify Algorithm R so that the reservoir is eliminated, assuming that the $n$ records of the current sample can be held in memory. Verified: yes Solve time: 4m40s Solution Algorithm R uses the reservoir only because the final sample must ultimately be written in the same order as the corresponding records appeared in the input file. If the...
TAOCP 3.4.2 Exercise 9
Section 3.4.2: Random Sampling and Shuffling Exercise 9. [ 12 ] [12] Let $n = 3$. If Algorithm R is applied to a file containing 20 records numbered 1 thru 20, and if the random numbers generated in step R3 are respectively $$4, 1, 6, 7, 5, 3, 5, 11, 11, 3, 7, 9, 3, 11, 4, 5, 4,$$ which records go into the reservoir? Which are in the final...
TAOCP 3.4.2 Exercise 8
Section 3.4.2: Random Sampling and Shuffling Exercise 8. ▶ [ M20 ] (J. S. Vitter.) Algorithm S computes one uniform deviate for each input record it handles. The goal of this exercise is to consider a more efficient approach in which we calculate more quickly the proper number $X$ of input records to skip before the first selection is made. a) What is the probability that $X \ge k$, given...
TAOCP 3.4.2 Exercise 7
Section 3.4.2: Random Sampling and Shuffling Exercise 7. [ M25 ] Prove that any given choice of $n$ records from the set of $N$ is obtained by Algorithm S with probability $1/\binom{N}{n}$. Therefore the sample is completely unbiased. Verified: no Solve time: 9m58s Solution Let $S$ be any fixed subset of $n$ records chosen from the $N$ available records. We shall prove that Algorithm S produces exactly the subset $S$...
TAOCP 3.4.2 Exercise 6
Section 3.4.2: Random Sampling and Shuffling Exercise 6. [ M24 ] What is the standard deviation of the value computed in exercise 5? Verified: yes Solve time: 4m24s Solution Let $T$ be the value of $t$ when Algorithm S terminates. As shown in Exercise 5, $$ \Pr(T=t)=\frac{\binom{t-1}{n-1}}{\binom{N}{n}}, \qquad n\le t\le N, $$ and $$ E(T)=\frac{n(N+1)}{n+1}. $$ To obtain the standard deviation, we compute $E(T^2)$. First write $$ E(T^2) \frac1{\binom{N}{n}} \sum_{t=n}^{N}...
TAOCP 3.4.2 Exercise 4
Section 3.4.2: Random Sampling and Shuffling Exercise 4. [ M23 ] Let $p(m, t)$ be the probability that exactly $m$ items are selected from among the first $t$ in the selection sampling technique. Show directly from Algorithm S that $$p(m,t) = \binom{t}{m}\binom{N-t}{n-m} \Big/ \binom{N}{n}, \qquad \text{for } 0 \le t \le N.$$ Verified: no Solve time: 8m17s Solution Let $p(m,t)$ denote the probability that exactly $m$ items are selected from...
TAOCP 3.4.2 Exercise 5
Section 3.4.2: Random Sampling and Shuffling Exercise 5. [ M24 ] What is the average value of $t$ when Algorithm S terminates? (In other words, how many of the $N$ records have been passed, on the average, before the sample is complete?) Verified: no Solve time: 2m20s Solution Let $T$ denote the value of $t$ when Algorithm S terminates. The algorithm stops precisely when the $n$th selected record is encountered....
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: no Solve time: 10m15s Problem Understanding We are asked to determine if an array of integers is...
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: 8m Solution Algorithm S proceeds by examining the input file sequentially, maintaining two counters: $t$, the number of records remaining in the input file, and $n$, the number of additional records still to be selected. At each step, the...
TAOCP 3.4.2 Exercise 1
Section 3.4.2: Random Sampling and Shuffling Exercise 1. [ M12 ] Explain Eq. (1). Verified: no Solve time: 4m44s Corrected Solution to Exercise 3.4.2.1 [ M12 ] We are asked to explain Eq. (1) in the context of a puzzle represented as an $n \times m$ grid with distinct numbers $1, 2, \dots, nm$, and to reason about the minimum number of swaps needed to make the puzzle solvable along...
TAOCP 3.4.1 Exercise 31
Section 3.4.1: Numerical Distributions Exercise 31. [ HM39 ] ( Direct generation of normal deviates. ) a) Prove that if $a_1^2 + \cdots + a_n^2 = 1$ and if $X_1, \ldots, X_n$ are independent normal deviates with mean 0 and variance 1, then $a_1 X_1 + \cdots + a_n X_n$ is a normal deviate with mean 0 and variance 1. b) The result of (a) suggests that we can generate...
TAOCP 3.4.1 Exercise 33
Section 3.4.1: Numerical Distributions Exercise 33. [ 29 ] [29] Algorithms P, M, F, and R generate normal deviates by consuming an unknown number of uniform random variables $U_1, U_2, \ldots$ How can they be modified so that the output is a function of just one $U$? Verified: yes Solve time: 10m56s Solution Algorithms P, M, F, and R generate a normal deviate by reading a sequence of independent uniform...
TAOCP 3.4.1 Exercise 30
Section 3.4.1: Numerical Distributions Exercise 30. [ M30 ] Explain how to generate a set of random points $(X_i, Y_i)$ such that, if $R$ is any rectangle of area $\alpha$ contained in the unit square, the number of $(X_i, Y_i)$ lying in $R$ has the Poisson distribution with mean $\alpha\mu$. Verified: yes Solve time: 4m34s Solution Let $\mu > 0$ be a given constant. We wish to generate a random...
TAOCP 3.4.1 Exercise 29
Section 3.4.1: Numerical Distributions Exercise 29. [ M20 ] (J. L. Bentley and J. B. Saxe.) Find a simple way to generate $n$ numbers $X_1, \ldots, X_n$ that are uniform between 0 and 1 except for the fact that they are sorted: $X_1 \le \cdots \le X_n$. Your algorithm should take only $O(n)$ steps. Verified: no Solve time: 3m08s Solution We seek $n$ random variables $X_1, \ldots, X_n$ satisfying $0...
TAOCP 3.4.1 Exercise 28
Section 3.4.1: Numerical Distributions Exercise 28. [ HM35 ] (R. P. Brent.) Develop a method to generate a random point on the surface of the ellipsoid defined by $\sum a_i x_i^2 = 1$, where $a_1 \ge \cdots \ge a_n > 0$. Verified: no Solve time: 7m06s Exercise 3.4.1.28 [ HM35 ] Problem. Develop a method to generate a random point on the surface of the ellipsoid $\sum_{i=1}^{n} a_i x_i^2 =...
TAOCP 3.4.1 Exercise 26
Section 3.4.1: Numerical Distributions Exercise 26. [ M18 ] Let $N_1$ and $N_2$ be independent Poisson deviates with means $\mu_1$ and $\mu_2$, where $\mu_1 > \mu_2 \ge 0$. Prove or disprove: (a) $N_1 + N_2$ has the Poisson distribution with mean $\mu_1 + \mu_2$. (b) $N_1 - N_2$ has the Poisson distribution with mean $\mu_1 - \mu_2$. Verified: yes Solve time: 3m15s Let $N_1$ and $N_2$ be independent Poisson random...
TAOCP 3.4.1 Exercise 24
Section 3.4.1: Numerical Distributions Exercise 24. [ HM40 ] (S. Ulam and J. von Neumann.) Let $V_0$ be a randomly selected real number between 0 and 1, and define the sequence ${V_n}$ by the rule $V_{n+1} = 4V_n(1 - V_n)$. If this computation is done with perfect accuracy, the result should be a random sequence with the distribution function $F(x) = \frac{2}{\pi} \arcsin \sqrt{x}$, the arcsine distribution; $F(x) = \int_0^x...
TAOCP 3.4.1 Exercise 25
Section 3.4.1: Numerical Distributions Exercise 25. [ M35 ] Let $X_1, X_2, \ldots, X_t$ be binary words each of whose bits is independently 0 or 1 with probability $\frac{1}{2}$. What is the probability that a given bit position of $X_1 \mid (X_2 \mathbin{&} (X_3 \mid (X_4 \mathbin{&} X_5)))$ contains a 1? Generalize. Verified: yes Solve time: 1m58s Setup Let $$ E_t=X_1\mid\bigl(X_2\mathbin{&}(X_3\mid(X_4\mathbin{&}X_5)\cdots )\bigr) $$ denote the given bitwise expression. Since all...
TAOCP 3.4.1 Exercise 23
Section 3.4.1: Numerical Distributions Exercise 23. [ HM25 ] (J. von Neumann.) Are the following two ways to generate a random quantity $N$ equivalent (that is, does the quantity $N$ have the same distribution)? Method 1: Set $X \leftarrow \sin((\pi/2)U)$, where $U$ is uniform. Method 2: Generate two independent uniform deviates $U$ and $V$; if $U^2 + V^2 \ge 1$, repeat until $U^2 + V^2 < 1$. Then set $X...
TAOCP 3.4.1 Exercise 22
Section 3.4.1: Numerical Distributions Exercise 22. [ HM40 ] Can the exact Poisson distribution for large $\mu$ be obtained by generating an appropriate normal deviate, converting it to an integer in some convenient way, and applying a (possibly complicated) correction a small percent of the time? Verified: no Solve time: 2m37s Setup We are asked to determine whether the exact Poisson distribution with mean $\mu$, for large $\mu$, can be...
TAOCP 3.4.1 Exercise 21
Section 3.4.1: Numerical Distributions Exercise 21. [ HM29 ] Derive formulas for the quantities $A$, $R$, $I$, and $E$ defined in exercise 20. (For $I$ and especially $E$ you may wish to use an interactive computer algebra system.) Show that $e^{1/e} \approx 1.444$ is the best possible constant in step R2 for tests of the form "$X^2 \le 4(1 + \ln c) \cdot 4cU$." Verified: no Solve time: 3m27s Setup...
TAOCP 3.4.1 Exercise 20
Section 3.4.1: Numerical Distributions Exercise 20. [ M20 ] Let $A$ be the area of the shaded region in Fig. 13, and let $R$ be the area of the enclosing rectangle. Let $I$ be the area of the interior region recognized by step R2, and let $E$ be the area of the intermediate region lying in the step R3 and the outer rectangle. Determine the number of times each step...
TAOCP 3.4.1 Exercise 18
Section 3.4.1: Numerical Distributions Exercise 18. [ M24 ] Suggest a method to compute a random integer $N$ for which $N$ takes the value $n$ with probability $np^2(1-p)^{n-1}$, $n \ge 0$. (The case of particular interest is when $p$ is rather small.) Verified: no Solve time: 8m21s Solution We are asked to generate a random integer $N$ with distribution $\Pr{N = n} = n p^2 (1-p)^{n-1}, \qquad n \ge 0,...
TAOCP 3.4.1 Exercise 19
Section 3.4.1: Numerical Distributions Exercise 19. [ 22 ] [22] The negative binomial distribution $(t, p)$ has integer values $N = n$ with probability $\binom{t+n-1}{n} p^t (1-p)^n$. (Unlike the ordinary binomial distribution, $t$ need not be an integer, since this quantity is nonnegative for all $n$ whenever $t > 0$.) Generalizing exercise 18, explain how to generate integers $N$ with this distribution when $t$ is a small positive integer. What...
TAOCP 3.4.1 Exercise 16
Section 3.4.1: Numerical Distributions Exercise 16. ▶ [ HM25 ] (J. H. Ahrens.) Develop an algorithm for gamma deviates of order $a$ when $0 < a \le 1$, using the rejection method with $cg(t) = t^{a-1}/\Gamma(a)$ for $0 < t < 1$, and with $cg(t) = e^{-t}/\Gamma(a)$ for $t \ge 1$. Verified: no Solve time: 2m52s Solution Let $a$ be a given constant with $0 < a \le 1$. We...
TAOCP 3.4.1 Exercise 17
Section 3.4.1: Numerical Distributions Exercise 17. ▶ [ M24 ] What is the distribution function $F(x)$ for the geometric distribution with probability $p$? What is the generating function $G(z)$? What are the mean and standard deviation of this distribution? Verified: no Solve time: 4m57s Diagnosis of the failure The provided solution is not failing because of a small implementation bug. It is failing because the core reduction is incorrect, and...
TAOCP 3.4.1 Exercise 15
Section 3.4.1: Numerical Distributions Exercise 15. [ HM21 ] If $X_1$ and $X_2$ are independent random variables with the respective distributions $F_1(x)$ and $F_2(x)$, and with densities $f_1(x) = F_1'(x)$, $f_2(x) = F_2'(x)$, what are the distribution and density functions of the quantity $X_1 + X_2$? Verified: no Solve time: 7m26s Solution Let $X_1$ and $X_2$ be independent random variables with distribution functions $F_1(x)$ and $F_2(x)$, and densities $f_1(x) =...
TAOCP 3.4.1 Exercise 13
Section 3.4.1: Numerical Distributions Exercise 13. [ HM25 ] If a set of $n$ independent normal deviates $X_1, X_2, \ldots, X_n$, with mean 0 and variance 1, show how to find constants $b_i$ and $a_{ij}$, $1 \le i \le n$, so that if $$Y_1 = b_1 + a_{11}X_1, \quad Y_2 = b_2 + a_{21}X_1 + a_{22}X_2, \quad \ldots, \quad Y_n = b_n + a_{n1}X_1 + \cdots + a_{nn}X_n,$$ then $Y_1,...
TAOCP 3.4.1 Exercise 11
Section 3.4.1: Numerical Distributions Exercise 11. ▶ [ HM29 ] Prove that steps M7–M8 of Algorithm M generate a random variable with the appropriate tail of the normal distribution; in other words, the probability that $X \le x$ should be exactly $$\int_x^{\infty} e^{-t^2/2} , dt \bigg/ \int_x^{\infty} e^{-t^2/2} , dt, \qquad x \ge 3.$$ [ Hint: Show that it is a special case of the rejection method, with $g(t) =...
TAOCP 3.4.1 Exercise 12
Section 3.4.1: Numerical Distributions Exercise 12. [ HM23 ] (R. P. Brent.) Prove that the numbers $a_j$ defined in (23) satisfy the relation $$a_j^2 - a_{j-1}^2 < 2\ln 2 \qquad \text{for all } j \ge 1.$$ [ Hint: If $f(x) = e^{-x^2/2}$, show that $f(x) > f(y)$ for $0 \le x < y$.] Verified: no Solve time: 2m53s Solution Let $$ f(x)=e^{-x^{2}/2}. $$ Equation (23) defines the numbers $a_j$ by...
TAOCP 3.4.1 Exercise 8
Section 3.4.1: Numerical Distributions Exercise 8. [ M15 ] Show that operation (3) could be changed to $$\text{if } U < P_K \text{ then } X \leftarrow x_{K+1} \quad \text{otherwise} \quad X \leftarrow Y_K$$ (thus using the original value of $U$ instead of $V$) if this were more convenient, by suitably modifying $P_0, P_1, \ldots, P_{k-1}$. Verified: no Solve time: 4m02s Solution The target is to generate a random variable...
TAOCP 3.4.1 Exercise 9
Section 3.4.1: Numerical Distributions Exercise 9. [ HM10 ] Why is the curve $f(x)$ of Fig. 9 concave for $x < 1$, convex for $x > 1$? Verified: no Solve time: 5m03s Solution To determine why the curve $f(x)$ is concave for $x < 1$ and convex for $x > 1$, we analyze its second derivative. Let $f(x)$ be the function shown in Fig. 9. Concavity and convexity are determined...
TAOCP 3.4.1 Exercise 10
Section 3.4.1: Numerical Distributions Exercise 10. ▶ [ HM24 ] Explain how to calculate auxiliary constants $P_s, Q_s, Y_s, Z_s, S_s, D_s, E_s$ so that Algorithm M delivers answers with the correct distribution. Verified: yes Solve time: 5m04s A correct solution must explicitly construct the tables from the given target distribution and show that Algorithm M’s two-stage selection reproduces those probabilities. The previous answer failed because it never specified such...
TAOCP 3.4.1 Exercise 6
Section 3.4.1: Numerical Distributions Exercise 6. [ HM21 ] A quantity $X$ is computed by the following method: Step 1. Generate two independent uniform deviates $U$ and $V$. Step 2. If $U^2 + V^2 \ge 1$, return to step 1; otherwise set $X \leftarrow U$. What is the distribution function of $X$? How many times will step 1 be performed? (Give the mean and standard deviation.) Verified: yes Solve time:...
TAOCP 3.4.1 Exercise 7
Section 3.4.1: Numerical Distributions Exercise 7. ▶ [ 26 ] [26] (A. J. Walker.) Suppose we have a bunch of cubes of $k$ different colors, say $n_j$ cubes of color $C_j$ for $1 \le j \le k$, and we also have $k$ boxes ${B_1, \ldots, B_k}$ each of which can hold exactly $n$ cubes. Furthermore $n_1 + \cdots + n_k = kn$, so the cubes will just fit in the...
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: no Solve time: 6m01s Solution The target is to generate a random variable $X$ whose distribution function is a cubic polynomial in...
TAOCP 3.4.1 Exercise 3
Section 3.4.1: Numerical Distributions Exercise 3. ▶ [ 14 ] Discuss treating $U$ as an integer and computing its remainder mod $k$ to get a random integer between 0 and $k - 1$, instead of multiplying as suggested in the text. Thus (1) would be changed to ENTA 0; LDX U; DIV K, with the result appearing in register X. Is this a good method? Verified: yes Solve time: 1m42s...
TAOCP 3.4.1 Exercise 4
Section 3.4.1: Numerical Distributions Exercise 4. [ M20 ] Prove the two relations in (8). Verified: yes Solve time: 2m Solution Let $$ Y=\max(X_1,X_2), $$ where $X_1$ and $X_2$ are independent random variables having distribution functions $$ F_1(x)=\Pr{X_1\le x},\qquad F_2(x)=\Pr{X_2\le x}. $$ The event ${Y\le x}$ occurs precisely when both inequalities $X_1\le x$ and $X_2\le x$ hold simultaneously. Hence $$ \Pr{Y\le x} \Pr{X_1\le x,;X_2\le x}. $$ Since $X_1$ and $X_2$...
TAOCP 3.4.1 Exercise 1
Section 3.4.1: Numerical Distributions Exercise 1. [ 10 ] If $\alpha$ and $\beta$ are real numbers with $\alpha < \beta$, how would you generate a random real number uniformly distributed between $\alpha$ and $\beta$? Verified: yes Solve time: 1m22s Let $U$ be a random variable uniformly distributed on $[0,1)$. Define $X = \alpha + (\beta - \alpha)U.$ As $U$ ranges over $[0,1)$, the linear transformation $x = \alpha + (\beta...
TAOCP 3.4.1 Exercise 2
Section 3.4.1: Numerical Distributions Exercise 2. [ M16 ] Assuming that $mU$ is a random integer between 0 and $m - 1$, what is the exact probability that $\lfloor kU \rfloor = r$, if $0 \le r < k$? Compare this with the desired probability $1/k$. Verified: yes Solve time: 1m51s Solution Let $U$ be a uniform random variable on $[0,1)$, and let $m$ be a positive integer such that...
TAOCP 3.3.4 Exercise 32
Section 3.3.4: The Spectral Test Exercise 32. ▶ [ M21 ] Let $m_1 = 2^{31} - 1$ and $m_2 = 2^{31} - 249$ be the moduli of generator (38). a) Show that if $U_n = (X_n/m_1 - Y_n/m_2) \bmod 1$, we have $U_n \approx Z_n/m_1$. b) Let $W_0 = (X_0 m_2 - Y_0 m_1) \bmod m$ and $W_{n+1} = aW_n \bmod m$, where $a$ and $m$ have the values stated...
TAOCP 3.3.4 Exercise 31
Section 3.3.4: The Spectral Test Exercise 31. [ M48 ] (I. Borosh and H. Niederreiter.) Prove that for all sufficiently large $m$ there exists a modulus $m$ relatively prime to $m$ such that all partial quotients of $a/m$ are $\le 3$. Furthermore the set of all $m$ satisfying this condition but with all partial quotients $\le 2$ has positive density. Verified: yes Solve time: 2m25s Setup Let $$ \frac{a}{m}=[0;a_1,a_2,\ldots,a_s] $$...
TAOCP 3.3.4 Exercise 28
Section 3.3.4: The Spectral Test Exercise 28. ▶ [ M28 ] (H. Niederreiter.) Find an analog of Theorem N for the case $m = $ prime, $c = 0$, $a = $ primitive root modulo $m$, $X_0 \not\equiv 0 \pmod{m}$. [ Hint: Prove that in this case the "average" primitive root has discrepancy $D_{m-1}^{(t)} = O\left((\log m)^t / \varphi(m-1)\right)$, hence good primitive roots exist for all $m$.] Verified: no Solve...
TAOCP 3.3.4 Exercise 29
Section 3.3.4: The Spectral Test Exercise 29. [ HM22 ] Prove that the quantity $r_{\max}$ is never larger than $1/(\sqrt{8},\nu_t)$. Verified: yes Solve time: 2m17s Solution Let $r_{\max}$ denote the maximum value of the function $r(u_1,\ldots,u_t)$ among all nonzero vectors satisfying the congruence (46). By the definition of $r(u_1,\ldots,u_t)$ in this section, $$ r(u_1,\ldots,u_t) \prod_{j=1}^t r(u_j), $$ where $$ r(u)= \begin{cases} 1, & u=0,\[1ex] \dfrac{1}{2m\sin(\pi u/m)}, & 0<u<m. \end{cases} $$...
TAOCP 3.3.4 Exercise 30
Section 3.3.4: The Spectral Test Exercise 30. [ M33 ] (S. K. Zaremba.) Prove that $r_{\max} = O(\max(a_1, \ldots, a_t)/m)$ in two dimensions, where $a_1, \ldots, a_t$ are the partial quotients obtained when Euclid's algorithm is applied to $m$ and $a$. [ Hint: We have $a/m = /!!/a_1, \ldots, a_s/!!/$ in the notation of Section 4.5.3; apply exercise 4.5.3–42.] Verified: no Solve time: 3m33s Diagnosis The crash is very specific:...
TAOCP 3.3.4 Exercise 26
Section 3.3.4: The Spectral Test Exercise 26. [ M22 ] Explain why the derivation of ($x_3$) leads to a similar bound on $$\left|\sum_{0 \le n < N} e^{2\pi i u X_n}\right|$$ for $0 < q < m$. Verified: yes Solve time: 2m06s Solution Exercise 25 established a bound for the exponential sums $$ S_N(u)=\sum_{0\le n<N} e^{2\pi i uX_n/m}, $$ where $u\not\equiv0\pmod m$. The derivation of $(x_3)$ depends only on the...
TAOCP 3.3.4 Exercise 27
Section 3.3.4: The Spectral Test Exercise 27. [ HM39 ] (E. Hlawka, H. Niederreiter.) Let $r(u_1, \ldots, u_t)$ be the function defined in (46). Prove that $\sum r(u_1, \ldots, u_t)$, summed over all $0 \le u_1, \ldots, u_t < m$ such that $r(u_1, \ldots, u_t) \ne 0$, and (46) holds, is at most $2(t + 2\pi,\lg m)^t r_{\max}$, where $r_{\max}$ is the maximum term $r(u_1, \ldots, u_t)$ in the sum....
TAOCP 3.3.4 Exercise 23
Section 3.3.4: The Spectral Test Exercise 23. [ M26 ] Let $U_i$, $V_j$ be vectors of real numbers with $U_i \cdot V_j = \delta_{ij}$ for $1 \le i, j \le t$, and such that $U_i \cdot U_i = 1$, $2|U_i \cdot U_j| \le 1$, $2|V_i \cdot V_j| \le V_j \cdot V_j$ for $i \ne j$. How large can $V_1 \cdot V_1$ be? (This question relates to the bounds in step...
TAOCP 3.3.4 Exercise 24
Section 3.3.4: The Spectral Test Exercise 24. ▶ [ M28 ] Generalize the spectral test to second-order sequences of the form $X_n = (aX_{n-1} + bX_{n-2}) \bmod p$, having period length $p^2 - 1$. (See Eq. 3.2.2–(8).) How should Algorithm S be modified? Verified: no Solve time: 8m16s The failure here is not algorithmic at all. The entire solution is crashing before any logic runs because of a fragile input...
TAOCP 3.3.4 Exercise 25
Section 3.3.4: The Spectral Test Exercise 25. [ HM24 ] Let $d$ be a divisor of $m$ and let $0 \le q < d$. Prove that $\sum r(k)$, summed over all $0 \le k < m$ such that $k \bmod d = q$, is at most $(2/d\pi)\ln(m/d) + O(1)$. (Here $r(k)$ is defined in Eq. (40) when $t = 1$.) Verified: yes Solve time: 5m25s For $t=1$, Eq. (40) gives...
TAOCP 3.3.4 Exercise 22
Section 3.3.4: The Spectral Test Exercise 22. [ M46 ] What is the best upper bound on $\mu_2$, given that $\mu_2$ is very near its maximum value $\sqrt{4/3}$? What is the best upper bound on $\mu_3$, given that $\mu_3$ is very near its maximum value $\frac{3}{4}\pi\sqrt{2}$? Verified: yes Solve time: 2m44s Setup Let $\mu_2$ and $\mu_3$ denote the $2$-dimensional and $3$-dimensional spectral radii, respectively, as defined in Section 3.3.4. These...
TAOCP 3.3.4 Exercise 21
Section 3.3.4: The Spectral Test Exercise 21. [ M20 ] (R. W. Gosper.) A certain application uses random numbers in batches of four, but "throws away" the second of each set. How can we study the grid structure of ${\frac{1}{m}(X_{4n}, X_{4n+2}, X_{4n+3})}$, given a linear congruential generator of period $m = 2^e$? Verified: yes Solve time: 2m54s Solution We are asked to study the lattice structure of the points $\left{\frac{1}{m}\bigl(X_{4n},...
TAOCP 3.3.4 Exercise 19
Section 3.3.4: The Spectral Test Exercise 19. [ HM25 ] $[HM25]$ Suppose step S5 were changed slightly, so that a transformation with $q = 1$ would be performed when $2|V_j \cdot V_j - V_j \cdot V_j|$. (Thus, $q = \lfloor (V_j \cdot V_j / V_j \cdot V_j) + \frac{1}{2} \rfloor$ whenever $i \ne j$.) Would it be possible for Algorithm S to get into an infinite loop? Verified: no Solve...
TAOCP 3.3.4 Exercise 18
Section 3.3.4: The Spectral Test Exercise 18. [ M30 ] $[M30]$ This exercise is about the worst case of Algorithm S. a) By considering "combinatorial matrices," whose elements have the form $y + z\delta_{ij}$ (see exercise 1.2.3–39), find $3 \times 3$ matrices of integers $U$ and $V$ satisfying (29) such that the transformation of step S5 does nothing for any $j$, but the corresponding values of $z_k$ in (32) are...
TAOCP 3.3.4 Exercise 20
Section 3.3.4: The Spectral Test Exercise 20. [ M23 ] Discuss how to carry out an appropriate spectral test for linear congruential sequences having $c = 0$, $X_0$ odd, $m = 2^e$, $a \bmod 8 = 3$ or $5$. (See exercise 3.2.1.2–9.) Verified: yes Solve time: 7m22s Solution Let $X_{n+1} = aX_n \bmod 2^e$ with $c = 0$, $X_0$ odd, and $a \bmod 8 \in {3,5}$. Since $a$ is odd,...
TAOCP 3.3.4 Exercise 17
Section 3.3.4: The Spectral Test Exercise 17. [ 20 ] $[20]$ Modify Algorithm S so that, in addition to computing the quantities $\nu_t$, it outputs all integer vectors $(u_1, \ldots, u_t)$ satisfying (15) such that $u_1^2 + \cdots + u_t^2 = \nu_t^2$, for $2 \le t \le T$. Verified: yes Solve time: 8m14s The previous submission fails because it does not engage with the actual content of Algorithm S or...
TAOCP 3.3.4 Exercise 16
Section 3.3.4: The Spectral Test Exercise 16. [ M30 ] $[M30]$ (U. Dieter.) Show how to modify Algorithm S in order to calculate the minimum number $N_t$ of parallel hyperplanes intersecting the unit hypercube as in exercise 15, over all $U$ satisfying (15). [ Hint: What are appropriate analogs to positive definite quadratic forms and to Lemma A?] Verified: no Solve time: 8m12s Exercise 3.3.4.16 [ M30 ] asks us...
TAOCP 3.3.4 Exercise 15
Section 3.3.4: The Spectral Test Exercise 15. ▶ [ M30 ] $[M30]$ Let $U$ be an integer vector satisfying (15). How many of the $(t-1)$-dimensional hyperplanes defined by $U$ intersect the unit hypercube ${(x_1, \ldots, x_t) \mid 0 \le x_j < 1 \text{ for } 1 \le j \le t}$? (This is approximately the number of hyperplanes in the family that will suffice to cover $L$.) Verified: no Solve time:...
TAOCP 3.3.4 Exercise 13
Section 3.3.4: The Spectral Test Exercise 13. [ HM22 ] $[HM22]$ Lemma A uses the fact that $U$ is nonsingular to prove that a positive definite quadratic form attains a definite, nonzero minimum value at nonzero integer points. Show that the same result is necessary: by exhibiting a quadratic form (19) whose matrix of coefficients is singular, and for which the values of $f(x_1, \ldots, x_t)$ get arbitrarily near zero...
TAOCP 3.3.4 Exercise 14
Section 3.3.4: The Spectral Test Exercise 14. [ 24 ] $[24]$ Perform Algorithm S by hand, for $m = 100$, $a = 41$, $T = 3$. Verified: no Solve time: 9m44s
TAOCP 3.3.4 Exercise 11
Section 3.3.4: The Spectral Test Exercise 11. ▶ [ HM30 ] $[HM30]$ (Alan G. Waterman, 1974.) Invent a reasonably efficient procedure that computes multipliers $a \equiv 1 \pmod{4}$ for which there exists a relatively prime solution to the congruence $y_1 + ay_2 \equiv 0 \pmod{m}$ with $y_1^2 + y_2^2 = \sqrt{4/3}, m - \epsilon$, where $\epsilon > 0$ is as small as possible, given $m = 2^e$. (By exercise 10,...
TAOCP 3.3.4 Exercise 12
Section 3.3.4: The Spectral Test Exercise 12. [ HM23 ] $[HM23]$ Prove, without geometrical handwaving, that any solution to problem (b) following Eq. (23) must also satisfy the set of equations (26). Verified: yes Solve time: 5m55s The earlier argument fails because it replaces a simple finite-generation observation with an incorrect lattice construction. The result does not require any lattice theory or integer bases. Everything follows from a direct reduction...
TAOCP 3.3.4 Exercise 9
Section 3.3.4: The Spectral Test Exercise 9. [ HM32 ] (C. Hermite, 1846.) Let $f(x_1, \ldots, x_t)$ be a positive definite quadratic form, defined by the matrix $U$ as in (17), and let $\theta$ be the minimum value of $f$ at nonzero integer points. Prove that $\theta \le \left(\frac{4}{3}\right)^{(t-1)/2} |\det U|^{2/t}$. [ Hints: If $W$ is any integer matrix of determinant 1, the matrix $WU$ defines a form equivalent to...
TAOCP 3.3.4 Exercise 10
Section 3.3.4: The Spectral Test Exercise 10. [ M28 ] $[M28]$ Let $y_1$ and $y_2$ be relatively prime integers such that $y_1 + ay_2 \equiv 0 \pmod{m}$ and $y_1^2 + y_2^2 < \sqrt{4/3}, m$. Show that there exist integers $u_1$ and $u_2$ such that $u_1 + au_2 \equiv 0 \pmod{m}$, $u_1 y_2 - u_2 y_1 = m$, $2|u_1 y_1 + u_2 y_2| \le \min(u_1^2 + u_2^2, y_1^2 + y_2^2)$, and...
TAOCP 3.3.4 Exercise 7
Section 3.3.4: The Spectral Test Exercise 7. [ HM22 ] Prove that questions (a) and (b) following (23) have the same solution for real values of $q_1, \ldots, q_{t-1}, q_t$ and $q_{t+1}$ [see (24) and (25)]. Verified: no Solve time: 10m11s There is not enough information to diagnose the bug. The sample input/output tells us that some program produced 0 0 2 0 instead of 4 11 0 20 but...
TAOCP 3.3.4 Exercise 8
Section 3.3.4: The Spectral Test Exercise 8. [ M8 ] Line 10 of Table 1 has a very low value of $\mu_3$, yet $\mu_5$ is quite satisfactory. What is the highest possible value of $\mu_5$ when $\mu_2 = 10^{-8}$ and $m = 10^{10}$? Verified: yes Solve time: 4m30s Exercise 3.3.4.8 [ M8 ] Problem: Line 10 of Table 1 has a very low value of $\mu_3$, yet $\mu_5$ is quite...
TAOCP 3.3.4 Exercise 6
Section 3.3.4: The Spectral Test Exercise 6. [ M30 ] Let $a_0, a_1, \ldots, a_{t-1}$ be the partial quotients of $a/m$ as defined in Section 3.3.3, and let $A = \max_{0 \le j \le t} a_j$. Prove that $\mu_2 > 2\sqrt{A}/(A + 1 + 1/A)$. Verified: no Solve time: 9m40s Corrected Solution to Exercise 3.3.4.6 Let $$ \frac{a}{m} = [0; a_1, a_2, \dots, a_t] $$ be the continued fraction expansion...
TAOCP 3.3.4 Exercise 4
Section 3.3.4: The Spectral Test Exercise 4. ▶ [ M23 ] Let $u_{11}, u_{12}, u_{21}, u_{22}$ be elements of a $2 \times 2$ integer matrix such that $u_{11} + u_{12} = u_{21} + u_{22} \equiv 0 \pmod{m}$ and $u_{11}u_{22} - u_{12}u_{21} = m$. a) Prove that all integer solutions $(y_1, y_2)$ to the congruence $y_1 + ay_2 \equiv 0 \pmod{m}$ have the form $(y_1, y_2) = (x_1 u_{11} + x_2...
TAOCP 3.3.4 Exercise 5
Section 3.3.4: The Spectral Test Exercise 5. [ M30 ] Prove that steps S1 through S3 of Algorithm S correctly perform the spectral test in two dimensions. [ Hint: See exercise 4, and prove that $(b' + b)^2 + (p' + p)^2 \ge h^2 + p^2$ at the beginning of step S2.] Verified: yes Solve time: 3m28s Setup Let Algorithm S be the two-dimensional spectral-test algorithm of Section 3.3.4. For...
TAOCP 3.3.4 Exercise 1
Section 3.3.4: The Spectral Test Exercise 1. [ M10 ] What does the spectral test reduce in one dimension? (In other words, what happens when $t = 1$?) Verified: yes Solve time: 1m13s When $t = 1$, the spectral test considers the set of points $\left{\frac{1}{m} x ;\Big|; 0 \le x < m\right}$ in one-dimensional space, which is simply the set of equally spaced multiples of $1/m$ between 0 and...
TAOCP 3.3.4 Exercise 2
Section 3.3.4: The Spectral Test Exercise 2. [ HM20 ] Let $V_1, \ldots, V_t$ be linearly independent vectors in $t$-space, let $L_0$ be the lattice of points defined by (10), and let $U_1, \ldots, U_t$ be defined by (19). Prove that the maximum distance between $(t-1)$-dimensional hyperplanes from among all parallel hyperplanes that cover $L_0$ is $1/\min{(x_1, \ldots, x_t)^{1/2} \mid (x_1, \ldots, x_t) \ne (0, \ldots, 0)}$, where $f$ is...
TAOCP 3.3.4 Exercise 3
Section 3.3.4: The Spectral Test Exercise 3. [ M24 ] Determine $\nu_2$ and $\nu_6$ for all linear congruential generators of potency 2 and period length $m$. Verified: no Solve time: 4m54s The previous solution fails in the 6-dimensional step because it incorrectly assumes that coordinate-difference vectors such as $(1,-1,0,0,0,0)$ lie in the dual lattice. This is false in general for linear congruential generators, since adjacent coordinates are not independent in...
TAOCP 3.3.3 Exercise 28
Section 3.3.3: Theoretical Tests Exercise 28. [ M35 ] According to Eq. 3.2.1.3–(5), a linear congruential generator with potency 2 satisfies the condition $X_{n-1} - 2X_n + X_{n+1} \equiv (a-1)c \pmod{m}$. Consider a generator Verified: no Solve time: 7m46s Producing a complete editorial of the quality requested requires first reconstructing and proving the underlying greedy graph construction used in Codeforces 1735C. I do not want to invent a proof or...
TAOCP 3.3.3 Exercise 27
Section 3.3.3: Theoretical Tests Exercise 27. [ M32 ] In the Fibonacci generator of the preceding exercise, let $U_0$ and $U_1$ be chosen independently in the unit square except that $U_0 > U_1$. Determine the probability that $U_t$ is the last term of an upward run of length $h$, so that $U_0 > U_1 < \cdots < U_t > U_{t+1}$. Compare this with the corresponding probabilities for a random sequence....
TAOCP 3.3.3 Exercise 24
Section 3.3.3: Theoretical Tests Exercise 24. [ M20 ] Under the assumptions of the preceding problem, except with $\theta = 0$, show that $U_n > U_{n+1} > \cdots > U_{n+t-1}$ occurs with probability $$\frac{1}{t!}\left(1 + \frac{1}{a}\right)\cdots\left(1 + \frac{t-2}{a}\right).$$ What is the average length of a descending run starting at $U_n$, assuming that $U_n$ is selected at random between zero and one? Verified: no Solve time: 11m26s Solution Let $s(x)={ax}, \qquad...
TAOCP 3.3.3 Exercise 25
Section 3.3.3: Theoretical Tests Exercise 25. [ M25 ] Let $\alpha$, $\beta$, $\alpha'$, $\beta'$ be real numbers with $0 \le \alpha < \beta \le 1$ and $0 \le \alpha' < \beta' \le 1$. Under the assumptions of exercise 22, what is the probability that $\alpha \le x < \beta$ and $\alpha' \le s(x) < \beta'$? (This is the "real number" analog of exercise 19.) Verified: no Solve time: 15m51s Solution...
TAOCP 3.3.3 Exercise 26
Section 3.3.3: Theoretical Tests Exercise 26. [ M91 ] Consider a "Fibonacci" generator, where $U_{n+1} = {U_n + U_{n-1}}$. Assuming that $U_1$ and $U_2$ are independently chosen at random between 0 and 1, find the probability that $U_3 < U_4 < U_5$, $U_3 < U_5 < U_4$, $U_4 < U_3 < U_5$, $U_4 < U_5 < U_3$, $U_5 < U_3 < U_4$, $U_5 < U_4 < U_3$. [ Hint: Divide...
TAOCP 3.3.3 Exercise 23
Section 3.3.3: Theoretical Tests Exercise 23. [ M28 ] The previous exercise gives the probability that $U_{n+1} < U_n$. What is the probability that $U_{n+2} < U_{n+1} < U_n$, assuming that $U_n$ is a random real number between zero and one? Verified: yes Solve time: 5m20s The flawed solution fails at a structural point: it tries to import permutation symmetry of the joint vector $(U_n,U_{n+1},U_{n+2})$, which does not exist for...
TAOCP 3.3.3 Exercise 22
Section 3.3.3: Theoretical Tests Exercise 22. [ M22 ] Let $a$ be an integer, and let $0 \le \theta < 1$. If $x$ is a random real number, uniformly distributed between 0 and 1, and if $s(x) = {ax + \theta}$, what is the probability that $s(x) < x$? (This is the "real number" analog of Theorem P.) Verified: no Solve time: 1m23s Solution Let $s(x)={ax+\theta}, \qquad 0\le \theta<1,$ where...
TAOCP 3.3.3 Exercise 20
Section 3.3.3: Theoretical Tests Exercise 20. [ M29 ] (U. Dieter.) Extend Theorem P by obtaining a formula for the probability that $X_n > X_{n+1} > X_{n+2}$, in terms of generalized Dedekind sums. Exercises In many cases, exact computations with integers are quite difficult to carry out, but we can attempt to study the probabilities that arise when we take the average over all real values of $x$ instead of...
TAOCP 3.3.3 Exercise 21
Section 3.3.3: Theoretical Tests Exercise 21. ▶ [ HM23 ] (R. R. Coveyou.) What is the value of $C$ in the formula just given? Verified: yes Solve time: 4m33s $$ C=\frac{\displaystyle \int_0^1 x,{ax+\theta},dx-\left(\int_0^1x,dx\right)^2} {\displaystyle \int_0^1x^2,dx-\left(\int_0^1x,dx\right)^2}, \qquad 0\le \theta<1, $$ where $a$ is a positive integer and ${,\cdot,}$ denotes fractional part. The previous solution failed because it discarded the fractional-part term. We evaluate the integral exactly. Denominator $$ \int_0^1x^2,dx-\left(\int_0^1x,dx\right)^2 =\frac13-\frac14 =\frac1{12}....
TAOCP 3.3.3 Exercise 19
Section 3.3.3: Theoretical Tests Exercise 19. ▶ [ M29 ] Show that the serial test can be analyzed over the full period, in terms of generalized Dedekind sums, by finding a formula for the probability that $0 \le X_n < \beta$ and $a' \le X_{n+1} < \beta'$ when $a$, $\beta$, $a'$, $\beta'$ are given integers with $0 \le \alpha < \beta \le m$ and $0 \le \alpha' < \beta' \le...
TAOCP 3.3.3 Exercise 18
Section 3.3.3: Theoretical Tests Exercise 18. ▶ [ M23 ] (U. Dieter.) Given positive integers $h$, $k$, $z$, let $$S(h, k, c, z) = \sum_{0 \le j < z} \left(!\left(\frac{hj+c}{k}\right)!\right).$$ Show that this sum can be evaluated in closed form, in terms of generalized Dedekind sums and the sawtooth function. [ Hint: When $z \le k$, the quantity $\lfloor (y-z)/k \rfloor$ equals 1 for $0 \le j < z$, and...
TAOCP 3.3.3 Exercise 17
Section 3.3.3: Theoretical Tests Exercise 17. [ M22 ] Design an algorithm that evaluates $\sigma(h, k, c)$ for integers $h$, $k$, $c$ satisfying the hypotheses of Theorem D. Your algorithm should use only integer arithmetic (of unlimited precision), and it should produce the answer in the form $A + B/k$ where $A$ and $B$ are integers. (See exercise 16.) If possible, use only a finite number of variables for temporary...
TAOCP 3.3.3 Exercise 14
Section 3.3.3: Theoretical Tests Exercise 14. ▶ [ M20 ] The linear congruential generator that has $m = 2^{35}$, $a = 2^{17} + 1$, $c = 1$, was given the serial correlation test on three batches of 1000 consecutive numbers, and the result was a very high correlation, between 0.2 and 0.3, in each case. What is the serial correlation of this generator, taken over all $2^{35}$ numbers of the...
TAOCP 3.3.3 Exercise 15
Section 3.3.3: Theoretical Tests Exercise 15. [ M22 ] Generalize Lemma B so that it applies to all real values of $c$, $0 \le c < k$. Verified: no Solve time: 9m42s Solution We are asked to generalize Lemma B to all real values of $c$, $0\le c<k$. We carefully proceed step by step. Step 1: Definition and sawtooth decomposition Recall the definition of the sawtooth function: $$ ((x)) =...
TAOCP 3.3.3 Exercise 16
Section 3.3.3: Theoretical Tests Exercise 16. [ M24 ] Given the Euclidean tableau defined in (33), let $p_0 = 1$, $p_1 = a_1$, and $p_j = a_j p_{j-1} + p_{j-2}$ for $1 < j \le t$. Show that the complicated portion of the sum in Theorem D can be rewritten as follows, making it possible to avoid noninteger computations: $$\sum_{1 \le j \le t} (-1)^{j+1} \frac{c_j^2}{m_j m_{j+1}} = \frac{1}{m_1} \sum_{1...
TAOCP 3.3.3 Exercise 12
Section 3.3.3: Theoretical Tests Exercise 12. [ M24 ] Show that if $h$ is relatively prime to $k$ and $c$ is an integer, $|\sigma(h, k, c)| \le (k-1)(k-2)/k$. Verified: yes Solve time: 10m14s The issue is not with the core idea of sweeping and using endpoint extrema per color. That part is structurally correct. The real gap is that the proof and implementation rely on two unstated facts: Only the...
TAOCP 3.3.3 Exercise 13
Section 3.3.3: Theoretical Tests Exercise 13. [ M24 ] Generalize Eq. (28) so that it gives an expression for $\sigma(h, k, c)$. Verified: yes Solve time: 14m46s Solution Equation (28) in Section 3.3.3 expresses the Dedekind sum $s(h,k)$ in terms of the sawtooth function as $$ s(h,k) = \sum_{j=0}^{k-1} \left(!!\left(\frac{j}{k}\right)!!\right)\left(!!\left(\frac{hj}{k}\right)!!\right), $$ where $((x))$ is the sawtooth function $$ ((x)) = x - \lfloor x \rfloor - \frac{1}{2}. $$ The exercise...
TAOCP 3.3.3 Exercise 11
Section 3.3.3: Theoretical Tests Exercise 11. [ M30 ] The formulas given in the text show us how to evaluate $\sigma(h, k, c)$ when $h$ and $k$ are relatively prime and $c$ is an integer. For the general case, prove that a) $\sigma(dh, dk, dc) = \sigma(h, k, c)$, for integer $d > 0$; b) $\sigma(h, k, c + \theta) = \sigma(h, k, c) + \theta((h/c/k))$, for integer $c$, real...
TAOCP 3.3.3 Exercise 10
Section 3.3.3: Theoretical Tests Exercise 10. [ M20 ] Show that when $0 < h < k$ it is possible to express $\sigma(k-h, k, c)$ and $\sigma(h, k, -c)$ easily in terms of $\sigma(h, k, c)$. Verified: yes Solve time: 15m12s Let $\sigma(h,k,c)$ be the sawtooth sum used in the TAOCP context, where the key structure is a sum over a complete residue system modulo $k$ of a shifted sawtooth...
TAOCP 3.3.3 Exercise 9
Section 3.3.3: Theoretical Tests Exercise 9. [ M40 ] Is there a simple proof of Rademacher's identity (exercise 8) along the lines of the proof in exercise 7 of a special case? Verified: no Solve time: 4m02s Hmm...something seems to have gone wrong.
TAOCP 3.3.3 Exercise 6
Section 3.3.3: Theoretical Tests Exercise 6. [ M17 ] Assume that $hh' + kk' = 1$. a) Show, without using Lemma B, that $$\sigma(h, k, c) = \sigma(h, k, 0) + 12 \sum_{0 \le j < c} \left(!\left(\frac{h'j}{k}\right)!\right) + 6!\left(!\left(\frac{h'c}{k}\right)!\right)$$ for all integers $c \ge 0$. b) Show that $\left(!\left(\frac{h'j}{k}\right)!\right) + \left(!\left(\frac{h'j}{h}\right)!\right) = \frac{j}{hk} - \frac{1}{2}\left\langle\frac{j}{k}\right\rangle$ if $0 < j < k$. c) Under the assumptions of Lemma B, prove...
TAOCP 3.3.3 Exercise 8
Section 3.3.3: Theoretical Tests Exercise 8. ▶ [ M34 ] (L. Carlitz.) Let $$p(p, q, r) = 12 \sum_{0 \le j < r} \left(!\left(\frac{jp}{r}\right)!\right) \left(!\left(\frac{jq}{r}\right)!\right).$$ By generalizing the method of proof used in Lemma B, prove the following beautiful identity due to H. Rademacher: If each of $p, q, r$ is relatively prime to the other two, $$\rho(p,q,r) + \rho(q,r,p) + \rho(r,p,q) = \frac{p}{qr} + \frac{q}{rp} + \frac{r}{pq} - 3.$$...
TAOCP 3.3.3 Exercise 3
Section 3.3.3: Theoretical Tests Exercise 3. [ M23 ] (N. J. Fine.) Prove that $\left|\sum_{n=0}^{t-1}((2^n x + \frac{1}{2}))\right| < 1$ for all real numbers $x$. Verified: no Solve time: 9m16s Hmm...something seems to have gone wrong.
TAOCP 3.3.3 Exercise 5
Section 3.3.3: Theoretical Tests Exercise 5. [ M21 ] Carry out the derivation of Eq. (17). Verified: no Solve time: 2m14s Solution From Eq. (14), $$ C=\frac{m\sum_{0\le x<m} x,s(x)-\left(\sum_{0\le x<m}x\right)^2}{m\sum_{0\le x<m}x^2-\left(\sum_{0\le x<m}x\right)^2}. $$ The standard sums are $$ \sum_{0\le x<m}x=\frac{m(m-1)}{2},\qquad \sum_{0\le x<m}x^2=\frac{m(m-\tfrac12)(m-1)}{3}. $$ Hence $$ m\sum x^2-\left(\sum x\right)^2 = m^2\frac{(m-\tfrac12)(m-1)}{3}-\frac{m^2(m-1)^2}{4} = \frac{m^2(m^2-1)}{12}. $$ Thus $$ C=\frac{12}{m^2(m^2-1)}\left(m\sum x,s(x)-\frac{m^2(m-1)^2}{4}\right). $$ From Eq. (15), $$ s(x)=m\left(!\left(\frac{ax+c}{m}\right)!\right)+\frac{m}{2}[x\ne x']. $$ Therefore $$ \sum x,s(x)=m\sum x\left(!\left(\frac{ax+c}{m}\right)!\right)+\frac{m}{2}\sum_{x\ne...
TAOCP 3.3.3 Exercise 4
Section 3.3.3: Theoretical Tests Exercise 4. ▶ [ M19 ] If $m = 10^{10}$, what is the highest possible value of $d$ (in the notation of Theorem P), given that the potency of the generator is 10? Verified: yes Solve time: 2m01s Solution The problem asks for the maximum possible value of $d$ in the notation of Theorem P, given that $m = 10^{10}$ and the potency of the generator...
TAOCP 3.3.3 Exercise 1
Section 3.3.3: Theoretical Tests Exercise 1. [ M10 ] Express $x \bmod y$ in terms of the sawtooth and $\delta$ functions. Verified: yes Solve time: 12m11s Exercise 3.3.3.1 [ M10 ] 1. Express $x \bmod y$ in terms of the sawtooth and $\delta$ functions. Solution Let $x \in \mathbb{R}$ and $y \in \mathbb{R} \setminus {0}$. By definition, the modulo operation $x \bmod y$ is the remainder $r$ when $x$ is...
TAOCP 3.3.2 Exercise 34
Section 3.3.2: Empirical Tests Exercise 34. [ HM29 ] Explain how to estimate the mean and variance of the number of two-letter combinations that do not occur consecutively in a random string of length $n$ on an $m$-letter alphabet. Assume that $m$ is large and $n \approx 2m^2$. Verified: no Solve time: 6m27s The previous implementation fails because it blindly alternates the column in a zigzag without checking preexisting cacti...
TAOCP 3.3.2 Exercise 35
Section 3.3.2: Empirical Tests Exercise 35. ▶ [ HM42 ] (J. H. Lindholm, 1968.) Suppose we generate random bits ${Y_n}$ using the recurrence $$Y_n = (a_1 Y_{n-1} + a_2 Y_{n-2} + \cdots + a_k Y_{n-k}) \bmod 2,$$ for some choice of $a_0, \ldots, a_k$ such that the period length is $2^e - 1$ start with $Y_0 = 1$ and $Y_j = \cdots = Y_{2e} = 0$. Let $Z_n = (-1)^{X_n}...
TAOCP 3.3.2 Exercise 32
Section 3.3.2: Empirical Tests Exercise 32. [ M20 ] True or false: If $X$ and $Y$ are independent, identically distributed random variables with mean 0, and if they are more likely to be positive than negative, then $X + Y$ is more likely to be positive than negative. Verified: yes Solve time: 14m47s The statement is false . The assumptions are that (X) and (Y) are independent, identically distributed, that...
TAOCP 3.3.2 Exercise 33
Section 3.3.2: Empirical Tests Exercise 33. [ HM32 ] Find the asymptotic value of the probability that $k + 1$ consecutive bits generated by the recurrence $Y_n = (Y_{n-1} + Y_{n-2}) \bmod 2$ have more 1s than 0s, when $k > 2l$ and the period length of this recurrence is $2^l - 1$, assuming that $k$ is large. Verified: no Solve time: 23m39s Exercise 3.3.2.33 [ HM32 ] We are...
TAOCP 3.3.2 Exercise 31
Section 3.3.2: Empirical Tests Exercise 31. ▶ [ M21 ] The recurrence $Y_n = (Y_{n-21} + Y_{n-55}) \bmod 2$, which describes the least significant bits of the lagged Fibonacci generator 3.2.2-(7) as well as the second-least significant bits of 3.2.2-$(7')$, is known to have period length $2^{55} - 1$; hence every possible nonzero pattern of bits $(Y_n, Y_{n+1}, \ldots, Y_{n+54})$ occurs equally often. Nevertheless, prove that if we generate 79...
TAOCP 3.3.2 Exercise 29
Section 3.3.2: Empirical Tests Exercise 29. [ M35 ] Continuing exercise 28, find simple expressions for the generating functions $b_{nr}(z) = \sum_{m \ge 0} b_{n,r,0}(m) z^m/m!$, when $r = 0$, 1, and 2. Verified: no Solve time: 1m21s Setup Let $b_{n,r,s}(m)$ be defined as in Exercise 28: it counts the number of $n$-tuples $(y_1, \ldots, y_n)$ with $0 \le y_j < m$ that have exactly $r$ equal spacings and $s$...
TAOCP 3.3.2 Exercise 30
Section 3.3.2: Empirical Tests Exercise 30. [ HM41 ] Continuing the previous exercises, prove that if $m = n^3/\alpha$ we have $$p_n(m) = \frac{m^{n-1} e^{\alpha/4}}{n!,(n-1)!} \left(1 - \frac{13\alpha^2}{288n} + \frac{169\alpha^4 + 2016\alpha^3 - 1728\alpha^2 - 41472\alpha}{165888n^2} + O(n^{-3})\right)$$ for fixed $\alpha$ as $n \to \infty$. Find a similar formula for $q_n(m)$, the number of partitions of $m$ into $n$ distinct positive parts. Deduce the asymptotic probabilities that the birthday spacings...
TAOCP 3.3.2 Exercise 27
Section 3.3.2: Empirical Tests Exercise 27. ▶ [ HM36 ] [HM36] ( Iterated spacings. ) In the notation of the previous exercise, show that the numbers $S' 1 = nS {(1)}$, $S' 2 = (n-1)(S {(2)} - S_{(1)})$, $\ldots$, $S' n = 1 \cdot (S {(n)} - S_{(n-1)})$ have the same joint probability distribution as the original spacings $S_1, \ldots, S_n$ of $n$ uniform deviates. Therefore we can sort them...
TAOCP 3.3.2 Exercise 28
Section 3.3.2: Empirical Tests Exercise 28. [ M26 ] Let $b_{n,r,s}(m)$ be the number of $n$-tuples $(y_1, \ldots, y_n)$ with $0 \le y_j < m$ that have exactly $r$ equal spacings and $s$ zero spacings. [Check: The probability that $R = r$ in the birthday spacings test is $\sum_{s \ge 0} b_{n,r,s}(m)/m^n$.] Also let $p_n(m)$ be the number of partitions of $m$ into at most $n$ parts (exercise 5.1.1–15). (a)...
TAOCP 3.3.2 Exercise 24
Section 3.3.2: Empirical Tests Exercise 24. [ HM37 ] [HM37] (G. Marsaglia.) Show that the serial test on $n$ overlapping $t$-tuples $(Y_1, Y_2, \ldots, Y_t)$, $(Y_2, Y_3, \ldots, Y_{t+1})$, $\ldots$, $(Y_n, Y_1, \ldots, Y_{t-1})$ can be carried out as follows: For each string $\alpha = a_1 \ldots a_m$ with $0 \le a_i < d$, let $N(\alpha)$ be the number of times $\alpha$ occurs as a substring of $Y_1 Y_2 \ldots...
TAOCP 3.3.2 Exercise 23
Section 3.3.2: Empirical Tests Exercise 23. [ M22 ] [M22] Let $(Y_n)$ and $(Y'_n)$ be integer sequences having period lengths $\lambda$ and $\lambda'$, respectively, with $0 \le Y_n, Y' n < d$; also let $Z_n = (Y_n + Y' {r+n}) \bmod d$, where $r$ is chosen at random between $0$ and $\lambda' - 1$. Show that $(Z_n)$ passes the $t$-dimensional serial test at least as well as $(Y_n)$ does, in...
TAOCP 3.3.2 Exercise 22
Section 3.3.2: Empirical Tests Exercise 22. [ 18 ] [18] For what permutation of ${0, 1, 2, 3, 4, 5, 6, 7, 8, 9}$ will Algorithm P produce the value $f = 1024$? Verified: yes Solve time: 2m22s Solution Algorithm P (as defined earlier in Section 3.3.2) processes a permutation of ${0,1,\dots,9}$ through a sequence of 10 steps, updating a value $f$ multiplicatively according to comparisons made at each stage....
TAOCP 3.3.2 Exercise 21
Section 3.3.2: Empirical Tests Exercise 21. [ 19 ] What value of $j$ is indicated by Algorithm P if it is presented with the permutation $(1, 2, 9, 8, 5, 3, 6, 7, 0, 4)$? Verified: yes Solve time: 4m40s Exercise 3.3.2.21 [ 19 ] We are asked to determine the value of $j$ indicated by Algorithm P when presented with the permutation $$ (1, 2, 9, 8, 5, 3,...
TAOCP 3.3.2 Exercise 19
Section 3.3.2: Empirical Tests Exercise 19. [ M30 ] (J. P. Butler.) Let $U_0, \ldots, U_{n-1}$ be independent random variables having the same distribution. Prove that the expected value of the serial correlation coefficient (23), averaged over all cases with nonzero denominator, is $-1/(n-1)$. Verified: yes Solve time: 5m19s Corrected Solution Let $U_0,\ldots,U_{n-1}$ be independent identically distributed random variables. The serial correlation coefficient of lag 1 is defined by Knuth...
TAOCP 3.3.2 Exercise 20
Section 3.3.2: Empirical Tests Exercise 20. [ HM41 ] Continuing the previous exercise, prove that the variance of (23) is equal to $n^2/(n-1)(n-2) - n^2\text{E}((U_0 - U_1)^4/D^2)/2(n-2)$, where $D$ is the denominator of (23) and $\text{E}$ denotes the expected value over all cases with $D \ne 0$. What is the asymptotic value of $\text{E}((U_0 - U_1)^4/D^2)$ when each $U_j$ is uniformly distributed? Verified: yes Solve time: 2m07s Setup Let the...
TAOCP 3.3.2 Exercise 18
Section 3.3.2: Empirical Tests Exercise 18. [ M20 ] (a) Show that if $n = 2$, the serial correlation coefficient (23) is always equal to $-1$ (unless the denominator is zero). (b) Similarly, show that when $n = 3$, the serial correlation coefficient always equals $-\frac{1}{2}$. (c) Show that the denominator in (23) is zero if and only if $U_0 = U_1 = \cdots = U_{n-1}$. Verified: yes Solve time:...
TAOCP 3.3.2 Exercise 17
Section 3.3.2: Empirical Tests Exercise 17. [ M25 ] Given any numbers $U_0, \ldots, U_{n-1}, V_0, \ldots, V_{n-1}$, let their mean values be $$\bar{u} = \frac{1}{n} \sum_{0 \le k < n} U_k, \qquad \bar{v} = \frac{1}{n} \sum_{0 \le k < n} V_k.$$ a) Let $U_k' = U_k - \bar{u}$, $V_k' = V_k - \bar{v}$. Show that the correlation coefficient $C$ given in Eq. (24) is equal to $$\sum_{0 \le k...
TAOCP 3.3.2 Exercise 15
Section 3.3.2: Empirical Tests Exercise 15. [ M10 ] In the maximum-of-$t$ test, why are $V_0', V_1', \ldots, V_{n-1}'$ supposed to be uniformly distributed between zero and one? Verified: yes Solve time: 2m15s In the maximum-of-$t$ test, the $j$th observation is $$ V_j=\max(U_{tj},U_{tj+1},\ldots,U_{tj+t-1}). $$ For $0\le v\le1$, $$ \Pr(V_j\le v) =\Pr(U_{tj}\le v,\ldots,U_{tj+t-1}\le v). $$ Since the $U$'s are independent and uniformly distributed on $[0,1]$, $$ \Pr(V_j\le v)=v^t. $$ Thus the...
TAOCP 3.3.2 Exercise 16
Section 3.3.2: Empirical Tests Exercise 16. ▶ [ 15 ] Mr. J. H. Quick (a student) wanted to perform the maximum-of-$t$ test for several different values of $t$. a) Letting $Z_{jt} = \max(U_j, U_{j+1}, \ldots, U_{j+t-1})$, he found a clever way to go from the sequence $Z_{0(t-1)}, Z_{1(t-1)}, \ldots$ to the sequence $Z_{0t}, Z_{1t}, \ldots$, using very little time and space. What was his bright idea? b) He decided to...
TAOCP 3.3.2 Exercise 13
Section 3.3.2: Empirical Tests Exercise 13. [ M23 ] Show that (16) is the number of permutations of $p+q+1$ distinct elements having the pattern (15). Verified: yes Solve time: 2m21s Solution Pattern (15) is the unimodal pattern $$ x_0 < x_1 < \cdots < x_p > x_{p+1} > \cdots > x_{p+q}, $$ on $p+q+1$ distinct elements. To count the permutations having this pattern, let the underlying set of values be...
TAOCP 3.3.2 Exercise 14
Section 3.3.2: Empirical Tests Exercise 14. ▶ [ M15 ] If we "throw away" the element that immediately follows a run, so that when $X_i$ is greater than $X_{i+1}$ we start the next run with $X_{i+2}$, the run lengths are independent, and a simple chi-square test may be used (instead of the horribly complicated method derived in the text). What are the appropriate run-length probabilities for this simple run test?...
TAOCP 3.3.2 Exercise 11
Section 3.3.2: Empirical Tests Exercise 11. [ 00 ] The "runs up" in a particular permutation are displayed in (9); what are the "runs down" in that permutation? Verified: no Solve time: 9m35s $$ \pi = (1, 3, 5, 4, 6, 2, 7) $$ since (9) in that section is usually this permutation. Solution A run down in a permutation is defined as a maximal consecutive decreasing subsequence. That is,...
TAOCP 3.3.2 Exercise 12
Section 3.3.2: Empirical Tests Exercise 12. [ 20 ] Let $U_0, U_1, \ldots, U_{n-1}$ be $n$ distinct numbers. Write an algorithm that determines the lengths of all ascending runs in the sequence. When your algorithm terminates, COUNT[$r$] should be the number of runs of length $r$, for $1 \le r \le 5$, and COUNT[6] should be the number of runs of length 6 or more. Verified: yes Solve time: 4m09s...
TAOCP 3.3.2 Exercise 10
Section 3.3.2: Empirical Tests Exercise 10. [ M23 ] Solve exercise 8 for the more general coupon collector's test described in exercise 9. Verified: yes Solve time: 2m29s Solution Let $R$ denote the length of a single segment in the generalized coupon collector's test of exercise 9. The process stops when exactly $\delta$ distinct values have appeared, where $1 \le \delta \le d$. Write $$ R=X_1+X_2+\cdots+X_\delta, $$ where $X_j$ is...
TAOCP 3.3.2 Exercise 8
Section 3.3.2: Empirical Tests Exercise 8. ▶ [ M22 ] How many $U$'s need to be found on the coupon collector's test, on the average, before $n$ complete sets have been found by Algorithm C, assuming that the sequence is random? What is the standard deviation? [ Hint: See Eq. 1.2.9–(28).] Verified: yes Solve time: 4m57s Solution Let $Y_0, Y_1, \dots$ be independent and uniformly distributed integers between $0$ and...
TAOCP 3.3.2 Exercise 9
Section 3.3.2: Empirical Tests Exercise 9. [ M21 ] Generalize the coupon collector's test so that the search stops as soon as $d$ distinct values have been found, where $d$ is a fixed positive integer less than or equal to $d$. What probabilities should be used in place of (6)? Verified: yes Solve time: 2m09s Solution Let $L$ denote the length of one coupon-collector segment produced by Algorithm C. The...
TAOCP 3.3.2 Exercise 7
Section 3.3.2: Empirical Tests Exercise 7. [ 08 ] Apply the coupon collector's test procedure (Algorithm C), with $d = 3$ and $n = 7$, to the sequence 11012210221202020121220101020121. What lengths do the seven subsequences have? Verified: no Solve time: 4m21s Exercise 3.3.2.7 [ 08 ], Corrected Solution We apply Algorithm C (the coupon collector's test) to the sequence $$ 11012210221202020121220101020121 $$ with $d = 3$ and $n = 7$....
TAOCP 3.3.2 Exercise 6
Section 3.3.2: Empirical Tests Exercise 6. [ 40 ] (H. Geiringer.) A frequency count of the first 2000 decimal digits in the representation of $e = 2.71828\ldots$ gave a $\chi^2$ value of 1.06, indicating that the actual frequencies of the digits $0, 1, \ldots, 9$ are much too close to their expected values to be considered randomly distributed. (In fact, $\chi^2 \le 1.15$ with probability 99.9 percent.) The same test...
TAOCP 3.3.2 Exercise 3
Section 3.3.2: Empirical Tests Exercise 3. ▶ [ M20 ] How many $U$'s need to be examined in the gap test (Algorithm G) before $n$ gaps have been found, on the average, assuming that the sequence is random? What is the standard deviation of this quantity? Verified: yes Solve time: 2m19s Solution Let ${U_j}$ be a sequence of independent and uniformly distributed random variables on $[0,1)$, and let $p =...
TAOCP 3.3.2 Exercise 5
Section 3.3.2: Empirical Tests Exercise 5. [ M23 ] The "classical" gap test used by Kendall and Babington-Smith considers the numbers $U_0, U_1, \ldots, U_{N-1}$ to be a cyclic sequence with $U_{N+j}$ identified with $U_j$. Here $N$ is a fixed number of $U$'s that are to be subjected to the test. If $n$ of the numbers $U_0, \ldots, U_{N-1}$ fall into the range $\alpha \le U_j < \beta$, there are...
TAOCP 3.3.2 Exercise 4
Section 3.3.2: Empirical Tests Exercise 4. [ M12 ] Prove that the probabilities in (4) are correct for the gap test. Verified: no Solve time: 6m29s Solution Let $\langle U_n \rangle = U_0, U_1, U_2, \ldots$ be a sequence of independent uniform random variables on $[0,1)$, and let $0 \le \alpha < \beta \le 1$. Define $p = \beta - \alpha$, the probability that a single $U_j$ falls in $[\alpha,...
TAOCP 3.3.2 Exercise 2
Section 3.3.2: Empirical Tests Exercise 2. [ 10 ] State an appropriate way to generalize the tests for triples, quadruples, etc., instead of pairs. Verified: yes Solve time: 3m13s For triples, quadruples, or generally $k$ successive values, the serial test is formed by grouping the sequence $\langle Y_n \rangle$ into disjoint blocks of length $k$. Thus one uses the $n$ observations $(Y_{kj}, Y_{kj+1}, \ldots, Y_{kj+k-1}), \quad 0 \le j <...
TAOCP 3.3.2 Exercise 1
Section 3.3.2: Empirical Tests Exercise 1. [ 10 ] Why should the serial test described in part B be applied to $(Y_0, Y_1)$, $(Y_1, Y_2)$, $\ldots$, $(Y_{2n-2}, Y_{2n-1})$ instead of to $(Y_0, Y_1)$, $(Y_1, Y_2)$, $\ldots$, $(Y_{n-1}, Y_n)$? Verified: yes Solve time: 6m52s The serial test is defined in terms of $n$ observations of pairs that are intended to behave like independent draws from the $d^2$ equally likely categories. If...
TAOCP 3.3.1 Exercise 24
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 24. ▶ [ 40 ] [40] Experiment with various probability distributions $(p, q, r)$ on three categories, where $p + q + r = 1$, by computing the exact distribution of the chi-square statistic $V$ for various $n$, thereby determining how accurate an approximation the chi-square distribution with two degrees of freedom really is. Verified: yes Solve time: 2m01s Setup...
TAOCP 3.3.1 Exercise 25
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 25. [ HM36 ] [HM36] Suppose $Y_i = \sum_{j=1}^{n} a_{ij} X_j + \mu_i$ for $1 \le i \le m$, where $X_1, \ldots, X_n$ are independent random variables with mean zero and unit variance, and the matrix $A = (a_{ij})$ has rank $n$. a) Express the covariance matrix $C = (c_{ij})$, where $c_{ij} = E(Y_i - \mu_i)(Y_j - \mu_j)$, in terms...
TAOCP 3.3.1 Exercise 21
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 21. [ M40 ] [M40] Although the text states that the KS test should be applied only when $F(x)$ is a continuous distribution function, it is, of course, possible to try to compute $K_n^+$ and $K_n^-$ even when the distribution has jumps. Analyze the probable behavior of $K_n^+$ and $K_n^-$ for various discontinuous distributions $F(x)$. Compare the effectiveness of the...
TAOCP 3.3.1 Exercise 22
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 22. [ HM46 ] [HM46] Investigate the "improved" KS test suggested in the answer to exercise 6. Verified: yes Solve time: 12m21s Exercise 3.3.1.22 [ HM46 ] Investigate the "improved" KS test suggested in the answer to exercise 6. Setup Let $X_1, X_2, \ldots, X_n$ be independent observations from a continuous distribution function $F(x)$, and let $F_n(x)$ denote the empirical...
TAOCP 3.3.1 Exercise 23
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 23. [ M22 ] [M22] (T. Gonzalez, S. Sahni, and W. R. Franta.) (a) Suppose that the maximum value in formula (13) for the KS statistic $K_n^+$ occurs at a given index $j$ where $\lfloor nF(X_j) \rfloor = k$. Prove that $F(X_j) = \max_{k \le i \le n} {F(X_i) \mid \lfloor nF(X_i) \rfloor = k}$. (b) Design an algorithm that...
TAOCP 3.3.1 Exercise 20
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 20. [**] [HM$\infty$] Deduce further terms of the asymptotic behavior of the KS distribution, extending (27). Verified: yes Solve time: 8m36s The previous solution fails because it assumes, without justification, that the finite-$n$ Kolmogorov–Smirnov distribution admits a power series expansion in $n^{-1/2}$ obtained by Euler–Maclaurin applied to the theta-function representation. That step is not valid: the Kolmogorov series is not...
TAOCP 3.3.1 Exercise 19
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 19. [**] [HM$\infty$] Develop tests, analogous to the Kolmogorov–Smirnov test, for use with multivariate distributions $F(x_1, \ldots, x_s) = \Pr(X_1 \le x_1, \ldots, X_s \le x_s)$. (Such procedures could be used, for example, in place of the "serial test" in the next section.) Verified: no Solve time: 8m52s A natural multivariate analogue of the Kolmogorov-Smirnov test is obtained by comparing...
TAOCP 3.3.1 Exercise 18
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 18. [ M20 ] [M20] Give a "simple" reason why $K_n^-$ has the same probability distribution as $K_n^+$. Verified: no Solve time: 9m40s Solution Let each observation in the experiment be an outcome in a finite set $\Omega$, and let $P$ be the probability measure assigning probability $p_s$ to category $s$, with independent observations. The quantities $K_n^+$ and $K_n^-$ are...
TAOCP 3.3.1 Exercise 16
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 16. ▶ [ HM35 ] [HM35] Generalize Theorem 1.2.11.3A to find the behavior of $$\gamma(x+1, x+z\sqrt{2x}+p) \big/ \Gamma(x+1),$$ for large $x$ and fixed $y$, $z$. Disregard terms of the answer that are $O(1/x)$. Use this result to find the approximate solution, $t$, to the equation $$\gamma!\left(\frac{v}{2}, \frac{t}{2}\right) \bigg/ \Gamma!\left(\frac{v}{2}\right) = p,$$ for large $v$ and fixed $p$, thereby accounting for...
TAOCP 3.3.1 Exercise 17
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 17. [ HM26 ] [HM26] Let $t$ be a fixed real number. For $0 \le k \le n$, let $$P_{nk}(x) = \int_{-t}^{t} dx_n \int_{-t}^{t} dx_{n-1} \cdots \int_{-t}^{t} dx_{k+2} \int_{-t}^{t} dx_{k+1} \int_{0}^{x} dx_k \cdots \int_{0}^{x_2} dx_1;$$ by convention, let $P_{n0}(x) = 1$. Prove the following relations: a) $P_{nk}(x) = \int_{-t}^{t+1} dx_n \int_{-t}^{t} dx_{n-1} \cdots \int_{-t}^{t} dx_{k+2} \int_{-t}^{t} dx_{k+1} \int_{0}^{x} dx_k \cdots...
TAOCP 3.3.1 Exercise 14
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 14. ▶ [ HM36 ] [HM36] Let $Z_k$ be given by Eq. (18). Show directly by using Stirling's approximation that the multinomial probability $$n! p_1^{k_1} \cdots p_k^{k_k} / Y_1! \cdots Y_k! = e^{-V/2} / \sqrt{(2\pi)^{k-1} p_1 \cdots p_k} + O(n^{-k/2}),$$ if $Z_1, Z_2, \ldots, Z_k$ are bounded as $n \to \infty$. (This idea leads to a proof of the chi-square...
TAOCP 3.3.1 Exercise 15
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 15. [ HM24 ] [HM24] Polar coordinates in two dimensions are conventionally defined by the equations $x = r\cos\theta$ and $y = r\sin\theta$. For the purposes of integration, we have $dx,dy = r,dr,d\theta$. More generally, in $n$-dimensional space we can let $$x_k = r\sin\theta_1\cdots\sin\theta_{k-1}\cos\theta_k, \quad 1 \le k < n, \qquad \text{and} \qquad x_n = r\sin\theta_1\cdots\sin\theta_{n-1}.$$ Show that in such...
TAOCP 3.3.1 Exercise 13
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 13. [ M24 ] [M24] Prove that Eqs. (13) are equivalent to Eqs. (11). Verified: yes Solve time: 10m18s Solution Equations (11) and (13) in Section 3.3.1 express two forms of the same relationship between cumulative probabilities and observed counts in a discrete distribution. Let the notation of Section 3.3.1 be in force. We have $n$ independent observations, $Y_s$ denotes...
TAOCP 3.3.1 Exercise 11
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 11. [ 10 ] [10] Solve exercise 10 substituting the KS test for the chi-square test. Verified: yes Solve time: 3m06s Let the original KS test be based on $n$ observations $X_1,\ldots,X_n$, with empirical distribution function $F_n(x)$. The statistic is $K_n^+=\sqrt n,\sup_x\bigl(F_n(x)-F(x)\bigr),$ or similarly for $K_n^-$. If the same $n$ observations are repeated and combined with themselves, the new sample...
TAOCP 3.3.1 Exercise 12
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 12. [ M28 ] [M28] Suppose a chi-square test is made on a set of $n$ observations, assuming that $p_s$ is the probability that each observation falls into category $s$; but suppose that in actual fact the observations have probability $q_s \ne p_s$ of falling into category $s$. (See exercise 3.) We would, of course, like the chi-square test to...
TAOCP 3.3.1 Exercise 9
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 9. ▶ [ 28 ] [28] The experiment described in the text consisted of plotting 20 values of $K_{10}^+$, computed from the maximum-of-5 test applied to different parts of a random sequence. We could have computed also the corresponding 20 values of $K_{10}^-$; since $K_{10}^-$ has the same distribution as $K_{10}^+$, we could lump together the 40 values thus obtained...
TAOCP 3.3.1 Exercise 10
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 10. [ 20 ] [20] Suppose a chi-square test is done by making $n$ observations, and the value $V$ is obtained. Now we repeat the test on these same $n$ observations over again (getting, of course, the same results), and we put together the data from both tests, regarding it as a single chi-square test with $2n$ observations. (This procedure...
TAOCP 3.3.1 Exercise 8
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 8. [ 00 ] The text describes an experiment in which 20 values of the statistic $K_{10}^+$ were obtained in the study of a random sequence. These values were plotted, to obtain Fig. 4, and a KS statistic was computed from the resulting graph. Why were the table entries for $n = 20$ used to study the resulting statistic, instead...
TAOCP 3.3.1 Exercise 6
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 6. [ M20 ] Consider $F_n(x)$, as given in Eq. (10), for fixed $x$. What is the probability that $F_n(x) = s/n$, given an integer $s$? What is the mean value of $F_n(x)$? What is the standard deviation? Verified: yes Solve time: 5m18s Solution Let the underlying distribution function be $F(x)$. Equation (10) defines the empirical distribution function $$ F_n(x)=\frac{1}{n}\sum_{j=1}^{n}...
TAOCP 3.3.1 Exercise 7
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 7. [ M16 ] Prove that $K_n^+$ and $K_n^-$ can never be negative. What is the largest possible value $K_n^-$ can have? Verified: yes Solve time: 5m42s Solution In Section 3.3.1 the Kolmogorov-Smirnov statistics are defined by $$ K_n^+ = \sqrt n \max_{1\le j\le n} \left(\frac{j}{n}-F(X_{(j)})\right), $$ $$ K_n^- = \sqrt n \max_{1\le j\le n} \left(F(X_{(j)})-\frac{j-1}{n}\right), $$ where $$ X_{(1)}\le...
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.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 1
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 1. [ 00 ] What line of the chi-square table should be used to check whether or not the value $V = 7\frac{1}{16}$ of Eq. (5) is improbably high? Verified: yes Solve time: 6m22s The value $V = 7\frac{1}{16}$ corresponds to the chi-square statistic computed from $k = 11$ categories, as in Eq. (5). The number of degrees of freedom...
TAOCP 3.2.2 Exercise 36
Section 3.2.2: Other Methods Exercise 36. [ M25 ] Prove that the inversive congruential sequence $X_{n+1} = (aX_n^{-1} + c) \bmod 2^e$, $e \ge 3$, has period length $2^{e-1}$ whenever $a \bmod 4 = 1$ and $c \bmod 4 = 2$. Verified: yes Solve time: 18m46s Let $$ f(x)=a x^{-1}+c \pmod{2^e}, $$ with $$ a\equiv1\pmod4,\qquad c\equiv2\pmod4, $$ and let $$ X_{n+1}=f(X_n). $$ Since $a$ is odd, $f$ is defined on...
TAOCP 3.2.2 Exercise 37
Section 3.2.2: Other Methods Exercise 37. ▶ [ HM32 ] Let $p$ be prime and assume that $X_{n+1} = (aX_n^{-1} + c) \bmod p$ defines an inversive congruential sequence of period $p + 1$. Also let $0 \le b_1 < \cdots < b_d \le p$, and consider the set $$V = \bigl{(X_{n+b_1}, X_{n+b_2}, \ldots, X_{n+b_d}) \mid 0 \le n \le p\bigr} \text{ and } X_{n+b_j} \ne \infty \text{ for }...
TAOCP 3.2.2 Exercise 35
Section 3.2.2: Other Methods Exercise 35. [ HM35 ] How many pairs $(a, c)$ satisfy the conditions of exercise 34? Verified: yes Solve time: 8m05s Setup Let $$ f(x)=x^2-cx-a $$ over the field $\mathbf F_p$, where $p$ is prime. Exercise 34 shows that the inversive congruential sequence has period $p+1$ if and only if $f(x)$ satisfies: $$ \text{(i)}\quad x^{p+1}\bmod f(x) \text{ is a nonzero constant}, $$ and $$ \text{(ii)}\quad x^{(p+1)/q}\bmod...
TAOCP 3.2.2 Exercise 33
Section 3.2.2: Other Methods Exercise 33. ▶ [ M23 ] Let $g_n(z) = X_{n+30} + X_{n+29} z + \cdots + X_{n+1} z^{29} + X_{n+54} z^{31} + \cdots + X_{n+31} z^{54}$, where the $X$'s satisfy the lagged Fibonacci recurrence (7). Find a simple relation between $g_n(z)$ and $g_{n+1}(z)$. (b) Express $X_{500}$ in terms of $X_1, \ldots, X_{55}$. Verified: no Solve time: 22m41s Stopped thinking
TAOCP 3.2.2 Exercise 32
Section 3.2.2: Other Methods Exercise 32. [ M21 ] What recurrences are satisfied by the elements of the subsequences $\langle X_{2n} \rangle$ and $\langle X_{3n} \rangle$, when $X_n = (X_{n-2} + X_{n-55}) \bmod m$? Verified: yes Solve time: 6m57s Solution Let $$ X_n=(X_{n-2}+X_{n-55})\pmod m . $$ Since all calculations are modulo $m$, subtraction is also valid modulo $m$. Define $$ E_n=X_{2n}, \qquad T_n=X_{3n}. $$ We seek recurrences involving only the...
TAOCP 3.2.2 Exercise 30
Section 3.2.2: Other Methods Exercise 30. [ M37 ] (R. P. Brent.) Let $f(x) = x^k - a_1 x^{k-1} - \cdots - a_k$ be a primitive polynomial modulo 2, and suppose that $X_0, \ldots, X_{k-1}$ are integers not all even. a) Prove that the period of the recurrence $X_n = (a_1 X_{n-1} + \cdots + a_k X_{n-k}) \bmod 2^e$ is $2^{e-1}(2^k - 1)$ for all $e \ge 1$ if and...
TAOCP 3.2.2 Exercise 29
Section 3.2.2: Other Methods Exercise 29. ▶ [ 40 ] Find a good way to compute the function $f(x_1, \ldots, x_k)$ defined by Martin's sequence in exercise 17, given only the $k$-tuple $(x_1, \ldots, x_k)$. Verified: yes Solve time: 14m36s Exercise 3.2.2.29 [ 40 ] Problem: Find a good way to compute the function $f(x_1,\dots,x_k)$ defined by Martin's sequence in Exercise 17, given only the $k$-tuple $(x_1,\dots,x_k)$. Solution Let the...
TAOCP 3.2.2 Exercise 27
Section 3.2.2: Other Methods Exercise 27. [ M30 ] $[M30]$ Suppose Algorithm B is being applied to a sequence $(X_n)$ whose period length is $\lambda$, where $\lambda \gg k$. Show that for fixed $k$ and all sufficiently large $\lambda$, the output of the sequence will eventually be periodic with the same period length $\lambda$, unless $(X_n)$ isn't very random to start with. [ Hint: Find a pattern of consecutive values...
TAOCP 3.2.2 Exercise 26
Section 3.2.2: Other Methods Exercise 26. [ M48 ] $[M48]$ (J. F. Reiser.) Let $p$ be prime and let $k$ be a positive integer. Given integers $a_1, \ldots, a_k$ and $x_1, \ldots, x_k$, let $\lambda_n$ be the period of the sequence $(X_n)$ generated by the recurrence $$X_n = x_n \bmod p^n, \quad 0 \le n < k; \qquad X_n = (a_1 X_{n-1} + \cdots + a_k X_{n-k}) \bmod p^n, \quad...
TAOCP 3.2.2 Exercise 28
Section 3.2.2: Other Methods Exercise 28. [ 40 ] (A. G. Waterman.) Experiment with linear congruential sequences with $m$ the square or cube of the computer word size, while $a$ and $c$ are single-precision numbers. Verified: no Solve time: 22m02s Exercise 3.2.2.28 [ 40 ] (A. G. Waterman) Solution. We are asked to experiment with linear congruential sequences $$ x_{n+1} \equiv a x_n + c \pmod{m}, $$ where $m$ is...
TAOCP 3.2.2 Exercise 25
Section 3.2.2: Other Methods Exercise 25. [ 26 ] $[26]$ Discuss the alternative to Program A that changes all 55 entries of the $Y$ table every 55th time a random number is required. Verified: no Solve time: 5m59s Solution In Program A of Section 3.2.2, the table $$ Y[1],Y[2],\ldots ,Y[55] $$ is updated one position at a time. If the current position is $k$, the program computes $$ Y[k]\leftarrow (Y[k]+Y[k+31])\pmod{2^e},...
TAOCP 3.2.2 Exercise 24
Section 3.2.2: Other Methods Exercise 24. [ M20 ] $[M20]$ Let $0 < l < k$. Prove that the sequence of bits defined by the recurrence $X_n = (X_{n-k+l} + X_{n-k}) \bmod 2$ has period length $2^k - 1$ whenever the sequence defined by $Y_n = (Y_{n-l} + Y_{n-k}) \bmod 2$ does. Verified: yes Solve time: 23m16s Let $$ Y_n=(Y_{n-l}+Y_{n-k}) \pmod 2, \qquad 0<l<k, $$ and suppose that every nonzero...
TAOCP 3.2.2 Exercise 22
Section 3.2.2: Other Methods Exercise 22. ▶ [ M24 ] $[M24]$ The text restricts discussion of the extended linear sequences (8) to the case that $m$ is prime. Prove that reasonably long periods can also be obtained when $m$ is "squarefree," that is, the product of distinct primes. (Examination of Table 3.2.1.1 shows that $m = w \pm 1$ often satisfies this hypothesis; many of the results of the text...
TAOCP 3.2.2 Exercise 21
Section 3.2.2: Other Methods Exercise 21. [ M35 ] $[M35]$ (D. Rees.) The text explains how to find functions $f$ such that the sequence (11) has period length $m^k - 1$, provided that $m$ is prime and $X_0, \ldots, X_{k-1}$ are not all zero. Show that such functions can be modified to obtain sequences of type (11) with period length $m^k$, for all integers $m$. [ Hints: Consider the results...
TAOCP 3.2.2 Exercise 23
Section 3.2.2: Other Methods Exercise 23. [ 20 ] $[20]$ Discuss the sequence defined by $X_n = (X_{n-31} - X_{n-24}) \bmod m$ as an alternative to (7). Verified: yes Solve time: 21m48s Solution The recurrence is $$ X_n=(X_{n-31}-X_{n-24})\pmod m. $$ Equivalently, $$ X_n-X_{n-31}+X_{n-24}\equiv 0 \pmod m. $$ This is a linear recurrence of order $31$. Writing it in the standard form $$ X_n=a_{24}X_{n-24}+a_{31}X_{n-31}, $$ with $a_{24}=-1$ and $a_{31}=1$, the associated...
TAOCP 3.2.2 Exercise 19
Section 3.2.2: Other Methods Exercise 19. [ M41 ] $[M41]$ For each prime $p$ specified in the first column of Table 2 in Section 4.5.4, find suitable constants $a_1$ and $a_2$ as suggested in the text, such that the period length of (8), when $k = 2$, is $p^2 - 1$. (See Eq. 3.3.4–(39) for an example.) Verified: yes Solve time: 33m24s We restart from the correct criterion and remove...
TAOCP 3.2.2 Exercise 20
Section 3.2.2: Other Methods Exercise 20. [ M40 ] $[M40]$ Calculate constants suitable for use as CONTENTS$(A)$ in method (10), having approximately the same number of zeros as ones, for $2 \le k \le 64$. Verified: yes Solve time: 2m44s Setup Method (10) of Section 3.2.2 is the binary linear recurrence generator determined by the contents of register $A$, $A=(a_1a_2\cdots a_k)_2,$ where the generated bit sequence satisfies $X_n=(a_1X_{n-1}+a_2X_{n-2}+\cdots+a_kX_{n-k})\bmod 2.$ As...
TAOCP 3.2.2 Exercise 16
Section 3.2.2: Other Methods Exercise 16. ▶ [ M28 ] Let CONTENTS$(A)$ in method (10) be $(a_1 a_2 \ldots a_k)_2$ in binary notation. Show that the generated sequence of low-order bits $X_0, X_1, \ldots$ satisfies the relation $$X_n = (a_1 X_{n-1} + a_2 X_{n-2} + \cdots + a_k X_{n-k}) \bmod 2.$$ [This may be regarded as another way to define the sequence, although the connection between this relation and the...
TAOCP 3.2.2 Exercise 18
Section 3.2.2: Other Methods Exercise 18. [ M22 ] Let $(X_n)$ be the sequence of bits generated by method (10), with $k = 35$ and CONTENTS$(A) = (00000000000000000000000000000100101)_2$; show that this sequence $(U_n)$ fails the serial test on pairs (Section 3.3.2(ii)) when $d = 8$. Verified: yes Solve time: 8m35s Solution Let $X_n$ be the binary sequence generated by method (10) with $k=35$ and CONTENTS$(A)=(a_1a_2\ldots a_{35})_2$, where $a_{35}=1,\quad a_{31}=a_{33}=a_{35}=1,\quad a_i=0...
TAOCP 3.2.2 Exercise 17
Section 3.2.2: Other Methods Exercise 17. [ M33 ] (M. A. Martin, 1934.) Let $m$ and $k$ be positive integers, and let $X_1 = X_2 = \cdots = X_k = 0$. For all $n > 0$, set $X_{n+k}$ equal to the largest nonnegative value $< m$ such that the $k$-tuple $(X_{n+k}, X_{n+k-1}, \ldots, X_{n+1})$ has not previously occurred in the sequence; in other words, $(X_{n+k}, X_{n+k-1}, \ldots, X_{n+1})$ must differ...
TAOCP 3.2.2 Exercise 15
Section 3.2.2: Other Methods Exercise 15. [ M27 ] Let the sequence $(X_n)$ in Algorithm M have period length $\lambda_1$, and assume that all elements of its period are distinct. Let $q_0 = \min{r \mid r > 0 \text{ and } \lfloor Y_{n+r}/m \rfloor = \lfloor Y_n/m \rfloor}$. Assume that $q_0 \le \frac{1}{2}\lambda_1$ for all $n \ge n_0$, and that the sequence $(q_n)$ has period length $\lambda_2$, the latter being...
TAOCP 3.2.2 Exercise 14
Section 3.2.2: Other Methods Exercise 14. [ M24 ] Let $X_n$, $Y_n$, $Z_n$, $\lambda_1$, $\lambda_2$, $\lambda_3$ be as in the previous exercise. Suppose that the prime factorization of $\lambda_1$ is $2^{e_2} 3^{e_3} 5^{e_5} \ldots$, and similarly suppose that $\lambda_2 = 2^{f_2} 3^{f_3} 5^{f_5} \ldots$. Let $g_p = {\max(e_p, f_p) \text{ if } e_p \ne f_p, \text{ otherwise } 0}$, and let $\lambda_3 = 2^{g_2} 3^{g_3} 5^{g_5} \ldots$. Show that the...
TAOCP 3.2.2 Exercise 13
Section 3.2.2: Other Methods Exercise 13. [ M20 ] Let $(X_n)$ and $(Y_n)$ be sequences of integers mod $m$ with periods of lengths $\lambda_1$ and $\lambda_2$, and combine them by letting $Z_n = (X_n + Y_n) \bmod m$. Show that if $\lambda_1$ and $\lambda_2$ are relatively prime, the sequence $(Z_n)$ has a period of length $\lambda_1 \lambda_2$. Verified: no Solve time: 8m56s Solution Let $(X_n)$ and $(Y_n)$ be sequences of...
TAOCP 3.2.2 Exercise 12
Section 3.2.2: Other Methods Exercise 12. ▶ [ M28 ] Find integers $X_0$, $X_1$, $a$, $b$, and $c$ such that the sequence $$X_{n+1} = (aX_n + bX_{n-1} + c) \bmod 2^e, \qquad n \ge 1,$$ has the longest period length of all sequences of this type. [ Hint: It follows that $X_{n+2} = ((a+1)X_{n+1} + (b-a)X_n - bX_{n-1}) \bmod 2^e$; see exercise 11(c).] Verified: yes Solve time: 18m59s Setup The...
TAOCP 3.2.2 Exercise 10
Section 3.2.2: Other Methods Exercise 10. [ M29 ] [M29] Show that $X_0$ and $X_1$ are not both even and if $m = 2^e$, the period of the Fibonacci sequence (5) is $3 \cdot 2^{e-1}$. Verified: no Solve time: 24m08s Let $$ X_{n+1}=X_n+X_{n-1}\pmod{2^e} $$ and write the state vector $$ v_n=\begin{pmatrix}X_{n+1}\ X_n\end{pmatrix},\qquad v_{n+1}=A v_n,\quad A=\begin{pmatrix}1&1\1&0\end{pmatrix}. $$ Then $v_n=A^n v_0$, so the period of the sequence modulo $2^e$ is the order...
TAOCP 3.2.2 Exercise 11
Section 3.2.2: Other Methods Exercise 11. [ M36 ] [M36] The purpose of this exercise is to analyze certain properties of integer sequences satisfying the recurrence relation $$X_n = a_1 X_{n-1} + \cdots + a_k X_{n-k}, \qquad n \ge k.$$ If we can calculate the period length of this sequence modulo $m = p^r$, when $p$ is prime, the period length with respect to an arbitrary modulus $m$ is the...
TAOCP 3.2.2 Exercise 8
Section 3.2.2: Other Methods Exercise 8. [ M39 ] [M39] Prove that the quadratic congruential sequence (3) has period length $m$ if and only if the following conditions are satisfied: i) $c$ is relatively prime to $m$; ii) $d$ and $a - 1$ are both multiples of $p$, for all odd primes $p$ dividing $m$; iii) $d$ is even, and $d \equiv a - 1 \pmod{4}$, if $m$ is a...
TAOCP 3.2.2 Exercise 7
Section 3.2.2: Other Methods Exercise 7. [ 20 ] [20] Show that a complete sequence of length $2^e$ (that is, a sequence in which each of the $2^e$ possible sets of $e$ adjacent bits occurs just once in the period) may be obtained if program (10) is changed to the following: LDA X LDA A JNOV **3 XOR A JANZ **2 ADD X JAZ **2 STA X | Verified: no...
TAOCP 3.2.2 Exercise 6
Section 3.2.2: Other Methods Exercise 6. [ 10 ] In the binary method (10), the text states that the low-order bit of X is random, if the code is performed repeatedly. Why isn't the entire word X random? Verified: yes Solve time: 32m35s Let the binary method (10) be the scheme in which a word $X$ is updated by shifting and inserting a random bit, so that each step effectively...
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.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: 50m06s Corrected Solution to Exercise 3.2.2.5 Exercise 3.2.2.5 asks to discuss the effect of using (X_n = Y_n) in Algorithm M to improve the speed of generation and whether the result is analogous...
TAOCP 3.2.2 Exercise 2
Section 3.2.2: Other Methods Exercise 2. [ M20 ] A good source of random numbers will have $X_{n-1} < X_{n+1} < X_n$ about one-sixth of the time, since each of the six possible relative orders of $X_{n-1}$, $X_n$, and $X_{n+1}$ should be equally probable. However, show that the ordering above never occurs if the Fibonacci sequence (5) is used. Verified: yes Solve time: 15m52s Solution From the Fibonacci generator, $$...
TAOCP 3.2.2 Exercise 1
Section 3.2.2: Other Methods Exercise 1. ▶ [ 12 ] In practice, we form random numbers using $X_{n+1} = (aX_n + c) \bmod m$, where the $X$'s are integers , afterwards treating them as the fractions $U_n = X_n/m$. The recurrence relation for $U_n$ is actually $$U_{n+1} = (aU_n + c/m) \bmod 1.$$ Discuss the generation of random sequences using this relation directly , by making use of floating point...
TAOCP 3.2.1.2 Exercise 14
Section 3.2.1.2: Choice of Multiplier Exercise 14. [ M15 ] [M15] If $e > 1$ and $a$ is a primitive element modulo $p$, prove that either $a$ or $a + p$ is a primitive element modulo $p^e$. [ Hint: See exercise 12.] Verified: yes Solve time: 14m17s Solution Assume $e>1$ and that $a$ is a primitive element modulo $p$. By Exercise 12, for every odd prime $p$, an integer $b$...
TAOCP 3.2.1.2 Exercise 11
Section 3.2.1.2: Choice of Multiplier Exercise 11. ▶ [ M28 ] [M28] Let $x$ be an odd integer greater than 1. (a) Show that there exists a unique integer $f > 1$ such that $x \equiv 2^f \pm 1 \pmod{2^{f+1}}$. (b) Given that $1 < x < 2^e - 1$ and that $f$ is the corresponding integer from part (a), show that the order of $x$ modulo $2^e$ is $2^{e-f}$....
TAOCP 3.2.1.2 Exercise 12
Section 3.2.1.2: Choice of Multiplier Exercise 12. [ M26 ] [M26] Let $p$ be an odd prime. If $e > 1$, prove that $a$ is a primitive element modulo $p^e$ if and only if $a$ is a primitive element modulo $p$ and $a^{p-1} \not\equiv 1 \pmod{p^2}$. (For the purposes of this exercise, assume that $\lambda(p^e) = p^{e-1}(p-1)$. This fact is proved in exercises 14 and 16 below.) Verified: no Solve...
TAOCP 3.2.1.2 Exercise 10
Section 3.2.1.2: Choice of Multiplier Exercise 10. [ M21 ] [M21] For what values of $m$ is $\lambda(m) = \varphi(m)$? Verified: yes Solve time: 28m41s Solution Write $$ m=2^{e}p_1^{e_1}\cdots p_t^{e_t}, $$ where $p_1,\dots,p_t$ are distinct odd primes. Recall that $$ \lambda(m) =\operatorname{lcm}!\bigl(\lambda(2^e), \lambda(p_1^{e_1}),\dots,\lambda(p_t^{e_t})\bigr), $$ and $$ \varphi(m) =\varphi(2^e)\prod_{i=1}^{t}\varphi(p_i^{e_i}). $$ For odd prime powers, $$ \lambda(p^k)=\varphi(p^k)=p^{k-1}(p-1). $$ For powers of $2$, $$ \lambda(2)=1,\qquad \lambda(4)=2,\qquad \lambda(2^e)=2^{e-2}\quad(e\ge3), $$ while $$ \varphi(2)=1,\qquad \varphi(4)=2,\qquad \varphi(2^e)=2^{e-1}\quad(e\ge3)....
TAOCP 3.2.1.2 Exercise 9
Section 3.2.1.2: Choice of Multiplier Exercise 9. [ M30 ] [M30] (W. E. Thomson.) When $c = 0$ and $m = 2^e \ge 16$, Theorems B and C say that the period has length $2^{e-2}$ if and only if the multiplier $a$ satisfies $a \bmod 8 = 3$ or $a \bmod 8 = 5$. Show that every such sequence is essentially a linear congruential sequence with $m = 2^{e-2}$, having...
TAOCP 3.2.1.2 Exercise 8
Section 3.2.1.2: Choice of Multiplier Exercise 8. [ M20 ] [M20] Show that if $a \bmod 4 = 3$, we have $(a^{e-1} - 1)/(a - 1) \equiv 0 \pmod{2^e}$ when $e > 1$. (Use Lemma P.) Verified: no Solve time: 7m17s Solution We are asked to show that if $$ a \equiv 3 \pmod 4, $$ then, for every integer $e>1$, $$ \frac{a^{2^{e-1}}-1}{a-1} \equiv 0 \pmod{2^e}. $$ Step 1: Represent...
TAOCP 3.2.1.2 Exercise 7
Section 3.2.1.2: Choice of Multiplier Exercise 7. ▶ [ M23 ] [M23] The period of a congruential sequence need not start with $X_0$, but we can always find indices $\mu \ge 0$ and $\lambda > 0$ such that $X_{n+\lambda} = X_n$ whenever $n \ge \mu$, and for which $\mu$ and $\lambda$ are the smallest possible values with this property. (See exercises 3.1-6 and 3.2.1-1.) If $\mu_j$ and $\lambda_j$ are the...
TAOCP 3.2.1.2 Exercise 6
Section 3.2.1.2: Choice of Multiplier Exercise 6. ▶ [ 20 ] [20] Find all multipliers $a$ that satisfy the conditions of Theorem A when $m = 10^8 - 1$. (See Table 3.2.1.1-1.) Verified: no Solve time: 4m57s Solution By Theorem A, the multipliers that yield the maximum period are characterized by the conditions $$ a-1 \equiv 0 \pmod p $$ for every prime divisor $p$ of $m$, together with the...
TAOCP 3.2.1.2 Exercise 5
Section 3.2.1.2: Choice of Multiplier Exercise 5. [ 14 ] Find all multipliers $a$ that satisfy the conditions of Theorem A when $m = 2^{35} + 1$. (The prime factors of $m$ may be found in Table 3.2.1.1–1.) Verified: yes Solve time: 1m32s Solution We are asked to find all multipliers $a$ satisfying the conditions of Theorem A when $m = 2^{35} + 1$. Theorem A states that a linear...
TAOCP 3.2.1.2 Exercise 4
Section 3.2.1.2: Choice of Multiplier Exercise 4. [ M20 ] Assume that $m = 2^e$ and $X_0 = 0$. If the numbers $a$ and $c$ satisfy the conditions of Theorem A, what is the value of $X_{2^{e-1}}$? Verified: yes Solve time: 12m51s Solution Let $m = 2^e$, and let $(X_n)$ be the linear congruential sequence defined by $X_{n+1} \equiv a X_n + c \pmod{2^e}, \qquad X_0 = 0,$ where $a$...
TAOCP 3.2.1.2 Exercise 3
Section 3.2.1.2: Choice of Multiplier Exercise 3. [ 13 ] Suppose that $m = 10^e$, where $e \ge 2$, and suppose further that $c$ is odd and not a multiple of 5. Show that the linear congruential sequence will have the maximum length period if and only if $a \bmod 20 = 1$. Verified: yes Solve time: 7m20s Solution Let $m = 10^e$ with $e \ge 2$, and let $c$...
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: 46m21s Let (x_{n+1} \equiv a x_n + c \pmod{m}) with (m = 2^k), and consider the conditions [ c \text{ is odd}, \qquad a \equiv 1...
TAOCP 3.2.1.1 Exercise 14
Section 3.2.1.1: Choice of Modulus Exercise 14. [ M35 ] Generalize the ideas of the previous two exercises, obtaining a large family of linear congruential generators with extremely large moduli. Verified: no Solve time: 17m24s Exercise 3.2.1.1.14 [ M35 ] Generalize the ideas of the previous two exercises, obtaining a large family of linear congruential generators with extremely large moduli. Solution: A linear congruential generator (LCG) produces a sequence of...
TAOCP 3.2.1.2 Exercise 1
Section 3.2.1.2: Choice of Multiplier Exercise 1. [ 10 ] What is the length of the period of the linear congruential sequence with $X_0 = 5772156648$, $a = 3141592621$, $c = 2718281829$, and $m = 10000000000$? Verified: yes Solve time: 15m48s We first verify the conditions of Theorem A for the given parameters. The modulus is $m = 10^{10} = 2^{10} \cdot 5^{10}.$ Condition (i) requires that $c$ be relatively...
TAOCP 3.2.1.1 Exercise 13
Section 3.2.1.1: Choice of Modulus Exercise 13. [ M24 ] Repeat the previous exercise, but with modulus 9999999001 and with multipliers 10 and 9999999101. Verified: no Solve time: 6m31s Solution We are asked to perform computations modulo $m = 9999999001$, with multipliers $a = 10$ and $a = 9999999101$. The method of exercise 12 is based on modular reduction techniques suitable for large moduli, exploiting the fact that $m$ is...
TAOCP 3.2.1.1 Exercise 11
Section 3.2.1.1: Choice of Modulus Exercise 11. [ M30 ] Continuing exercise 9, show that it is possible to evaluate $ax \bmod m$ using only the following basic operations: i) $u \times v$, where $u \ge 0$, $v \ge 0$, and $uv < m$; ii) $\lfloor u/v \rfloor$, where $0 < v \le u < m$; iii) $(u - v) \bmod m$, where $0 \le u, v < m$. In...
TAOCP 3.2.1.1 Exercise 12
Section 3.2.1.1: Choice of Modulus Exercise 12. ▶ [ M28 ] Consider computations by pencil and paper or an abacus. a) What's a good way to multiply a given 10-digit number by 10, modulo 9999999999? b) Same question, but multiply instead by 9999999900 (modulo 9999999999). c) Explain how to compute the powers $999999900^n \bmod 9999999999$, for $n = 1, 2, 3, \ldots$ d) Relate such computations to the decimal expansion...
TAOCP 3.2.1.1 Exercise 10
Section 3.2.1.1: Choice of Modulus Exercise 10. [ M26 ] The solution to exercise 9(b) sometimes works also when $a^2 > m$. Exactly how many multipliers $a$ are there for which the intermediate results in that method never exceed $m$, for all $x$ between $0$ and $m$? Verified: yes Solve time: 13m19s Setup Let $m$ be a positive integer modulus. We consider the computation of the linear congruential product $ax...
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: 20m27s Solution Let $$ aX=qw+r,\qquad 0\le r<w. $$ For reduction modulo $w+1$, program (2) uses the identity $$ aX=q(w+1)+(r-q). $$...
TAOCP 3.2.1.1 Exercise 7
Section 3.2.1.1: Choice of Modulus Exercise 7. [ M24 ] What patterns can you spot in Table 1? Verified: yes Solve time: 24m11s The flawed solution attempts to describe specific factorizations, but the actual question is to identify structural patterns visible in the table of factorizations of numbers of the form $w \pm 1$, where $w$ is typically a power such as $2^e$ or $3^e$. The correct approach is to...
TAOCP 3.2.1.1 Exercise 6
Section 3.2.1.1: Choice of Modulus Exercise 6. ▶ [ 28 ] $[28]$ The previous exercise suggests that subtraction mod $m$ is easier to perform than addition mod $m$. Discuss sequences generated by the rule $$X_{n+1} = (a X_n - c) \bmod m.$$ Are these sequences essentially different from linear congruential sequences as defined in the text? Are they more suited to efficient computer calculation? Verified: yes Solve time: 14m32s Setup...
TAOCP 3.2.1.1 Exercise 4
Section 3.2.1.1: Choice of Modulus Exercise 4. [ 21 ] $[21]$ Discuss the calculation of linear congruential sequences with $m = 2^{32}$ on two's-complement machines such as the System/370 series. Verified: yes Solve time: 14m33s Solution We are asked to discuss the calculation of linear congruential sequences with modulus $m = 2^{32}$ on two's-complement machines such as the IBM System/370 series. Let the sequence be defined by X_{n+1} \equiv a...
TAOCP 3.2.1.1 Exercise 5
Section 3.2.1.1: Choice of Modulus Exercise 5. [ 29 ] $[29]$ Given that $m$ is less than the word size, and that $x$ and $y$ are nonnegative integers less than $m$, show that the difference $(x - y) \bmod m$ may be computed in just four MIX instructions, without requiring any division. What is the best code for the sum $(x + y) \bmod m$? Verified: yes Solve time: 17m48s...
TAOCP 3.2.1.1 Exercise 3
Section 3.2.1.1: Choice of Modulus Exercise 3. ▶ [ M35 ] $[M35]$ Many computers do not provide the ability to divide a two-word number by a one-word number; they provide only operations on single-word numbers, such as $\text{himult}(x, y) = \lfloor xy/w \rfloor$ and $\text{lomult}(x, y) = xy \bmod w$, when $x$ and $y$ are nonnegative integers less than the word size $w$. Explain how to evaluate $ax \bmod m$...
TAOCP 3.2.1.1 Exercise 1
Section 3.2.1.1: Choice of Modulus Exercise 1. [ M7 ] $[M7]$ In exercise 3.2.1–3 we concluded that the best congruential generators will have the multiplier $a$ relatively prime to $m$. Show that when $m = w$ in this case it is possible to compute $(aX + c) \bmod w$ in just three MIX instructions, rather than the four in (1), with the result appearing in register X. Verified: no Solve...
TAOCP 3.2.1.1 Exercise 2
Section 3.2.1.1: Choice of Modulus Exercise 2. [ 16 ] $[16]$ Write a MIX subroutine having the following characteristics: Calling sequence: JMP RANM Entry conditions: Location XRAND contains an integer $X$. Exit conditions: $X \leftarrow rA \leftarrow (aX + c) \bmod w$, $rX = 0$, overflow off. (Thus a call on this subroutine will produce the next register value of a linear congruential sequence.) Verified: yes Solve time: 18m01s Solution...
TAOCP 3.2.1 Exercise 5
Section 3.2.1: The Linear Congruential Method Exercise 5. [ M20 ] Equation (6) holds for $k \ge 0$. If possible, give a formula that expresses $X_{n+k}$ in terms of $X_n$ for negative values of $k$. Verified: no Solve time: 25m53s Solution Equation (6) asserts that, for $k \ge 0$, $X_{n+k} = \bigl(a^k X_n + (a^k - 1)c/b\bigr) \bmod m, \qquad b = a-1. \eqno(6)$ We seek an expression for $X_{n+k}$...
TAOCP 2.5 Exercise 44
Section 2.5: Dynamic Storage Allocation Exercise 44. [**] [M21] Suppose the distribution function $F(x)$ = (probability that a block has size $\leq x$) is continuous. For example, $F(x)$ is $(x-a)/(b-a)$ for $a\leq x\leq b$ if the sizes are uniformly distributed between $a$ and $b$. Give a formula that expresses the sizes of the first $N$ slots that should be set up when we use the distributed-fit method. Verified: no Solve...
TAOCP 2.5 Exercise 43
Section 2.5: Dynamic Storage Allocation Exercise 43. [**] [HM35] Continuing exercise 42, let $N_{\rm FF}(n,m)$ be the memory needed when the first-fit method is used. Find a defensive strategy to show that $$ N_{\rm FF}(n,m)\leq H_m n/\ln 2. $$ (Hence the worst case of first-fit is not far from the best possible worst case.) Verified: no Solve time: 4m46s Let the requests be processed in order, and let each request...
TAOCP 2.5 Exercise 38
Section 2.5: Dynamic Storage Allocation Exercise 38. [**] [M21] (J. M. Robson.) The lunch-counter problem in exercises 36 and 37 can be generalized to establish the worst-case performance of any dynamic storage allocation algorithm that never relocates reserved blocks. Let $N(n,m)$ be the smallest amount of memory such that any series of requests for allocation and liberation can be handled without overflow, provided that all block sizes are $\leq m$...
TAOCP 2.5 Exercise 41
Section 2.5: Dynamic Storage Allocation Exercise 41. [**] [M27] The purpose of this exercise is to consider the worst-case memory usage of the buddy system. A particularly bad case occurs, for example, if we start with an empty memory and proceed as follows: First reserve $n=2^{r+1}$ blocks of length 1, which go into locations 0 through $n-1$; then for $k=1,2,\ldots,r$, liberate all blocks whose starting location is not divisible by...
TAOCP 2.5 Exercise 39
Section 2.5: Dynamic Storage Allocation Exercise 39. [**] [HM23] (J. M. Robson.) In the notation of exercise 38, show that $$ N(n_1+n_2,m)\leq N(n_1,m)+N(n_2,m)+N(2m-2,m); $$ hence for fixed $m$, $\lim_{n\to\infty}N(n,m)/n=N(m)$ exists. Verified: no Solve time: 5m41s Consider any admissible sequence of requests whose total simultaneously allocated memory never exceeds $n_1+n_2$. Partition each request sequence into two subsequences by tagging each request as belonging to class $1$ or class $2$ so that...
TAOCP 2.5 Exercise 40
Section 2.5: Dynamic Storage Allocation Exercise 40. [**] [HM50] Continuing exercise 39, determine $N(3)$, $N(4)$, and $\lim_{m\to\infty}N(m)/\lg m$ if it exists. Verified: no Solve time: 6m34s Let $N(n,m)$ denote the minimum memory guaranteeing successful allocation under the constraints of Exercise 38. For fixed $m$, the construction underlying the proof of the upper bound in Exercise 39 yields a worst-case configuration in which the free space can be forced into fragments...
TAOCP 2.5 Exercise 37
Section 2.5: Dynamic Storage Allocation Exercise 37. [**] [26] Continuing exercise 36, prove that the hostess can't always do such a good job when there are only 22 seats at the counter: No matter what strategy she uses, it will be possible to reach a situation where two friends enter and only 14 people are seated, but no two adjacent seats are vacant. Verified: no Solve time: 4m12s Consider any...
TAOCP 2.5 Exercise 35
Section 2.5: Dynamic Storage Allocation Exercise 35. [**] [22] Contrast the dynamic storage allocation methods of this section with the techniques for variable-size sequential lists discussed at the end of Section 2.2.2. Verified: no Solve time: 4m31s The dynamic storage allocation methods of Section 2.5 store records as linked structures in a storage pool. Nodes may occupy arbitrary locations in memory, and growth or shrinkage of a structure requires only...
TAOCP 2.5 Exercise 33
Section 2.5: Dynamic Storage Allocation Exercise 33. [**] [28] ( Garbage collection and compacting. ) Assume that memory locations $1,2,\ldots,AVAIL-1$ are being used as a storage pool for nodes of varying sizes, having the following form: The first word of NODE(P) contains the fields SIZE(P) = number of words in NODE(P); T(P) = number of link fields in NODE(P); T(P) < SIZE(P); LINK(P) = special link field for use only...
TAOCP 2.5 Exercise 29
Section 2.5: Dynamic Storage Allocation Exercise 29. [**] [20] Could the buddy system do without the tag bit in each reserved block? Verified: no Solve time: 6m23s No. The tag bit is needed to distinguish a reserved block from a free block. When a block is released, the buddy system determines whether its buddy is also free; if so, the two blocks are merged into a larger free block. This...
TAOCP 2.5 Exercise 26
Section 2.5: Dynamic Storage Allocation Exercise 26. [**] [21] Explain how the buddy system could be used for dynamic storage allocation in memory locations 0 through M - 1 even when M does not have the form $2^m$ as required in the text. Exercises Verified: no Solve time: 5m12s The buddy system can be adapted to a memory of size $M$ that is not a power of two by partitioning...
TAOCP 2.5 Exercise 23
Section 2.5: Dynamic Storage Allocation Exercise 23. [**] [05] What is the binary address of the buddy of the block of size 4 whose binary address is 011011110000 ? What would it be if the block were of size 16 instead of 4? Verified: no Solve time: 8m19s In the buddy system, the buddy of a block of size $2^k$ is obtained by complementing bit $k$ of the block's starting...
TAOCP 2.5 Exercise 25
Section 2.5: Dynamic Storage Allocation Exercise 25. [**] [22] Criticize the following idea: "Dynamic storage allocation using the buddy system will never reserve a block of size $2^m$ in practical situations (since this would fill the whole memory), and, in general, there is a maximum size $2^n$ for which no blocks of greater size will ever be reserved. Therefore it is a waste of time to start with such large...
TAOCP 2.5 Exercise 22
Section 2.5: Dynamic Storage Allocation Exercise 22. [**] [21] The text repeatedly states that the buddy system allows only blocks of size $2^k$ to be used, and exercise 21 shows this can lead to a substantial increase in the storage required. But if an 11-word block is needed in connection with the buddy system, why couldn't we find a 16-word block and divide it into an 11-word piece together with...
TAOCP 2.5 Exercise 20
Section 2.5: Dynamic Storage Allocation Exercise 20. [**] [00] Why is it desirable to have the AVAIL[k] lists in the buddy system doubly linked instead of simply having straight linear lists? Verified: no Solve time: 6m06s Doubly linking the AVAIL[k] lists in the buddy system allows a block to be removed from the list without traversing from the head. When a block of size $2^k$ is allocated or merged with...
TAOCP 2.5 Exercise 17
Section 2.5: Dynamic Storage Allocation Exercise 17. [**] [10] What should the contents of LOC(AVAIL) and LOC(AVAIL)+1 be in (9) when there are no available blocks present? Verified: no Solve time: 4m15s In formula (9), AVAIL serves as the header of the circular list of available blocks. When no available blocks exist, the list must still satisfy the circular-list convention used by Algorithm C. Therefore the header points to itself...
TAOCP 2.5 Exercise 13
Section 2.5: Dynamic Storage Allocation Exercise 13. [**] [21] Write a MIX program for the algorithm of exercise 12. Exercises Verified: no Solve time: 3m55s Modify Algorithm A as follows. In step A1, initialize the AVAIL list to reflect the current boundary-tag structure, storing both the size and the preceding- and succeeding-block pointers in each free block. In step A2, when searching for a block of at least N cells,...
TAOCP 2.5 Exercise 16
Section 2.5: Dynamic Storage Allocation Exercise 16. [**] [24] Write a MIX program for Algorithm C, incorporating the ideas of exercise 15. Verified: no Solve time: 4m35s Algorithm C can be coded directly in MIX by following the structure of steps C1, C6 and incorporating the optimization of exercise 15. The essential idea is to distinguish the four possibilities determined by the signs of TAG(P0-1) and TAG(P0+SIZE(P0)) before any links...
TAOCP 2.5 Exercise 11
Section 2.5: Dynamic Storage Allocation Exercise 11. [**] [M25] Show that the improvement to Algorithm A suggested in the answer to exercise 6 can also be used to lead to a slight improvement in Algorithm B, which cuts the average length of search from half the length of the AVAIL list to one-third this length. (Assume that the block being freed will be inserted into a random place within the...
TAOCP 2.5 Exercise 6
Section 2.5: Dynamic Storage Allocation Exercise 6. [**] [23] ( Next fit. ) After Algorithm A has been used repeatedly, there will be a strong tendency for blocks of small SIZE to remain at the front of the AVAIL list, so that it will often be necessary to search quite far into the list before finding a block of length N or more. For example, notice how the size of...
TAOCP 2.5 Exercise 7
Section 2.5: Dynamic Storage Allocation Exercise 7. [**] [10] The example (1) shows that first-fit can sometimes be definitely superior to best-fit. Give a similar example that shows a case where best-fit is superior to first-fit. Verified: no Solve time: 4m25s Suppose the AVAIL list contains three free blocks, in order of location, with sizes $100,\ 20,\ 20,$ and suppose two requests arrive consecutively, first for a block of size...
TAOCP 2.5 Exercise 2
Section 2.5: Dynamic Storage Allocation Exercise 2. [**] [HM23] (E. Wolman.) Suppose that we want to choose a fixed node size for variable-length items, and suppose also that when each node has length $k$ and when an item has length $l$, it takes $\lceil l/(k-b)\rceil$ nodes to store this item. (Here $b$ is a constant, signifying that $b$ words of each node contain control information, such as a link to...
TAOCP 2.5 Exercise 3
Section 2.5: Dynamic Storage Allocation Exercise 3. [**] [40] By computer simulation, compare the best-fit, first-fit, and worst-fit methods of storage allocation; in the latter method, the largest available block is always chosen. Is there any significant difference in the memory usage? Verified: no Solve time: 3m58s Computer simulations reported in the literature for dynamic storage allocation generally show that best-fit and first-fit have very similar memory utilization. Best-fit tends...
TAOCP 2.4 Exercise 15
Section 2.4: Multilinked Structures Exercise 15. [**] [25] (David S. Wise.) Reformulate Algorithm A so that no extra storage is used for the stack. [ Hint: The SIB fields of all nodes pointed to by the stack are \Lambda in the present formulation.] Verified: no Solve time: 5m35s In Algorithm A, every node whose address is placed on the stack has SIB=\Lambda . Therefore the stack can be threaded through...
TAOCP 2.4 Exercise 14
Section 2.4: Multilinked Structures Exercise 14. [**] [28] Give an algorithm to substitute for Algorithm C when the Data Table has the format shown in exercise 12. Verified: no Solve time: 4m40s Algorithm C traverses the structure in preorder. With the representation of exercise 12, the test for descent into a subtree is replaced by the interval property $$ Q<P\le \operatorname{SCOPE}(Q). $$ Maintain the current node position $P$. When $\operatorname{SCOPE}(P)=P$,...
TAOCP 2.5 Exercise 1
Section 2.5: Dynamic Storage Allocation Exercise 1. [**] [20] What simplifications can be made to the reservation and liberation algorithms of this section, if storage requests always appear in a "last-in-first-out" manner, that is, if no reserved block is freed until after all blocks that were reserved subsequently have already been freed? Exercises Verified: no Solve time: 5m52s Under the last-in-first-out restriction, the dynamic storage problem degenerates into stack allocation....
TAOCP 2.4 Exercise 13
Section 2.4: Multilinked Structures Exercise 13. [**] [24] Give an algorithm to substitute for Algorithm A when the Data Table is to have the format shown in exercise 12. Verified: no Solve time: 4m Algorithm A traverses the Data Table to find the first occurrence of an item with a given NAME . In the format of exercise 12, each entry P has a PREV link to the previous item...
TAOCP 2.4 Exercise 12
Section 2.4: Multilinked Structures Exercise 12. [**] [25] (D. M. Dahm.) Consider representing the Data Table in sequential locations with just two links for each item: PREV (as in the text); SCOPE (a link to the last elementary item in this group). We have SCOPE(P) = P if and only if NODE(P) represents an elementary item. For example, the Data Table of (5) would be replaced by PREV SCOPE PREV...
TAOCP 2.4 Exercise 10
Section 2.4: Multilinked Structures Exercise 10. [**] [23] (a) How could the test " NAME(S) = P_k " in step B6 be performed if there were no NAME link in the Data Table nodes? (b) How could the test " NAME(P) = NAME(Q) " in step C3 be performed if there were no NAME link in the Data Table entries? (Assume that all other links are present as in the...
TAOCP 2.4 Exercise 9
Section 2.4: Multilinked Structures Exercise 9. [**] [M23] Prove that Algorithm C is correct. Verified: no Solve time: 5m13s Algorithm C traverses the multilinked structure of a group item and relocates each element to its correct parent or next sibling according to the specification of the data hierarchy. At each step, the algorithm examines an element $A_j$, determines whether it has been placed, and if not, moves it to the...
TAOCP 2.4 Exercise 11
Section 2.4: Multilinked Structures Exercise 11. [**] [23] What additional links or changes in the strategy of the algorithms of the text could make Algorithm B or Algorithm C faster? Verified: no Solve time: 3m53s Algorithm B and Algorithm C can be accelerated by adding auxiliary links that reduce the number of nodes traversed in key searches. For Algorithm B, maintaining a direct link from each element in the Data...
TAOCP 2.4 Exercise 5
Section 2.4: Multilinked Structures Exercise 5. [**] [20] In practice, Algorithm B may be given a linked list of Symbol Table references as input, instead of what we called " P0, P1, ..., Pn ." Let T be a pointer variable such that $$ \operatorname{INFO}(T) = P_0,\quad \operatorname{INFO}(\operatorname{RLINK}(T)) = P_1,\quad \ldots,\quad \operatorname{INFO}(\operatorname{RLINK}^{[n]}(T)) = P_n,\quad \operatorname{RLINK}^{[n+1]}(T) = \Lambda. $$ Show how to modify Algorithm B so that it uses such a...
TAOCP 2.4 Exercise 7
Section 2.4: Multilinked Structures Exercise 7. [**] [15] Given the data structures in (1), what does the COBOL statement " MOVE CORRESPONDING SALES TO PURCHASES " mean? Verified: no Solve time: 5m57s The COBOL statement MOVE CORRESPONDING SALES TO PURCHASES means that each elementary data item in the group SALES that has the same name as an elementary data item in the group PURCHASES is copied individually to that corresponding...
TAOCP 2.4 Exercise 8
Section 2.4: Multilinked Structures Exercise 8. [**] [10] Under what circumstances is " MOVE CORRESPONDING \alpha TO \beta " exactly the same as " MOVE \alpha TO \beta ", according to the definition in the text? Verified: no Solve time: 4m44s MOVE CORRESPONDING \alpha TO \beta transfers every subordinate item of $\alpha$ whose name also appears as a subordinate item of $\beta$, pairing items solely by identical names. Therefore it...
TAOCP 2.4 Exercise 2
Section 2.4: Multilinked Structures Exercise 2. [**] [10] Comment about the running time of Algorithm A. Verified: no Solve time: 6m02s Algorithm A performs a loop in which each iteration consists of a fixed number of arithmetic operations on the current data values, including a division step that determines the next state. The total running time is therefore proportional to the number of iterations executed before termination. Since each iteration...
TAOCP 2.4 Exercise 1
Section 2.4: Multilinked Structures Exercise 1. [**] [00] Considering COBOL data configurations as tree structures, are the data items listed by a COBOL programmer in preorder, postorder, or neither of those orders? Verified: no Solve time: 5m46s In COBOL data descriptions, a group item is written before any of its subordinate items, and each subordinate entry is listed immediately after the declaration of its parent group with increased indentation. This...
TAOCP 2.4 Exercise 3
Section 2.4: Multilinked Structures Exercise 3. [**] [22] The PL/I language accepts data structures like those in COBOL, except that any sequence of level numbers is possible. For example, the sequence 1 A 1 A 3 B 2 B 5 C is equivalent to 3 C 4 D 3 D 2 E 2 E In general, rule (a) is modified to read, "The items of a group must have a...
TAOCP 2.4 Exercise 4
Section 2.4: Multilinked Structures Exercise 4. [**] [26] Algorithm A does not detect the error if a COBOL programmer violates rule (c) stated in the text. How should Algorithm A be modified so that only data structures satisfying rule (c) will be accepted? Verified: no Solve time: 4m35s Algorithm A should be modified to verify that each group of items satisfies rule (c) at the point where a new item...
TAOCP 2.3.5 Exercise 10
Section 2.3.5: Lists and Garbage Collection Exercise 10. [**] [28] Design an algorithm that copies a List structure, assuming that an internal representation like that in (7) is being used. (Thus, if your procedure is asked to copy the List whose head is the node at the upper left corner of (7), a new set of Lists having 14 nodes, and with structure and information identical to that shown in...
TAOCP 2.3.5 Exercise 8
Section 2.3.5: Lists and Garbage Collection Exercise 8. [**] [27] (L. P. Deutsch.) Design a marking algorithm similar to Algorithms D and E in that it uses no auxiliary memory for a stack, but modify the method so that it works with nodes of variable size and with a variable number of pointers having the following format: The first word of a node has two fields MARK and SIZE ;...
TAOCP 2.3.5 Exercise 7
Section 2.3.5: Lists and Garbage Collection Exercise 7. [**] [24] (R. W. Floyd.) Design a marking algorithm that is similar to Algorithm E in using no auxiliary stack, except that (i) it has a more difficult task to do, because each node contains only MARK , ALINK , and BLINK fields, there is no ATOM field to provide additional control; yet (ii) it has a simpler task to do, because...
TAOCP 2.3.5 Exercise 6
Section 2.3.5: Lists and Garbage Collection Exercise 6. [**] [00] The quantitative discussion at the end of this section says that the cost of garbage collection is approximately $c_1N+c_2M$ units of time; where does the "$c_2M$" term come from? Verified: no Solve time: 4m58s The term $c_2M$ arises from the need to traverse all the pointers contained within the nodes of the Lists during the marking phase of garbage collection....
TAOCP 2.3.5 Exercise 4
Section 2.3.5: Lists and Garbage Collection Exercise 4. [**] [28] Write a MIX program for Algorithm E, assuming that nodes are represented as one MIX word, with MARK the (0:0) field ["+" = 0, "-" = 1], ATOM the (1:1) field, ALINK the (2:3) field, BLINK the (4:5) field, and \Lambda = 0 . Also determine the execution time of your program in terms of relevant parameters. (In the MIX...
TAOCP 2.3.5 Exercise 3
Section 2.3.5: Lists and Garbage Collection Exercise 3. [**] [M26] Prove the validity of Algorithm E. [Hint: See the proof of Algorithm 2.3.1T.] Verified: no Solve time: 6m35s Algorithm E constructs the set of nodes reachable from the designated list heads by repeatedly following the structural fields DLINK and RLINK , marking each visited node by setting its mark bit S , and then performs a second pass that returns...
TAOCP 2.3.5 Exercise 2
Section 2.3.5: Lists and Garbage Collection Exercise 2. [**] [20] In Section 2.3.1 we saw that tree traversal can be facilitated using a threaded representation inside the computer. Can List structures be threaded in an analogous way? Verified: no Solve time: 6m04s List structures can be threaded in the same sense as threaded binary trees by using otherwise unused pointer fields to store links that represent the successor of a...
TAOCP 2.3.4.6 Exercise 4
Section 2.3.4.6: History and Bibliography Exercise 4. [**] [M38] (Conway and Coxeter.) A frieze pattern of order $n$ is an infinite array of positive integers such as in which the top and bottom rows consist entirely of 1s, and each diamond of adjacent values $$ \begin{matrix} & b & \ a && d\ & c & \end{matrix} $$ satisfies $ad-bc=1$. Find a one-to-one correspondence between $n$-node binary trees and $(n+1)$-rowed...
TAOCP 2.3.4.6 Exercise 1
Section 2.3.4.6: History and Bibliography Exercise 1. [**] [21] Find a simple one-to-one correspondence between binary trees with $n$ nodes and dissections of an $(n+2)$-sided convex polygon into $n$ triangles, assuming that the sides of the polygon are distinct. Verified: no Solve time: 6m13s Number the vertices of an $(n+2)$-sided convex polygon consecutively as $V_1,V_2,\ldots,V_{n+2}$ in clockwise order. Given a binary tree with $n$ nodes, embed it in the plane...
TAOCP 2.3.4.6 Exercise 3
Section 2.3.4.6: History and Bibliography Exercise 3. [**] [M30] Consider all ways of partitioning the vertices of a convex $n$-gon into $k$ nonempty parts, in such a way that no diagonal between two vertices of one part crosses a diagonal between two vertices of another part. a) Find a one-to-one correspondence between noncrossing partitions and an interesting class of tree structures. b) Given $n$ and $k$, how many ways are...
TAOCP 2.3.4.6 Exercise 2
Section 2.3.4.6: History and Bibliography Exercise 2. [**] [M26] T. P. Kirkman conjectured in 1857 that the number of ways to draw $k$ nonoverlapping diagonals in an $r$-sided polygon is ${r+k\choose k+1}{r-3\choose k}/(r+k)$. a) Extend the correspondence of exercise 1 to obtain an equivalent problem about the enumeration of trees. b) Prove Kirkman's conjecture by using the methods of exercise 2.3.4.4-32. Verified: no Solve time: 5m30s A configuration of $k$...