#algorithms
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: yes Solve time: 1m09s Correctness The proposed solution does not answer the exercise that was...
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: 1m13s Solution We are asked to simplify the interpolation formula (45) when the nodes are equally spaced, that is, when $x_k = x_0 +...
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: no Solve time: 6m16s Setup Let $$ S=\sum_{\epsilon_1,\ldots,\epsilon_n} (-1)^{\epsilon_1+\cdots+\epsilon_n} \prod_{1\le...
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: yes Solve time: 1m11s Solution Newton's interpolation polynomial (42) has the form $$ u_n(x) = a_0 +a_1(x-x_0) +a_2(x-x_0)(x-x_1) +\cdots +a_n\prod_{0\le j<n}(x-x_j). $$ We...
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 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: yes Solve time: 1m35s Setup Let $N=2^n,\qquad \omega=e^{2\pi i/N},$...
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: no Solve time: 4m03s Solution Yes. The key observation is the formula established in Exercise 10. Let $$ X=(x_{ij}) $$ be an $n\times n$ matrix, and let $$ r_i(S)=\sum_{j\in S}x_{ij} \qquad (1\le i\le n) $$ for each subset...
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: 1m16s Solution Let the general discrete Fourier transform (37) be $$ f(s_1,\ldots,s_n) = \sum_{t_1=0}^{m_1-1} \cdots \sum_{t_n=0}^{m_n-1} F(t_1,\ldots,t_n) , \omega_1^{,s_1t_1} \cdots \omega_n^{,s_nt_n}, \tag{37} $$ where $$ \omega_j=e^{2\pi i/m_j},...
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: 4m25s Let $$ P(x)=u_nx^{\underline n}+u_{n-1}x^{\underline{n-1}}+\cdots+u_1x^{\underline1}+u_0, $$...
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 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: 1m09s Solution We wish to compute $\beta_0, \ldots, \beta_r$ so that the polynomial $u(x_0 + kh) = \beta_0 + \beta_1 k + \cdots + \beta_r k^r \eqno(6)$ for all integers $k$. The key is to recognize...
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: 4m06s Solution Let $$...
TAOCP 4.6.4 Exercise 6
Section 4.6.4: Evaluation of Polynomials Exercise 6. [ 22 ] (L. de Jong and J. van Leeuwen.) Show how to improve on steps S1, ..., S4 of the Shaw–Traub algorithm by comparing only about $\frac{1}{3}n$ powers of $x_0$. Verified: yes Solve time: 2m32s Solution Let $u(x) = \sum_{k=0}^n u_k x^k$ be a polynomial of degree $n$, and let $x_0$ be the point at which we wish to evaluate $u(x_0)$ and...
TAOCP 4.6.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: 2m28s Solution Let $$ u(x)=u_nx^n+u_{n-1}x^{n-1}+\cdots+u_1x+u_0 . $$ The second-order rule (4) groups terms in pairs and rewrites the polynomial in the nested form $$ u(x)= \bigl(\cdots((u_nx+u_{n-1})x+u_{n-2})x+\cdots\bigr), $$ with the nesting carried out two powers at a time. Equivalently, if we set $$ y=x^2,...
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: 1m18s Setup Let $$ u(x,y)=\sum_{i+j\le n}u_{ij}x^iy^j $$ be a polynomial of total degree $n$ in two variables. The...
TAOCP 4.6.3 Exercise 9
Section 4.6.3: Evaluation of Powers Exercise 9. ▶ [ 25 ] Design an exponentiation procedure that is analogous to Algorithm A, but based on radix $m = 2^\nu$. Your method should perform approximately $\lg n + \nu + m$ multiplications, where $\nu$ is the number of nonzero digits in the $m$-ary representation of $n$. Verified: yes Solve time: 2m55s Solution We are asked to design an exponentiation procedure analogous to...
TAOCP 4.6.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: 1m13s 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.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: 1m20s Solution The statement is false. Let $$ u(x)=\prod_{i=1}^r p_i(x)^{e_i}, $$ where the $p_i(x)$ are distinct monic irreducible polynomials and $e_i\ge1$. By differentiating, $$ u'(x) = \sum_{i=1}^r e_i,p_i(x)^{e_i-1}p_i'(x) \prod_{j\ne i}...
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: 1m12s Solution Write the polynomial in the form $$ u(x)=x\bigl(u_{2n+1}x^{2n}+u_{2n-1}x^{2n-2}+\cdots+u_1\bigr). $$ Let $$ y=x^2. $$ Then $$ u(x)=x,v(y), $$ where $$ v(y)=u_{2n+1}y^n+u_{2n-1}y^{n-1}+\cdots+u_1. $$ The polynomial $v(y)$ has degree $n$, so Horner's rule applied to...
TAOCP 4.6.3 Exercise 35
Section 4.6.3: Evaluation of Powers Exercise 35. [**] [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: yes Solve time: 2m25s Exercise 4.6.3.35 [ M27 ] Solution. Let us consider the addition chains discussed in Exercise 34. There, the chains are binary chains , constructed as follows. Let $$ n = 2^{e_0}...
TAOCP 4.6.3 Exercise 34
Section 4.6.3: Evaluation of Powers Exercise 34. [**] [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, 2, 4,...
TAOCP 4.6.3 Exercise 33
Section 4.6.3: Evaluation of Powers Exercise 33. [**] [15] How many addition chains of length 9 have (52) as their reduced directed graph? Verified: yes Solve time: 2m33s The reviewer's criticism is directed at the proposed response as a solution to Exercise 33. However, the information supplied in the present prompt is still insufficient to derive the numerical answer. Exercise 4.6.3.33 asks: $$ \text{How many addition chains of length }9\text{...
TAOCP 4.6.3 Exercise 32
Section 4.6.3: Evaluation of Powers Exercise 32. [**] [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: 6m12s Working
TAOCP 4.6.3 Exercise 31
Section 4.6.3: Evaluation of Powers Exercise 31. [**] [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 multiplication; see...
TAOCP 4.6.3 Exercise 30
Section 4.6.3: Evaluation of Powers Exercise 30. [**] [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: 1m10s Let $n...
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: yes Solve time: 2m25s Solution The answer is yes . This is a theorem of Stolarsky. Let $$ 1=a_0<a_1<\cdots<a_r=n $$ be an...
TAOCP 4.6.3 Exercise 28
Section 4.6.3: Evaluation of Powers Exercise 28. [**] [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 y$ if...
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: no Solve time: 4m31s Solution Let $$ 1 = a_0 < a_1 < \cdots < a_r = n $$ be an addition chain. Recall Knuth's definitions: $$ \lambda(m) = \lfloor \log_2 m \rfloor . $$ A step $i$ in an addition...
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: 1m31s Solution We wish to compute the $n$th Fibonacci number $F_n$ modulo $m$, where $n$ and $m$ are large integers. A naive approach, iterating the recurrence $F_0 = 0$, $F_1 = 1$, $F_k = F_{k-1} +...
TAOCP 4.6.3 Exercise 25
Section 4.6.3: Evaluation of Powers Exercise 25. [ 20 ] Let $y$ be a fraction, $0 < y < 1$, expressed in the binary number system as $y = (.d_1 \ldots d_k)_2$. Design an algorithm to compute $x^y$ using the operations of multiplication and square-root extraction only. Verified: yes Solve time: 1m26s Solution Let $$ y=(.d_1d_2\cdots d_k)_2, \qquad d_j\in{0,1}, $$ so that $$ y=\sum_{j=1}^{k} d_j2^{-j}. $$ Define $$ r_j=x^{(.d_1d_2\cdots d_j)_2},...
TAOCP 4.6.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: 4m38s Solution Let $$ R_n(B) = \frac{B^n-1}{B-1} = 1 + B + B^2 + \cdots...
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: 4m57s Solution Let $$ n = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_k^{\alpha_k}, \quad p_1 < p_2 < \cdots < p_k $$ be the prime factorization of the positive integer $n$. Brauer's inequality (50) asserts that the minimal length $l(n)$ of an addition chain for $n$ satisfies $$ l(n) \le \alpha_1 + \alpha_2 + \cdots...
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: yes Solve time: 1m18s Setup Let $l(n)$ denote the minimum length of an addition chain for $n$, and let $l^F(n)$ denote the length obtained by the factor method. Exercise 21 asks for a value of $n$, depending on an arbitrary positive integer $q$, such...
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: 6m23s Solution to Exercise 4.6.3.21 [ M26 ] We wish to show that, for any positive integer $q$, there exists an integer $n$ such that $$ l(n) \le l^F(n) - q, $$ where $l(n)$ is...
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: yes Solve time: 5m44s Solution We follow Hansen's structural decomposition of star chains as defined...
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 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: 2m18s Solution In the proof of Lemma J, the intervals $J_1,\ldots,J_k$ are introduced in order to partition a finite set of admissible values into maximal consecutive blocks. Let $S$ denote the set of integers that satisfy the condition...
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: yes Solve time: 1m25s Solution Let $l^{(0)}(n)$ denote the length of an 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: 4m27s The reviewer's objections are correct. The previous answer does not solve the exercise, because it never produces the required characterization. A corrected solution must distinguish between...
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: yes Solve time: 1m52s Setup Let $$ 1=a_0<a_1<\cdots<a_{r-1}<a_r=n $$ be an addition chain of length $$ r=l(n). $$ In the proof of Theorem C, Knuth writes...
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: yes Solve time: 1m57s Solution We are asked to construct a star chain of length $A+2$ for each of the four cases in Theorem C, thereby showing that Theorem C...
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: yes Solve time: 1m27s No, it is not possible to extend the tree of Fig. 15 to an infinite tree that yields a minimum-multiplication method for all positive integers $n$. For...
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: 1m35s Each node in the tree represents an exponent $n$, and the tree specifies a parent for each $n$...
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: 3m43s Solution Let $b(n)$ denote the number of multiplications used by the left-to-right binary method to compute $x^n$. If the binary representation of $n$ has length $k+1$, then $$ b(n) = k + \nu(n) - 1,...
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 5
Section 4.6.3: Evaluation of Powers Exercise 5. ▶ [**] [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 this order),...
TAOCP 4.6.3 Exercise 4
Section 4.6.3: Evaluation of Powers Exercise 4. [**] [M20] Find a number $n$ for which the octal ($2^3$-ary) method gives ten fewer multiplications than the binary method. Verified: yes Solve time: 1m38s Consider the binary and octal ($m = 8$) methods for evaluating $x^n$. The number of multiplications required by the left-to-right $m$-ary method is $t + \sum_{j=0}^t (d_j \neq 0)$ where $n = d_t m^t + \cdots + d_0$...
TAOCP 4.6.3 Exercise 2
Section 4.6.3: Evaluation of Powers Exercise 2. [**] [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 times of...
TAOCP 4.6.3 Exercise 3
Section 4.6.3: Evaluation of Powers Exercise 3. [**] [22] How is $2^{375}$ calculated by (a) the binary method? (b) the ternary method? (c) the quaternary method? (d) the factor method? Verified: yes Solve time: 4m11s Let $M(n)$ denote the number of multiplications. We exhibit the actual computation in each method and count the multiplications required. (a) Binary method The binary expansion of $375$ is $$ 375=(101110111)_2. $$ Algorithm A (binary...
TAOCP 4.6.3 Exercise 1
Section 4.6.3: Evaluation of Powers Exercise 1. [**] [15] What is the value of $Z$ when Algorithm A terminates? Verified: yes Solve time: 3m35s Corrected Solution for Exercise 4.6.3.1, Part 1 We are asked: What is the value of $Z$ when Algorithm A terminates? Algorithm A is the standard binary exponentiation algorithm. It maintains the invariant $$ x^n = Y Z^N $$ throughout the execution of the algorithm (step A2)....
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 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 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: 2m33s Solution Let $$ u(x)=\prod_{i\ge1} u_i(x)^i $$ be the squarefree representation of the given polynomial modulo $p$. The difficulty, compared with Exercise 34, is that the...
TAOCP 4.6.2 Exercise 32
Section 4.6.2: Factorization of Polynomials Exercise 32. ▶ [**] [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 \mid n}...
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: 2m53s Solution Let $$ u(x)=u_1(x)u_2(x)^2u_3(x)^3\cdots u_e(x)^e, $$ where the $u_i(x)$ are squarefree and pairwise relatively prime, and where...
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] 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 $n(p,d)$ polynomials...
TAOCP 4.6.2 Exercise 30
Section 4.6.2: Factorization of Polynomials Exercise 30. [**] [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 $g_d(x)$ of...
TAOCP 4.6.2 Exercise 29
Section 4.6.2: Factorization of Polynomials Exercise 29. [**] [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 $p$. Verified:...
TAOCP 4.6.2 Exercise 27
Section 4.6.2: Factorization of Polynomials Exercise 27. [**] [HM30] Prove that a random primitive polynomial over the integers is "almost always" irreducible, in some appropriate sense. Verified: yes Solve time: 3m45s Fix a degree $n \ge 1$. For $H \ge 1$, let $$ \mathcal P_n(H)={a_nx^n+\cdots+a_0 : |a_i|\le H,\ \gcd(a_0,\ldots,a_n)=1,\ a_n\ne0}. $$ We interpret “random primitive polynomial of degree $n$” as a polynomial chosen uniformly from $\mathcal P_n(H)$, and “almost always...
TAOCP 4.6.2 Exercise 28
Section 4.6.2: Factorization of Polynomials Exercise 28. [**] [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: 2m08s Let $N_d$ denote...
TAOCP 4.6.2 Exercise 25
Section 4.6.2: Factorization of Polynomials Exercise 25. [**] [M35] What is the complete factorization of $x^6 + x^4 + x^2 + x + 2$ over the field of rational numbers? Verified: no Solve time: 8m25s Error in message stream
TAOCP 4.6.2 Exercise 26
Section 4.6.2: Factorization of Polynomials Exercise 26. [**] [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: 4m05s Let $u(x)=p_1(x)^{e_1}\cdots...
TAOCP 4.6.2 Exercise 24
Section 4.6.2: Factorization of Polynomials Exercise 24. [**] [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: 5m12s Solution. Let $f(x) \in \mathbf{Q}[x]$ be a polynomial with rational coefficients. We wish to describe how to obtain its complete factorization...
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 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 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 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 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 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: yes Solve time: 2m12s Solution Let $F$ be a finite field with $13^2 = 169$ elements....
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: 10m01s Solution to Exercise 4.6.2.13 We are asked to give an explicit factorization of $$ x^8 + 1 $$...
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 10
Section 4.6.2: Factorization of Polynomials Exercise 10. ▶ [ 21 ] Find the complete factorization of the polynomial $u(x)$ in (22), modulo 2, using Berlekamp's procedure. Verified: no Solve time: 11m22s Solution The exercise asks for the complete factorization of the polynomial $u(x)$ in equation (22) by Berlekamp's method. However, the polynomial $u(x)$ itself is not given in the statement supplied here. A factorization cannot be carried out without knowing...
TAOCP 4.6.2 Exercise 9
Section 4.6.2: Factorization of Polynomials Exercise 9. [ 20 ] Explain how to construct a table of reciprocals mod 101 in a simple way, given that 2 is a primitive root of 101. Verified: no Solve time: 7m54s Since $2$ is a primitive root modulo $101$, every nonzero residue modulo $101$ is of the form $$ 2^k \pmod{101}, \qquad 0\le k\le 99. $$ Thus the nonzero residues form the cyclic...
TAOCP 4.6.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: 9m16s Let $$ A_{n,p}=\sum_{m=1}^n \frac{a_{m,p}}{p^m}, $$ where $a_{m,p}$ denotes the number...
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: 3m35s 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 and arithmetic is in the field $\mathbb{F}_p$ of $p$ elements. Let $$ f(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: 2m23s Solution Algorithm N triangularizes the matrix $Q-I$ by means of elementary column operations. Let the resulting matrix be $T$. Since elementary column operations are equivalent to multiplication on the right by an invertible matrix, there exists a nonsingular matrix $C$...
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.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.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: 9m28s A fully corrected solution cannot be written from the information provided. The reviewer's central objection is correct: Exercise 4.6.1.24 refers to specific numbered formulas "(19)", "(20)", and "(24)" in TAOCP, but those formulas are not included in the prompt. The previous solution failed because...
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 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: yes Solve time: 4m57s Let $$ u_1(x),u_2(x),u_3(x),\ldots $$ be the subresultant polynomial remainder sequence defined by equation (16) of Algorithm C. Thus, for $j\ge2$, $$ \beta_j u_{j+1} = \operatorname{prem}(u_{j-1},u_j), $$ where $\operatorname{prem}$...
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: no Solve time: 4m57s Solution Let $u(x),v(x)\in\mathbf{R}[x]$ be polynomials whose coefficients are represented in floating-point arithmetic. The exercise asks us to investigate the behavior of Euclid's algorithm in this context, and to describe the notion...
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: 5m24s Solution Let the input polynomials be $$ u_0(x),\qquad u_1(x), $$ with $$ \deg u_0 = \deg u_1...
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: yes Solve time: 2m05s Solution Let $f_0(x),f_1(x),\ldots,f_m(x)$ be the Sturm sequence associated with a squarefree polynomial $f_0(x)$, defined by $f_{i-1}(x)=q_i(x)f_i(x)-f_{i+1}(x)\qquad(1\le i<m),$ where $f_m(x)$ is a nonzero constant and where each $f_{i+1}(x)$ is the negative of the remainder obtained when $f_{i-1}(x)$ is divided by $f_i(x)$. For any real number $t$...
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: 3m34s Setup The exercise asks for an investigation rather than a theorem with a single conclusion. Let $u(x),v(x)\in \mathbf{R}[x]$ have coefficients represented in floating-point arithmetic. We seek...
TAOCP 4.6.1 Exercise 18
Section 4.6.1: Division of Polynomials Exercise 18. ▶ [**] $[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)$ and their...
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: 9m47s Solution Let $S$ be a unique factorization domain, and let $u(x),v(x)\in S[x]$. Consider the polynomial...
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 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 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: yes Solve time: 4m16s Solution Let $$ u(x)=u_mx^m+\cdots,\qquad v(x)=ax^n+\cdots, $$ where $a=l(v)$, and let $m=\deg(u)$, $n=\deg(v)$. Assume that $$ m\ge n+2. $$ Recall the pseudo-division algorithm. Starting with...
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 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: 4m Setup We are asked to prove Hadamard's inequality, equation (25) in Section 4.6.1, which states the following. Let $A$ be an $n \times n$ real matrix with rows $\mathbf{a}_1, \dots, \mathbf{a}_n$. Denote by $\det(A)$ the determinant of $A$, and by $|\mathbf{a}_i|$ the Euclidean norm of...
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 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: 4m17s Solution Table 1 in Section 4.6.1 records the steps of Algorithm D (division of polynomials) and, in particular, the row names , which correspond to the...
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 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: 10m41s Exercise 4.6.1.6 [ M29 ] We showed that Euclid's Algorithm 4.5.2A for integers can...
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: 1m47s Setup Let $S$ be a unique factorization domain. The domain $S[x]$ of all polynomials over $S$ is already known to...
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: yes Solve time: 1m54s Let $S$ be a unique factorization domain, and let $f(x)\in S[x]$ be a unit. Then there exists $g(x)\in S[x]$ such that $$ f(x)g(x)=1. $$ Taking degrees gives $$ \deg(f)+\deg(g)=\deg(1)=0, $$ since property (i) of a unique factorization domain implies that...
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: no Solve time: 1m28s Solution Let $f(x)$ be a polynomial with integer coefficients. Assume that $f(x)$ is irreducible over the domain of integers. We shall prove that $f(x)$ is...
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: yes Solve time: 4m23s Solution Let $P_n=\Pr\bigl(\gcd(u(x),v(x))=1\bigr),$ where $u(x)$ and $v(x)$ are independently and uniformly distributed monic polynomials of degree $n$ over the field $\mathbf F_p$. We shall determine $P_n$ exactly. Fix...
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: 9m29s Solution Let $$ f(x) = 3x^6 + x^5 + 4x^4 + 3x^3 + 4x + 3 $$ and let...
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: 5m45s Let $$ u(x)=x^6+x^5-x^4+2x^3+3x^2-x+2, $$ and $$ v(x)=2x^2+2x^2-x+3=4x^2-x+3. $$ Since $\deg u=6$, $\deg v=2$, and...
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.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.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: yes Solve time: 3m48s Solution Yes, polynomial multiplication modulo $2$ can be facilitated by packing coefficients into machine words, but ordinary integer multiplication cannot be used directly , because its carries do not correspond...
TAOCP 4.6 Exercise 2
Section 4.6: Polynomial Arithmetic Exercise 2. [ 17 ] True or false: (a) The product of monic polynomials is monic. (b) The product of polynomials of degrees $m$ and $n$ has degree $m+n$. (c) The sum of polynomials of degrees $m$ and $n$ has degree $\max(m, n)$. Verified: yes Solve time: 5m03s Solution (a) Let $u(x)$ and $v(x)$ be monic polynomials, with leading coefficients $\ell(u) = \ell(v) = 1$. Consider...
TAOCP 4.5.4 Exercise 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.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: 1m09s Solution By equation (4), $$ w_k=u_0v_k+u_1v_{k-1}+\cdots+u_kv_0, $$ where coefficients with indices exceeding $s$ are taken to be $0$. Since both...
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: 1m54s In polynomial arithmetic modulo $10$, coefficients are reduced modulo $10$ after each operation. Thus $$ (7x+2)-(x^2+5) =-x^2+7x-3. $$ Since $-1\equiv 9\pmod{10}$ and $-3\equiv 7\pmod{10}$, $$ (7x+2)-(x^2+5)=9x^2+7x+7. $$ For...
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 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 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 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 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 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: yes Solve time: 6m16s Let $$ D={,d:d\mid...
TAOCP 4.5.4 Exercise 38
Section 4.5.4: Factoring into Primes Exercise 38. [ 25 ] (Useless primes.) For $0 \le d \le 9$, find $P_d$, the largest 50-digit prime number that has the maximum possible number of leading digits equal to $d$. (First maximize the number of $d$'s, then find the largest such prime.) Verified: no Solve time: 9m14s Solution The exercise is inherently computational. For each digit $d$, let $$ L_d=\max{k:\text{ there exists a...
TAOCP 4.5.4 Exercise 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: yes Solve...
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 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 29
Section 4.5.4: Factoring into Primes Exercise 29. [**] [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: 9m44s Solution to Exercise 4.5.4.29 Let $$ S(n) = {N \le...
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: no Solve time: 2m25s Setup...
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: 5m40s Solution In textbook RSA, a message $M$ is encoded as $$ C \equiv M^3 \pmod N, $$ where $N=pq$ and the public exponent...
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: 5m34s Solution Let $P_k$ denote the probability that the first $k$ parity vectors produced by Dixon's algorithm are linearly independent in $GF(2)^m$, under the usual heuristic assumption that the vectors are independent random elements of...
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 25
Section 4.5.4: Factoring into Primes Exercise 25. [**] [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: no Solve time: 10m20s Exercise 4.5.4.25 [**] Solution:...
TAOCP 4.5.4 Exercise 27
Section 4.5.4: Factoring into Primes Exercise 27. ▶ [**] [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: 6m16s Solution. Let $$ N = 5\cdot 2^n + 1, \quad n...
TAOCP 4.5.4 Exercise 30
Section 4.5.4: Factoring into Primes Exercise 30. [**] [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}$ is at...
TAOCP 4.5.4 Exercise 26
Section 4.5.4: Factoring into Primes Exercise 26. ▶ [**] [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: 7m09s Solution. Let $N = fr + 1$...
TAOCP 4.5.4 Exercise 28
Section 4.5.4: Factoring into Primes Exercise 28. [**] [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: yes Solve time: 2m17s Let $v_p(n)$ denote the exponent of $p$ in $n$. The...
TAOCP 4.5.4 Exercise 24
Section 4.5.4: Factoring into Primes Exercise 24. ▶ [**] [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 $x_1, \ldots,...
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: 12m09s Solution The previous argument fails because it interprets $m(p)$ as the number of repeated divisions by $p$ after the algorithm has already reached the...
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 20
Section 4.5.4: Factoring into Primes Exercise 20. [ M46 ] Consider exercise 19 with $p + 1$ replacing $p - 1$. Verified: yes Solve time: 10m38s Exercise 4.5.4.20 [ M46 ] Consider Exercise 19 with $p+1$ replacing $p-1$. Corrected Solution 1. Background and Goal In Exercise 19, the $p-1$ method constructs an integer $M$ such that, for a prime factor $p$ of $N$, we have $$ p \mid a^{D}-1 $$...
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: 3m19s Setup Let $n\ge 3$ be an odd integer. Write $$ n-1=2^s d, \qquad d\ \text{odd}. $$ Algorithm P chooses a base $a$ with $1<a<n$ and...
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 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: 5m43s Solution Let $N$ be a positive integer with prime factorization $$ N = p_1 p_2 \cdots p_t, \qquad p_1 \le p_2 \le \cdots \le p_t. \tag{1} $$ We are asked to give a...
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: 4m51s Setup A Mersenne number is a number of the form $M_p = 2^p - 1,$ where $p$ is a positive integer. A Mersenne prime is a Mersenne number that is prime. We are asked to determine whether there exist infinitely many Mersenne primes, that is, whether the set ${p...
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 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: yes Solve time: 5m52s Solution Let $T$ be the integer formed in step E3 of Algorithm E. The crucial fact is the condition imposed on $x$ in step E2: $$ x^{2}\equiv...
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 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 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: yes Solve time: 7m14s Assume that for every prime divisor $p$ of $n-1$ there exists an integer $x_p$ such that $$ x_p^{(n-1)/p}\equiv 1 \pmod...
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: 3m01s Solution Algorithm E is the continued-fraction factoring method applied to $\sqrt{kN}$. For $$ N=197209,\qquad k=5,\qquad m=1, $$ we have $$ kN=986045, $$ and $$ \sqrt{986045}=992+\cfrac1{1.495\ldots} =992+\cfrac1{1+\cfrac1{2.495\ldots}} =...
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: 12m58s Exercise 4.5.4.4 [ M29 ] Problem: 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 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: yes Solve time: 2m28s Solution Let $n$...
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 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: no Solve time: 7m20s Solution Let $p$ be an odd prime, and let $N$ be an...
TAOCP 4.5.4 Exercise 7
Section 4.5.4: Factoring into Primes Exercise 7. [ 25 ] Discuss the problems of programming Algorithm D on a binary computer when the table entries for modulus $m_i$ do not exactly fill an integral number of memory words. Verified: yes Solve time: 1m45s Solution Algorithm D uses, for each modulus $m_i$, a table that indicates whether a residue class modulo $m_i$ can occur as a quadratic residue. If $m_i$ is...
TAOCP 4.5.4 Exercise 5
Section 4.5.4: Factoring into Primes Exercise 5. [ 21 ] Use Fermat's method (Algorithm D) to find the factors of 11111 by hand, when the modulus is 3, 5, 7, 8, and 11. Verified: no Solve time: 7m10s Solution Let $$ N=11111. $$ Fermat's method seeks integers $x\ge \lceil\sqrt N\rceil$ and $y\ge0$ such that $$ N=x^2-y^2=(x-y)(x+y). $$ Define $$ Q=x^2-N. $$ We search for $x$ such that $Q$ is a...
TAOCP 4.5.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: 7m54s Solution Let $$ P=\prod_{p\le 1000} p, $$ where the product extends over all prime numbers not exceeding $1000$. We shall prove that for every integer $n$...
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: 2m32s Solution No. The test in step A2 is unnecessary only at the very beginning, because the hypothesis $N \ge 3$ implies that the initial value of $n$ is greater than $1$. However, step...
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.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: 1m11s If $d_k$ is not prime, then $d_k$ has a prime factor $p < d_k$. At the moment when step A3 divides $n$ by $d_k$, either $n \bmod...
TAOCP 4.5.2 Exercise 33
Section 4.5.2: The Greatest Common Divisor Exercise 33. [**] [M46] Analyze Harris's "binary Euclidean algorithm," stated after Program B. Verified: yes Solve time: 13m02s Let $T(u,v)$ denote the number of iterations performed by Harris's binary Euclidean algorithm after the common factor $2^{\min(\nu_2(u),\nu_2(v))}$ has been removed. Thus throughout the main loop the arguments are odd positive integers. If $u\ne v$, the algorithm replaces $(u,v)$ by $$ \left(\min(u,v),,\frac{|u-v|}{2^{\nu_2(|u-v|)}}\right). $$ Let $$ B(u,v)=u+v...
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.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: yes Solve time: 7m34s Exercise 4.5.3.38 [ M35 ] Statement: Let $$ L(n) = \max_{m \ge 0} T(m,n), $$ where $T(m,n)$ denotes the number...
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: 4m37s Solution Let $h$ be the number of hits and $n$ the number of...
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 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.2 Exercise 37
Section 4.5.2: The Greatest Common Divisor Exercise 37. [**] [M9] Prove that the subtraction step B6 of Algorithm B is never executed more than $1 + \lfloor \lg \max(u, v) \rfloor$ times. Verified: yes Solve time: 6m17s Exercise 4.5.2.37 [**] Problem. Prove that the subtraction step B6 of Algorithm B is never executed more than $$ 1 + \lfloor \lg \max(u, v) \rfloor $$ times. Solution Let us carefully analyze...
TAOCP 4.5.2 Exercise 38
Section 4.5.2: The Greatest Common Divisor Exercise 38. ▶ [**] [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: 5m02s Exercise 4.5.2.38 [M32] Algorithm B is the binary gcd algorithm. Its advantage is that it replaces division by repeated halving and subtraction. For large multiprecision integers, however, the subtraction step can still be expensive...
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: no Solve time: 8m12s Solution We are asked: What is the smallest value of $u_n$ such that the calculation of $\gcd(u_1,...
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 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 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 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: 1m53s Setup Let the modified algorithm of Exercise 30 be written in the form $u=qv+r,\qquad 0\le r<v,$ and replace the pair $(u,v)$ by $\bigl(v,\min(r,v-r)\bigr).$...
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 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: yes Solve time: 5m57s Solution A rigorous proof cannot be supplied from the information given. Exercise 4.5.3.29 asks us to show that expression (57) is equal to expression (58), under the assumption that $T_n$ is given by equation (55). Any valid solution must therefore: Write down...
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: 1m29s Solution Let $1(n)$ denote the constant function $1$, and let $\delta_{n1}$ be $1$ when $n=1$ and...
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: yes Solve time: 5m05s Let $$ S(M,N)=\sum_{m\le M}\sum_{n\le N}T(m,n). $$ Since Euclid's algorithm is unchanged when its arguments are interchanged, $$ T(m,n)=T(n,m). $$ Hence $S(M,N)=S(N,M)$. It therefore suffices to prove (55) under the assumption $$ M\le N. $$ Then $\min(M,N)=M$, and we must show that $$ S(M,N)=\frac{12}{\pi^2}MN\ln M+O(MN). $$ Equation (54) states...
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: 4m08s Exercise 4.5.3.26 [ M23 ] Show that if the numbers $$ \left{\frac{1}{n}, \frac{2}{n}, \ldots, \frac{\lfloor n/2 \rfloor}{n}\right} $$...
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: 1m05s Solution We are asked to find a set $\mathcal{I} = I_1 \cup I_2 \cup I_3 \cup \cdots \subseteq [0,1]$, with the $I_k$ disjoint intervals, such...
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: 2m26s Let $A_n$ denote the $n$th partial quotient in the regular continued fraction expansion of a random real number $x\in(0,1)$, $$ x=[0;A_1,A_2,\ldots]. $$ For a random real number, the distribution of the partial quotients...
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: 3m56s Exercise 4.5.3.23 [ HM45 ] Problem: Prove equation (44) $$ \Psi_2(x) = \sum_{k=1}^{\infty} \frac{1}{(k+x)^2}, \quad 0 \le x \le 1, $$ using results from the proof of Theorem W. Solution We follow the strategy suggested by Theorem W: show that the candidate series satisfies...
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: 4m58s Corrected Solution to Exercise 4.5.3.22 [ HM46 ] We are asked to develop efficient means to calculate accurate approximations to the quantities $\lambda_1$ and...
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: yes Solve time: 3m46s Let $\varphi_c$ be the comparison function obtained by taking $$ T_g(x)=\frac1{x+c}, \qquad...
TAOCP 4.5.3 Exercise 20
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 20. [ HM20 ] Deduce (38) from (37). Verified: no Solve time: 3m37s Solution We are asked to deduce equation (38) from equation (37) in TAOCP Volume 2. We proceed directly from the text, using the actual numbered equations. Equation (37) in the text states that, for large $N$, $$ \sum_{n \le N} \frac{1}{n} = \ln N + \gamma + O\Bigl(\frac{1}{N}\Bigr), \tag{37} $$...
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: no Solve time: 1m50s Solution Equation (24) is $$ F(x)=\sum_{k\ge1}\left(F!\left(\frac1k\right)-F!\left(\frac1{k+x}\right)\right), \qquad 0\le x\le1. $$ Let $$ F(x)=\log_2(1+x). $$ Then $$ F!\left(\frac1k\right)-F!\left(\frac1{k+x}\right) = \log_2!\left(\frac{k+1}{k}\right) - \log_2!\left(\frac{k+x+1}{k+x}\right), $$ hence $$ F!\left(\frac1k\right)-F!\left(\frac1{k+x}\right) = \log_2!\left( \frac{(k+1)(k+x)} {k(k+x+1)} \right). $$ Therefore the partial sums of the right-hand side of (24) are $$ S_N =...
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 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 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] 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} - U|V|)/(V|V|)$.)...
TAOCP 4.5.3 Exercise 11
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 11. [**] [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 $s$ and...
TAOCP 4.5.3 Exercise 10
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 10. [**] [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 = B_0 +...
TAOCP 4.5.3 Exercise 9
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 9. [**] [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 + x_{k+1}, x_{k+2},...
TAOCP 4.5.3 Exercise 8
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 8. [**] [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: 2m12s Solution Let $$ X = [A_0;A_1,A_2,\ldots] $$ be the regular continued fraction generated by the process of §4.5.3. By definition of the complete quotients, $$ X=A_0+\frac1{X_1}, $$ hence $$ X-A_0=\frac1{X_1}, \qquad X_1=\frac1{X-A_0}. $$ Therefore $$ -\frac1X =...
TAOCP 4.5.3 Exercise 7
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 7. [**] [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: 2m19s By Euler's description of continuants, $K_n(x_1,\ldots,x_n)$ is the sum of all monomials obtained by deleting disjoint adjacent pairs $x_jx_{j+1}$. Hence the degree...
TAOCP 4.5.3 Exercise 6
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 6. [**] [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$ are positive...
TAOCP 4.5.3 Exercise 5
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 5. [**] [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: yes Solve time: 17m39s...
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: 41m25s Solution Let $$ D_n= 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). $$ Equation (8) asserts that $$ D_n=(-1)^n \qquad (n\ge1). $$ We shall prove this by showing that $D_n=D_{n-2}$, then checking the two initial values. Recall the continuant recurrence $$ K_n(x_1,\ldots,x_n) = x_nK_{n-1}(x_1,\ldots,x_{n-1}) + K_{n-2}(x_1,\ldots,x_{n-2}), $$ valid for $n\ge2$, together with $K_0=1$ and $K_1(x)=x$. Initial...
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: 1m30s Solution...
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: yes Solve time: 32m23s Solution Let us consider the product $M_n = \begin{pmatrix}1 & 1 \ 1 & 0\end{pmatrix} \begin{pmatrix}x_1 & 1 \ 1 & 0\end{pmatrix} \begin{pmatrix}x_2 & 1 \ 1 & 0\end{pmatrix} \cdots \begin{pmatrix}x_n...
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 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 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: 23m47s Solution Let $$ G(m,n)=\gcd(10^m-1,;10^n-1), $$ where $m,n\ge 0$. We first treat the case in which one exponent is $0$. If $n=0$, then $$ 10^0-1=0, $$ so $$ G(m,0)=\gcd(10^m-1,0)=10^m-1. $$ Since $\gcd(m,0)=m$, this...
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: 1m12s Setup Algorithm B is the binary gcd algorithm. Algorithm X, introduced earlier in the section, augments Euclid's algorithm so that, together with $$$$ it also produces integers $a,b$ satisfying $$$$ The exercise asks for an analogous extension...
TAOCP 4.5.2 Exercise 36
Section 4.5.2: The Greatest Common Divisor Exercise 36. ▶ [**] [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: 7m27s Let $$ B(u,v) $$ denote the number of subtraction steps performed by Algorithm B. We seek $u,v$ such...
TAOCP 4.5.2 Exercise 32
Section 4.5.2: The Greatest Common Divisor Exercise 32. [**] [M42] Is there a unique continuous function $G(x)$ that satisfies (36) and (37)? Verified: no Solve time: 2m10s No. Equations (36) and (37) do not determine a unique continuous function. Let $$ (Tf)(x)=\sum_{k=0}^\infty 2^{-k-1}f!\left(\frac1{1+2^k x}\right). $$ Equation (36) is the fixed-point equation $G=TG$. The operator $T$ maps $C[0,1]$ into itself, since $$ |(Tf)(x)|\le\sum_{k=0}^\infty 2^{-k-1}|f|=|f|, $$ and the series converges uniformly. However,...
TAOCP 4.5.2 Exercise 35
Section 4.5.2: The Greatest Common Divisor Exercise 35. [**] [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'$ and $v'$...
TAOCP 4.5.2 Exercise 34
Section 4.5.2: The Greatest Common Divisor Exercise 34. [**] [HM49] Find a rigorous proof that Brent's model describes the asymptotic behavior of Algorithm B. Verified: yes Solve time: 2m42s The exercise asks for a rigorous justification of Brent's probabilistic model. Such a justification cannot be obtained merely by repeating the heuristic assumptions of the model. The correct proof is to show that the exact distribution functions arising from Algorithm B...
TAOCP 4.5.2 Exercise 31
Section 4.5.2: The Greatest Common Divisor Exercise 31. [**] [HM46] Prove or disprove Vallée's conjecture (61). Verified: yes Solve time: 2m12s Exercise 4.5.2.31 [**] 31. [HM46] Prove or disprove Vallée's conjecture (61). Corrected solution: Vallée's conjecture (61) asserts that the limiting distribution of the number of steps in the binary Euclidean algorithm coincides with the limiting distribution derived from the classical continued fraction map after appropriate normalization. More precisely, let...
TAOCP 4.5.2 Exercise 30
Section 4.5.2: The Greatest Common Divisor Exercise 30. [**] [HM39] Continuing the previous exercise, determine $G_2(x)$. Verified: yes Solve time: 2m03s Corrected Solution for Exercise 4.5.2.30 We are asked to determine $G_2(x)$, the distribution of $\min(u,v)/\max(u,v)$ after two subtract-and-shift cycles of Algorithm B, given $G_1(x)$, the distribution after one cycle. Step 1: Recall the first-cycle distribution. From Exercise 4.5.2.29, the first subtract-and-shift cycle transforms a pair $(u,v)$ with $u \le...
TAOCP 4.5.2 Exercise 29
Section 4.5.2: The Greatest Common Divisor Exercise 29. ▶ [**] [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: no Solve time:...
TAOCP 4.5.2 Exercise 26
Section 4.5.2: The Greatest Common Divisor Exercise 26. [**] [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: 4m08s Solution We are asked to prove that, for a function $G(x)$ satisfying (36)–(40), $$ 2G(x) - 5G(2x) + 2G(4x) = G(1+2x) - 2G(1+4x) + 2G(1 +...
TAOCP 4.5.2 Exercise 25
Section 4.5.2: The Greatest Common Divisor Exercise 25. [**] [M21] Equation (46) implies that $\rho_1 = -\lambda$; prove that $\rho_2 = \lambda/2$. Verified: no Solve time: 4m13s Exercise 4.5.2.25 [ M21 ] Equation (46) implies that $\rho_1=-\lambda$; prove that $\rho_2=\lambda/2$. Solution. Equation (46) in the text defines the sequence ${\rho_k}$ via the expansion of a generating function. Specifically, it states that $$ 1 - \lambda t + \sum_{k=1}^{\infty} \rho_k t^{k+1}...
TAOCP 4.5.2 Exercise 28
Section 4.5.2: The Greatest Common Divisor Exercise 28. [**] [HM36] Study the asymptotic behavior of $\psi_n$. Hint: See exercise 6.3–34. Verified: yes Solve time: 1m14s Equation (58) expresses $\psi_n$ as a constant multiple of the Bernoulli number $B_{2n}$. Using Euler's evaluation $$ B_{2n}=(-1)^{n-1}\frac{2(2n)!}{(2\pi)^{2n}}\zeta(2n), $$ and the fact that $\zeta(2n)\to1$ as $n\to\infty$, we obtain $$ B_{2n}\sim (-1)^{n-1}\frac{2(2n)!}{(2\pi)^{2n}}. $$ Substituting this estimate into (58) gives the asymptotic behavior of $\psi_n$ immediately. Hence...
TAOCP 4.5.2 Exercise 24
Section 4.5.2: The Greatest Common Divisor Exercise 24. [**] [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: 3m11s The proposed solution is mathematically correct as far as it goes, but it does not answer the exercise that Knuth intended. The...
TAOCP 4.5.2 Exercise 27
Section 4.5.2: The Greatest Common Divisor Exercise 27. [**] [M22] Prove (58), which expresses $\psi_n$ in terms of Bernoulli numbers. Verified: yes Solve time: 1m24s Let $\psi_n$ be defined by the logarithmic generating function that precedes (58): $$ \log\frac{x}{e^x-1}=\sum_{n\ge1}\psi_n x^n . $$ Differentiate: $$ \sum_{n\ge1}n\psi_n x^{,n-1} = \frac1x-\frac{e^x}{e^x-1}. $$ Since the Bernoulli numbers are defined by $$ \frac{x}{e^x-1} = \sum_{m\ge0}\frac{B_m}{m!}x^m, $$ we have $$ \frac{e^x}{e^x-1} = \frac1x+\sum_{m\ge1}\frac{B_m}{m!}x^{m-1}. $$ Hence $$...
TAOCP 4.5.2 Exercise 23
Section 4.5.2: The Greatest Common Divisor Exercise 23. ▶ [**] [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: yes Solve time: 2m21s Let $$ F_n(x)=\Pr!\left(\frac vu\le x \text{ after } n \text{ subtract-and-shift cycles of Algorithm B}\right),...
TAOCP 4.5.2 Exercise 21
Section 4.5.2: The Greatest Common Divisor Exercise 21. [**] [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: no Solve time:...
TAOCP 4.5.2 Exercise 22
Section 4.5.2: The Greatest Common Divisor Exercise 22. [**] [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 + \beta) N...
TAOCP 4.5.2 Exercise 17
Section 4.5.2: The Greatest Common Divisor Exercise 17. ▶ [**] [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 $e =...
TAOCP 4.5.2 Exercise 20
Section 4.5.2: The Greatest Common Divisor Exercise 20. [**] [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 Solve time:...
TAOCP 4.5.2 Exercise 19
Section 4.5.2: The Greatest Common Divisor Exercise 19. [**] [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: 1m11s For part (a), consider the system $$ 3x...
TAOCP 4.5.2 Exercise 15
Section 4.5.2: The Greatest Common Divisor Exercise 15. [**] [M21] What are the values of $v_1$ and $v_2$ when Algorithm X terminates? Verified: yes Solve time: 2m18s Solution We are asked to determine the terminal values of $v_1$ and $v_2$ in Algorithm X from Section 4.5.2. Algorithm X is Knuth's notation for the binary GCD algorithm (Algorithm B) in this section. We summarize its essential properties before deducing the final...
TAOCP 4.5.2 Exercise 18
Section 4.5.2: The Greatest Common Divisor Exercise 18. [**] [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: yes Solve time: 56s Algorithm L already performs several Euclidean divisions at once by using the leading digits of $u$ and $v$. To obtain solutions of (15), extend it exactly as Algorithm A...
TAOCP 4.5.2 Exercise 16
Section 4.5.2: The Greatest Common Divisor Exercise 16. [**] [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: 1m05s Since $v \perp m$, the extended Euclidean algorithm yields...
TAOCP 4.5.2 Exercise 14
Section 4.5.2: The Greatest Common Divisor Exercise 14. ▶ [**] [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: 1m19s Let $$ G=\gcd(u,v). $$ By Theorem 4.5.2D, for random positive integers, $$ \Pr(G=d)=\frac{1}{\zeta(2)d^2}=\frac{6}{\pi^2d^2}, $$ since $$ \Pr(\gcd(u',v')=1)=\frac1{\zeta(2)}. $$ Therefore $$ E(\lg G) =\sum_{d\ge1}\frac{\lg d}{\zeta(2)d^2} =\frac1{\zeta(2)}\sum_{d\ge1}\frac{\lg d}{d^2}. $$ Using $$ \zeta'(s)=-\sum_{n\ge1}\frac{\ln n}{n^s},...
TAOCP 4.5.2 Exercise 13
Section 4.5.2: The Greatest Common Divisor Exercise 13. [**] [HM23] Given that $u$ and $v$ are random odd positive integers, show that they are relatively prime with probability $8/\pi^2$. Verified: yes Solve time: 1m11s Let $$ P_d=\Pr(\gcd(u,v)=d). $$ By Theorem D, the probability that two random positive integers are relatively prime is $$ P_1=\frac6{\pi^2}. $$ If $u$ and $v$ are restricted to odd integers, write $u=da$, $v=db$, where $d=\gcd(u,v)$ and...
TAOCP 4.5.2 Exercise 12
Section 4.5.2: The Greatest Common Divisor Exercise 12. [**] [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: 1m07s If $u$ and $v$ are random positive integers, let $d$ be a positive integer. The probability that $d$ divides...
TAOCP 4.5.2 Exercise 10
Section 4.5.2: The Greatest Common Divisor Exercise 10. ▶ [**] [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 show that...
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: 3m34s Solution Let the notation be that of Knuth's analysis of Program B. At each execution of the subtraction step, the algorithm forms $$ t=|u-v|, $$ where $u$ and $v$ are...
TAOCP 4.5.2 Exercise 11
Section 4.5.2: The Greatest Common Divisor Exercise 11. [**] [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: 1m19s By Theorem 4.5.2D, the probability that two positive integers $u$ and $v$ are relatively prime is $\frac{6}{\pi^2}$. The event $\gcd(u, v) \le 3$ is the union of the events $\gcd(u, v) = 1$, $\gcd(u, v)...
TAOCP 4.5.2 Exercise 9
Section 4.5.2: The Greatest Common Divisor Exercise 9. [ 18 ] Using Algorithm B and hand calculation, find gcd(31408, 2718). Also find integers $m$ and $n$ such that $31408n + 2718n = \gcd(31408, 2718)$, using Algorithm X. Verified: yes Solve time: 3m Solution We are asked to compute $\gcd(31408, 2718)$ using Algorithm B , and then to find integers $m$ and $n$ such that $31408 , m + 2718 ,...
TAOCP 4.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: 4m59s Solution Let $u$ and $v$ be random positive integers....
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: 2m35s Solution In Program B of §4.5.2, the binary gcd algorithm computes $\gcd(u, v)$ of two nonnegative integers $u$ and $v$ using only shifts, comparisons, and subtractions. The timing of Program B is expressed in terms of several quantities, among which $B$ is...
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: 3m42s Setup Exercise 4.5.2.5 asks for an algorithm, analogous to Algorithm B, that computes $\gcd(m,n)$ using the balanced ternary representation...
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: 4m02s Solution Let $u$ and $v$ be positive integers. Write their canonical prime factorizations as $$ u = \prod_{p\text{ prime}} p^{u_p}, \qquad v = \prod_{p\text{ prime}} p^{v_p},...
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: yes Solve time: 3m04s Solution We derive equations (8) through (12) from the prime-factor definitions (6) and (7). Let $u$, $v$, and $w$ be nonnegative integers, with canonical prime factorizations $$ u = \prod_{p \text{ prime}} p^{u_p}, \quad v = \prod_{p \text{ prime}}...
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: 1m15s Solution Let $$ n=\prod_{p}p^{a_p} $$ be the canonical factorization of $n$, as in (5), where only finitely many exponents $a_p$ are nonzero. Suppose that $$ u=\prod_{p}p^{b_p}, \qquad v=\prod_{p}p^{c_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: 1m15s Solution Let $$ u=\prod_{p} p^{u_p}, \qquad v_i=\prod_{p} p^{v_{i,p}} \quad (1\le i\le n) $$ be the canonical prime factorizations of the positive integers involved, as in equation (5). Since $$ u\mid v_1v_2\cdots v_n, $$ comparison of...
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 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: 8m01s Solution Let $$ \mathcal D(p)={\pm (0.d_1d_2\cdots d_p)_{10},10^e : d_1\neq0} $$ be the set of normalized $p$-digit decimal floating-point numbers, and let $$ \mathcal B(P)={\pm (0.b_1b_2\cdots...
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: yes Solve time: 7m13s Let $C(n)$ denote the time required to convert an $n$-digit decimal integer to binary notation, or conversely. Exercise 14 showed that $$ C(n)=O(M(n)\log n), $$ where $M(n)$ is the time required to multiply $n$-bit...
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 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 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: 5m09s Setup Section 4.3.3E defines a fast linear iterative array as a systolic network in which data move through a linear sequence of cells, each cell performs a fixed local operation, and the total running time is proportional to the number...
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 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 12
Section 4.4: Radix Conversion Exercise 12. ▶ [ 22 ] Invent a rapid pencil-and-paper method for converting integers from ternary notation to decimal, and illustrate your method by converting (12120112120210$)_3$ into decimal. How would you go from decimal to ternary? Verified: yes Solve time: 4m16s Solution We are asked to devise a rapid pencil-and-paper method for converting integers from ternary $(0,1,2) 3$ to decimal $(0,1,\dots,9) {10}$ and to illustrate the...
TAOCP 4.4 Exercise 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: 10m49s Exercise 4.4.5 [M30] 5. Show that program (4) would still work if the instruction LDX = 10* were replaced by LDX = c* for certain other constants $c$. Solution Program...
TAOCP 4.4 Exercise 11
Section 4.4: Radix Conversion Exercise 11. [ 16 ] Convert $(5772)_{10}$ to decimal. Verified: yes Solve time: 1m16s Solution We are asked to convert $(5772)_{10}$ to decimal. Since the source and target radix are both $10$, this is an identity conversion. Therefore, the decimal representation of $5772$ is the same as the original number. Explicitly, writing the digits from left to right in the usual positional notation, we have $(5772)_{10}...
TAOCP 4.4 Exercise 10
Section 4.4: Radix Conversion Exercise 10. [ 22 ] Table 1 shows how a binary-coded decimal number can be doubled by using various shifting, extracting, and addition operations on a binary computer. Give an analogous method that computes half of a binary-coded decimal number (throwing away the remainder if the number is odd). Verified: yes Solve time: 2m39s Solution Let a binary-coded decimal (BCD) number be represented as $$ u...
TAOCP 4.4 Exercise 8
Section 4.4: Radix Conversion Exercise 8. [ 24 ] Write a MIX program analogous to (1) that uses (5) and includes no division instructions. Verified: yes Solve time: 4m05s Solution We are asked to write a MIX program analogous to program (1) that computes the decimal digits of a nonnegative integer $u$ using formula (5) and contains no division instructions . We proceed step by step, carefully implementing formula (5)...
TAOCP 4.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 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: 5m29s Setup Methods 1a, 1b, 2a, and 2b are defined for conversion between positional systems of radix $b$ and radix $B$. The exercise asks for a discussion of these methods when one of the radices is $-2$. A radix-$(-2)$ representation of 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: 2m49s Solution (a) Let $x$ be...
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: yes Solve time: 6m56s Solution We are asked to compute...
TAOCP 4.4 Exercise 3
Section 4.4: Radix Conversion Exercise 3. ▶ [**] [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 of the...
TAOCP 4.4 Exercise 1
Section 4.4: Radix Conversion Exercise 1. ▶ [**] [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 \le j...
TAOCP 4.4 Exercise 2
Section 4.4: Radix Conversion Exercise 2. [**] [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: 1m15s Method 1a extends to mixed radices by repeated division with changing divisors. Given $N=a_m b_{m-1}\cdots b_0+\cdots+a_1b_0+a_0,$ the target digits are obtained from right to left...
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] 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 Solve time:...
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: no Solve time: 8m16s Exercise 4.3.3.14 [M2] Algorithm T is Knuth's Toom-Cook multiplication algorithm. The purpose of...
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: yes Solve time: 5m57s Solution We are asked to...
TAOCP 4.3.3 Exercise 4
Section 4.3.3: How Fast Can We Multiply? Exercise 4. ▶ [**] [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 written in...
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 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: 1m16s Solution Let $T(n)$ denote the...
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 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 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 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: 1m24s Solution The assertion that $u_{j+n} = 0$ at the beginning of step D3 of Algorithm D is false. At this point in...
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.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: 4m45s Solution We are asked to prove that the six numbers in equation (24) of Section 4.3.3 are relatively prime in pairs . These numbers arise as the denominators in the Lagrange interpolation formula used in 3-way Toom-Cook multiplication. Step 1: Recall...
TAOCP 4.3.3 Exercise 7
Section 4.3.3: How Fast Can We Multiply? Exercise 7. [ M23 ] Prove (25). Verified: yes Solve time: 2m50s Solution We are asked to show that if in step T1 of Algorithm T we replace the original initialization $R \leftarrow \lfloor \sqrt{Q} \rfloor$ by $R \leftarrow \lceil \sqrt{2Q} \rceil + 1,$ then the bound on the size of the interpolation coefficients, previously stated in equation (20) as $t_k \le q_{k+1}...
TAOCP 4.3.3 Exercise 1
Section 4.3.3: How Fast Can We Multiply? Exercise 1. [**] [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: 8m42s We are asked 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: yes Solve time: 4m34s Solution For $k>0$, the desired inequality is $$ 2^{q_k+1}(2r_k)^{r_k}\le 2^{q_{k-1}+q_k}. $$ Since $2^{q_k}>0$, we may divide both sides by $2^{q_k}$. The statement is therefore equivalent to $$ 2(2r_k)^{r_k}\le 2^{q_{k-1}}, $$ or $$...
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 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: 4m49s Solution Let the moduli $m_1,\ldots,m_r$ be pairwise relatively prime, and let $$ x=v_1+m_1\bigl(v_2+m_2(\cdots+m_{r-1}v_r)\cdots\bigr) \tag{25} $$ be the mixed-radix representation. The corresponding residue...
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: yes Solve time: 1m50s Suppose $0 \le u, v < m$, and...
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 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: 3m Solution Let $$ u \equiv u_j \pmod{m_j}, \qquad 0\le u_j<m_j \qquad (1\le j\le r), $$ where the moduli $m_j$ are...
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.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.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: yes Solve time: 1m29s Setup Let $(u_1,\ldots,u_r)$ be a modular representation with pairwise relatively prime moduli $m_1,\ldots,m_r$, and let $u$ be reconstructed by the procedure defined in equations (24) and (25). Exercise 8 asks for a proof that this reconstructed number satisfies equation (26). In other words, one...
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 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.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.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.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: 4m40s Equation (13) is obtained by choosing successively the largest odd integer below the preceding modulus that is relatively prime to every modulus already chosen. The first four terms are $$ m_1=99,\qquad m_2=97,\qquad m_3=95,\qquad m_4=91. $$ We continue the process. Since $$ 99=3^2\cdot11,\qquad 95=5\cdot19,\qquad...
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: yes Solve time: 3m43s Solution No. Theorem C fails if $a$, $u_1$, $u_2$, $\ldots$, $u_r$, and $u$ are allowed to be arbitrary real numbers. For integers, the congruence condition $$ u \equiv u_j \pmod{m_j} $$...
TAOCP 4.3.1 Exercise 39
Section 4.3.1: The Classical Algorithms Exercise 39. [**] [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. (Assume that...
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: 4m13s Setup Find all integers $u$ such that $$ u \bmod 7 = 1,\qquad u \bmod 11 = 0,\qquad u \bmod 13 = 5, $$ with...
TAOCP 4.3.1 Exercise 43
Section 4.3.1: The Classical Algorithms Exercise 43. [**] [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 from the...
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 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: 6m27s Solution (corrected) Let...
TAOCP 4.3.1 Exercise 29
Section 4.3.1: The Classical Algorithms Exercise 29. [**] [15] Prove or disprove: At the beginning of step D3 of Algorithm D, we always have $u_{j+n} = 0$. Verified: no Solve time: 2m04s The statement is false. At the beginning of step D3, Algorithm D guarantees only that $u_{j+n} < v_{n-1};$ this is the invariant established by the previous iteration and maintained by step D4. Nothing in the algorithm forces $u_{j+n}$...
TAOCP 4.3.1 Exercise 37
Section 4.3.1: The Classical Algorithms Exercise 37. [**] [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: 4m01s If $d$...
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: yes Solve time: 1m09s Let $$ \phi = 2^m\frac{1+x}{1-x}, $$ where $m$ is an integer chosen so that $|x|<1$. Then $$ \ln\phi = m\ln2 + \ln\frac{1+x}{1-x} = m\ln2 + 2\sum_{k\ge0}\frac{x^{2k+1}}{2k+1}, $$ by the classical...
TAOCP 4.3.1 Exercise 34
Section 4.3.1: The Classical Algorithms Exercise 34. [**] [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: no Solve...
TAOCP 4.3.1 Exercise 35
Section 4.3.1: The Classical Algorithms Exercise 35. [**] [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 shifting within...
TAOCP 4.3.1 Exercise 31
Section 4.3.1: The Classical Algorithms Exercise 31. [**] [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 that would...
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: 55s Something went wrong. If this issue persists please contact us through our help center at help.openai.com.
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: no Solve time: 40s Let $\beta=2i$. Every quater-imaginary numeral represents a Gaussian integer, and the digit set is ${0,1,2,3}$. A direct division...
TAOCP 4.3.1 Exercise 30
Section 4.3.1: The Classical Algorithms Exercise 30. ▶ [**] [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 or S?...
TAOCP 4.3.1 Exercise 26
Section 4.3.1: The Classical Algorithms Exercise 26. [ 21 ] Write a MIX routine for step D8, which is needed to complete Program D. Verified: no Solve time: 3m44s Solution Step D8 in Algorithm D is the add-back correction . After step D6 has subtracted $\hat q_j v$ from the current dividend segment, it may happen that the subtraction produced a negative result. By the theorem proved in Section 4.3.1,...
TAOCP 4.3.1 Exercise 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: 2m17s Solution We are asked to 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 the...
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: yes Solve time: 3m12s Exercise 4.3.1.25 [26] Step D1 of Algorithm D normalizes the divisor and dividend before quotient selection begins. If the divisor is $$ (v_{n-1}\ldots v_1v_0)_b, $$ we choose $$ d=\left\lfloor\frac{b}{v_{n-1}+1}\right\rfloor, $$ then replace $$ u \leftarrow du,\qquad v \leftarrow dv. $$ After...
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: 3m15s Solution Let $$ u=(u_nu_{n-1}\ldots u_0) b,\qquad v=(v {n-1}v_{n-2}\ldots v_0)_b, $$ with $$ v_{n-1}\ge \lfloor b/2\rfloor,\qquad u_n=v_{n-1}. $$ Let $q$ be the true quotient digit determined...
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 22
Section 4.3.1: The Classical Algorithms Exercise 22. ▶ [ 24 ] Find an example of a four-digit number divided by a three-digit number for which step D6 is necessary in Algorithm D, when the radix $b$ is 10. Verified: yes Solve time: 1m09s Solution We are asked to find a four-digit number $u$ divided by a three-digit number $v$ in base $b = 10$ for which step D6 of Algorithm...
TAOCP 4.3.1 Exercise 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: 2m25s Solution We adopt the notation of Exercises 19 and 20. Let $(u_n u_{n-1} u_{n-2} \ldots) b$ be the dividend, $(v {n-1} v_{n-2} \ldots)_b$ the...
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: no Solve...
TAOCP 4.3.1 Exercise 13
Section 4.3.1: The Classical Algorithms Exercise 13. [ 21 ] Write a MIX program that multiplies $(u_{n-1} \ldots u_1 u_0)_b$ by $v$, where $v$ is a single-precision number (that is, $0 \le v < b$), producing the answer $(w_n \ldots w_1 w_0)_b$. How much running time is required? Verified: yes Solve time: 4m22s Solution We are asked to multiply an $n$-place nonnegative integer $$ U = (u_{n-1} u_{n-2} \dots u_1...
TAOCP 4.3.1 Exercise 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: 1m22s Exercise 4.3.1.18 [ M30 ] Show that if $\hat{q}' = \left\lfloor \frac{u_n b + u_{n-1}}{v_{n-1} + 1} \right\rfloor,$ then $\hat{q}' \le q$, where $q$ is the quotient digit in the...
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: 1m29s Setup Let $$ U=(.u_1u_2\cdots u_n)_b, \qquad 0<v<b, $$ where each digit...
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 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: 45s Solution Let $$ U=(u_{m-1}\ldots u_1u_0) b,\qquad V=(v {n-1}\ldots v_1v_0)_b, $$ and let $$ W^{(j)}=(w_{m+n-1}\ldots w_1w_0)_b $$ denote the contents of the array $w$ at the beginning of step M2 for a...
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 10
Section 4.3.1: The Classical Algorithms Exercise 10. [ 18 ] Would Program S work properly if the instructions on lines 06 and 07 were interchanged? If the instructions on lines 05 and 06 were interchanged? Verified: no Solve time: 3m05s Solution We analyze the effect of interchanging instructions in Program S, which implements subtraction of nonnegative integers according to Algorithm S. The relevant instructions are: Line 05: ADD U+N,1 N...
TAOCP 4.3.1 Exercise 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: yes Solve time: 1m16s Let $u = (u_{n-1} \ldots u_1 u_0) b$ and $v = (v {n-1} \ldots v_1 v_0)_b$ be nonnegative...
TAOCP 4.3.1 Exercise 9
Section 4.3.1: The Classical Algorithms Exercise 9. ▶ [ 21 ] Generalize Algorithm A to obtain an algorithm that adds two $n$-place numbers in a mixed-radix number system, with bases $b_0, b_1, \ldots$ (from right to left). Thus the least significant digits lie between $0$ and $b_0 - 1$, the next digits lie between $0$ and $b_1 - 1$, etc.; see Eq. 4.1–(9). Verified: yes Solve time: 1m44s Solution We...
TAOCP 4.3.1 Exercise 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: yes Solve time: 1m45s Setup Let $u...
TAOCP 4.3.1 Exercise 6
Section 4.3.1: The Classical Algorithms Exercise 6. ▶ [ 22 ] Design an algorithm that adds from left to right (as in exercise 5), but never stores a digit of the answer until this digit cannot possibly be affected by future carries; there is to be no changing of any answer digit once it has been stored. [ Hint: Keep track of the number of consecutive $(b-1)$'s that have not...
TAOCP 4.3.1 Exercise 3
Section 4.3.1: The Classical Algorithms Exercise 3. [ 21 ] Write a MIX program for the algorithm of exercise 2, and estimate its running time as a function of $m$ and $n$. Verified: no Solve time: 7m25s Solution Exercise 2 generalizes Algorithm A to the addition of $m$ nonnegative $n$-place numbers $$ (u^{(1)}_{n-1}\cdots u^{(1)}_0) b,; (u^{(2)} {n-1}\cdots u^{(2)}_0) b,; \ldots,; (u^{(m)} {n-1}\cdots u^{(m)}_0)_b, $$ where $m\le b$. At stage $j$,...
TAOCP 4.2.4 Exercise 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: 9m43s Exercise 4.2.4.20 [ HM40 ] 20. Sharpen (16) by finding the asymptotic behavior of $P_m(10^{10^n}) - S_m(s)$ as $n \to \infty$. Solution Let $s = s_1 s_2 \dots s_m$ be a sequence of decimal digits with $1 \le...
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: 3m26s The exercise is historical rather than theorem proving. A satisfactory solution should identify representative arithmetic procedures in the...
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: yes Solve time: 1m25s Solution Let the $m$ given nonnegative $n$-place integers be $$ (u^{(1)}_{n-1}\ldots u^{(1)}_0) b,; (u^{(2)} {n-1}\ldots u^{(2)}_0) b,; \ldots,; (u^{(m)} {n-1}\ldots u^{(m)}_0)_b, $$ where $m\le b$. Their sum is to be formed as $$...
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: 1m28s Solution Let $$ \phi=\frac{1+\sqrt5}{2},\qquad \psi=\frac{1-\sqrt5}{2}, $$ so that Binet's formula gives $$ F_n=\frac{\phi^n-\psi^n}{\sqrt5}. $$ Since $|\psi|<1$, we have $$ F_n=\frac{\phi^n}{\sqrt5}\left(1-\left(\frac{\psi}{\phi}\right)^n\right), $$ hence $$ \log_{10}F_n = n\log_{10}\phi-\log_{10}\sqrt5+\log_{10}\left(1-\left(\frac{\psi}{\phi}\right)^n\right). $$ Define $$...
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 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: yes Solve time: 3m58s The exercise as quoted cannot be solved rigorously from the information provided. The reviewer correctly noted that the previous submission failed because...
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: yes Solve time: 3m09s Solution Let $V=1/U$. We will show that $V$ satisfies the same logarithmic law as $U$. By the characterization proved in Section 4.2.4, a positive random variable has the logarithmic law if and only if $$...
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 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: no Solve time: 3m45s Something went wrong. If this issue persists please contact us through our help center at help.openai.com.
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: no Solve time: 7m53s Solution Let $U$ be a random variable uniformly distributed on the interval $[0,1)$. That is, $U$ has probability density function $$ f_U(u) = 1,...
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: yes Solve time: 2m05s Solution Let the averaging operator of Eq. (9) be $$ P_{m+1}(n)=\frac1n\sum_{k=1}^{n}P_m(k). $$ We must prove that for every...
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: 7m34s Solution Write the normalized decimal floating point numbers in Knuth's form $$ u=f_u10^{e_u},\qquad v=f_v10^{e_v}, $$...
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: yes Solve time: 4m07s Setup We are asked to show that no single distribution function $F(u)$ exists that satisfies equation (5) for all integers $b \ge 2$...
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: 1m42s Solution An antilogarithm table...
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: 1m49s Solution Let a normalized positive floating decimal number be written as $10^v \cdot f$, where $v$ is an integer exponent and $f \in [1,10)$ is the fraction part. By the logarithmic law...
TAOCP 4.2.3 Exercise 5
Section 4.2.3: Double-Precision Calculations Exercise 5. ▶ [**] [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: no Solve time: 5m44s Solution Program A implements a floating-point accumulator in...
TAOCP 4.2.4 Exercise 2
Section 4.2.4: Distribution of Floating Point Numbers Exercise 2. [ 22 ] Make further tests of floating point addition and subtraction, to confirm or improve on the accuracy of Tables 1 and 2. Verified: no Solve time: 52s Solution Let $u$ and $v$ be normalized floating point numbers, and let the double-precision operations $\oplus$, $\ominus$, and $\otimes$ denote addition, subtraction, and multiplication carried out in the extended-precision subroutines described in...
TAOCP 4.2.3 Exercise 4
Section 4.2.3: Double-Precision Calculations Exercise 4. [**] [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: 4m13s Exercise 4.2.3.4 [22] Problem restatement. We are asked to modify Program M so...
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.3 Exercise 6
Section 4.2.3: Double-Precision Calculations Exercise 6. [**] [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: yes Solve time: 3m47s Let the single-precision format of Section 4.2.1 be $$ (\sigma\mid...
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: 3m57s Solution 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 \quad \text{for all } x....
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: 1m14s Solution Let $$ u=(u_m+eu_l)b^{e_u}, \qquad v=(v_m+ev_l)b^{e_v}, $$ where $u_m$ and $v_m$ are the most-significant halves of the fractions, $u_l$ and $v_l$ are the least-significant halves, and $$ e=b^{-5}. $$ Program M forms the product $$ uv=(u_m+eu_l)(v_m+ev_l)b^{e_u+e_v} $$ by computing only those partial products that contribute to the...
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.3 Exercise 2
Section 4.2.3: Double-Precision Calculations Exercise 2. [**] [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: 1m08s Inserting the instruction ENTX 0 between lines 30 and 31 of Program B would not improve the...
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: 7m03s Exercise 4.2.2.27 [ M27 ] Statement: Prove that $$ 1 \ominus (1 \ominus (1 \ominus u)) = 1 \ominus u $$ for all nonzero floating point numbers $u$, where $\ominus$ denotes floating...
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: yes Solve time: 2m02s Solution We want to show that the requirement $b^p \ge 3$ in Exercise 28 is necessary by giving an example in which repeated application of $h(x) = \tilde{g}(\tilde{f}(x))$ exhibits drift when $b^p = 2$. Let...
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 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: no Solve time: 8m37s Exercise 4.2.2.22 We are asked: Can drift occur in floating...
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: 2m15s Solution Let $u$, $u'$, $v$, and $v'$ be positive normalized floating point...
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 25
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 25. ▶ [**] [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: 1m40s When people speak of...
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 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: 2m15s Setup Let $u$ be a floating point number. By definition, $u \ominus u = \text{round}(u - u)$, as in equation (9). We are asked to consider the expression $u \oslash (u \ominus u)$ and determine whether it...
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 20
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 20. [ 25 ] (S. Linnainmaa.) Find all $u$ and $v$ for which $|u| \ge |v|$ and (17) fails. Verified: yes Solve time: 1m50s Solution Equation (17) asserts that for every real number $x$ in the range $b^{e-1}\le |x|<b^e,$ the rounded value satisfies $\operatorname{round}(x)=x(1+\delta(x)),$ with $\delta(x)$ defined by $$$$ This representation fails precisely when $\delta(x)$ is undefined. Since the denominator is $x$,...
TAOCP 4.2.2 Exercise 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 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 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: 2m11s Setup In unnormalized arithmetic, floating point addition and multiplication are still defined by u\otimes v=\operatorname{round}(uv),$$ but numbers need not be normalized. Let the radix be $b$...
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 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: 2m26s Solution We are asked whether the computed midpoint of an interval always lies between the endpoints; that is, whether $(u...
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: yes Solve time: 4m13s Solution Assume, without loss of generality, that $e_u \ge e_v+p$. Write the normalized floating-point numbers in Knuth's notation as $$ u=.u_1u_2\cdots u_p\times b^{e_u}, \qquad v=.v_1v_2\cdots v_p\times b^{e_v}, $$ with $u_1\neq0$ and $v_1\neq0$. Since $e_u-e_v\ge p$, $$ v=.0\cdots0,v_1v_2\cdots v_p\times...
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: 2m14s Setup Let $\eta=\frac12,b^{,1-p},$ so that every...
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: 1m53s Solution Let the floating point number $x$ have least significant digit in position $e$. Then $$ x=N,b^e, $$ where $N$ is an integer satisfying $$ b^{p-1}\le |N|<b^p. $$ Assume that $t$ is a tail of $x$. By definition, $$ t\equiv x \pmod{b^e}, \qquad |t|\le \frac12 b^e . $$ The congruence...
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: yes Solve time: 3m48s Let the approximation relation be defined as in §4.2.2: $$ u \approx v(\epsilon) $$ means that there exists a number $\delta$ with $|\delta|\le \epsilon$ such that $$ v=u(1+\delta). $$ Equation (33) states that $$...
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 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: 5m07s Solution We seek floating binary numbers $u$ and $v$ for which $$ (u\oplus v)^2>2(u^2\oplus v^2), $$ where $\oplus$ denotes floating-point addition with rounding to the nearest floating binary number. It...
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: yes Solve time: 6m43s Solution We are asked whether the identity $$ u \oslash v = u \otimes (1 \oslash v) $$ holds for all floating-point...
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: 3m40s Solution (a) $0 \oplus (0 \oplus u) = u$ By equation (6) of the floating point arithmetic defined in this section, $$ 0...
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: 7m Solution Yes. Let $\Omega$ denote the largest positive floating-point number. We will construct floating-point numbers $u,v,w$...
TAOCP 4.2.1 Exercise 19
Section 4.2.1: Single-Precision Calculations Exercise 19. [ 24 ] What is the running time for the FADD subroutine in Program A, in terms of relevant characteristics of the data? What is the maximum running time, over all inputs that do not cause exponent overflow or underflow? Verified: yes Solve time: 7m09s Solution We analyze the actual MIX FADD subroutine in Program A , which adds two floating-point numbers $u=(e_u,f_u)$ and...
TAOCP 4.2.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: 3m31s Exercise 4.2.2.3 [ M30 ] Setup We are asked to find eight-digit floating point numbers $u$, $v$, $w$ such...
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: 3m23s Solution Since $y \ge 0$, property (8) implies that $$ v \oplus 0 \le v \oplus y. $$ By property (6), $v \oplus 0 = v$....
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: yes Solve time: 1m40s Solution We are asked to prove identity (7): $u \ominus v = -(v \ominus u)$ using only identities (2) through (6): $$ \begin{aligned} &(2) && u \oplus v = v \oplus u, \ &(3) && u \ominus v = u \oplus (-v),...
TAOCP 4.2.1 Exercise 14
Section 4.2.1: Single-Precision Calculations Exercise 14. [ 25 ] Write a MIX subroutine that begins with an arbitrary floating point number in register A, not necessarily normalized, and converts it to the nearest fixed point integer (or determines that the number is too large in absolute value to make such a conversion possible). Verified: no Solve time: 7m39s Solution 1. Preliminaries A MIX floating-point number has the form $$ x...
TAOCP 4.2.1 Exercise 18
Section 4.2.1: Single-Precision Calculations Exercise 18. [ 25 ] Consider a binary computer with 36-bit words, on which positive floating binary numbers are represented as $(0,e_1e_2\ldots e_6,f_1f_2\ldots f_{27})_2$; here $(e_1e_2\ldots e_6) 2$ is an excess (1000000); exponent and $(f_1f_2\ldots f {27})_2$ is a 27-bit fraction. Negative floating point numbers are represented by the two's complement of the corresponding positive representation (see Section 4.1). Thus, 1.5 is $201|000000000$ in octal notation,...
TAOCP 4.2.1 Exercise 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: 12m04s We are...
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: 1m41s Setup A floating point number is represented by a single word containing a fraction and exponent. Exercise 17 asks for an exploration of...
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 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: no Solve time: 6m17s Let $$ u=(e_u,f_u), \qquad v=(e_v,f_v) $$ be normalized floating point numbers in radix $b$, with $p$-digit fractions. Thus $$ \frac1b \le |f_u| < 1, \qquad \frac1b \le |f_v| < 1. $$ In floating point division, before normalization the quotient fraction is $$...
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: 9m56s Balanced ternary uses radix $b=3$ and digits $$ \bar 1=-1,\qquad 0,\qquad 1. $$ A real number is represented in normalized floating-point form as $$ x=\pm m,3^e, $$ where the significand (mantissa) $$ m=.d_1d_2\cdots d_p $$ is a balanced ternary fraction with digits $d_i\in{\bar1,0,1}$, and normalization requires $d_1\neq0$. Since...
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 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: 2m26s Solution We are asked to give an example of normalized, excess-50, eight-digit floating decimal numbers $u$ and $v$ such that multiplication produces a rounding overflow. Let the floating point format be a normalized, eight-digit...
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: 1m41s Solution Let the floating point numbers have base $10$ and $p=8$ digits. Rounding overflow occurs in step N5 of Algorithm N when rounding causes the fraction to become $1.00000000$, forcing a return to step N4. Choose $$...
TAOCP 4.1 Exercise 30
Section 4.1: Positional Number Systems Exercise 30. [**] $[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 $b_0, b_1,...
TAOCP 4.2.1 Exercise 8
Section 4.2.1: Single-Precision Calculations Exercise 8. [ 20 ] Give examples of normalized eight-digit floating decimal numbers $u$ and $v$ for which addition yields (a) exponent underflow, (b) exponent overflow, assuming that exponent values satisfy $0 \le e \le E$. Verified: no Solve time: 1m39s Solution Let the floating decimal numbers have $p=8$ digits in the fraction, and let normalized numbers satisfy $\frac1{10}\le |f|<1,$ unless the number is zero. Exponents...
TAOCP 4.2.1 Exercise 5
Section 4.2.1: Single-Precision Calculations Exercise 5. [ 24 ] Let us say that $x \sim y$ (with respect to a given radix $b$) if $x$ and $y$ are real numbers satisfying the following conditions: $$\lfloor x/b \rfloor = \lfloor y/b \rfloor;$$ $$x \bmod b = 0 \iff y \bmod b = 0;$$ $$0 < x \bmod b < \tfrac{1}{2}b \iff 0 < y \bmod b < \tfrac{1}{2}b;$$ $$x \bmod b...
TAOCP 4.2.1 Exercise 6
Section 4.2.1: Single-Precision Calculations Exercise 6. [ 20 ] If the result of a FADD instruction is zero, what will be the sign of rA, according to the definitions of MIX's floating point attachment given in this section? Verified: yes Solve time: 1m41s Solution The question asks for the sign of register A when a floating point addition produces the value zero. Program A performs floating point addition by adding...
TAOCP 4.2.1 Exercise 4
Section 4.2.1: Single-Precision Calculations Exercise 4. ▶ [ 16 ] Assume that $b = 10$, $p = 8$. What result does Algorithm A give for $(50, +.89765432) \oplus (49, +.33333333)$? For $(53, -.99987654) \oplus (54, +1.0000000)$? For $(45, -.50000001) \oplus (54, +1.0000000)$? Verified: yes Solve time: 4m30s Solution We apply Algorithm A with base $b=10$ and precision $p=8$. After the fractions are aligned and added, Algorithm N is used to...
TAOCP 4.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: 4m45s In...
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: yes Solve...
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 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: yes Solve time: 5m45s Setup Let $$ A=\left{\sum_{i=0}^{m} a_i3^i ;:; a_i\in{0,1},\ m\ge0\right} $$ be the set of nonnegative integers whose ternary representation uses only the digits $0$ and $1$. Let $$ B=\left{\sum_{j=0}^{n} b_j5^j ;:; b_j\in{0,1},\...
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: 1m14s Using the values given in §3, $$ h \approx 6.62\times10^{-27}\ \text{erg sec}. $$ In base $100$, normalized four-digit floating point...
TAOCP 4.1 Exercise 34
Section 4.1: Positional Number Systems Exercise 34. ▶ [ 22 ] (G. W. Reitwiesner, 1960.) Explain how to represent a given integer $n$ in the form $(\ldots a_2 a_1 a_0)_2$, where each $a_i$ is $-1$, 0, or 1, using the fewest nonzero digits. Verified: yes Solve time: 3m41s Solution We are asked to represent an integer $n$ in a balanced binary form , that is, as $(\ldots a_2 a_1 a_0)...
TAOCP 4.1 Exercise 28
Section 4.1: Positional Number Systems Exercise 28. ▶ [**] $[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: 9m35s Let $$ R(e_0,\ldots,e_r) =(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}, \qquad e_0<\cdots<e_r. $$...
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 27
Section 4.1: Positional Number Systems Exercise 27. [**] $[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: 10m13s Exercise 4.1.27 [ M21 ] Show that every nonzero integer has a unique reversing binary representation $$ n=2^{e_0}-2^{e_1}+\cdots+(-1)^r2^{e_r}, \qquad e_0<e_1<\cdots<e_r. $$ Solution For integers $a\le b$, define $$ A(a,b)=2^a-2^{a+1}+2^{a+2}-\cdots+(-1)^{b-a}2^b...
TAOCP 4.1 Exercise 29
Section 4.1: Positional Number Systems Exercise 29. [**] $[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 $0 \in...
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 3
Section 4.1: Positional Number Systems Exercise 3. [ 29 ] Express $-49 + i$ in the quarter-imaginary system. Verified: yes Solve time: 13m Exercise 4.1.3, Problem 3 [29] Express $-49+i$ in the quarter-imaginary system, that is, in base $2i$. Solution Let $$ -49+i=\sum_{k} d_k(2i)^k, \qquad d_k\in{0,1,2,3}. $$ A standard property of the quarter-imaginary system is that the even and odd powers separate: $$ (2i)^{2k}=(-4)^k, \qquad (2i)^{2k+1}=2i(-4)^k. $$ Hence $$ \sum_k...
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 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: yes Solve time: 2m14s Setup Let $$ D={d_0,d_1,\ldots,d_9} $$ be a set of ten nonnegative...
TAOCP 4.1 Exercise 18
Section 4.1: Positional Number Systems Exercise 18. [**] [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: no Solve time: 6m35s Exercise 4.1.18 [**] 18. [HM32] Show that the twindragon of Fig. 1 is a closed set that contains a neighborhood of the origin. Consequently, every...
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: 8m24s Solution Let the linear congruential generator be $$ X_{n+1} \equiv a X_n +...
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: 7m56s Solution Let $Y = cX$, where $X$ is a continuous random variable with distribution function $$ F(x) = \Pr{X \le x}. $$ We want the distribution function of $Y$: $$ G(y)...
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: yes Solve time: 7m04s Solution Let $$ T(x) = \int_x^\infty e^{-t^2/2}, dt. $$ The numbers $a_j$...
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 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 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 19
Section 4.1: Positional Number Systems Exercise 19. ▶ [**] [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 only if...
TAOCP 4.1 Exercise 16
Section 4.1: Positional Number Systems Exercise 16. [**] [M24] Design an algorithm to add 1 to $(a_n \ldots a_1 a_0)_{i-1}$ in the $i-1$ number system. Verified: no Solve time: 5m20s Let $$ \beta=i-1. $$ In the $(i-1)$-number system the digits are $0$ and $1$, and every number is represented as $$ N=\sum_{k\ge0} a_k\beta^k, \qquad a_k\in{0,1}. $$ We first derive the carry rule. Since $$ \beta^2=(i-1)^2=-2i, $$ we have $$ \beta^2+2\beta+2...
TAOCP 4.1 Exercise 17
Section 4.1: Positional Number Systems Exercise 17. [**] [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: no Solve time: 3m26s Exercise 4.1.17 [**] ▶...
TAOCP 4.1 Exercise 15
Section 4.1: Positional Number Systems Exercise 15. [**] [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: 2m41s Exercise 4.1.15 [**] 15. [M24] Determine the sets $$ S = \left{\sum_{k \ge 0} a_k b^{-k} ;\middle|; a_k \text{ an allowable digit}\right} $$ for the...
TAOCP 4.1 Exercise 14
Section 4.1: Positional Number Systems Exercise 14. [**] [14] Multiply $(1132)_{i-1}$ by itself in the quater-imaginary system using the method illustrated in the text. Verified: no Solve time: 4m02s Exercise 4.1.14 [**] Problem: Multiply $(1132)_{i-1}$ by itself in the quater-imaginary system using the method illustrated in the text. Solution: Let $b = 2i$. Then the quater-imaginary number $(1132)_{i-1}$ has the value $$ (1132)_{i-1} = 1\cdot b^3 + 1\cdot b^2 +...
TAOCP 4.1 Exercise 13
Section 4.1: Positional Number Systems Exercise 13. ▶ [**] [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: 3m58s $$ x=(0.d_1d_2d_3\cdots)_{-10} $$ means $$ x=\sum_{k\ge1} d_k(-10)^{-k}, \qquad 0\le d_k\le 9. $$ The negadecimal...
TAOCP 4.1 Exercise 12
Section 4.1: Positional Number Systems Exercise 12. [**] [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: yes Solve time: 2m35s Exercise 4.1.12 [23] Specify algorithms that convert (a) the binary signed-magnitude number...
TAOCP 4.1 Exercise 10
Section 4.1: Positional Number Systems Exercise 10. [**] [M22] Generalize Eq. (5) to mixed-radix notation as in (9). Verified: yes Solve time: 3m48s Exercise 4.1.10 [**] 10. [M22] Generalize Eq. (5) to mixed-radix notation as in (9). Solution. Let the mixed radices be $r_1,r_2,r_3,\ldots$, with $r_j\ge2$, and define $$ R_j=r_1r_2\cdots r_j \qquad (j\ge1). $$ In the notation of (9), $$ (.a_1a_2a_3\ldots) = \sum_{j\ge1}\frac{a_j}{R_j}, \qquad 0\le a_j<r_j. $$ Equation (5) for...
TAOCP 4.1 Exercise 11
Section 4.1: Positional Number Systems Exercise 11. [**] [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: 1m27s Let $$ s_i=a_i+b_i+t_i, $$ where $t_i$ is the carry entering position $i$. Since $$ (-2)^i+(-2)^i=2(-2)^i=-(-2)^{i+1}, $$ a carry in radix $-2$ may be negative....
TAOCP 4.1 Exercise 9
Section 4.1: Positional Number Systems Exercise 9. ▶ [**] [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: yes Solve time: 2m21s Each octal digit corresponds to three binary digits, and each hexadecimal digit corresponds to four binary digits. Therefore, to convert an octal number to hexadecimal,...
TAOCP 4.1 Exercise 7
Section 4.1: Positional Number Systems Exercise 7. [**] [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: 2m28s Represent each real...
TAOCP 4.1 Exercise 8
Section 4.1: Positional Number Systems Exercise 8. [**] [M10] Prove Eq. (5). Verified: yes Solve time: 1m09s Equation (5) asserts that a distribution function $F(x)$ is monotonically nondecreasing, with $F(x_1) \le F(x_2) \quad \text{if } x_1 \le x_2; \qquad F(-\infty) = 0, \quad F(+\infty) = 1.$ By definition, $F(x) = \Pr{X \le x}$, which is the probability that the random variable $X$ takes a value less than or equal to...
TAOCP 4.1 Exercise 6
Section 4.1: Positional Number Systems Exercise 6. [**] [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: 1m07s In signed magnitude notation, one bit is reserved for the sign, leaving $p-1$ bits for magnitude. The largest positive integer has all magnitude bits...
TAOCP 4.1 Exercise 4
Section 4.1: Positional Number Systems Exercise 4. [**] [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 following instructions?...
TAOCP 4.1 Exercise 5
Section 4.1: Positional Number Systems Exercise 5. [**] [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: 1m17s A negative integer $-N$ has a nines' complement representation obtained by replacing each decimal digit $d$ of $N$ with $9 - d$. In ten's complement notation, the representation...
TAOCP 4.1 Exercise 2
Section 4.1: Positional Number Systems Exercise 2. ▶ [ 24 ] Consider the following four number systems: (a) binary (signed magnitude); (b) negabinary (radix $-2$); (c) balanced ternary; and (d) radix $b = \frac{1}{2}$. Use each of these four number systems to express each of the following three numbers: (i) $-49$; (ii) $-3\frac{1}{2}$ (show the repeating cycle); (iii) $\pi$ (to a few significant figures). Verified: no Solve time: 4m40s Exercise...
TAOCP 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: 4m03s Solution We are asked to express the integers $$ -10, -9, \ldots, 9, 10 $$ in the number system whose radix is $-2$. Let the base $-2$ representation of an integer $n$ be $$ (d_k d_{k-1} \dots d_1 d_0)_{-2}, \quad d_i...
TAOCP 3.5 Exercise 44
Section 3.5: What Is a Random Sequence? Exercise 44. ▶ [ 16 ] (J. Good.) Can a valid table of random digits contain just one misprint? Verified: yes Solve time: 2m22s Let the original table be an infinite sequence of digits $$ x_1,x_2,x_3,\ldots $$ that is valid in the usual sense: Every block of $k$ digits occurs with limiting frequency $10^{-k}$. Suppose that a single misprint occurs at position $m$,...
TAOCP 3.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 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: yes Solve time: 1m48s Setup Let Theorem P be the result asserting that a prediction algorithm for the...
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 40
Section 3.5: What Is a Random Sequence? Exercise 40. [ M28 ] Complete the proof of Lemma P1. Verified: no Solve time: 2m01s Exercise 3.5.40 [ M28 ] Complete the proof of Lemma P1. Setup Let us restate the relevant definitions and the statement of Lemma P1 as needed for rigor. Suppose ${U_n}$ is a $k$-distributed $[0\ldots 1)$ sequence, and let $f(x_1, \ldots, x_k)$ be a Riemann-integrable function of $k$...
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 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: yes Solve time: 3m39s Let...
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: 3m14s Setup Let $(X_n)$ be an R5-random binary sequence, and let $(a_n)$ be any computable increasing sequence of...
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: yes Solve time: 1m15s Setup We are asked to construct a sequence that satisfies Definition R4 but fails Definition R5 . Let us first recall the relevant definitions from Section...
TAOCP 3.5 Exercise 34
Section 3.5: What Is a Random Sequence? Exercise 34. ▶ [**] [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: 2m24s Exercise 3.5.34 [ M25 ] Solution. We are asked to define a countable family of computable subsequence rules $\mathcal{R}_1, \mathcal{R}_2, \mathcal{R}_3, \dots$ such...
TAOCP 3.5 Exercise 30
Section 3.5: What Is a Random Sequence? Exercise 30. ▶ [**] [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: 1m57s Let $k$ be a positive integer. Consider all binary sequences of length $2k$ that contain exactly $k$ zeros and $k$ ones. There are...
TAOCP 3.5 Exercise 31
Section 3.5: What Is a Random Sequence? Exercise 31. [**] [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: 4m25s Let $$ \nu_n=#{,0\le j<n : U_j<\tfrac12,}. $$ We must construct a sequence...
TAOCP 3.5 Exercise 32
Section 3.5: What Is a Random Sequence? Exercise 32. [**] [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: yes Solve time: 2m24s Solution. Let $\langle X_n \rangle$ be an R5-random $b$-ary...
TAOCP 3.5 Exercise 29
Section 3.5: What Is a Random Sequence? Exercise 29. [**] [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: 3m34s Exercise 3.5.29, Corrected Solution Let $X_0, X_1, \dots$ be a $(2k)$-distributed binary sequence. By definition, every binary block of length $2k$ occurs with equal frequency $1/2^{2k}$. We wish to show that $$ \Pr{X_{2n} =...
TAOCP 3.5 Exercise 27
Section 3.5: What Is a Random Sequence? Exercise 27. [**] [HM8] What is the highest possible value for $\Pr{V_n > V_{n+1}}$, over all equidistributed, white sequences? (D. Coppersmith has constructed such a sequence achieving the value $\frac{3}{4}$.) Verified: no Solve time: 3m12s Exercise 3.5.27 [ HM8 ] Problem. Determine the highest possible value of $$ \Pr{V_n > V_{n+1}} $$ over all equidistributed, white sequences $(V_n)_{n\ge 0}$. D. Coppersmith has constructed...
TAOCP 3.5 Exercise 26
Section 3.5: What Is a Random Sequence? Exercise 26. [**] [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}, U_{2n}) \notin...
TAOCP 3.5 Exercise 28
Section 3.5: What Is a Random Sequence? Exercise 28. ▶ [**] [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: yes Solve time: 1m14s Let $(X_n)$ be the periodic binary sequence (11), $0001,0001,1101,1101,0001,0001,1101,1101\cdots,$ which is 3-distributed. Define $$ U_n=\frac12,X_nX_{n+1}. $$ Since $(X_n)$ is 3-distributed, the pair $(X_n,X_{n+1})$ assumes each of the four binary values $00,01,10,11$ with probability $1/4$....
TAOCP 3.5 Exercise 24
Section 3.5: What Is a Random Sequence? Exercise 24. ▶ [**] [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. Verified: no...
TAOCP 3.5 Exercise 23
Section 3.5: What Is a Random Sequence? Exercise 23. [**] [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 the sequences...
TAOCP 3.5 Exercise 20
Section 3.5: What Is a Random Sequence? Exercise 20. ▶ [**] [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 measure the...
TAOCP 3.5 Exercise 25
Section 3.5: What Is a Random Sequence? Exercise 25. [**] [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 j <...
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: 3m44s Solution We are asked to prove: If $(U_n)$ is $k$-distributed, so is the sequence $$ V_n = \frac{\lfloor n U_n \rfloor}{n}. $$ We proceed carefully using a limiting argument....
TAOCP 3.5 Exercise 22
Section 3.5: What Is a Random Sequence? Exercise 22. ▶ [**] [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: 1m01s Suppose $(U_n)$ is $k$-distributed. By Definition B,...
TAOCP 3.5 Exercise 21
Section 3.5: What Is a Random Sequence? Exercise 21. [**] [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) \mid l_1...
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: 1m27s Setup Definition R4 asserts that for every positive integer $s$, every residue class $t$ with $0...
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: 1m13s Setup Let $r =...
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: yes Solve time: 3m40s Solution Let ${U_n}_{n\ge0}$ be an $\infty$-distributed sequence and...
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 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: 1m11s Setup Let $b \ge 2$ be an integer representing the number of kinds of coupons. Let $X_1, X_2, \ldots$ be an $\infty$-distributed $b$-ary sequence, so that by Definition D, for any $k...
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: no Solve time: 3m43s Exercise 3.5.11 [ M10 ] Let $(U_n)$ be $\infty$-distributed. By definition, $(U_n)$ is $m$-distributed for every positive integer $m$. To prove that $(U_{2n})$ is $\infty$-distributed, it suffices to show that it is $k$-distributed for every positive...
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: 3m46s Solution In TAOCP §3.5, Theorem C states that if a sequence $\langle U_n\rangle$ is $(m,k)$-distributed, then for any integer $q > 0$ divisible by $m$ and for all integers $h$ with $0 \le h < m^k$, we...
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: 1m07s Solution Let $$ M_n=\max(U_n,U_{n+1},\ldots,U_{n+k-1}). $$ We must prove that for every $0\le u<v\le1$, $$ \Pr(u\le M_n<v)=v^k-u^k. $$ The event $M_n<v$ occurs exactly when each of the $k$ terms...
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 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: 1m22s 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 $\varepsilon>0$, $$ \lim_{n\to\infty} \frac1n #{j:1\le j\le n,\ |y_{jn}-a|\ge\varepsilon} =0. $$ Consider $$ \sum_{j=1}^{n}(y_{jn}-a)^2. $$ Expanding the square gives $$ \frac1n\sum_{j=1}^{n}(y_{jn}-a)^2 = \frac1n\sum_{j=1}^{n}y_{jn}^{,2} -\frac{2a}{n}\sum_{j=1}^{n}y_{jn} +a^2. $$ By the hypotheses, $$ \lim_{n\to\infty}...
TAOCP 3.4.2 Exercise 9
Section 3.4.2: Random Sampling and Shuffling Exercise 9. [**] [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 sample? Verified:...
TAOCP 3.5 Exercise 8
Section 3.5: What Is a Random Sequence? Exercise 8. [ M15 ] Prove (13). Verified: yes Solve time: 1m24s Solution Assume that the sequence $\langle U_n\rangle$ is $(m,k)$-distributed, and let $d$ be a divisor of $m$. Write $$ m=rd. $$ We must prove that $\langle U_n\rangle$ is $(d,k)$-distributed. Fix real numbers $u_t,v_t$ with $0\le u_t<v_t\le1$ for $1\le t\le k$, and fix an integer $j$ with $0\le j<d$. Let $S(n)$ be...
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: yes Solve time: 1m11s Solution We are asked to determine $\Pr{U_n < \tfrac{1}{2}}$ for the sequence $U_n = \bigl(2^{9(n+1)/3}\bigr) \bmod 1.$ First, observe that $2^{9(n+1)/3} = 2^{3(n+1)} = 2^{3n+3} = 8 \cdot 2^{3n}.$ Hence we may write $U_n = (8 \cdot 2^{3n}) \bmod 1.$ Since...
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 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: 1m13s Solution We are asked to construct a periodic ternary sequence that is 3-distributed. Let the ternary alphabet be ${0,1,2}$. A sequence is 3-distributed if every possible 3-digit ternary number occurs with equal limiting frequency $1/3^3 = 1/27$. For a periodic sequence, it suffices that every...
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: 1m12s The sequence has period $4$: $0,0,1,1,0,0,1,1,\ldots$ To test 2-distribution, examine the successive pairs: $00,\ 01,\ 11,\ 10,\ 00,\ 01,\ 11,\ 10,\ldots$ Each of the four binary numbers $00$, $01$, $10$,...
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 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.5 Exercise 1
Section 3.5: What Is a Random Sequence? Exercise 1. [ 10 ] Can a periodic sequence be equidistributed? Verified: yes Solve time: 1m12s No. Let the sequence have period $p$, so that $U_{n+p}=U_n$ for all $n$. Then the sequence contains only the $p$ values $U_0,U_1,\ldots,U_{p-1}$ repeated indefinitely. For any interval $[u,v)$, the quantity $\nu(n)/n$ approaches the fraction of these $p$ values that lie in $[u,v)$. Hence every limiting frequency is...
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 14
Section 3.4.2: Random Sampling and Shuffling Exercise 14. [**] [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$ and $y...
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 3.4.2 Exercise 12
Section 3.4.2: Random Sampling and Shuffling Exercise 12. [**] [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) \cdots (b_1...
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 13
Section 3.4.2: Random Sampling and Shuffling Exercise 13. [**] [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.") Consider a...
TAOCP 3.4.2 Exercise 10
Section 3.4.2: Random Sampling and Shuffling Exercise 10. [**] [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: 3m18s Exercise 3.4.2.10 [**] ▶ 10. [15] Modify Algorithm R so that the reservoir is eliminated, assuming that the $n$ records of the current sample can be held in memory. Solution. Algorithm R...
TAOCP 3.4.2 Exercise 11
Section 3.4.2: Random Sampling and Shuffling Exercise 11. ▶ [**] [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: 2m19s Let $M$ be the number of elements that are placed into...
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: 4m07s Solution Let $S = {s_1 < s_2 < \dots < s_n}$ be any fixed $n$-subset of ${1,2,\dots,N}$. We want to compute the probability that Algorithm S selects...
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: no Solve time: 4m31s Solution Let $T$ denote the value of $t$ when Algorithm S terminates. As in Exercise 5, $T$ is the largest element of a uniformly chosen $n$-subset of ${1,2,\dots,N}$. Its distribution is $$ \Pr{T=t} = \frac{\binom{t-1}{n-1}}{\binom{N}{n}}, \qquad n \le t \le N. $$...
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 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: yes Solve time: 4m33s Solution Let $p(m,t)$ denote the probability that exactly $m$ items have been selected...
TAOCP 3.4.2 Exercise 3
Section 3.4.2: Random Sampling and Shuffling Exercise 3. ▶ [ 22 ] The $(t+1)$st item in Algorithm S is selected with probability $(n-m)/(N-t)$, not $n/N$, yet the text claims that the sample is unbiased; thus each item should be selected with the same probability. How can both of these statements be true? Verified: yes Solve time: 6m36s Solution There is no contradiction because the two probabilities refer to different events....
TAOCP 3.4.2 Exercise 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: yes Solve time: 2m48s Solution Let $T$ denote the value of $t$ at termination of Algorithm S, i.e., the total number of input records examined before all...
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] 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: 5m44s The reviewer's principal objection is incorrect. A deterministic function of a single random variable can indeed produce several independent random...
TAOCP 3.4.2 Exercise 2
Section 3.4.2: Random Sampling and Shuffling Exercise 2. [ 20 ] Prove that Algorithm S never tries to read more than $N$ records of its input file. Verified: yes Solve time: 1m09s Solution Let $N$ be the total number of records in the input file, and let $n$ be the number of records to be selected by Algorithm S. We will prove that the algorithm never reads more than $N$...
TAOCP 3.4.2 Exercise 1
Section 3.4.2: Random Sampling and Shuffling Exercise 1. [ M12 ] Explain Eq. (1). Verified: yes Solve time: 1m46s Solution Equation (1) in Section 3.4.2 gives the probability with which the $(t+1)$st record of a file should be selected in Algorithm S, in order to maintain a uniform sample of size $n$ from the first $t+1$ records. Specifically, the probability that the new record is chosen to replace an element...
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 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 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 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: no Solve time: 2m12s Setup Let $\Pi$ be a Poisson random variable with mean $\mu$. Generate $\Pi=N$, then generate...
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: yes Solve time: 1m31s Solution We want $X_1 \le X_2 \le \cdots \le X_n$ such...
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: yes Solve time: 1m43s Setup Let $$ \qquad a_1\ge \cdots \ge a_n>0. $$ The problem is to generate a random point on the surface of the ellipsoid $E$,...
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 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: 3m28s Solution Let $N_1$ and $N_2$ be independent Poisson...
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: 1m48s Setup Let $$ E_t=X_1\mid\bigl(X_2\mathbin{&}(X_3\mid(X_4\mathbin{&}X_5)\cdots)\bigr) $$ denote the nested expression. Since corresponding bit positions...
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 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: 4m46s Let...
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: yes Solve time: 1m35s Exercise 3.4.1.22 [ HM40 ] Can the exact Poisson distribution for large $\mu$ be obtained by generating an appropriate...
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 19
Section 3.4.1: Numerical Distributions Exercise 19. [**] [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 method would...
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: yes Solve time: 4m33s Exercise 3.4.1.18 [ M24 ] We are asked to generate a random integer $N$ such that $$ \Pr{N=n} = n p^2 (1-p)^{,n-1}, \qquad...
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: yes Solve time: 4m35s Solution Let $$ f(x)=\frac{x^{a-1}e^{-x}}{\Gamma(a)}, \qquad x>0,\qquad 0<a\le1, $$ be the gamma density of order...
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: yes Solve time: 1m33s Solution Let $X$ be a random variable representing the number of trials until the first success in a sequence of independent Bernoulli trials with success probability...
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: yes Solve time: 1m17s Solution Let $S=X_1+X_2.$ Since $X_1$ and $X_2$ are independent, the distribution function of $S$ is =\Pr{X_1+X_2\le x}.$$ Condition on...
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: no Solve time: 5m13s Solution Algorithm M is designed to generate a discrete random variable $$ X \in {x_0, x_1, \dots, x_{n-1}}, \quad \Pr{X = x_j} = p_j \ge 0, \quad \sum_{j=0}^{n-1} p_j = 1 $$...
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 7
Section 3.4.1: Numerical Distributions Exercise 7. ▶ [**] [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 boxes. Prove...
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: 2m06s Correctness The proposed solution does not answer Exercise 3.4.1.4. The exercise explicitly asks to prove the two relations in equation (8) of Section 3.4.1, which describe the distribution functions of $\max(X_1, X_2)$ and $\min(X_1, X_2)$ in terms of...
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: yes Solve time: 1m20s Solution We are asked to show that the alias...
TAOCP 3.4.1 Exercise 4
Section 3.4.1: Numerical Distributions Exercise 4. [ M20 ] Prove the two relations in (8). Verified: no Solve time: 3m28s Correctness The solution does not answer the exercise as stated. The exercise concerns the two-dimensional spectral test of Section 3.3.4 and asks for a proof that $$ r_{\max}=O!\left(\frac{\max(a_1,\ldots,a_s)}{m}\right), $$ where $a_1,\ldots,a_s$ are the partial quotients in the continued fraction expansion of $a/m$, using the hint that refers to Exercise 4.5.3-42....
TAOCP 3.4.1 Exercise 5
Section 3.4.1: Numerical Distributions Exercise 5. ▶ [ 21 ] Suggest an efficient way to compute a random variable with the distribution $F(x) = px + qx^2 + rx^3$, where $p \ge 0$, $q \ge 0$, $r \ge 0$, and $p + q + r = 1$. Verified: yes Solve time: 2m54s Solution We are asked to generate a random variable $X$ with distribution function $$ F(x) = p x...
TAOCP 3.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 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: 1m34s...
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: 1m26s Solution Let $U$ be a random variable uniformly distributed on $[0,1)$, and suppose that $mU$ is interpreted as a...
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: 3m25s Setup Let $(X_n)$ be a linear...
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: 1m09s Let $U$ be a random variable uniformly distributed between 0 and 1. To transform $U$ into a random variable $X$ uniformly distributed between $\alpha$ and $\beta$, we apply an affine mapping...
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: 4m56s Exercise 3.3.4.31 [M48] Let $$...
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: no Solve time: 4m58s Solution We are asked to prove that $$ r_{\max} \le \frac{1}{\sqrt{8}, \nu_t}, $$ where $r_{\max}$ is the maximal value of $$ r(u_1,\ldots,u_t) = \prod_{j=1}^t r(u_j), \qquad r(u) = \begin{cases} 1,& u=0,\[1mm] \dfrac{1}{2|u|},& u \neq 0, \end{cases} $$ over all nonzero integer vectors $(u_1,\ldots,u_t)$ satisfying the...
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: yes Solve...
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: no Solve time: 3m33s Solution Consider a linear congruential sequence $(X_n)$ defined by $X_{n+1} = (a X_n + c) \bmod m, \quad 0 \le X_n < m,$ with full period length $m$....
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 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: 5m55s Solution Let $r(k)$ be defined...
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: yes Solve time: 5m34s Exercise 3.3.4.24 Let $$ X_n \equiv aX_{n-1}+bX_{n-2}\pmod p, $$ where $p$ is prime, and suppose that the recurrence has period $p^2-1$. By...
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: no Solve time: 3m27s Setup Let $\mu_2$ and $\mu_3$ denote the two- and three-dimensional spectral constants associated with a linear congruential sequence, as...
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 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: 1m31s Solution Let $X_{n+1} \equiv aX_n \pmod{2^e}, \qquad X_0 \text{ odd}, \qquad a \equiv 3 \text{ or } 5 \pmod 8.$ By exercise 3.2.1.2-9,...
TAOCP 3.3.4 Exercise 19
Section 3.3.4: The Spectral Test Exercise 19. [**] $[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: yes Solve time: 1m50s...
TAOCP 3.3.4 Exercise 16
Section 3.3.4: The Spectral Test Exercise 16. [**] $[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: 4m58s Corrected Solution for Exercise 3.3.4.16 We are asked to...
TAOCP 3.3.4 Exercise 18
Section 3.3.4: The Spectral Test Exercise 18. [**] $[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 so huge...
TAOCP 3.3.4 Exercise 17
Section 3.3.4: The Spectral Test Exercise 17. [**] $[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: 2m38s For each dimension $t$, Algorithm S examines all integer vectors $$ U=(u_1,\ldots,u_t) $$ satisfying (15), and computes $$ \nu_t^2=\max...
TAOCP 3.3.4 Exercise 14
Section 3.3.4: The Spectral Test Exercise 14. [**] $[24]$ Perform Algorithm S by hand, for $m = 100$, $a = 41$, $T = 3$. Verified: yes Solve time: 3m49s Exercise 3.3.4.14 [24] Perform Algorithm S by hand for $m=100$, $a=41$, $T=3$. Solution Algorithm S constructs a sequence of shortest nonzero vectors in a lattice defined by the congruences $$ u_1 + a u_2 + a^2 u_3 + \cdots + a^{t-1}...
TAOCP 3.3.4 Exercise 13
Section 3.3.4: The Spectral Test Exercise 13. [**] $[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 (but never...
TAOCP 3.3.4 Exercise 15
Section 3.3.4: The Spectral Test Exercise 15. ▶ [**] $[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: yes Solve time: 1m14s Let...
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 11
Section 3.3.4: The Spectral Test Exercise 11. ▶ [**] $[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, this choice...
TAOCP 3.3.4 Exercise 12
Section 3.3.4: The Spectral Test Exercise 12. [**] $[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: 1m38s Let $(u_1,\ldots,u_t)$ be a solution to problem (b) following Eq. (23). By definition, problem (b) seeks an extremum of the quantity in Eq. (23) subject to the constraints that define the admissible vectors. Since the...
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: 14m16s Solution Let $$ \frac{m}{a} =[a_0,a_1,\ldots,a_t] $$ be the continued-fraction expansion used in §3.3.3, and let $$ A=\max_{0\le j\le t} a_j...
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: yes Solve time: 8m50s Solution Let $b=a-1$. For a linear congruential generator of period length $m$ and potency $2$, $b^2\equiv0\pmod m,\qquad b\not\equiv0\pmod m.$ By the theory of the spectral test, $\nu_t$ is the minimum Euclidean length of a nonzero integer vector $(u_1,\ldots,u_t)$ satisfying...
TAOCP 3.3.4 Exercise 10
Section 3.3.4: The Spectral Test Exercise 10. [**] $[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 $(u_1^2 +...
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: 1m40s Solution Let $m = p_1 p_2...
TAOCP 3.2.2 Exercise 25
Section 3.2.2: Other Methods Exercise 25. [**] $[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: yes Solve time: 1m23s An alternative to Program A that updates all 55 entries of the $Y$ table every 55th output can be implemented by performing a full sweep of the table instead of a single additive step....
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.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: yes Solve time: 4m39s Solution Let $$ p=\beta-\alpha . $$ For a random sequence, each value $U_j$ is independently distributed on $[0,1)$, hence $$ \Pr(\alpha \le U_j < \beta)=p, \qquad \Pr(U_j<\alpha \text{ or } U_j\ge\beta)=1-p. $$ A gap of length $r$ is defined to be a subsequence $$...
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: 1m55s Solution Let $N$ be the total number of values $U_j$ examined by Algorithm G when $n$ gaps have been...
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: 1m26s For triples, the serial test should count the occurrences of each ordered triple $(Y_{3j},Y_{3j+1},Y_{3j+2}), \qquad 0\le j<n,$ for all $d^3$ possible values $(q,r,s)$ with $0\le q,r,s<d$. The observed frequencies are then subjected to a chi-square test with $k=d^3$ categories, each having...
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: 4m05s If the serial test is applied to the overlapping pairs $(Y_0, Y_1)$, $(Y_1, Y_2)$, $\ldots$, $(Y_{n-1}, Y_n)$, then each number $Y_j$ contributes to two successive pairs, except...
TAOCP 3.3.1 Exercise 24
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 24. ▶ [**] [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: 2m29s For fixed $(p,q,r)$...
TAOCP 3.3.1 Exercise 22
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 22. [**] [HM46] Investigate the "improved" KS test suggested in the answer to exercise 6. Verified: yes Solve time: 6m44s Let $$ D_n(u)=\sqrt n,(G_n(u)-u), \qquad 0\le u\le1, $$ where $G_n$ is the empirical distribution function of the transformed variables $$ U_j=F(X_j). $$ Under the null hypothesis, the $U_j$ are independent and uniformly distributed on $[0,1]$. The ordinary Kolmogorov-Smirnov statistic is...
TAOCP 3.3.1 Exercise 23
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 23. [**] [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 calculates $K_n^+$...
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: 9m46s Exercise 3.3.1.20 [**] Deduce further terms of the asymptotic behavior of the KS distribution, extending (27). Let $$ K(x)=\Pr!\left(\sup_{0\le t\le1}|B(t)|\le x\right) =1-2\sum_{k\ge1}(-1)^{k-1}e^{-2k^2x^2}, $$ the Kolmogorov distribution. Equation (27) gives the leading asymptotic form obtained from Jacobi's transformation, $$ K(x)...
TAOCP 3.3.1 Exercise 21
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 21. [**] [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 resulting statistical...
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: yes Solve time: 6m18s Let $$ X^{(1)},X^{(2)},\ldots,X^{(n)} $$ be independent observations from an unknown distribution on...
TAOCP 3.3.1 Exercise 18
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 18. [**] [M20] Give a "simple" reason why $K_n^-$ has the same probability distribution as $K_n^+$. Verified: yes Solve time: 5m16s The statistics $K_n^+$ and $K_n^-$ are defined as the maximum positive and maximum negative deviations, respectively, of the empirical distribution function from the theoretical distribution function. Consider the transformation $X_i \mapsto 1 - X_i$ for each observation. Under this...
TAOCP 3.3.1 Exercise 17
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 17. [**] [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 \int_{0}^{x_2} dx_1$...
TAOCP 3.3.1 Exercise 15
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 15. [**] [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 a case...
TAOCP 3.3.1 Exercise 16
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 16. ▶ [**] [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 the asymptotic...
TAOCP 3.3.1 Exercise 13
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 13. [**] [M24] Prove that Eqs. (13) are equivalent to Eqs. (11). Verified: yes Solve time: 6m46s Let $$ F_n(x)=\frac1n#{x_j\le x}, $$ and let $$ x_{(1)}\le x_{(2)}\le \cdots \le x_{(n)} $$ be the observations in sorted order. Equations (11) define $$ K_n^+=\sqrt n,\sup_x\bigl(F_n(x)-x\bigr), \qquad K_n^-=\sqrt n,\sup_x\bigl(x-F_n(x)\bigr). $$ Equations (13) assert that $$ K_n^+=\max_{1\le j\le n}\left(\frac{j}{\sqrt n}-\sqrt n,x_{(j)}\right), $$ $$ K_n^-=\max_{1\le...
TAOCP 3.3.1 Exercise 14
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 14. ▶ [**] [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 test that...
TAOCP 3.3.1 Exercise 12
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 12. [**] [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 detect the...
TAOCP 3.3.1 Exercise 11
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 11. [**] [10] Solve exercise 10 substituting the KS test for the chi-square test. Verified: yes Solve time: 10m50s Let $$ F_n(x)=\frac{1}{n}#{j:X_j\le x} $$ be the empirical distribution function of the original sample $$ X_1,X_2,\dots,X_n, $$ and let $$ D_n=\sup_x |F_n(x)-F(x)| $$ be the corresponding Kolmogorov-Smirnov statistic, where $F(x)$ is the hypothesized distribution function. Now repeat the test on the...
TAOCP 3.3.1 Exercise 9
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 9. ▶ [**] [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 (that is,...
TAOCP 3.3.1 Exercise 10
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 10. [**] [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 violates the...
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: no Solve time: 6m48s Solution Let $F_n(x)$ be the empirical distribution function based on $n$ independent observations $X_1, \dots, X_n$ from a continuous distribution $F(x)$. Recall the definitions of the Kolmogorov statistics: $$ K_n^+ = \sup_x \bigl(F_n(x)...
TAOCP 3.3.1 Exercise 5
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 5. [ 22 ] Let $F(x)$ be the uniform distribution, Fig. 3(b). Find $K_{20}^+$ and $K_{20}^-$ for the following 20 observations: 0.14, 0.732, 0.442, 0.162, 0.259, 0.442, 0.189, 0.693, 0.698, 0.302, 0.442, 0.434, 0.141, 0.017, 0.318, 0.869, 0.772, 0.678, 0.354, 0.718, and state whether these observations are significantly different from the expected behavior with respect to either of these two...
TAOCP 3.3.1 Exercise 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: 2m41s Solution Let $F_n(x)$ be the empirical distribution function defined by equation (10) of Section 3.3.1: $F_n(x) =...
TAOCP 3.3.1 Exercise 3
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 3. ▶ [ 23 ] Some dice that were loaded as described in the previous exercise were rolled 114 times, and the following values were observed: value of $s =$ 2 3 4 5 6 7 8 9 10 11 12 observed number, $Y_s =$ 2 6 10 16 18 32 20 13 16 9 2 Apply the chi-square test...
TAOCP 3.3.1 Exercise 4
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 4. ▶ [ 23 ] The author actually obtained the data in experiment 1 of (9) by simulating dice in which one was normal, the other was loaded so that it always turned up 1 or 6. (The latter two possibilities were equally probable.) Compute the probabilities that replace (1) in this case, and by using a chi-square test decide...
TAOCP 3.3.1 Exercise 2
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 2. [ 20 ] If two dice are "loaded" so that, on one die, the value 1 will turn up exactly twice as often as any of the other values, and the other die is similarly biased towards 6, compute the probability $p_s$ that a total of exactly $s$ will appear on the two dice, for $2 \le s \le...
TAOCP 3.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: 1m03s Equation (5) arises from the dice-throwing experiment with eleven categories, namely the possible sums $2,3,\ldots,12$. Therefore $k=11$. By the rule stated in Section 3.3.1, the chi-square...
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: 2m48s Exercise 3.2.2.35 [ HM35 ] Problem: Determine the number of pairs $(a,c)\in \mathbb{F}_p \times \mathbb{F}_p$ satisfying the conditions of Exercise 34, i.e., such that the inversive congruential sequence $$ x_{n+1} \equiv a x_n^{-1} + c \pmod p, \quad x_n \neq 0 $$ has maximal period...
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: 1m31s Solution We consider the inversive congruential sequence defined by $X_{n+1} \equiv a X_n^{-1} + c \pmod{2^e}, \qquad e \ge 3, \eqno(1)$ where $a \bmod...
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 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: yes Solve time: 1m23s Solution The sequence $\langle X_n \rangle$ satisfies the lagged...
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: no Solve time: 5m07s Exercise 3.2.2.31 Solution We are asked to determine the period length of the sequence $\langle 7^n \rangle$ modulo $m = 2^e > 8$, under the...
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: 2m29s Solution Let $$ X_n \equiv X_{n-2}+X_{n-55}\pmod m . $$ Introduce the shift operator $E$, where $E(X_n)=X_{n+1}$. Then the recurrence may be written $$ (1-E^{-2}-E^{-55})X_n=0 . $$ Equivalently, $$ (E^{55}-E^{53}-1)X_n=0 . $$...
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: 1m19s Exercise 3.2.2.29 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,...
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: yes Solve time: 1m27s Setup Exercise 28 asks for an experimental investigation of linear congruential sequences when the modulus is much larger than the machine word size, while the multiplier and increment remain single-precision...
TAOCP 3.2.2 Exercise 23
Section 3.2.2: Other Methods Exercise 23. [**] $[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: 4m Exercise 3.2.2.23 [**] Discuss the sequence defined by $$ X_n = (X_{n-31} - X_{n-24}) \bmod m $$ as an alternative to (7). Corrected Solution. We analyze the sequence $$ X_n = (X_{n-31} - X_{n-24}) \bmod m $$ as a lagged...
TAOCP 3.2.2 Exercise 26
Section 3.2.2: Other Methods Exercise 26. [**] $[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 n \ge...
TAOCP 3.2.2 Exercise 20
Section 3.2.2: Other Methods Exercise 20. [**] $[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: no Solve time: 2m01s A suitable choice for CONTENTS$(A)$ in method (10) is any $k$-bit binary number in which roughly half of the bits are $0$ and half are $1$. For example, one can alternate $0$...
TAOCP 3.2.2 Exercise 24
Section 3.2.2: Other Methods Exercise 24. [**] $[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: 1m10s Let $\langle Y_n \rangle$ be the sequence with period $2^k - 1$, satisfying $Y_n =...
TAOCP 3.2.2 Exercise 22
Section 3.2.2: Other Methods Exercise 22. ▶ [**] $[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 can therefore...
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: no Solve time: 4m27s Solution We are asked to show that the sequence of $8$-bit blocks $$ U_n = (X_{8n}, X_{8n+1}, \dots, X_{8n+7}) $$...
TAOCP 3.2.2 Exercise 21
Section 3.2.2: Other Methods Exercise 21. [**] $[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 of exercises...
TAOCP 3.2.2 Exercise 19
Section 3.2.2: Other Methods Exercise 19. [**] $[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: no Solve time: 3m11s Let $X_n=(a_1X_{n-1}+a_2X_{n-2})\bmod p,$ with $k=2$. By the criterion stated in...
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 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: 4m54s Corrected Solution Let $(X_n)$ and $(Y_n)$ be sequences...
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 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 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 11
Section 3.2.2: Other Methods Exercise 11. [**] [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 least common...
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: 3m06s Exercise 3.2.2.12...
TAOCP 3.2.2 Exercise 8
Section 3.2.2: Other Methods Exercise 8. [**] [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 multiple of...
TAOCP 3.2.2 Exercise 7
Section 3.2.2: Other Methods Exercise 7. [**] [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: yes Solve time:...
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: no Solve time: 2m11s In the binary method (10), the contents of $X$ form a shift-register sequence determined by a primitive polynomial modulo $2$. If the word has $k$ bits, the...
TAOCP 3.2.2 Exercise 10
Section 3.2.2: Other Methods Exercise 10. [**] [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: yes Solve time: 1m15s Let $P_e$ be the period of the Fibonacci sequence $$ X_{n+1}=(X_n+X_{n-1})\bmod 2^e. $$ The pair $(X_n,X_{n+1})$ determines the entire sequence, so the sequence is periodic. Since $$ \begin{pmatrix}X_n\X_{n+1}\end{pmatrix} = \begin{pmatrix}0&1\1&1\end{pmatrix}^n \begin{pmatrix}X_0\X_1\end{pmatrix}, $$...
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: 2m09s Let $m = w \pm 1$ be one of the moduli listed in Table 3.2.1.1–1. We wish to determine which of these moduli can be used in a linear congruential sequence of maximum period whose potency $s$...
TAOCP 3.2.2 Exercise 5
Section 3.2.2: Other Methods Exercise 5. ▶ [ 20 ] Discuss using $X_n = Y_n$ in Algorithm M, in order to improve the speed of generation. Is the result analogous to Algorithm B? Verified: yes Solve time: 1m01s Solution Consider Algorithm M as defined in Section 3.2.2. Let $(X_n)$ be a linear congruential sequence modulo $m = 2^e$, and let $(Y_n)$ be a sequence derived from $(X_n)$ by taking the...
TAOCP 3.2.2 Exercise 3
Section 3.2.2: Other Methods Exercise 3. [ 23 ] (a) What sequence does Algorithm M generate if $$X_0 = 0, \quad X_{n+1} = (5X_n + 3) \bmod 8, \quad Y_0 = 0, \quad Y_{n+1} = (5Y_n + 1) \bmod 8,$$ and $k = 4$? (Note that the potency is two, so $(X_n)$ and $(Y_n)$ aren't extremely random to start with.) (b) What happens if Algorithm B is applied to this...
TAOCP 3.2.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 12
Section 3.2.1.2: Choice of Multiplier Exercise 12. [**] [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: yes Solve time: 4m57s...
TAOCP 3.2.1.2 Exercise 13
Section 3.2.1.2: Choice of Multiplier Exercise 13. [**] [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: yes Solve time: 2m49s Solution. Let $p$ be prime, and let $a$ be an integer. If $a \equiv 0 \pmod{p}$, the conclusion...
TAOCP 3.2.1.2 Exercise 11
Section 3.2.1.2: Choice of Multiplier Exercise 11. ▶ [**] [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}$. (c) In...
TAOCP 3.2.1.2 Exercise 14
Section 3.2.1.2: Choice of Multiplier Exercise 14. [**] [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: 1m53s Assume $a$ is a primitive element modulo $p$, so that $a^{p-1} \equiv 1 \pmod{p}$ and $a^k \not\equiv 1 \pmod{p}$ for $1 \le k < p-1$....
TAOCP 3.2.1.2 Exercise 7
Section 3.2.1.2: Choice of Multiplier Exercise 7. ▶ [**] [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 indices corresponding...
TAOCP 3.2.1.2 Exercise 10
Section 3.2.1.2: Choice of Multiplier Exercise 10. [**] [M21] For what values of $m$ is $\lambda(m) = \varphi(m)$? Verified: yes Solve time: 1m34s From (9), $\lambda(m)=\varphi(m)$ holds for odd prime powers $p^e$, since $\lambda(p^e)=p^{e-1}(p-1)=\varphi(p^e), \qquad p>2.$ For powers of $2$, $\lambda(2)=1=\varphi(2),\qquad \lambda(4)=2=\varphi(4),$ but for $e\ge3$, $\lambda(2^e)=2^{e-2}<2^{e-1}=\varphi(2^e).$ If $m=p_1^{e_1}\cdots p_t^{e_t},$ then $$$$ while $$$$ The least common multiple of positive integers equals their product if and only if they are pairwise...
TAOCP 3.2.1.2 Exercise 8
Section 3.2.1.2: Choice of Multiplier Exercise 8. [**] [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: yes Solve time: 2m52s Exercise 3.2.1.2.8 [ M20 ] Statement. Show that if $a \equiv 3 \pmod 4$, we have $$ \frac{a^{,e-1}-1}{a-1} \equiv 0 \pmod{2^e} \quad \text{for } e>1. $$ Solution. Assume $a \equiv 3...
TAOCP 3.2.1.2 Exercise 9
Section 3.2.1.2: Choice of Multiplier Exercise 9. [**] [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 full period,...
TAOCP 3.2.1.2 Exercise 6
Section 3.2.1.2: Choice of Multiplier Exercise 6. ▶ [**] [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: yes Solve time: 2m30s From Table 3.2.1.1-1, $$ m=10^8-1=99,999,999=3^2\cdot 11\cdot 73\cdot 101\cdot 137. $$ Theorem A states that a mixed congruential generator $$ X_{n+1}\equiv aX_n+c \pmod m $$ has period $m$ for all initial values if and only if:...
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: 1m31s Solution We are asked to find all multipliers $a$ that satisfy the conditions of Theorem A when $m = 2^{35} + 1$. Let us proceed systematically. First,...
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: 1m32s Solution We are given a linear congruential sequence $X_{n+1} = (a X_n + c) \bmod 2^e$ with $X_0 = 0$, and where $a$ and $c$ satisfy the conditions...
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: 1m26s Solution Let $m = 10^e$ with $e \ge 2$, and let $c$...
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: yes Solve time: 1m44s Setup Exercises 12 and 13 exploit moduli of the form $m=b^e\mp1,$ where multiplication by suitable powers of the radix $b$ can be reduced to cyclic shifts of digits. Exercise 14 asks for a general formulation...
TAOCP 3.2.1.2 Exercise 2
Section 3.2.1.2: Choice of Multiplier Exercise 2. ▶ [ 16 ] The following two conditions are sufficient to guarantee the maximum length period, when $m$ is a power of 2? "(i) $c$ is odd; (ii) $a \bmod 4 = 1$." Verified: yes Solve time: 1m14s Solution Let $m = 2^e$, where $e \ge 1$. The linear congruential sequence is defined by $X_{n+1} = (aX_n + c) \bmod m.$ According to...
TAOCP 3.2.1.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: 59s The modulus $m = 10^{10}$ factors as $2^{10} \cdot 5^{10}$. Let $b = a - 1 = 3141592620$. We check the conditions of Theorem A for a full-period sequence....
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: 6m12s Let $$ m=aq+r,\qquad q=\left\lfloor \frac{m}{a}\right\rfloor,\qquad 0\le r<a. $$ Exercise 9(b) computes $ax \bmod m$ by writing $$ x=q\left\lfloor...
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: yes Solve time: 1m54s Solution Let $m=9999999001=10^{10}-999=10^{10}-31^2.$ The congruence $$$$ is the fundamental relation. Every computation modulo $m$ can therefore be reduced by replacing a block of ten decimal digits by $999$ times that block. (a) Multiplication by $10$ modulo $9999999001$ Write $$$$ where $0\le...
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 8
Section 3.2.1.1: Choice of Modulus Exercise 8. ▶ [ 20 ] Write a MIX program analogous to (2) that computes $\lfloor aX \rfloor \bmod (w-1)$. The values $0$ and $w - 1$ are to be treated as equivalent in the input and output of your program. Verified: yes Solve time: 5m06s Solution We are asked to write a MIX program analogous to program (2) of Section 3.2.1.1 that computes $$...
TAOCP 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 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.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: 1m48s Solution Examination of Table 1 reveals several recurring structural patterns in the factorizations of $w \pm 1$. First, consider numbers of the form $2^e - 1$. Many of these are Mersenne numbers , and when $e$ is prime, $2^e - 1$ is often prime, as in $2^{17}-1...
TAOCP 3.2.1.1 Exercise 4
Section 3.2.1.1: Choice of Modulus Exercise 4. [**] $[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: 4m23s Exercise 3.2.1.1.4 [**] 4. $[21]$ Discuss the calculation of linear congruential sequences with $m = 2^{32}$ on two's-complement machines such as the System/370 series. Corrected Solution Consider a linear congruential generator (LCG) defined by $$ X_{n+1} \equiv...
TAOCP 3.2.1.1 Exercise 3
Section 3.2.1.1: Choice of Modulus Exercise 3. ▶ [**] $[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$ in terms...
TAOCP 3.2.1.1 Exercise 6
Section 3.2.1.1: Choice of Modulus Exercise 6. ▶ [**] $[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: 2m36s Solution Consider the...
TAOCP 3.2.1.1 Exercise 2
Section 3.2.1.1: Choice of Modulus Exercise 2. [**] $[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: no Solve time: 4m34s Corrected Solution We...
TAOCP 3.2.1.1 Exercise 5
Section 3.2.1.1: Choice of Modulus Exercise 5. [**] $[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: 1m30s Since $0...
TAOCP 3.2.1.1 Exercise 1
Section 3.2.1.1: Choice of Modulus Exercise 1. [**] $[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: yes Solve time: 2m41s...
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: yes Solve time: 2m44s Solution Equation (6) states that for $k \ge 0$, $$ X_{n+k}\equiv a^kX_n+\frac{a^k-1}{a-1},c \pmod m. $$ We seek an analogous formula for negative $k$. The recurrence $$ X_{n+1}\equiv aX_n+c \pmod m...
TAOCP 3.2.1 Exercise 3
Section 3.2.1: The Linear Congruential Method Exercise 3. [ M10 ] If $a$ and $m$ are not relatively prime, explain why the sequence will be somewhat handicapped and probably not very random; hence we will generally want the multiplier $a$ to be relatively prime to the modulus $m$. Verified: yes Solve time: 2m43s Corrected Solution for Exercise 3.2.1.3 [ M10 ] Let the linear congruential generator (LCG) be defined by...
TAOCP 3.2.1 Exercise 4
Section 3.2.1: The Linear Congruential Method Exercise 4. [ 11 ] Prove Eq. (6). Verified: yes Solve time: 1m05s Solution We prove Eq. (6) by induction on $k$. The claim is that for all integers $k \ge 0$ and $n \ge 0$, the linear congruential sequence defined by $X_{n+1} = (a X_n + c) \bmod m$ satisfies $X_{n+k} = \bigl(a^k X_n + \frac{a^k - 1}{b} c \bmod m\bigr), \qquad b...
TAOCP 3.2.1 Exercise 2
Section 3.2.1: The Linear Congruential Method Exercise 2. ▶ [ M20 ] Show that if $a$ and $m$ are relatively prime, the number $X_0$ will always appear in the period. Verified: yes Solve time: 4m21s Solution Let $$ X_{n+1}\equiv aX_n+c \pmod m, $$ and assume that $$ (a,m)=1. $$ We must show that $X_0$ belongs to the periodic part of the sequence, that is, there exists $k>0$ such that $$...
TAOCP 3.2.1 Exercise 1
Section 3.2.1: The Linear Congruential Method Exercise 1. [ 10 ] Example (3) shows a situation in which $X_4 = X_0$, so the sequence begins again from the beginning. Give an example of a linear congruential sequence with $m = 10$ for which $X_0$ never appears again in the sequence. Verified: yes Solve time: 55s Take, for example, $m=10$, $a=2$, $c=0$, and $X_0=1$. Then $X_{n+1}=2X_n \pmod{10},$ and the sequence is...
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 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 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 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: yes Solve time: 4m16s Exercise 2.5.39 [**] Let $$ C=N(2m-2,m). $$ We shall prove that $$ N(n_1+n_2,m)\le N(n_1,m)+N(n_2,m)+C, $$ and then deduce the existence of $$ \lim_{n\to\infty}\frac{N(n,m)}n . $$ Recall the notation of Exercise 38. A request sequence consists...
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: yes Solve time: 29s Let the requested block sizes be independent random variables with distribution $\Pr{2^k}=p_k$, where $\sum_k p_k=1$, and suppose that deallocations occur after independent holding times with finite mean. Under these assumptions the state of the available-space structure approaches a...
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: yes Solve time: 2m36s The reviewer's principal objection is valid: without the precise algorithm of Exercise 33, no correct MIX program can be written. Exercise 34 is not a self-contained problem. It asks for a MIX implementation of the algorithm of Exercise 33 . Therefore...
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: yes Solve time: 1m08s The dynamic storage allocation methods of Section 2.5 store list elements in a storage pool and connect them by links. Insertions and deletions therefore require only local changes of links, independent of the...
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: 2m10s No. When a block of size $2^k$ is returned to the free-storage system, Algorithm R computes the address of its buddy and must determine whether that buddy is currently free. If the buddy is free, the two blocks are merged into a block...
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: yes Solve time: 1m07s A block of size $4$ contains addresses that differ only in the last two bits. Therefore the buddy is obtained by complementing...
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: 57s Suppose a request for a block of size $r$ is the...
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 27
Section 2.5: Dynamic Storage Allocation Exercise 27. [**] [24] Write a MIX program for Algorithm R, and determine its running time. Verified: yes Solve time: 1m12s Exercise 2.5.27 depends on the precise statement of Algorithm R in Section 2.5. The text provided here includes Exercises 24–27, but it does not include Algorithm R itself. A MIX program cannot be written, and its running time cannot be determined, without the algorithm's...
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: yes Solve time: 3m01s Let $N(n,m)$ denote the minimum amount of storage that guarantees successful allocation when the total amount of live data is at most $n$, and when block sizes are restricted to the interval $[1,m]$. As in Exercise 39, define $$ N(m)=\limsup_{n\to\infty}\frac{N(n,m)}{n}. $$ Exercise 39 established...
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: yes Solve time: 1m23s Write $$ M=\sum_{j=1}^{t}2^{m_j}, \qquad m_1>m_2>\cdots>m_t\ge0, $$ the binary expansion of $M$. Partition the memory locations $0,\ldots,M-1$ into consecutive blocks of these...
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 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 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: yes Solve time: 3m55s Exercise 2.5.42 [**] Let $N_{\rm BF}(n,m)$ denote the amount of memory required to guarantee non-overflow when...
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 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: 1m10s Yes. The essential operations of the buddy system remain valid...
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: 7m06s Working
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 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: 1m12s No. The test for $k=m$ in step S1 is necessary....
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: - Consider an AVAIL list whose first two free blocks have sizes $25$ and $12$, in that order. Suppose two allocation requests arrive consecutively, first for $N=12$ words and then...
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.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: 41s Let the usable payload in a node be $k-b$ words. An item of length $l$ then occupies $$ \left\lceil \frac{l}{k-b}\right\rceil $$ nodes, so...
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: no Solve time: 33s Modify Algorithm B by first adjoining the...
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.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: yes Solve time: 1m15s In the buddy system, a block is repeatedly split and recombined with its unique buddy. When a block becomes free, the deallocation algorithm must determine quickly whether its buddy is also free so that...
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 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: yes Solve time: 9m26s Exercise 2.4.13 [24] Solution. We want a replacement for Algorithm A when the Data Table is stored in the format of Exercise 12, in which each node contains only the two links $$ \text{PREV} \quad \text{and}...
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.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.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: 2m26s No. In the modified step A4', the parameter $c$ determines the threshold below which a block is not split. Setting $c=100$ means that whenever the remainder after allocation...
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.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: yes Solve time: 44s A COBOL data description is a rooted ordered forest in which each group item is followed in the source listing by the complete specification of its immediate subordinate items before the next item...
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: no Solve time: - To obtain the best-fit method, modify Algorithm A so that it does not stop when the first block with SIZE(P) \ge N is found. Instead, search the entire AVAIL list and keep track of the...
TAOCP 2.4 Exercise 9
Section 2.4: Multilinked Structures Exercise 9. [**] [M23] Prove that Algorithm C is correct. Verified: no Solve time: 1m14s Please provide the statement of Algorithm C from Section 2.4. The excerpt you included does not contain the algorithm itself, so a rigorous proof of correctness cannot be written without its steps and notation.
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: no Solve time: - The essential idea is to organize the free blocks by size instead of by location. If the blocks are kept...
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: yes Solve time: 1m30s When there are no available blocks, the list headed by AVAIL must be empty. In the representation used by Algorithm C, an empty doubly linked circular list is obtained by making the header point to itself in both...
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.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.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: 4m57s Let the available-space list of Algorithm C be the doubly linked circular list...
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: no Solve time: 6m39s Algorithm A is modified as follows to satisfy the...
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: 4m34s The reviewer’s criticisms are correct. The submitted response is not a solution of Exercise 13; it merely explains why the exercise cannot be solved from the excerpt provided in the prompt. The fundamental difficulty is that the statement of Exercise 13 is explicitly dependent on...
TAOCP 2.4 Exercise 2
Section 2.4: Multilinked Structures Exercise 2. [**] [10] Comment about the running time of Algorithm A. Verified: yes Solve time: 2m15s Corrected Solution to Exercise 2.4.2.2 We are asked to comment on the running time of Algorithm A. To analyze it rigorously, we examine the algorithm’s loop structure, the number of times each operation is executed, and the effect of the data structure. The argument must rely explicitly on the...
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.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: yes Solve time: 3m06s Exercise 2.5.16 asks for the MIX implementation of Algorithm C (boundary-tag storage allocation with coalescing), modified according to Exercise 15. The essential point of Exercise 15 is that the coalescing step should distinguish the four possibilities $$ (L,R)\in{+,-}^2, $$ where $$ L=\operatorname{TAG}(P_0-1),\qquad R=\operatorname{TAG}(P_0+\operatorname{SIZE}(P_0)),...
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 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: 4m32s Let the memory contain permanent boundary sentinels at both extremes. Specifically, assume that location $0$ and the final location $M+1$ contain the reserved tag $0$. Hence every...
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: no Solve time: - The reviewer is correct that the previous submission did not solve the exercise. However, there is still a fundamental obstacle to producing a mathematically...
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: yes Solve time: 47s “MOVE CORRESPONDING SALES TO PURCHASES” means that each elementary data item occurring in both group structures SALES and PURCHASES, identified by the same name within the COBOL record hierarchy, is transferred from SALES into the corresponding location in...
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 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 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: yes Solve time: 1m50s If reservations and liberations occur strictly in last-in-first-out order, the free storage...
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: yes Solve time: 2m27s Let rule (c) be the COBOL requirement that whenever two consecutive entries have level numbers $L_i$ and $L_{i+1}$, and $L_{i+1}>L_i$, the new...
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: 31s The statement " MOVE CORRESPONDING \(\alpha\) TO \(\beta\) " is exactly the same as " MOVE \(\alpha\) TO \(\beta\) " when the structures $\alpha$ and $\beta$ are identical in...
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: 2m51s To perform the function of Algorithm C with the representation of exercise 12, insert the new item exactly as before by setting its PREV field to the preceding item in the same group and setting its SCOPE field...
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 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: 2m45s Let $$ t_1,t_2,t_3,\ldots $$ be the sequence $$ 1,2,4,4,8,8,8,8,16,16,\ldots, $$ where $2^k$ appears exactly $2^k$ times. Define $$ a_n=\sum_{j=1}^{n}...
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: 1m06s Replace the explicit stack by a linked stack whose links occupy the unused SIB fields of the nodes that are on 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: yes Solve time: 1m18s Algorithm B spends much of its time locating all occurrences of a given name. A direct improvement is to maintain, for each Data Table entry, an additional link to the first Symbol Table node having...
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.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: yes Solve time: 2m39s Let the sides of the convex $(n+2)$-gon be distinct and let one of them be designated as the root side $r$. Since the sides are distinct,...
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: 5m42s Let the root node be $P$. The proof follows the pattern of the proof of Algorithm 2.3.1T, but it must be tied to the actual bookkeeping used in Algorithm E. For every node $X$, let $\mathrm{TYPE}(X)$ be the value of its type...
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.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: 59s The structure of an ordered tree with $n$ vertices is independent of labels; only the relative left-to-right ordering of subtrees...
TAOCP 2.3.4.4 Exercise 20
Section 2.3.4.4: Enumeration of Trees Exercise 20. [**] [M24] (J. Riordan.) How many different labeled, oriented trees are there having $n$ vertices, $k_0$ of which have in-degree 0, $k_1$ have in-degree 1, $k_2$ have in-degree 2, ...? (Note that necessarily $k_0+k_1+k_2+\cdots=n$, and $k_1+2k_2+3k_3+\cdots=n-1$.) Verified: yes Solve time: 3m38s Start from the correct expression that survives all prior reductions: Let the prescribed indegree sequence be $x_1,\dots,x_n$, a permutation of a multiset...
TAOCP 2.3.4.4 Exercise 25
Section 2.3.4.4: Enumeration of Trees Exercise 25. [**] [M20] What is the value of the quantity $r(n,q)$ that appears in Eqs. (18) and (19)? (Give an explicit formula; the text only mentions that $r(n,n-1)=n^{n-2}$.) Verified: yes Solve time: 1m46s Exercise 2.3.4.4.25 (Corrected Solution) We are asked to find an explicit formula for the quantity $r(n,q)$ appearing in Eqs. (18) and (19) of TAOCP. By definition, $r(n,q)$ counts labeled directed graphs...
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: 2m11s Exercise 2.3.4.4.22, Corrected Solution We wish to count...
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: yes Solve time: 1m01s A List can be described as a directed graph in which each node corresponds to a List element or atom, and each pointer ( DLINK , RLINK , or...
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: 1m10s Let $k_i$ denote the number of vertices of in-degree $i$. By hypothesis, every vertex has in-degree $0$ or $2$, so $k_i=0$ for $i\ne0,2$. Exercise 20 gives the number of labeled oriented trees with prescribed...
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: yes Solve time: 2m40s Corrected Solution to Exercise 2.3.4.4.28 We are asked to enumerate the number of labeled free trees on vertices $$ U_1,\ldots,U_m, \quad V_1,\ldots,V_n...
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: 2m04s...
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 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.4 Exercise 23
Section 2.3.4.4: Enumeration of Trees Exercise 23. [**] [M21] How many ordered trees are possible with $n$ labeled vertices? (Give a simple formula involving factorials.) Verified: yes Solve time: 1m19s Let $\mathcal{T}_n$ be the set of ordered trees with $n$ vertices whose vertices are labeled by ${1,2,\ldots,n}$. Let $\mathcal{S}_n$ be the set of ordered tree structures with $n$ unlabeled vertices. Each element of $\mathcal{S}_n$ admits exactly $n!$ distinct labelings by...
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 18
Section 2.3.4.4: Enumeration of Trees Exercise 18. [**] [24] Show that the following method is another way to define a one-to-one correspondence between $(n-1)$-tuples of numbers from 1 to $n$ and oriented trees with $n$ labeled vertices: Let the leaves of the tree be $V_1,\ldots,V_k$ in ascending order. Let $(V_1,V_{k+1},V_{k+2},\ldots,V_q)$ be the path from $V_1$ to the root, and write down the vertices $V_q,\ldots,V_{k+2},V_{k+1}$. Then let $(V_2,V_{q+1},V_{q+2},\ldots,V_r)$ be the shortest...
TAOCP 2.3.4.4 Exercise 26
Section 2.3.4.4: Enumeration of Trees Exercise 26. [**] [20] In terms of the notation at the end of this section, draw the $((3,2,4),(1,4,2))$-construction, analogous to (23), and find the number $k$ that corresponds to the canonical representation having $t=8$, the sequences of colors "red, yellow, blue, red, yellow, blue, red, blue, blue" and "red, yellow, blue, yellow, yellow, blue, yellow", and the index sequences $3;\ 1,2,2,1;\ 2,4$. Verified: yes Solve...
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 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: yes Solve time: 1m11s Yes, List structures can be threaded analogously to trees. A threaded List is one in which null RLINK or DLINK pointers are replaced by references to the logically...
TAOCP 2.3.4.4 Exercise 19
Section 2.3.4.4: Enumeration of Trees Exercise 19. [**] [M24] How many different labeled, oriented trees are there having $n$ vertices, $k$ of which are leaves (have in-degree zero)? Verified: yes Solve time: 3m30s The Prüfer-code framework is correct, but the constraint “exactly $k$ leaves” must be enforced as a surjectivity condition, not as a simple restriction on the alphabet. The earlier argument failed because it allowed vertices outside the leaf...
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.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 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.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: 2m53s Corrected Solution to Exercise...
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 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 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: yes Solve time: 43s The term $c_2 M$ arises from the tracing phase of the garbage collection procedure, in which all nodes that are reachable from the list heads are...
TAOCP 2.3.4.4 Exercise 10
Section 2.3.4.4: Enumeration of Trees Exercise 10. [**] [M22] Prove that a free tree with $n$ vertices and two centroids consists of two free trees with $n/2$ vertices, joined by an edge. Conversely, if two free trees with $m$ vertices are joined by an edge, we obtain a free tree with $2m$ vertices and two centroids. Verified: no Solve time: 2m19s Let $T$ be a free tree with $n$ vertices...
TAOCP 2.3.4.2 Exercise 6
Section 2.3.4.2: Oriented Trees Exercise 6. [**] $$M21$$ True or false: A directed graph that is rooted and contains no cycles and no oriented cycles is an oriented tree. Verified: yes Solve time: 2m14s $$ \boxed{\text{True}} $$ Let $G$ be a directed graph that is rooted and contains neither cycles nor oriented cycles. We must show that $G$ is an oriented tree. Let $R$ be a root of $G$. By...
TAOCP 2.3.4.4 Exercise 3
Section 2.3.4.4: Enumeration of Trees Exercise 3. [**] [M40] Write a computer program that determines the number of (unlabeled) free trees and of oriented trees with $n$ vertices, for $n\le100$. (Use the result of exercise 2.) Explore arithmetical properties of these numbers; can anything be said about their prime factors, or their residues modulo $p$? Verified: yes Solve time: 5m43s The failure occurs at a single decisive point: the sequence...
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: 1m49s The claim is true : any finite directed graph satisfying (a) and (b) and having no directed cycles must already be an oriented tree, because condition (c) is forced by the...
TAOCP 2.3.4.4 Exercise 12
Section 2.3.4.4: Enumeration of Trees Exercise 12. [**] [M20] Find the number of labeled oriented trees with $n$ vertices by using determinants and the result of exercise 2.3.4.2-19. (See also exercise 1.2.3-36.) Verified: no Solve time: 30s By exercise 2.3.4.2-19, the number of labeled oriented trees with a specified root is equal to the determinant of the reduced Laplacian matrix of the complete graph $K_n$, obtained by deleting the row...
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 8
Section 2.3.4.2: Oriented Trees Exercise 8. [**] $$HM40$$ Study the properties of automorphism groups of oriented trees, namely the groups consisting of all permutations $\pi$ of the vertices and arcs for which we have $\operatorname{init}(e\pi)=\operatorname{init}(e)\pi$, $\operatorname{fin}(e\pi)=\operatorname{fin}(e)\pi$. Verified: yes Solve time: 2m06s Let $G$ be a finite or locally finite oriented tree in which every vertex $v\neq R$ has exactly one outgoing arc and the root $R$ has no outgoing arc....
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: 6m15s The solution fails because it tries to classify the six trees using degree sequences, but degree sequences alone do not enumerate all non-isomorphic trees on six vertices. In particular, one listed “tree” was invalid, and one degree sequence...
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.4 Exercise 16
Section 2.3.4.4: Enumeration of Trees Exercise 16. [**] [25] Design an algorithm (as efficient as possible) that converts from the canonical representation of an oriented tree to a conventional computer representation using PARENT links. Verified: no Solve time: 1m11s The construction described after (16) already yields an efficient method. Let $x_1,x_2,\ldots,x_{n-1}$ be the canonical representation. Compute, for each vertex $v$, the number $C[v]$ of occurrences of $v$ among $x_1,\ldots,x_{n-1}$. Maintain...
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 17
Section 2.3.4.2: Oriented Trees Exercise 17. [**] $$M32$$ What is the probability of winning the solitaire game of clock (described in exercise 16), assuming the deck is randomly shuffled? What is the probability that exactly $k$ cards are still face down when the game is over? Verified: no Solve time: 4m12s Corrected Solution to Exercise 2.3.4.2.17 We analyze the solitaire game of clock and determine both the probability of winning...
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: no Solve time: 34s Let $G(z)=\sum_{n\ge1}g_nz^n$, where $g_n$ is the number of distinct oriented binary trees with $n$ vertices. An oriented binary tree consists of a root together with an unordered collection of $0$, $1$, or $2$ binary subtrees. Therefore the argument leading to equation (3) applies with...
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...
TAOCP 2.3.4.4 Exercise 5
Section 2.3.4.4: Enumeration of Trees Exercise 5. [**] [M25] (A. Cayley.) Let $c_n$ be the number of (unlabeled) oriented trees having $n$ leaves (namely, vertices with in-degree zero) and having at least two subtrees at every other vertex. Thus $c_3=2$, by virtue of the two trees Find a formula analogous to (3) for the generating function $$ C(z)=\sum_n c_nz^n. $$ Verified: no Solve time: 31s Let $C(z)$ be the generating...
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: 1m13s The degree of a node in the tree terminology of Section 2.3 is the number of children of that...
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: no Solve time: 2m41s Let $c_n$ be the...
TAOCP 2.3.4.2 Exercise 13
Section 2.3.4.2: Oriented Trees Exercise 13. [**] $$M24$$ Prove that if $R$ is a root of a (possibly infinite) directed graph $G$, then $G$ contains an oriented subtree with the same vertices as $G$ and with root $R$. (As a consequence, it is always possible to choose the free subtree in flow charts like Fig. 32 of Section 2.3.4.1 so that it is actually an oriented subtree; this would be...
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: yes Solve time: 55s Let $T$ be the free tree in Fig. 30 with distinguished vertex $G$. For each edge $V\text{--}V'$, there is a unique simple path in $T$ from $V$ to $G$. If this...
TAOCP 2.3.4.2 Exercise 19
Section 2.3.4.2: Oriented Trees Exercise 19. [**] $$M31$$ ( The matrix tree theorem. ) Let $G$ be a directed graph with $n+1$ vertices $V_0,V_1,\ldots,V_n$. Let $A$ be the $(n+1)\times(n+1)$ matrix with $$ a_{ij}= \begin{cases} -k, & \text{if } i\ne j \text{ and there are } k \text{ arcs from } V_i \text{ to } V_j;\ t, & \text{if } i=j \text{ and there are } t \text{ arcs from }...
TAOCP 2.3.4.2 Exercise 15
Section 2.3.4.2: Oriented Trees Exercise 15. [**] $$M20$$ True or false: A directed graph that is connected and balanced is strongly connected. Verified: yes Solve time: 1m04s True. Let $G$ be a finite directed graph that is connected and balanced, with no isolated vertices. By Theorem G, $G$ possesses an Eulerian trail $P=(e_1,\ldots,e_m)$ with $\operatorname{fin}(e_m)=\operatorname{init}(e_1)$, so $P$ is a closed oriented walk that uses every arc exactly once. Let $V$...
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 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 3
Section 2.3.4.2: Oriented Trees Exercise 3. [**] $$16$$ Draw the diagram for a directed graph that is connected but not rooted. Verified: yes Solve time: 2m47s Take three vertices $A,B,C$ and two arcs, $$ A\to B,\qquad C\to B. $$ The corresponding undirected graph has edges $A-B$ and $C-B$, hence it is connected. But the digraph is not rooted. Vertex $A$ is not a root, since there is no oriented path...
TAOCP 2.3.4.2 Exercise 25
Section 2.3.4.2: Oriented Trees Exercise 25. [**] $$26$$ Design a computer representation for directed graphs that generalizes the right-threaded binary tree representation of a tree. Use two link fields ALINK , BLINK and two one-bit fields ATAG , BTAG ; and design the representation so that: (i) there is one node for each arc of the directed graph (not for each vertex); (ii) if the directed graph is an oriented...
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 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: 4m51s Let $C_e$ be the fundamental cycle determined by a non-tree arc $e$. By definition, $$ C_e=e\cup P_e, $$ where $P_e$ is the unique path in the spanning tree $T$ joining the endpoints of...
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 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.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: 2m05s Let the oriented tree be rooted, with every edge directed toward the root. If $v$ is not the root, let $f(v)$ denote its parent. In such a tree, every nonroot vertex has outdegree...
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.4 Exercise 6
Section 2.3.4.4: Enumeration of Trees Exercise 6. [**] [M25] Let an "oriented binary tree" be an oriented tree in which each vertex has in-degree two or less. Find a reasonably simple relation that defines the generating function $G(z)$ for the number of distinct oriented binary trees with $n$ vertices, and find the first few values. Verified: no Solve time: 49s Let $g_n$ denote the number of distinct oriented binary trees...
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.4 Exercise 2
Section 2.3.4.4: Enumeration of Trees Exercise 2. [**] [HM24] (R. Otter.) Show that the numbers $a_n$ satisfy the following condition: $$ na_{n+1}=a_1s_{n1}+2a_2s_{n2}+\cdots+na_ns_{nn}, $$ where $$ s_{nk}=\sum_{1\le j\le n/k}a_{n+1-jk}. $$ (These formulas are useful for the calculation of the $a_n$, since $s_{nk}=s_{(n-k)k}+a_{n+1-k}$.) Verified: yes Solve time: 4m11s From exercise 1, write $$ A(z)=z,e^{C(z)},\qquad C(z)=\sum_{k\ge1}\frac{A(z^k)}{k}. $$ Taking logarithmic derivatives gives $$ \frac{A'(z)}{A(z)} =\frac1z+C'(z) =\frac1z+\sum_{k\ge1}z^{k-1}A'(z^k), $$ since $$ \frac{d}{dz}\frac{A(z^k)}k=z^{k-1}A'(z^k). $$ Hence $$ zA'(z)...
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 1
Section 2.3.4.2: Oriented Trees Exercise 1. [**] $$M20$$ Prove that if $V$ and $V'$ are vertices of a directed graph and if there is an oriented walk from $V$ to $V'$, then there is a simple oriented path from $V$ to $V'$. Verified: yes Solve time: 41s Let $(e_1,\ldots,e_n)$ be an oriented walk from $V$ to $V'$. Among all oriented walks from $V$ to $V'$, choose one of minimum length,...
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: no Solve time: 1m49s The given canonical sequence is $$ 3,1,4,1,5,9,2,6,5. $$ We reconstruct the oriented tree as follows. Let $V_1$ be the smallest integer not appearing in the sequence; this gives $V_1=7$. Draw an arc $7\to 3$. Next, the smallest unused integer is $V_2=8$, so draw $8\to...
TAOCP 2.3.4.2 Exercise 5
Section 2.3.4.2: Oriented Trees Exercise 5. [**] $$M16$$ Let $G$ be a directed graph that contains an oriented walk $(e_1,\ldots,e_n)$ with $\operatorname{fin}(e_n)=\operatorname{init}(e_1)$. Give a proof that $G$ is not an oriented tree, using the terminology defined in this section. Verified: yes Solve time: 1m02s Assume that $G$ contains an oriented walk $(e_1,\ldots,e_n)$ with $n\ge1$ and $$ \operatorname{fin}(e_n)=\operatorname{init}(e_1). $$ Let $$ V_0=\operatorname{init}(e_1),\qquad V_k=\operatorname{fin}(e_k)\quad(1\le k\le n). $$ Then $V_n=V_0$, so the walk...
TAOCP 2.3.4.2 Exercise 14
Section 2.3.4.2: Oriented Trees Exercise 14. [**] $$21$$ Let $G$ be the balanced digraph shown in Fig. 36, and let $G'$ be the oriented subtree with vertices $V_0,V_1,V_2$ and arcs $e_{01},e_{21}$. Find all oriented walks $P$ that meet the conditions of Theorem D, starting with arc $e_{12}$. Verified: yes Solve time: 1m07s The root of $G'$ is $V_1$, since the arcs of the oriented tree are $e_{01}$ and $e_{21}$. By...
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: 28s Let $T$ be a free tree. Suppose $X$ is a centroid of $T$, and let...
TAOCP 2.3.4.4 Exercise 14
Section 2.3.4.4: Enumeration of Trees Exercise 14. [**] [10] True or false: The last entry, $f(V_{n-1})$, in the canonical representation of an oriented tree is always the root of that tree. Verified: yes Solve time: 12m12s Corrected Solution for Exercise 2.3.4.4.14 Exercise 2.3.4.4.14.14 [10] True or false: The last entry, $f(V_{n-1})$, in the canonical representation of an oriented tree is always the root of that tree. Solution. We examine the...
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: 33s We start from equation (3) of the section, which states that $$ 1 + A(z) + A(z)^2 + A(z)^3 + \cdots = \prod_{n\ge1} \frac{1}{(1-z^n)^{a_n}}. $$ Taking natural logarithms of both sides gives $$ \ln\bigl(1 + A(z) + A(z)^2 + \cdots\bigr) = \sum_{n\ge1} -a_n...
TAOCP 2.3.4.2 Exercise 26
Section 2.3.4.2: Oriented Trees Exercise 26. [**] $$HM39$$ ( Analysis of a random algorithm. ) Let $G$ be a directed graph on the vertices $V_1,V_2,\ldots,V_n$. Assume that $G$ represents the flow chart for an algorithm, where $V_1$ is the Start vertex and $V_n$ is the Stop vertex. (Therefore $V_n$ is a root of $G$.) Suppose each arc $e$ of $G$ has been assigned a probability $p(e)$, where the probabilities satisfy...
TAOCP 2.3.3 Exercise 14
Section 2.3.3: Other Representations of Trees Exercise 14. [ 40 ] Give a formal proof (or disproof) of the validity of Algorithm A . Verified: yes Solve time: 3m14s Exercise 2.3.3.14 [ 40 ] Problem: Give a formal proof (or disproof) of the validity of Algorithm A . Corrected Solution We prove formally that Algorithm A correctly computes the sum of two polynomials represented as described in Section 2.3.2. Let...
TAOCP 2.3.2 Exercise 20
Section 2.3.2: Binary Tree Representation of Trees Exercise 20. [ M22 ] Prove that if u and v are nodes of a forest, u is a proper ancestor of v if and only if u precedes v in preorder and u follows v in postorder. Verified: yes Solve time: 1m22s Solution Let $F$ be a forest and let $u, v$ be nodes in $F$. We aim to prove that $u$...
TAOCP 2.3.2 Exercise 14
Section 2.3.2: Binary Tree Representation of Trees Exercise 14. [ M21 ] How long does it take the program of exercise 13 to copy a tree with n nodes? Verified: yes Solve time: 1m15s Solution Exercise 14 asks for the running time of the COPY subroutine of Exercise 13. The hint for Exercise 13 directs us to adapt Algorithm 2.3.1C to right-threaded binary trees. Therefore the copied tree is traversed...
TAOCP 2.3.2 Exercise 17
Section 2.3.2: Binary Tree Representation of Trees Exercise 17. [ M40 ] Write a program to do algebraic simplification capable of reducing, for example, (20) or (21) to (22) . [ Hints: Include a new field with each node, representing its coefficient (for summands) or its exponent (for factors in a product). Apply algebraic identities, like replacing \ln(u ↑ v) by v \ln u ; remove the operations - ,...
TAOCP 2.3.3 Exercise 12
Section 2.3.3: Other Representations of Trees Exercise 12. [ 21 ] At the beginning of Algorithm A , the variables P and Q point to the roots of two trees. Let P_0 and Q_0 denote the values of P and Q before execution of Algorithm A . (a) After the algorithm terminates, is Q_0 always the address of the root of the sum of the two given polynomials? (b) After...
TAOCP 2.3.2 Exercise 19
Section 2.3.2: Binary Tree Representation of Trees Exercise 19. [ M35 ] A free lattice is a mathematical system, which (for the purposes of this exercise) can be simply defined as the set of all formulas composed of variables and two abstract binary operators " ∨ " and " ∧ ". A relation " X ⪰ Y " is defined between certain formulas X and Y in the free lattice...
TAOCP 2.3.2 Exercise 13
Section 2.3.2: Binary Tree Representation of Trees Exercise 13. [ 26 ] Write a MIX program for the COPY subroutine (which fits in the program of the text between lines 063-104 ). [ Hint: Adapt Algorithm 2.3.1C to the case of right-threaded binary trees, with suitable initial conditions.] Verified: yes Solve time: 1m40s Exercise 2.3.2.13 The previous solution does not address the exercise. The problem is not to describe a...
TAOCP 2.3.3 Exercise 2
Section 2.3.3: Other Representations of Trees Exercise 2. [ 22 ] (Burks, Warren, and Wright, Math. Comp. 8 (1954), 53-57.) The trees (2) stored in preorder with degrees would be $$ \begin{array}{c|cccccccccc} \text{DEGREE} & 2 & 0 & 1 & 0 & 3 & 1 & 0 & 1 & 0 & 0\ \text{INFO} & A & B & C & K & D & E & H & F...
TAOCP 2.3.2 Exercise 16
Section 2.3.2: Binary Tree Representation of Trees Exercise 16. [ 24 ] Write a MIX program for the PWR routine, corresponding to DIFF[8] as specified in exercise 12 . (This routine should be added to the program in the text after the solution to exercise 15 .) Verified: yes Solve time: 1m26s Solution Exercise 12 specifies DIFF[8] for exponentiation, corresponding to rule (19) : $$ D(u \uparrow v) = D(u)...
TAOCP 2.3.3 Exercise 5
Section 2.3.3: Other Representations of Trees Exercise 5. [ 16 ] A triply linked tree, as shown in Fig. 26, contains PARENT , LCHILD , and RLINK fields in each node, with liberal use of Λ -links when there is no appropriate node to mention in the PARENT , LCHILD , or RLINK field. Would it be a good idea to extend this representation to a threaded tree , by...
TAOCP 2.3.3 Exercise 16
Section 2.3.3: Other Representations of Trees Exercise 16. [ M24 ] Prove the validity of Algorithm F . Verified: yes Solve time: 1m36s Solution Let the given forest be represented in postorder with degrees as in representation (9) . For each node $x$, let $\deg(x)$ denote its degree, and let $f(x)$ be the value to be computed. By hypothesis, $f(x)$ depends only on $x$ and on the values of $f$...
TAOCP 2.3.3 Exercise 15
Section 2.3.3: Other Representations of Trees Exercise 15. [ 40 ] Design an algorithm to compute the product of two polynomials represented as in Fig. 28. Verified: yes Solve time: 2m39s Exercise 2.3.3.15 [ 40 ] Problem restatement. Let two polynomials be represented in the linked-tree sequential form of Fig. 28. That is, each polynomial is a tree in which a node contains fields: COEFF , the coefficient of the...
TAOCP 2.3.3 Exercise 11
Section 2.3.3: Other Representations of Trees Exercise 11. [ 24 ] ( Equivalence declarations. ) Several compiler languages, notably FORTRAN, provide a facility for overlapping the memory locations assigned to sequentially stored tables. The programmer gives the compiler a set of relations of the form X[j] ≡ Y[k] , which means that variable X[j+s] is to be assigned to the same location as variable Y[k+s] for all s . Each...
TAOCP 2.3.3 Exercise 9
Section 2.3.3: Other Representations of Trees Exercise 9. [ 20 ] Give a table analogous to (15) and a diagram analogous to (16) that shows the trees present after Algorithm E has processed all of the equivalences in (11) from left to right. Verified: yes Solve time: 1m15s Solution We are asked to give a table analogous to (15) and a diagram analogous to (16) showing the trees present after...
TAOCP 2.3.3 Exercise 3
Section 2.3.3: Other Representations of Trees Exercise 3. [ 24 ] Modify Algorithm 2.3.2D so that it follows the ideas of Algorithm F , placing the derivatives it computes as intermediate results on a stack, instead of recording their locations in an anomalous fashion as is done in step D3 . (See exercise 2.3.2-21 .) The stack may be maintained by using the RLINK field in the root of each...
TAOCP 2.3.2 Exercise 18
Section 2.3.2: Binary Tree Representation of Trees Exercise 18. [ 25 ] An oriented tree specified by n links PARENT[j] for 1 <= j <= n implicitly defines an ordered tree if the nodes in each family are ordered by their location. Design an efficient algorithm that constructs a doubly linked circular list containing the nodes of this ordered tree in preorder. For example, given $$ \begin{aligned} j &= 1\...
TAOCP 2.3.3 Exercise 6
Section 2.3.3: Other Representations of Trees Exercise 6. [ 24 ] Suppose that the nodes of an oriented forest have three link fields, PARENT , LCHILD , and RLINK , but only the PARENT link has been set up to indicate the tree structure. The LCHILD field of each node is Λ and the RLINK fields are set as a linear list that simply links the nodes together in some...
TAOCP 2.3.2 Exercise 15
Section 2.3.2: Binary Tree Representation of Trees Exercise 15. [ 23 ] Write a MIX program for the DIV routine, corresponding to DIFF[7] as specified in the text. (This routine should be added to the program in the text after line 217 .) Verified: yes Solve time: 1m21s Solution The routine DIV computes the derivative of a formula of the form $u / v$ with respect to the variable $x$,...
TAOCP 2.3.3 Exercise 18
Section 2.3.3: Other Representations of Trees Exercise 18. [ 28 ] Design an algorithm that, given the two tables INFO1[j] and RLINK[j] for 1 <= j <= n corresponding to preorder sequential representation, forms tables INFO2[j] and DEGREE[j] for 1 <= j <= n , corresponding to postorder with degrees. For example, according to (3) and (9) , your algorithm should transform $$ \begin{array}{c|cccccccccc} j & 1 & 2 &...
TAOCP 2.3.3 Exercise 4
Section 2.3.3: Other Representations of Trees Exercise 4. [ 18 ] The trees (2) contain 10 nodes, five of which are terminal. Representation of these trees in the normal binary-tree fashion involves 10 LLINK fields and 10 RLINK fields (one for each node). Representation of these trees in the form (10) , where LLINK and INFO share the same space in a node, requires 5 LLINK s and 15 RLINK...
TAOCP 2.3.2 Exercise 22
Section 2.3.2: Binary Tree Representation of Trees Exercise 22. [ M26 ] If T and T' are trees, let us say T can be embedded in T' , written T ⊆ T' , if there is a one-to-one function f from the nodes of T into the nodes of T' such that f preserves both preorder and postorder. (In other words, u precedes v in preorder for T if and...
TAOCP 2.3.2 Exercise 12
Section 2.3.2: Binary Tree Representation of Trees Exercise 12. [ M21 ] Give specifications for the routine DIFF[8] (the " ↑ " operation), which was omitted from the algorithm in the text. Verified: yes Solve time: 4m07s Corrected Solution for Exercise 2.3.2.12 We are asked to give specifications for the routine DIFF[8] , which handles differentiation of expressions of the form $u \uparrow v$ (exponentiation) in the context of the...
TAOCP 2.3.3 Exercise 10
Section 2.3.3: Other Representations of Trees Exercise 10. [ 28 ] In the worst case, Algorithm E may take order n^2 steps to process n equivalences. Show how to modify the algorithm so that the worst case is not this bad. Verified: yes Solve time: 1m24s Setup Algorithm E constructs a forest whose nodes are the elements $1,\ldots,n$. Each tree represents one equivalence class. The PARENT field of a root...
TAOCP 2.3.3 Exercise 13
Section 2.3.3: Other Representations of Trees Exercise 13. [ M29 ] Give an informal proof that at the beginning of step A8 of Algorithm A we always have EXP(P) = EXP(Q) and CV(UP(P)) = CV(UP(Q)) . (This fact is important to the proper understanding of that algorithm.) Verified: yes Solve time: 1m23s Setup Algorithm A adds two polynomials represented as trees. Let P and Q denote the current nodes in...
TAOCP 2.3.3 Exercise 1
Section 2.3.3: Other Representations of Trees Exercise 1. [ 20 ] If we had only LTAG , INFO , and RTAG fields (not LLINK ) in a level order sequential representation like (8) , would it be possible to reconstruct the LLINK s? (In other words, are the LLINK s redundant in (8) , as the RLINK s are in (3) ?) Verified: no Solve time: 4m54s Solution We are...
TAOCP 2.3.3 Exercise 8
Section 2.3.3: Other Representations of Trees Exercise 8. [ 15 ] Algorithm E sets up a tree structure that represents the given pairs of equivalent elements, but the text does not mention explicitly how the result of Algorithm E can be used. Design an algorithm that answers the question, " Is j ≡ k? ", assuming that 1 <= j <= n , 1 <= k <= n , and...
TAOCP 2.3.3 Exercise 17
Section 2.3.3: Other Representations of Trees Exercise 17. [ 25 ] Algorithm F evaluates a "bottom-up" locally defined function, namely, one that should be evaluated at the children of a node before it is evaluated at the node. A "top-down" locally defined function f is one in which the value of f at a node x depends only on x and the value of f at the parent of x...
TAOCP 2.3.3 Exercise 19
Section 2.3.3: Other Representations of Trees Exercise 19. [ M27 ] Instead of using SCOPE links in (5) , we could simply list the number of descendants of each node, in preorder: $$ \begin{array}{c|cccccccccc} \text{DESC} & 3 & 0 & 1 & 0 & 5 & 1 & 0 & 1 & 0 & 0\ \text{INFO} & A & B & C & K & D & E & H...
TAOCP 2.3.3 Exercise 7
Section 2.3.3: Other Representations of Trees Exercise 7. [ 15 ] What classes would appear in (12) if the relation 9 ≡ 3 had not been given in (11) ? Verified: yes Solve time: 1m18s Solution The relation $9 \equiv 3$ serves only to place the element $9$ into the equivalence class containing $3$. If this relation is omitted from the list of relations in $(11)$, no chain of equivalences...
TAOCP 2.3.2 Exercise 21
Section 2.3.2: Binary Tree Representation of Trees Exercise 21. [ 25 ] Algorithm D controls the differentiation activity for binary operators, unary operators, and nullary operators, thus for trees whose nodes have degree 2, 1, and 0; but it does not indicate explicitly how the control would be handled for ternary operators and nodes of higher degree. (For example, exercise 17 suggests making addition and multiplication into operators with any...
TAOCP 2.3.1 Exercise 36
Section 2.3.1: Traversing Binary Trees Exercise 36. [ M23 ] Exercise 1.2.1-15 shows that lexicographic order extends a well-ordering of a set S to a well-ordering of the n -tuples of elements of S . Exercise 25 above shows that a linear ordering of the information in tree nodes can be extended to a linear ordering of trees, using a similar definition. If the relation \prec well-orders S , is...
TAOCP 2.3.1 Exercise 12
Section 2.3.1: Traversing Binary Trees Exercise 12. [ 22 ] Design an algorithm analogous to Algorithm T that traverses a binary tree in preorder , and prove that your algorithm is correct. Verified: no Solve time: 37s Solution We aim to construct an algorithm analogous to Algorithm T that traverses a binary tree in preorder , visiting each node exactly once, and then prove its correctness by induction on the...
TAOCP 2.3.2 Exercise 10
Section 2.3.2: Binary Tree Representation of Trees Exercise 10. [ M23 ] Let F be a forest of trees whose nodes in preorder are u_1, u_2, ..., u_n , and let F' be a forest whose nodes in preorder are u'_1, u'_2, ..., u'_{n'} . Let d(u) denote the degree (the number of children) of node u . In terms of these ideas, formulate and prove a theorem analogous to...
TAOCP 2.3.2 Exercise 6
Section 2.3.2: Binary Tree Representation of Trees Exercise 6. [ 25 ] Let T be a nonempty binary tree in which each node has 0 or 2 children. If we regard T as an ordinary tree, it corresponds (via the natural correspondence) to another binary tree T' . Is there any simple relation between preorder, inorder, and postorder of the nodes of T (as defined for binary trees) and the...
TAOCP 2.3.1 Exercise 4
Section 2.3.1: Traversing Binary Trees Exercise 4. [ 20 ] The text defines three basic orders for traversing a binary tree; another alternative would be to proceed in three steps as follows: a) Visit the root, b) traverse the right subtree, c) traverse the left subtree, using the same rule recursively on all nonempty subtrees. Does this new order bear any simple relation to the tree orders already discussed? Verified:...
TAOCP 2.3.1 Exercise 30
Section 2.3.1: Traversing Binary Trees Exercise 30. [ 22 ] Design an algorithm that threads an unthreaded tree; for example, it should transform (2) into (10) . Note: Always use notations like P* and P$ when possible, instead of repeating the steps for traversal algorithms like Algorithm T . Verified: yes Solve time: 1m13s Solution Let T be an unthreaded binary tree, represented in the standard form of (2), and...
TAOCP 2.3.1 Exercise 7
Section 2.3.1: Traversing Binary Trees Exercise 7. [ 22 ] Show that if we are given the preorder and the inorder of the nodes of a binary tree, the binary tree structure may be constructed. (Assume that the nodes are distinct.) Does the same result hold true if we are given the preorder and postorder, instead of preorder and inorder? Or if we are given the inorder and postorder? Verified:...
TAOCP 2.3.1 Exercise 9
Section 2.3.1: Traversing Binary Trees Exercise 9. [ M20 ] When a binary tree having n nodes is traversed using Algorithm T , state how many times each of steps T1, T2, T3, T4, and T5 is performed (as a function of n ). Verified: no Solve time: 38s Solution Let a binary tree with n nodes be traversed using Algorithm T . We determine the number of times each...
TAOCP 2.3.2 Exercise 4
Section 2.3.2: Binary Tree Representation of Trees Exercise 4. [ 19 ] Is the following statement true or false? "The terminal nodes of a tree occur in the same relative position in preorder and postorder." Verified: no Solve time: 33s Solution We are asked to determine whether the statement "The terminal nodes of a tree occur in the same relative position in preorder and postorder." is true or false. Let...
TAOCP 2.3.1 Exercise 24
Section 2.3.1: Traversing Binary Trees Exercise 24. [ M20 ] Is Theorem A still valid if the nodes of T and T' are given in symmetric order instead of preorder? Verified: no Solve time: 46s Solution No. Theorem A depends essentially on the nodes being listed in preorder. In preorder, the root of every subtree appears before all nodes of that subtree, so the sequence contains enough structural information to...
TAOCP 2.3.1 Exercise 28
Section 2.3.1: Traversing Binary Trees Exercise 28. [ 00 ] After Algorithm C has been used to make a copy of a tree, is the new binary tree equivalent to the original, or similar to it? Verified: no Solve time: 47s Algorithm C constructs a new tree by creating a new node corresponding to each node of the original tree, copying the information field, and preserving the left and right...
TAOCP 2.3.1 Exercise 15
Section 2.3.1: Traversing Binary Trees Exercise 15. [ 15 ] In a threaded-tree representation like (10) , each node except the list head has exactly one link pointing to it from above, namely the link from its parent. Some of the nodes also have links pointing to them from below; for example, the node containing C has two pointers coming up from below, while node E has just one. Is...
TAOCP 2.3.1 Exercise 37
Section 2.3.1: Traversing Binary Trees Exercise 37. [ 24 ] (D. Ferguson.) If two computer words are necessary to contain two link fields and an INFO field, representation (2) requires 2n words of memory for a tree with n nodes. Design a representation scheme for binary trees that uses less space, assuming that one link and an INFO field will fit in a single computer word. Verified: yes Solve time:...
TAOCP 2.3.1 Exercise 32
Section 2.3.1: Traversing Binary Trees Exercise 32. [ 21 ] Suppose that each node of a binary tree has four link fields: LLINK and RLINK , which point to left and right subtrees or \Lambda , as in an unthreaded tree; SUC and PRED , which point to the successor and predecessor of the node in symmetric order. (Thus \operatorname{SUC}(P) = P$ and \operatorname{PRED}(P) = $P . Such a tree...
TAOCP 2.3.1 Exercise 2
Section 2.3.1: Traversing Binary Trees Exercise 2. [ 11 ] List the nodes of the binary tree in (a) preorder; (b) symmetric order; (c) postorder. Verified: no Solve time: 38s Solution Let T denote the root of the binary tree in the figure. We denote the left and right links of a node P by LLINK(P) and RLINK(P) , and the value of the node by INFO(P) . Traversals are...
TAOCP 2.3.1 Exercise 10
Section 2.3.1: Traversing Binary Trees Exercise 10. [ 20 ] What is the largest number of entries that can be in the stack at once, during the execution of Algorithm T , if the binary tree has n nodes? (The answer to this question is very important for storage allocation, if the stack is being stored consecutively.) Verified: no Solve time: 40s Solution The stack grows only in step T3...
TAOCP 2.3.1 Exercise 22
Section 2.3.1: Traversing Binary Trees Exercise 22. [ 25 ] Write a MIX program for the algorithm given in exercise 21 and compare its execution time to Programs S and T . Verified: no Solve time: 36s Solution We are asked to write a MIX program that implements the algorithm of Exercise 21, which traverses an unthreaded binary tree in inorder without using any auxiliary stack , modifying the LLINK...
TAOCP 2.3.1 Exercise 18
Section 2.3.1: Traversing Binary Trees Exercise 18. [ 24 ] Many algorithms dealing with trees like to visit each node twice instead of once, using a combination of preorder and inorder that we might call double order . Traversal of a binary tree in double order is defined as follows: If the binary tree is empty, do nothing; otherwise a) visit the root, for the first time; b) traverse the...
TAOCP 2.3.2 Exercise 3
Section 2.3.2: Binary Tree Representation of Trees Exercise 3. [ 22 ] What is the relation between Dewey decimal notation for the nodes of a forest and the preorder and postorder of those nodes? Verified: yes Solve time: 1m35s Solution Let the Dewey decimal notation of a node be $$ d_1.d_2.\cdots.d_k, $$ where $d_1$ gives the position of the node among the trees of the forest, $d_2$ gives its position...
TAOCP 2.3.2 Exercise 9
Section 2.3.2: Binary Tree Representation of Trees Exercise 9. [ M21 ] Show that the total number of nonterminal nodes in a forest has a simple relation to the total number of right links equal to Λ in the corresponding unthreaded binary tree. Verified: yes Solve time: 2m27s Solution Let $F$ be a forest containing $t$ trees. Under the natural correspondence between forests and binary trees, each node of $F$...
TAOCP 2.3.2 Exercise 11
Section 2.3.2: Binary Tree Representation of Trees Exercise 11. [ 15 ] Draw trees analogous to those shown in (7) , corresponding to the formula $$ y = e^{-x^2}. $$ Verified: yes Solve time: 2m12s Solution We are asked to draw trees analogous to those in (7) corresponding to the formula $$ y = e^{-x^2}. $$ Following the conventions in Section 2.3.2, we first construct the rooted formula tree ....
TAOCP 2.3.1 Exercise 5
Section 2.3.1: Traversing Binary Trees Exercise 5. [ 22 ] The nodes of a binary tree may be identified by a sequence of zeros and ones, in a notation analogous to "Dewey decimal notation" for trees, as follows: The root (if present) is represented by the sequence "1" . Roots (if present) of the left and right subtrees of the node represented by \alpha are respectively represented by \alpha0 and...
TAOCP 2.3.1 Exercise 11
Section 2.3.1: Traversing Binary Trees Exercise 11. [ HM41 ] Analyze the average value of the largest stack size occurring during the execution of Algorithm T as a function of n , given that all binary trees with n nodes are considered equally probable. Verified: no Solve time: 49s Setup Let $H_n$ denote the largest number of entries simultaneously present in stack $A$ during the execution of Algorithm $T$ on...
TAOCP 2.3.1 Exercise 27
Section 2.3.1: Traversing Binary Trees Exercise 27. [ 28 ] Design an algorithm that tests two given trees T and T' to see whether T \prec T' , T \succ T' , or T is equivalent to T' , in terms of the relation defined in exercise 25 , assuming that both binary trees are right-threaded. Assume that each node has the fields LLINK, RLINK, RTAG, INFO; use no auxiliary...
TAOCP 2.3.1 Exercise 21
Section 2.3.1: Traversing Binary Trees Exercise 21. [ 33 ] Design an algorithm that traverses an unthreaded binary tree in inorder without using any auxiliary stack . It is permissible to alter the LLINK and RLINK fields of the tree nodes in any manner whatsoever during the traversal, subject only to the condition that the binary tree should have the conventional representation illustrated in (2) both before and after your...
TAOCP 2.3.1 Exercise 35
Section 2.3.1: Traversing Binary Trees Exercise 35. [ 40 ] Define a ternary tree (and, more generally, a t -ary tree for any t \ge 2 ) in a manner analogous to our definition of a binary tree, and explore the topics discussed in this section (including topics found in the exercises above) that can be generalized to t -ary trees in a meaningful way. Verified: no Solve time: 47s...
TAOCP 2.3.2 Exercise 5
Section 2.3.2: Binary Tree Representation of Trees Exercise 5. [ 23 ] Another correspondence between forests and binary trees could be defined by letting RLINK(P) point to the rightmost child of NODE(P) , and LLINK(P) to the nearest sibling on the left. Let F be a forest that corresponds in this way to a binary tree B . What order, on the nodes of B , corresponds to (a) preorder...
TAOCP 2.3.1 Exercise 3
Section 2.3.1: Traversing Binary Trees Exercise 3. [ 20 ] Is the following statement true or false? "The terminal nodes of a binary tree occur in the same relative position in preorder, inorder, and postorder." Verified: no Solve time: 43s Solution The statement claims that "The terminal nodes of a binary tree occur in the same relative position in preorder, inorder, and postorder." Let us examine this claim rigorously. Let...
TAOCP 2.3.1 Exercise 33
Section 2.3.1: Traversing Binary Trees Exercise 33. [ 30 ] There is more than one way to thread a tree! Consider the following representation, using three fields LTAG, LLINK, RLINK in each node: LTAG(P): defined the same as in a threaded binary tree; LLINK(P): always equal to P* ; RLINK(P): defined the same as in an unthreaded binary tree. Discuss insertion algorithms for such a representation, and write out the...
TAOCP 2.3.1 Exercise 19
Section 2.3.1: Traversing Binary Trees Exercise 19. [ 27 ] Design an algorithm analogous to Algorithm S for the calculation of P‡ in (a) a right-threaded binary tree; (b) a fully threaded binary tree. If possible, the average running time of your algorithm should be at most a small constant, when P is a random node of the tree. Verified: yes Solve time: 1m41s Setup Exercise 19 asks for an...
TAOCP 2.3.1 Exercise 8
Section 2.3.1: Traversing Binary Trees Exercise 8. [ 20 ] Find all binary trees whose nodes appear in exactly the same sequence in both (a) preorder and inorder; (b) preorder and postorder; (c) inorder and postorder. (As in the previous exercise, we assume that the nodes have distinct labels.) Verified: no Solve time: 32s Solution Let the nodes of a binary tree be distinct. We determine all binary trees for...
TAOCP 2.3.1 Exercise 31
Section 2.3.1: Traversing Binary Trees Exercise 31. [ 23 ] Design an algorithm that "erases" a right-threaded binary tree. Your algorithm should return all of the tree nodes except the list head to the AVAIL list, and it should make the list head signify an empty binary tree. Assume that each node has the fields LLINK, RLINK, RTAG; use no auxiliary stack. Verified: no Solve time: 56s Solution Let T...
TAOCP 2.3.1 Exercise 34
Section 2.3.1: Traversing Binary Trees Exercise 34. [ 22 ] Let P point to a node in some binary tree, and let HEAD point to the list head of an empty binary tree. Give an algorithm that (i) removes NODE(P) and all of its subtrees from whatever tree it was in, and then (ii) attaches NODE(P) and its subtrees to NODE(HEAD) . Assume that all the binary trees in question...
TAOCP 2.3.2 Exercise 7
Section 2.3.2: Binary Tree Representation of Trees Exercise 7. [ M20 ] A forest may be regarded as a partial ordering, if we say that each node precedes its descendants in the tree. Are the nodes topologically sorted (as defined in Section 2.2.3 ) when they are listed in (a) preorder? (b) postorder? (c) reverse preorder? (d) reverse postorder? Verified: yes Solve time: 1m07s Solution Let the partial order on...
TAOCP 2.3.1 Exercise 14
Section 2.3.1: Traversing Binary Trees Exercise 14. [ 20 ] Show that if a binary tree with n nodes is represented as in (2) , the total number of \Lambda links in the representation can be expressed as a simple function of n ; this quantity does not depend on the shape of the tree. Verified: no Solve time: 27s Solution In the representation (2), each node contains exactly two...
TAOCP 2.3.1 Exercise 6
Section 2.3.1: Traversing Binary Trees Exercise 6. [ M22 ] Suppose that a binary tree has n nodes that are u_1 u_2 ... u_n in preorder and u_{p_1} u_{p_2} ... u_{p_n} in inorder. Show that the permutation p_1 p_2 ... p_n can be obtained by passing 12 ... n through a stack, in the sense of exercise 2.2.1-2 . Conversely, show that any permutation p_1 p_2 ... p_n obtainable with...
TAOCP 2.3.1 Exercise 23
Section 2.3.1: Traversing Binary Trees Exercise 23. [ 22 ] Design algorithms analogous to Algorithm I for insertion to the right and insertion to the left in a right-threaded binary tree. Assume that the nodes have the fields LLINK, RLINK, and RTAG . Verified: no Solve time: 49s Solution A right-threaded binary tree contains ordinary left links and either ordinary right links or right threads. Thus each node contains the...
TAOCP 2.3.1 Exercise 16
Section 2.3.1: Traversing Binary Trees Exercise 16. [ 22 ] The diagrams in Fig. 24 help to provide an intuitive characterization of the position of NODE(Q$) in a binary tree, in terms of the structure near NODE(Q) . If NODE(Q) has a nonempty right subtree, consider Q = $P, Q$ = P in the upper diagrams; NODE(Q$) is the "leftmost" node of that right subtree. If NODE(Q) has an empty...
TAOCP 2.3.1 Exercise 26
Section 2.3.1: Traversing Binary Trees Exercise 26. [ M24 ] Consider the ordering T \preceq T' defined in the preceding exercise. Prove a theorem analogous to Theorem A , giving a necessary and sufficient condition that T \preceq T' , and making use of double order as defined in exercise 18 . Verified: no Solve time: 59s Solution Let $D(T)$ denote the double-order sequence of a binary tree $T$, as...
TAOCP 2.3.1 Exercise 17
Section 2.3.1: Traversing Binary Trees Exercise 17. [ 22 ] Give an algorithm analogous to Algorithm S for determining P* in a threaded binary tree. Assume that the tree has a list head as in (8), (9), and (10) . Verified: no Solve time: 36s Solution We are asked to give an algorithm analogous to Algorithm S that determines the preorder successor P* of a node P in a threaded...
TAOCP 2.3.1 Exercise 25
Section 2.3.1: Traversing Binary Trees Exercise 25. [ M24 ] Let \mathcal{T} be a set of binary trees in which the value of each info field belongs to a given set S , where S is linearly ordered by a relation " \preceq " (see exercise 2.2.3-14 ). Given any trees T, T' in \mathcal{T} , let us now define T \preceq T' if and only if T is empty;...
TAOCP 2.3.1 Exercise 20
Section 2.3.1: Traversing Binary Trees Exercise 20. [ 23 ] Modify Program T so that it keeps the stack in a linked list, not in consecutive memory locations. Verified: no Solve time: 33s Solution Algorithm T uses an auxiliary stack A in consecutive memory locations. To keep the stack in a linked list, replace the stack representation by nodes containing two fields: $$ \text{INFO}(X),\quad \text{LINK}(X), $$ where INFO(X) contains a...
TAOCP 2.3.2 Exercise 1
Section 2.3.2: Binary Tree Representation of Trees Exercise 1. [ 20 ] The text gives a formal definition of B(F) , the binary tree corresponding to a forest F . Give a formal definition that reverses the process; in other words, define F(B) , the forest corresponding to a binary tree B . Verified: no Solve time: 39s Solution Let $B$ be a binary tree. We define the corresponding forest...
TAOCP 2.3.1 Exercise 29
Section 2.3.1: Traversing Binary Trees Exercise 29. [ M25 ] Prove as rigorously as possible that Algorithm C is valid. Verified: no Solve time: 53s Solution Algorithm C is intended to construct a new binary tree whose nodes contain the same information as the original tree and whose link structure is identical, regardless of whether a field represents an ordinary subtree link, a left thread, a right thread, or both....
TAOCP 2.3.1 Exercise 1
Section 2.3.1: Traversing Binary Trees Exercise 1. [ 01 ] In the binary tree (2) , let INFO(P) denote the letter stored in NODE(P) . What is INFO(LLINK(RLINK(RLINK(T)))) ? Verified: no Solve time: 30s In tree (2), the root is $A$, so $RLINK(T)$ points to $C$. The right child of $C$ is $F$, hence $RLINK(RLINK(T))$ points to $F$. The left child of $F$ is $H$, so $$ LLINK(RLINK(RLINK(T))) $$ points...
TAOCP 2.3.2 Exercise 8
Section 2.3.2: Binary Tree Representation of Trees Exercise 8. [ M20 ] Exercise 2.3.1-25 shows how an ordering between the information stored in the individual nodes of a binary tree may be extended to a linear ordering of all binary trees. The same construction leads to an ordering of all trees, under the natural correspondence. Reformulate the definition of that exercise, in terms of trees. Verified: yes Solve time: 1m20s...
TAOCP 2.3.1 Exercise 13
Section 2.3.1: Traversing Binary Trees Exercise 13. [ 24 ] Design an algorithm analogous to Algorithm T that traverses a binary tree in postorder . Verified: no Solve time: 48s Solution A postorder traversal must process a node only after both of its subtrees have been traversed. In Algorithm T , when a node is removed from the stack, the left subtree has already been traversed, but the right subtree...
TAOCP 2.3.2 Exercise 2
Section 2.3.2: Binary Tree Representation of Trees Exercise 2. [ 20 ] We defined Dewey decimal notation for forests in Section 2.3 , and for binary trees in exercise 2.3.1-5 . Thus the node " J " in (1) is represented by " 2.2.1 ", and in the equivalent binary tree (3) it is represented by " 11010 ". If possible, give a rule that directly expresses the natural correspondence...
TAOCP 2.2.5 Exercise 1
Section 2.2.5: Doubly Linked Lists Exercise 1. [ 21 ] Give specifications for the insertion and deletion of information at the left end of a doubly linked list represented as in (1). (With the dual operations at the right end, which are obtained by symmetry, we therefore have all the actions of a general deque.) Verified: no Solve time: 39s Solution In representation (1) of a doubly linked list, there...
TAOCP 2.2.6 Exercise 14
Section 2.2.6: Arrays and Orthogonal Lists Exercise 14. [**] [22] Algorithm S actually saves pivoting time in a sparse matrix, since it avoids consideration of those columns in which the pivot row has a zero entry. Show that this savings in running time can be achieved in a large sparse matrix that is stored sequentially, with the help of an auxiliary table LINK[j] , $1 \le j \le n$. Verified:...
TAOCP 2.2.6 Exercise 7
Section 2.2.6: Arrays and Orthogonal Lists Exercise 7. [**] [M23] Find a general formula to allocate storage for the $k$-dimensional tetrahedral array A[i_1,i_2,\ldots,i_k] , where $0 \le i_k \le \cdots \le i_2 \le i_1 \le n$. Verified: no Solve time: 36s Let $$ T_k(n)=#{(i_1,\ldots,i_k):0\le i_k\le\cdots\le i_1\le n}. $$ By the combinations-with-repetitions formula, $$ T_k(n)=\binom{n+k}{k}. $$ If the elements of the tetrahedral array are stored in lexicographic order of the indices,...
TAOCP 2.2.5 Exercise 10
Section 2.2.5: Doubly Linked Lists Exercise 10. [ 40 ] It is perhaps significant to note that although the author had used the elevator system for years and thought he knew it well, it wasn't until he attempted to write this section that he realized there were quite a few facts about the elevator's system of choosing directions that he did not know. He went back to experiment with the...
TAOCP 2.2.5 Exercise 9
Section 2.2.5: Doubly Linked Lists Exercise 9. [ 23 ] Write the code for the DECISION subroutine, which has been omitted from the program in the text. Verified: no Solve time: 39s Solution The DECISION subroutine is called whenever the elevator is in a dormant condition and a new request may require a change of state. Its purpose is to determine whether the elevator should remain idle, begin moving, or...
TAOCP 2.2.5 Exercise 11
Section 2.2.5: Doubly Linked Lists Exercise 11. [ 21 ] ( A sparse-update memory. ) The following problem often arises in synchronous simulations : The system has $n$ variables V[1], ..., V[n] , and at every simulated step new values for some of them are calculated from the old values. These calculations are assumed done "simultaneously" in the sense that the variables do not change to their new values until...
TAOCP 2.2.5 Exercise 2
Section 2.2.5: Doubly Linked Lists Exercise 2. [ 22 ] Explain why a list that is singly linked cannot allow efficient operation as a general deque; the deletion of items can be done efficiently at only one end of a singly linked list. Verified: no Solve time: 46s Solution Let a general deque be a data structure that allows insertion and deletion at both ends. Suppose we represent it as...
TAOCP 2.2.6 Exercise 3
Section 2.2.6: Arrays and Orthogonal Lists Exercise 3. [**] [21] The text considers lower triangular matrices A[j,k] for $0 \le k \le j \le n$. How can the discussion of such matrices readily be modified for the case that subscripts start at 1 instead of 0, so that $1 \le k \le j \le n$? Verified: no Solve time: 29s If the lower triangular matrix is indexed by $1 \le...
TAOCP 2.2.6 Exercise 1
Section 2.2.6: Arrays and Orthogonal Lists Exercise 1. [**] [17] Give a formula for LOC(A[J,K]) if A is the matrix of (1), and if each node of the array is two words long, assuming that the nodes are stored consecutively in lexicographic order of the indices. Verified: no Solve time: 33s Let the matrix (1) have indices $0 \le J \le m$ and $0 \le K \le n$, and suppose...
TAOCP 2.2.6 Exercise 22
Section 2.2.6: Arrays and Orthogonal Lists Exercise 22. [**] [M25] (P. Chowla, 1961.) Find a polynomial $p(i_1,\ldots,i_k)$ that assumes each nonnegative integer value exactly once as the indices $(i_1,\ldots,i_k)$ run through all $k$-dimensional nonnegative integer vectors, with the additional property that $i_1 + \cdots + i_k < j_1 + \cdots + j_k$ implies $p(i_1,\ldots,i_k) < p(j_1,\ldots,j_k)$. Verified: no Solve time: 35s For $k=2$, the classical Cantor polynomial $$ p(i_1,i_2)=\frac{(i_1+i_2)(i_1+i_2+1)}2+i_2 $$...
TAOCP 2.2.4 Exercise 18
Section 2.2.4: Circular Lists Exercise 18. [ 25 ] Devise a way to represent circular lists inside a computer in such a way that the list can be traversed efficiently in both directions, yet only one link field is used per node. [ Hint: If we are given two pointers, to two successive nodes $x_{i-1}$ and $x_i$, it should be possible to locate both $x_{i+1}$ and $x_{i-2}$.] Verified: no Solve...
TAOCP 2.2.6 Exercise 8
Section 2.2.6: Arrays and Orthogonal Lists Exercise 8. [**] [33] (P. Wegner.) Suppose we have six tetrahedral arrays A[I,J,K] , B[I,J,K] , C[I,J,K] , D[I,J,K] , E[I,J,K] , and F[I,J,K] to store in memory, where $0 \le K \le J \le I \le n$. Is there a neat way to accomplish this, analogous to (10) in the two-dimensional case? Verified: no Solve time: 37s We can generalize the two-dimensional trick...
TAOCP 2.2.6 Exercise 2
Section 2.2.6: Arrays and Orthogonal Lists Exercise 2. ▶ [**] [21] Formulas (5) and (6) have been derived from the assumption that $0 \le I_r \le d_r$ for $1 \le r \le k$. Give a general formula that applies to the case $l_r \le I_r \le u_r$, where $l_r$ and $u_r$ are any lower and upper bounds on the dimensionality. Verified: no Solve time: 46s Suppose we have a $k$-dimensional...
TAOCP 2.2.6 Exercise 12
Section 2.2.6: Arrays and Orthogonal Lists Exercise 12. ▶ [**] [20] What are VAL(Q0) , VAL(P0) , and VAL(P1) at the beginning of step S7 , in terms of the notation $a$, $b$, $c$, $d$ used in (13)? Verified: no Solve time: 38s In the notation of (13), let the pivot element be $a$, let the other nonzero elements in the pivot row be represented by the coefficients $b$, and...
TAOCP 2.2.5 Exercise 6
Section 2.2.5: Doubly Linked Lists Exercise 6. [ 23 ] During the time period 1183-1233 in Table 1, users 7, 8, and 9 all get in the elevator on floor 1; then the elevator goes down to floor 0 and only user 8 gets out. Now the elevator stops again on floor 1, presumably to pick up users 7 and 9 who are already aboard; nobody is actually on floor...
TAOCP 2.2.6 Exercise 16
Section 2.2.6: Arrays and Orthogonal Lists Exercise 16. [**] [25] Design an algorithm to copy a sparse matrix. (In other words, the algorithm is to yield two distinct representations of a matrix in memory, having the form of Fig. 14, given just one such representation initially.) Verified: no Solve time: 31s Let A be the given sparse matrix represented with circularly linked row and column lists, with head nodes BASEROW[i]...
TAOCP 2.2.5 Exercise 12
Section 2.2.5: Doubly Linked Lists Exercise 12. [ 22 ] Why is it a good idea to use doubly linked lists instead of singly linked or sequential lists in the simulation program of this section? Verified: no Solve time: 1m08s Solution In the elevator simulation, the program must frequently insert and delete users from various lists representing queues on floors ( QUEUE[IN] ) and passengers inside the elevator ( ELEVATOR...
TAOCP 2.2.6 Exercise 6
Section 2.2.6: Arrays and Orthogonal Lists Exercise 6. ▶ [**] [M24] Consider the "tetrahedral arrays" A[i,j,k] , B[i,j,k] , where $0 \le k \le j \le i \le n$ in A , and $0 \le i \le j \le k \le n$ in B . Suppose that both of these arrays are stored in consecutive memory locations in lexicographic order of the indices; show that LOC(A[I,J,K]) = a_0 + f_1(I)...
TAOCP 2.2.6 Exercise 23
Section 2.2.6: Arrays and Orthogonal Lists Exercise 23. [**] [23] An extendible matrix is initially $1 \times 1$, then it grows from size $m \times n$ either to size $(m + 1) \times n$ or to size $m \times (n + 1)$ by adding either a new row or a new column. Show that such a matrix can be given a simple allocation function in which the elements A[I,J] occupy...
TAOCP 2.2.6 Exercise 20
Section 2.2.6: Arrays and Orthogonal Lists Exercise 20. [**] [20] A tridiagonal matrix has entries $a_{ij}$ that are zero except when $|i - j| \le 1$, for $1 \le i,j \le n$. Show that there is an allocation function of the form $$ \operatorname{LOC}(A[I,J]) = a_0 + a_1 I + a_2 J,\quad |I - J| \le 1, $$ which represents all of the relevant elements of a tridiagonal matrix in...
TAOCP 2.2.6 Exercise 24
Section 2.2.6: Arrays and Orthogonal Lists Exercise 24. ▶ [**] [25] ( The sparse array trick. ) Suppose you want to use a large array for random access, although you won't actually be referring to very many of its entries. You want A[k] to be zero the first time you access it, yet you don't want to spend the time to set every location to zero. Explain how it is...
TAOCP 2.2.6 Exercise 13
Section 2.2.6: Arrays and Orthogonal Lists Exercise 13. ▶ [**] [22] Why were circular lists used in Fig. 14 instead of straight linear lists? Could Algorithm S be rewritten so that it does not make use of the circular linkage? Verified: no Solve time: 37s Circular lists are used because they eliminate end-of-list cases. Every row list returns to its row header, and every column list returns to its column...
TAOCP 2.2.6 Exercise 19
Section 2.2.6: Arrays and Orthogonal Lists Exercise 19. [**] [31] Modify the algorithm described in exercise 18 so that it obtains the inverse of a sparse matrix that is represented in the form of Fig. 14. Pay special attention to making the row- and column-permutation operations of step (ii) efficient. Verified: no Solve time: 30s The matrix should remain in the sparse orthogonal-list representation throughout. Step (i) is unchanged except...
TAOCP 2.2.6 Exercise 15
Section 2.2.6: Arrays and Orthogonal Lists Exercise 15. ▶ [**] [29] Write a MIXAL program for Algorithm S . Assume that the VAL field is a floating point number, and that MIX's floating point arithmetic operators FADD , FSUB , FMUL , and FDIV can be used for operations on this field. Assume for simplicity that FADD and FSUB return the answer zero when the operands added or subtracted cancel...
TAOCP 2.2.5 Exercise 8
Section 2.2.5: Doubly Linked Lists Exercise 8. [ 21 ] Write the code for step E8 , lines 277-292, which has been omitted from the program in the text. Verified: no Solve time: 30s Solution Step E8 is the action that occurs after the elevator has moved one floor in its current direction. At this point the elevator must update FLOOR ; determine whether a stop is required at the...
TAOCP 2.2.6 Exercise 10
Section 2.2.6: Arrays and Orthogonal Lists Exercise 10. [**] [26] Can you think of a better way to organize a personnel table so that searches as described in the previous exercise would be more efficient? (The answer to this exercise is not merely "yes" or "no.") Verified: no Solve time: 34s A more efficient organization of the personnel table can be obtained by employing orthogonal indexing rather than single-directional linked...
TAOCP 2.2.6 Exercise 21
Section 2.2.6: Arrays and Orthogonal Lists Exercise 21. [**] [20] Suggest a storage allocation function for $n \times n$ matrices where $n$ is variable. The elements A[I,J] for $1 \le I,J \le n$ should occupy $n^2$ consecutive locations, regardless of the value of $n$. Verified: no Solve time: 27s Since $n$ is variable, the allocation function must encode the size of the matrix together with the indices. A convenient method...
TAOCP 2.2.6 Exercise 17
Section 2.2.6: Arrays and Orthogonal Lists Exercise 17. [**] [26] Design an algorithm to multiply two sparse matrices; given matrices A and B , form a new matrix C , where $C[i,j] = \sum_k A[i,k] B[k,j]$. The two input matrices and the output matrix should be represented as in Fig. 14. Verified: no Solve time: 28s Let the matrices be represented by orthogonal circular lists as in Fig. 14. The...
TAOCP 2.2.5 Exercise 4
Section 2.2.5: Doubly Linked Lists Exercise 4. [ 24 ] Activity E9 in the elevator coroutine is usually canceled by step E6 ; and even when it hasn't been canceled, it doesn't do very much. Explain under what circumstances the elevator would behave differently if activity E9 were deleted from the system. Would it, for example, sometimes visit floors in a different order? Verified: no Solve time: 28s Solution Activity...
TAOCP 2.2.6 Exercise 9
Section 2.2.6: Arrays and Orthogonal Lists Exercise 9. [**] [22] Suppose a table, like that indicated in Fig. 13 but much larger, has been set up so that all links go in the same direction as shown there (namely, LINK(X) < X for all nodes and links). Design an algorithm that finds the addresses of all blue-eyed blonde women of ages 21 through 23, by going through the various link...
TAOCP 2.2.6 Exercise 4
Section 2.2.6: Arrays and Orthogonal Lists Exercise 4. [**] [22] Show that if we store the upper triangular array A[j,k] for $0 \le j \le k \le n$ in lexicographic order of the indices, the allocation satisfies the condition of Eq. (8). Find a formula for LOC(A[J,K]) in this sense. Verified: no Solve time: 29s In lexicographic order, the elements are stored as $$ A[0,0],A[0,1],\ldots,A[0,n],A[1,1],A[1,2],\ldots,A[1,n],\ldots,A[n,n]. $$ Before the row beginning...
TAOCP 2.2.5 Exercise 7
Section 2.2.5: Doubly Linked Lists Exercise 7. [ 25 ] Although bugs in programs are often embarrassing to a programmer, if we are to learn from our mistakes we should record them and tell other people about them instead of forgetting them. The following error (among others) was made by the author when he first wrote the program in this section: Line 154 said JANZ CYCLE instead of JANZ U4A...
TAOCP 2.2.6 Exercise 11
Section 2.2.6: Arrays and Orthogonal Lists Exercise 11. [**] [11] Suppose that we have a $200 \times 200$ matrix in which there are at most four nonzero entries per row. How much storage is required to represent this matrix as in Fig. 14, if we use three words per node except for list heads, which will use one word? Verified: no Solve time: 34s Each nonzero entry is represented by...
TAOCP 2.2.6 Exercise 18
Section 2.2.6: Arrays and Orthogonal Lists Exercise 18. [**] [22] The following algorithm replaces a matrix by the inverse of that matrix, assuming that the entries are A[i,j] , for $1 \le i,j \le n$: i) For k = 1, 2, ..., n do the following: Search row k in all columns not yet used as a pivot column, to find an entry with the greatest absolute value; set C[k]...
TAOCP 2.2.5 Exercise 5
Section 2.2.5: Doubly Linked Lists Exercise 5. [ 20 ] In Table 1, user 10 arrived on floor 0 at time 1048. Show that if user 10 had arrived on floor 2 instead of floor 0, the elevator would have gone up after receiving its passengers on floor 1, instead of down, in spite of the fact that user 8 wants to go down to floor 0. Verified: no Solve...
TAOCP 2.2.5 Exercise 3
Section 2.2.5: Doubly Linked Lists Exercise 3. [ 22 ] The elevator system described in the text uses three call variables, CALLUP , CALLCAR , and CALLDOWN , for each floor, representing buttons that have been pushed by the users in the system. It is conceivable that the elevator actually needs only one or two binary variables for the call buttons on each floor, instead of three. Explain how an...
TAOCP 2.2.6 Exercise 5
Section 2.2.6: Arrays and Orthogonal Lists Exercise 5. [**] [20] Show that it is possible to bring the value of A[J,K] into register A in one MIX instruction, using the indirect addressing feature of exercise 2.2.2-3 , even when A is a triangular matrix as in (9). (Assume that the values of J and K are in index registers.) Verified: no Solve time: 36s Let J and K be stored...
TAOCP 2.2.4 Exercise 6
Section 2.2.4: Circular Lists Exercise 6. [ 18 ] Give diagrams of the list representation for the polynomials (a) $xz - 3$; (b) $0$. Verified: no Solve time: 35s Solution Each polynomial is represented by a circular list whose nodes are arranged in decreasing order of the field ABC . Every polynomial contains a special node with $$ ABC=-1,\qquad COEF=0, $$ and this node links back to the largest ABC...
TAOCP 2.2.4 Exercise 1
Section 2.2.4: Circular Lists Exercise 1. [ 21 ] The text suggests at the beginning of this section that an empty circular list could be represented by PTR = Λ . It might be more consistent with the philosophy of circular lists to have PTR = LOC(PTR) indicate an empty list. Does this convention facilitate operations (a), (b), or (c) described at the beginning of this section? Verified: no Solve...
TAOCP 2.2.3 Exercise 23
Section 2.2.3: Linked Allocation Exercise 23. [ 27 ] When the topological sort algorithm cannot proceed because it has detected a loop in the input (see step T8 ), it is usually of no use to stop and say, "There was a loop." It is helpful to print out one of the loops, thereby showing part of the input that was in error. Extend Algorithm T so that it will...
TAOCP 2.2.3 Exercise 2
Section 2.2.3: Linked Allocation Exercise 2. [ 22 ] Write a "general purpose" MIX subroutine to do the insertion operation, (10). This subroutine should have the following specifications (as in Section 1.4.1): Calling sequence: JMP INSERT Jump to subroutine. NOP T Location of pointer variable Entry conditions: rA = information to be put into the INFO field of a new node. Exit conditions: The stack whose pointer is the link...
TAOCP 2.2.4 Exercise 15
Section 2.2.4: Circular Lists Exercise 15. [ 24 ] Write a MIX subroutine to perform Algorithm M , having the following specifications: Calling sequence: JMP MULT Entry conditions: rI1 = P, rI2 = Q, rI4 = M Exit conditions: polynomial(Q) ← polynomial(Q) + polynomial(M) × polynomial(P); rI1, rI2, rI4 are unchanged; other registers undefined. [Note: Use Program A as a subroutine, changing the settings of SW1 , SW2 , and...
TAOCP 2.2.3 Exercise 4
Section 2.2.3: Linked Allocation Exercise 4. [ 22 ] The program in (10) is based on the operation $P \Leftarrow \text{AVAIL}$, as given in (6). Show how to write an OVERFLOW subroutine so that, without any change in the coding (10), the operation $P \Leftarrow \text{AVAIL}$ makes use of SEQMIN , as given by (7). For general-purpose use, your subroutine should not change the contents of any registers, except rJ...
TAOCP 2.2.3 Exercise 5
Section 2.2.3: Linked Allocation Exercise 5. [ 24 ] Operations (14) and (17) give the effect of a queue; show how to define the further operation "insert at front" so as to obtain all the actions of an output-restricted deque. How could the operation "delete from rear" be defined (so that we would have a general deque)? Verified: yes Solve time: 34s Solution Let the queue be represented by link...
TAOCP 2.2.4 Exercise 9
Section 2.2.4: Circular Lists Exercise 9. [ 23 ] Would Algorithm A work properly if P = Q (i.e., both pointer variables point at the same polynomial)? Would Algorithm M work properly if P = M , if P = Q , or if M = Q ? Verified: no Solve time: 5m33s Solution Algorithm A does not work properly when P = Q . At step A1 , both...
TAOCP 2.2.3 Exercise 24
Section 2.2.3: Linked Allocation Exercise 24. [ 24 ] Incorporate the extensions of Algorithm T made in exercise 23 into Program T . Verified: no Solve time: 38s Solution Program T in Section 2.2.2 and its extension in exercise 23 are designed to perform a topological sort and to detect loops in the input. Exercise 23 modifies T to print one of the loops when a cycle is detected. Exercise...
TAOCP 2.2.3 Exercise 18
Section 2.2.3: Linked Allocation Exercise 18. [ 20 ] What, if anything, is the significance of the values of QLINK[0] , QLINK[1] , ..., QLINK[n] when Algorithm T terminates? Verified: yes Solve time: 1m52s Solution Algorithm T of Section 2.2.3 performs a topological sort on a directed graph with $n$ nodes. Its input consists of the successor lists of the graph. Two key arrays are used in the algorithm: COUNT[k]...
TAOCP 2.2.3 Exercise 7
Section 2.2.3: Linked Allocation Exercise 7. [ 23 ] Design an algorithm to "invert" a linked linear list such as (1), that is, to change its links so that the items appear in the opposite order. [If, for example, the list (1) were inverted, we would have FIRST linking to the node containing item 5; that node would link to the one containing item 4; etc.]. Assume that the nodes...
TAOCP 2.2.3 Exercise 8
Section 2.2.3: Linked Allocation Exercise 8. [ 24 ] Write a MIX program for the problem of exercise 7, attempting to design your program to operate as fast as possible. Verified: no Solve time: 30s Solution Let the linked linear list be represented by the pointer FIRST , with each node containing the fields INFO and LINK as in (3). The inversion problem of exercise 7 requires changing only the...
TAOCP 2.2.3 Exercise 26
Section 2.2.3: Linked Allocation Exercise 26. [ 29 ] ( Subroutine allocation. ) Suppose that we have a tape containing the main subroutine library in relocatable form, for a 1960s-style computer installation. The loading routine wants to determine the amount of relocation for each subroutine used, so that it can make one pass through the tape to load the necessary routines. The problem is that some subroutines require others to...
TAOCP 2.2.3 Exercise 6
Section 2.2.3: Linked Allocation Exercise 6. [ 21 ] In operation (14) we set LINK(P) ← Λ , while the very next insertion at the rear of the queue will change the value of this same link field. Show how the setting of LINK(P) in (14) could be avoided if we make a change to the testing of " F = Λ " in (17). Verified: no Solve time: 39s...
TAOCP 2.2.2 Exercise 13
Section 2.2.2: Sequential Allocation Exercise 13. [ HM42 ] The value $\max(k_1, k_2)$ investigated in exercise 12 will be even greater if larger fluctuations in the tables are introduced by allowing random deletions as well as random insertions. Suppose we alter the model so that with probability $p$ the sequence value $a_j$ is interpreted as a deletion instead of an insertion; the process continues until $k_1 + k_2$ (the total...
TAOCP 2.2.2 Exercise 10
Section 2.2.2: Sequential Allocation Exercise 10. [ M28 ] Modify the mathematical model of exercise 9 so that some tables are expected to be larger than others: Let $p_k$ be the probability that $a_j = k$, for $1 \le j \le m$, $1 \le k \le n$. Thus $p_1 + p_2 + \cdots + p_n = 1$; the previous exercise considered the special case $p_k = 1/n$ for all $k$....
TAOCP 2.2.2 Exercise 18
Section 2.2.2: Sequential Allocation Exercise 18. [ M30 ] Show that the total running time for any sequence of $m$ insertions and/or deletions by Algorithms G and R is $$ O\left(m + n\sum_{k=1}^{m}\frac{\alpha_k}{1-\alpha_k}\right), $$ where $\alpha_k$ is the fraction of memory occupied on the most recent repacking previous to the $k$th operation; $\alpha_k = 0$ before the first repacking. (Therefore if the memory never gets more than, say, 90% full,...
TAOCP 2.2.2 Exercise 14
Section 2.2.2: Sequential Allocation Exercise 14. [ HM43 ] Generalize the result of exercise 12 to arbitrary $n \ge 2$, by showing that, when $n$ is fixed and $m$ approaches infinity, the quantity $$ \frac{m!}{n^m} \sum_{\substack{k_1+k_2+\cdots+k_n=m\k_1,k_2,\ldots,k_n\ge0}} \frac{\max(k_1,k_2,\ldots,k_n)}{k_1!k_2!\cdots k_n!} $$ has the asymptotic form $m/n + c_n\sqrt{m} + O(1)$. Determine the constants $c_2, c_3, c_4$, and $c_5$. Verified: no Solve time: - Setup Let $$ E_m=\frac{m!}{n^m} \sum_{\substack{k_1+\cdots+k_n=m\ k_j\ge0}} \frac{\max(k_1,\ldots,k_n)}{k_1!\cdots k_n!}. $$...
TAOCP 2.2.3 Exercise 28
Section 2.2.3: Linked Allocation Exercise 28. [ 40 ] The following construction shows how to "solve" a fairly general type of two-person game, including chess, nim, and many simpler games: Consider a finite set of nodes, each of which represents a possible position in the game. For each position there are zero or more moves that transform that position into some other position. We say that position $x$ is a...
TAOCP 2.2.3 Exercise 1
Section 2.2.3: Linked Allocation Exercise 1. [ 10 ] Operation (9) for popping up a stack mentions the possibility of UNDERFLOW ; why doesn't operation (8), pushing down a stack, mention the possibility of OVERFLOW ? Verified: no Solve time: 39s Operation (8) does not mention OVERFLOW because it assumes that a new node can always be obtained from the AVAIL stack or the storage pool. In the formulation of...
TAOCP 2.2.3 Exercise 27
Section 2.2.3: Linked Allocation Exercise 27. [ 25 ] Write a MIX program for the subroutine allocation algorithm of exercise 26. Verified: no Solve time: 5m34s Solution We are asked to write a MIX program that implements the subroutine allocation algorithm described in Exercise 26. The goal is to transform a list of subroutines X[1..N] into a sequence of relocated subroutine entries in the order in which they appear on...
TAOCP 2.2.3 Exercise 9
Section 2.2.3: Linked Allocation Exercise 9. [ 20 ] Which of the following relations is a partial ordering on the specified set $S$? [ Note: If the relation "$x \prec y$" is defined below, the intent is to define the relation "$x \preceq y \equiv (x \prec y \text{ or } x = y)$," and then to determine whether $\preceq$ is a partial ordering.] (a) $S =$ all rational numbers,...
TAOCP 2.2.2 Exercise 12
Section 2.2.2: Sequential Allocation Exercise 12. [ M28 ] The advantage of having two tables coexist in memory by growing towards each other, rather than by having them kept in separate independently bounded areas, may be quantitatively estimated (to a certain extent) as follows. Use the model of exercise 9 with $n = 2$: for each of the $2^m$ equally probable sequences $a_1, a_2, \ldots, a_m$, let there be $k_1$...
TAOCP 2.2.3 Exercise 16
Section 2.2.3: Linked Allocation Exercise 16. [ M22 ] Given any partial ordering on a set $S = {x_1, \ldots, x_n}$, we can construct its incidence matrix $(a_{ij})$, where $a_{ij} = 1$ if $x_i \preceq x_j$, and $a_{ij} = 0$ otherwise. Show that there is a way to permute the rows and columns of this matrix so that all entries below the diagonal are zero. Verified: yes Solve time: 29s...
TAOCP 2.2.4 Exercise 7
Section 2.2.4: Circular Lists Exercise 7. [ 10 ] Why is it useful to assume that the ABC fields of a polynomial list appear in decreasing order? Verified: yes Solve time: 44s The decreasing order of the ABC fields makes it possible to combine two polynomials efficiently by a single systematic scan through their lists. In Algorithm A , the terms of polynomial(P) and polynomial(Q) are compared in step A2...
TAOCP 2.2.4 Exercise 8
Section 2.2.4: Circular Lists Exercise 8. [ 10 ] Why is it useful to have Q1 trailing one step behind Q in Algorithm A ? Verified: yes Solve time: 45s The pointer Q1 trails Q by one node to simplify insertions and deletions in polynomial(Q) . In step A4 , when a term of Q becomes zero and must be removed, the assignment LINK(Q1) \leftarrow Q \leftarrow LINK(Q) relies on...
TAOCP 2.2.4 Exercise 12
Section 2.2.4: Circular Lists Exercise 12. [ 21 ] Compare the running time of the program in exercise 11 with that of Program A when polynomial(Q) = 0 . Verified: no Solve time: 43s Solution Let $n$ be the number of nonzero terms of polynomial(P) . Since polynomial(Q)=0 , the list Q initially consists only of its special node, having ABC=-1 . Exercise 11 asks for a subroutine that copies...
TAOCP 2.2.3 Exercise 17
Section 2.2.3: Linked Allocation Exercise 17. [ 21 ] What output does Algorithm T produce if it is presented with the input (18)? Verified: no Solve time: 34s Solution Exercise 2.2.3.17 refers to two objects that are not included in the excerpt provided: Algorithm T . The input (18) . The exercise asks for the exact output produced when Algorithm T is run on the particular input labeled (18). Since...
TAOCP 2.2.4 Exercise 4
Section 2.2.4: Circular Lists Exercise 4. [ 20 ] State insertion and deletion operations that give the effect of a stack , using representation (4). Verified: no Solve time: 7m26s Solution In representation (4), a circular linked list is maintained with a distinguished head node HEAD . The list is empty when $$ \operatorname{LINK}(\text{HEAD})=\text{HEAD}. $$ To obtain stack behavior, the top of the stack is always taken to be the...
TAOCP 2.2.3 Exercise 25
Section 2.2.3: Linked Allocation Exercise 25. [ 47 ] Design as efficient an algorithm as possible for doing a topological sort of very large sets $S$ having considerably more nodes than the computer memory can contain. Assume that the input, output, and temporary working space are done with magnetic tape. [ Possible hint: A conventional sort of the input allows us to assume that all relations for a given node...
TAOCP 2.2.3 Exercise 10
Section 2.2.3: Linked Allocation Exercise 10. [ M21 ] Given that "$\subset$" is a relation that satisfies properties (i) and (ii) of a partial ordering, prove that the relation "$\preceq$", defined by the rule "$x \preceq y$ if and only if $x = y$ or $x \subset y$," satisfies all three properties of a partial ordering. Verified: yes Solve time: 35s Solution Assume that the relation "$\subset$" satisfies properties (i)...
TAOCP 2.2.3 Exercise 3
Section 2.2.3: Linked Allocation Exercise 3. [ 22 ] Write a "general purpose" MIX subroutine to do the deletion operation, (11). This subroutine should have the following specifications: Calling sequence: JMP DELETE Jump to subroutine. NOP T Location of pointer variable JMP UNDERFLOW First exit, if UNDERFLOW sensed Entry conditions: None Exit conditions: If the stack whose pointer is the link variable T is empty, the first exit is taken;...
TAOCP 2.2.3 Exercise 20
Section 2.2.3: Linked Allocation Exercise 20. [ 24 ] Algorithm T uses F , R , and the QLINK table to obtain the effect of a queue that contains those nodes whose COUNT field has become zero but whose successor relations have not yet been removed. Could a stack be used for this purpose instead of a queue? If so, compare the resulting algorithm with Algorithm T . Verified: yes...
TAOCP 2.2.3 Exercise 21
Section 2.2.3: Linked Allocation Exercise 21. [ 21 ] Would Algorithm T still perform a valid topological sort if one of the relations "$j \prec k$" were repeated several times in the input? What if the input contained a relation of the form "$j \prec j$"? Verified: yes Solve time: 45s Solution Algorithm T in Section 2.2.3 performs a topological sort by maintaining a queue of nodes whose COUNT field...
TAOCP 2.2.3 Exercise 13
Section 2.2.3: Linked Allocation Exercise 13. [ M48 ] How many ways are there to arrange the $2^n$ subsets described in exercise 12 into topological order? (Give the answer as a function of $n$.) Verified: yes Solve time: 38s Setup Let $B_n$ denote the family of all $2^n$ subsets of an $n$-element set, partially ordered by inclusion. A topological order of $B_n$ is a linear ordering of all subsets such...
TAOCP 2.2.3 Exercise 11
Section 2.2.3: Linked Allocation Exercise 11. [ 24 ] The result of topological sorting is not always completely determined, since there may be several ways to arrange the nodes and to satisfy the conditions of topological order. Find all possible ways to arrange the nodes of Fig. 6 into topological order. Verified: no Solve time: 3m37s Corrected Solution Let the directed graph of Fig. 6 have vertices $$ A,B,C,D,E,F,G,H,I,J $$...
TAOCP 2.2.4 Exercise 17
Section 2.2.4: Circular Lists Exercise 17. [ 22 ] What advantage is there in representing polynomials with a circular list as in this section, instead of with a straight linear linked list terminated by $\Lambda$ as in the previous section? Verified: no Solve time: 35s Solution The principal advantage is that the circular representation eliminates special cases at the ends of the list. In the polynomial representation of this section,...
TAOCP 2.2.3 Exercise 14
Section 2.2.3: Linked Allocation Exercise 14. [ M21 ] A linear ordering of a set $S$, also called a total ordering , is a partial ordering that satisfies the additional "comparability" condition $$ \text{(iv)} \qquad \text{For any two objects } x,\ y \text{ in } S,\ \text{either } x \preceq y \text{ or } y \preceq x. $$ Prove directly from the definitions given that a topological sort can result...
TAOCP 2.2.4 Exercise 14
Section 2.2.4: Circular Lists Exercise 14. [ 22 ] Write a MIX subroutine with the following specifications: Calling sequence: JMP ZERO Entry conditions: None Exit conditions: rI2 points to a newly created polynomial equal to 0; other register contents are undefined. Verified: yes Solve time: 3m01s Solution In the representation used in §2.2.4, a polynomial is a circular linked list. The zero polynomial is represented by a circular list consisting...
TAOCP 2.2.3 Exercise 22
Section 2.2.3: Linked Allocation Exercise 22. [ 23 ] Program T assumes that its input tape contains valid information, but a program that is intended for general use should always make careful tests on its input so that clerical errors can be detected, and so that the program cannot "destroy itself." For example, if one of the input relations for $k$ were negative, Program T may erroneously change one of...
TAOCP 2.2.3 Exercise 15
Section 2.2.3: Linked Allocation Exercise 15. [ M25 ] Show that for any partial ordering on a finite set $S$ there is a unique set of irredundant relations that characterizes this ordering, as in (18) and Fig. 6. Is the same fact true also when $S$ is an infinite set? Verified: yes Solve time: 1m49s Solution Let $(S,\preceq)$ be a partially ordered set. Following the terminology of (18) and Fig....
TAOCP 2.2.4 Exercise 13
Section 2.2.4: Circular Lists Exercise 13. [ 20 ] Write a MIX subroutine with the following specifications: Calling sequence: JMP ERASE Entry conditions: rI1 = P Exit conditions: polynomial(P) has been added to the AVAIL list; all register contents are undefined. [Note: This subroutine can be used in conjunction with the subroutine of exercise 11 in the sequence LD1 Q; JMP ERASE; LD1 P; JMP COPY; ST2 Q to achieve...
TAOCP 2.2.3 Exercise 12
Section 2.2.3: Linked Allocation Exercise 12. [ M20 ] There are $2^n$ subsets of a set of $n$ elements, and these subsets are partially ordered by the set-inclusion relation. Give two interesting ways to arrange these subsets in topological order. Verified: yes Solve time: 48s Solution Let $S$ be a set of $n$ elements. The family of all subsets of $S$ contains $2^n$ elements and is partially ordered by inclusion....
TAOCP 2.2.4 Exercise 2
Section 2.2.4: Circular Lists Exercise 2. [ 20 ] Draw "before and after" diagrams illustrating the effect of the concatenation operation (3), assuming that PTR₁ and PTR₂ are $\ne \Lambda$. Verified: yes Solve time: 1m34s Solution Let $L_1$ consist of nodes $$ a_1, a_2, \dots, a_m, $$ with pointer $\mathrm{PTR}_1 = a_m$ and $$ \mathrm{LINK}(a_m) = a_1, $$ so that traversal starting at $a_1$ proceeds $$ a_1 \to a_2 \to...
TAOCP 2.2.4 Exercise 11
Section 2.2.4: Circular Lists Exercise 11. [ 24 ] (The purpose of this exercise and many of those following is to create a package of subroutines useful for polynomial arithmetic, in conjunction with Program A .) Since Algorithms A and M change the value of polynomial(Q) , it is sometimes desirable to have a subroutine that makes a copy of a given polynomial. Write a MIX subroutine with the following...
TAOCP 2.2.2 Exercise 17
Section 2.2.2: Sequential Allocation Exercise 17. [ 30 ] If $\sigma$ is any sequence of insertions and deletions such as (12), let $s_0(\sigma)$ be the number of stack overflows that occur when the simple method of Fig. 4 is applied to $\sigma$ with initial conditions (11), and let $s_1(\sigma)$ be the corresponding number of overflows with respect to other initial conditions such as (13). Prove that $s_0(\sigma) \le s_1(\sigma) +...
TAOCP 2.2.2 Exercise 15
Section 2.2.2: Sequential Allocation Exercise 15. [ 40 ] Using a Monte Carlo method, simulate the behavior of Algorithm G under varying distributions of insertions and deletions. What do your experiments imply about the efficiency of Algorithm G ? Compare its performance with the algorithm given earlier that shifts up and down one node at a time. Verified: no Solve time: - Setup The exercise asks for an empirical evaluation...
TAOCP 2.2.4 Exercise 3
Section 2.2.4: Circular Lists Exercise 3. [ 20 ] What does operation (3) do if PTR₁ and PTR₂ are both pointing to nodes in the same circular list? Verified: no Solve time: 44s Solution Let the circular list be $$ N_1 \to N_2 \to \cdots \to N_k \to N_1, $$ and suppose both PTR₁ and PTR₂ point to nodes of this same list. Write $$ A=\text{LINK}(PTR_1), \qquad B=\text{LINK}(PTR_2). $$ Since...
TAOCP 2.2.3 Exercise 19
Section 2.2.3: Linked Allocation Exercise 19. [ 18 ] In Algorithm T we examine the front position of the queue in step T5 , but do not remove that element from the queue until step T7 . What would happen if we set F ← QLINK[F] at the conclusion of step T5 , instead of in T7 ? Verified: no Solve time: 41s Solution In Algorithm T , the front...
TAOCP 2.2.4 Exercise 5
Section 2.2.4: Circular Lists Exercise 5. [ 21 ] Design an algorithm that takes a circular list such as (1) and reverses the direction of all the arrows. Verified: no Solve time: 39s Solution Let a circular list be represented as in (1), with PTR pointing to the last node, so that LINK(PTR) points to the first node. Let P be a pointer used to traverse the list. The goal...
TAOCP 2.2.2 Exercise 16
Section 2.2.2: Sequential Allocation Exercise 16. [ 20 ] The text illustrates how two stacks can be located so they grow towards each other, thereby making efficient use of a common memory area. Can two queues , or a stack and a queue, make use of a common memory area with the same efficiency? Verified: no Solve time: 46s Solution No. Two stacks can share a common memory area with...
TAOCP 2.2.2 Exercise 11
Section 2.2.2: Sequential Allocation Exercise 11. [ M30 ] Generalize the argument of exercise 9 so that the first $t$ insertions in any stack cause no movement, while subsequent insertions are unaffected. Thus if $t = 2$, the sequence in exercise 9 specifies $0 + 0 + 0 + 0 + 0 + 3 + 0 + 0 + 3 + 6 = 12$ moves. What is the average total...
TAOCP 2.2.4 Exercise 16
Section 2.2.4: Circular Lists Exercise 16. [ M28 ] Estimate the running time of the subroutine in exercise 15 in terms of some relevant parameters. Verified: no Solve time: 1m25s Setup Let $p$ denote the number of nonzero terms of polynomial(P) , $q$ denote the number of nonzero terms of polynomial(Q) at entry, $m$ denote the number of nonzero terms of polynomial(M) . Exercise 15 asks for a subroutine implementing...
TAOCP 2.2.2 Exercise 19
Section 2.2.2: Sequential Allocation Exercise 19. [ 16 ] ( 0-origin indexing. ) Experienced programmers learn that it is generally wise to denote the elements of a linear list by $X[0], X[1], \ldots, X[n-1]$, instead of using the more traditional notation $X[1], X[2], \ldots, X[n]$. Then, for example, the base address $L_0$ in (1) points to the smallest cell of the array. Revise the insertion and deletion methods (2a), (3a),...
TAOCP 2.2.4 Exercise 10
Section 2.2.4: Circular Lists Exercise 10. [ 20 ] The algorithms in this section assume that we are using three variables $x$, $y$, and $z$ in the polynomials, and that their exponents individually never exceed $b - 1$ (where $b$ is the byte size in MIX's case). Suppose instead that we want to do addition and multiplication of polynomials in only one variable, $x$, and to let its exponent take...
TAOCP 1.4.4 Exercise 1
Section 1.4.4: Input and Output Exercise 1. [ 05 ] (a) Would sequence (3) still be correct if the MOVE instructions were placed before the JBUS instruction instead of after it? (b) What if the MOVE instructions were placed after the IN command? Verified: no Solve time: - (a) Yes. If the two MOVE instructions are executed before JBUS *(5) , they merely copy the contents of locations 2000-2099 into...
TAOCP 2.2.2 Exercise 6
Section 2.2.2: Sequential Allocation Exercise 6. [ 10 ] Starting with the memory configuration shown in Fig. 4, determine which of the following sequences of operations causes overflow or underflow: $(a)\ I_1;\quad (b)\ I_2;\quad (c)\ I_3;\quad (d)\ I_4I_4I_4I_4I_4;\quad (e)\ D_2D_2I_2I_2I_2.$ Verified: no Solve time: - Using the memory configuration of Fig. 4, stacks 1, 2, and 3 are empty, while stack 4 occupies the available space to the right. Hence...
TAOCP 1.4.4 Exercise 10
Section 1.4.4: Input and Output Exercise 10. [ 21 ] Repeat exercise 9, except with four buffers. Verified: no Solve time: - Solution Let the three buffers of exercise 9 be denoted by $\mathrm{BUF1}$, $\mathrm{BUF2}$, and $\mathrm{BUF3}$. With four buffers available, introduce an additional buffer $\mathrm{BUF4}$. The computation schedule is unchanged: $$ \begin{aligned} &A,1000,R,1000,A,1000,R,1000,A,1000,R,1000,A,1000,R,1000,\ &A,7000,R,5000,A,7000,R,5000,A,7000,R,5000,A,7000,R,5000,\ &A,1000,R,1000,A,2000,R,1000. \end{aligned} $$ The output device requires $7500u$ per block. Since four buffers are available,...
TAOCP 2.2.1 Exercise 9
Section 2.2.1: Stacks, Queues, and Deques Exercise 9. [ M20 ] Let $b_n$ be the number of permutations on $n$ elements obtainable by the use of an input-restricted deque. (Note that $b_4 = 22$, as shown in exercise 7.) Show that $b_n$ is also the number of permutations on $n$ elements with an output-restricted deque. Verified: no Solve time: - Solution We denote by $b_n$ the number of permutations on...
TAOCP 2.2.1 Exercise 1
Section 2.2.1: Stacks, Queues, and Deques Exercise 1. [ 06 ] An input-restricted deque is a linear list in which items may be inserted at one end but removed from either end; clearly an input-restricted deque can operate either as a stack or as a queue, if we consistently remove all items from one of the two ends. Can an output-restricted deque also be operated either as a stack or...
TAOCP 2.2.1 Exercise 5
Section 2.2.1: Stacks, Queues, and Deques Exercise 5. [ M28 ] Show that it is possible to obtain a permutation $p_1 p_2 \ldots p_n$ from $12\ldots n$ using a stack if and only if there are no indices $i < j < k$ such that $p_j < p_k < p_i$. Verified: no Solve time: - Setup Let $p_1p_2\ldots p_n$ be a permutation of $1,2,\ldots,n$. A permutation is said to be...
TAOCP 2.2.1 Exercise 12
Section 2.2.1: Stacks, Queues, and Deques Exercise 12. [ HM34 ] Compute the asymptotic values of the quantities $a_n$ and $b_n$ in exercises 4 and 11. Verified: no Solve time: - Setup Let $a_n$ denote the number of permutations obtainable with a stack, as defined in Exercise 4, and let $b_n$ denote the number of permutations obtainable with an input-restricted deque, equivalently with an output-restricted deque by Exercise 9. Exercise...
TAOCP 1.4.4 Exercise 5
Section 1.4.4: Input and Output Exercise 5. [ M21 ] Generalize the situation of the preceding exercise to the case when the program refers to $n$ I/O devices instead of just one. Verified: no Solve time: - Solution Let $$ T_i $$ be the total time during which I/O device $i$ is actually transferring data, for $1\le i\le n$, and let $$ C $$ be the total computation time exclusive...
TAOCP 2.2.1 Exercise 4
Section 2.2.1: Stacks, Queues, and Deques Exercise 4. [ M34 ] Find a simple formula for $a_n$, the number of permutations on $n$ elements that can be obtained with a stack like that in exercise 2. Verified: no Solve time: - Setup Let $a_n$ denote the number of permutations of ${1,2,\ldots,n}$ obtainable by a stack of the type described in exercise 2. By exercise 3, each obtainable permutation corresponds to...
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: - Solution To initialize the WORDIN subroutine (4) correctly, we must ensure that the first call to WORDIN will access the first...
TAOCP 2.2.2 Exercise 8
Section 2.2.2: Sequential Allocation Exercise 8. [ 26 ] Explain how to modify (9), (10), and the repacking algorithms for the case that one or more of the lists is a queue being handled circularly as in (6a) and (7a). Verified: no Solve time: - Setup We are asked to extend the sequential allocation scheme for multiple stacks, as described by equations (9) and (10) and the repacking algorithm (Algorithm...
TAOCP 2.2.1 Exercise 7
Section 2.2.1: Stacks, Queues, and Deques Exercise 7. [ 25 ] Consider the problem of exercise 2, with a deque substituted for a stack. (a) Find a permutation of $1234$ that can be obtained with an input-restricted deque, but it cannot be obtained with an output-restricted deque. (b) Find a permutation of $1234$ that can be obtained with an output-restricted deque but not with an input-restricted deque. [As a consequence...
TAOCP 1.4.3.2 Exercise 5
Section 1.4.3.2: Trace Routines Exercise 5. [ 28 ] In a manner similar to that used to solve the previous exercise, consider the situation in which two copies of the trace routine are placed in different places in memory, and each is set up to trace the other. What would happen? Verified: yes Solve time: 49s Setup Let trace routines $T_A$ and $T_B$ occupy disjoint regions of memory. Each routine...
TAOCP 2.2.1 Exercise 11
Section 2.2.1: Stacks, Queues, and Deques Exercise 11. [ M40 ] As a consequence of exercises 9 and 10, the number $b_n$ is the number of admissible sequences of length $2n$. Find a closed form for the generating function $\sum_{n \ge 0} b_n z^n$. Verified: yes Solve time: 1m12s Setup Let $b_n$ denote the number of permutations of $1,2,\ldots,n$ obtainable with an input-restricted deque. By Exercise 9, $b_n$ is also...
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.3.2 Exercise 6
Section 1.4.3.2: Trace Routines Exercise 6. [ 40 ] Write a trace routine that is capable of tracing itself, in the sense of exercise 4: It should print out the steps of its own program at slower speed, and that program will be tracing itself at still slower speed, ad infinitum, until memory capacity is exceeded. Verified: no Solve time: 2m41s The previous submission failed because it attempted a high-level...
TAOCP 1.4.4 Exercise 7
Section 1.4.4: Input and Output Exercise 7. [ 22 ] Write a subroutine called WORDIN that is essentially like (4) except that it does not make use of a sentinel. Verified: yes Solve time: 50s Solution Let each buffer contain exactly $100$ consecutive words. Let $\beta_1$ denote the base address of INBUF1 and $\beta_2$ the base address of INBUF2 . Let the word in each buffer that formerly contained SENTINEL...
TAOCP 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 2.2.2 Exercise 4
Section 2.2.2: Sequential Allocation Exercise 4. [ 25 ] Assume that MIX has been extended as in exercise 3. Show how to give a single instruction (plus auxiliary constants) for each of the following actions: a) To loop indefinitely because indirect addressing never terminates. b) To bring into register A the value LINK(LINK(x)) , where the value of link variable $x$ is stored in the (0:2) field of the location...
TAOCP 1.4.3.2 Exercise 7
Section 1.4.3.2: Trace Routines Exercise 7. [ 25 ] Discuss how to write an efficient jump trace routine , which emits much less output than a normal trace. Instead of displaying the register contents, a jump trace simply records the jumps that occur. It outputs a sequence of pairs $(x_1, y_1)$, $(x_2, y_2)$, ... , meaning that the program jumped from location $x_1$ to $y_1$, then (after performing the instructions...
TAOCP 1.4.4 Exercise 15
Section 1.4.4: Input and Output Exercise 15. [ 22 ] Write a complete MIX program that copies 100 blocks from tape unit 0 to tape unit 1, using just three buffers. The program should be as fast as possible. Verified: no Solve time: - Solution We are asked to write a complete MIX program to copy 100 blocks from tape unit 0 to tape unit 1 using three buffers, with...
TAOCP 2.2.1 Exercise 13
Section 2.2.1: Stacks, Queues, and Deques Exercise 13. [ M48 ] How many permutations of $n$ elements are obtainable with the use of a general deque? [See Rosenstiehl and Tarjan, J. Algorithms 5 (1984), 389-390, for an algorithm that decides in $O(n)$ steps whether or not a given permutation is obtainable.] Verified: no Solve time: - Setup Let $d_n$ denote the number of permutations of ${1,2,\ldots,n}$ that can be obtained...
TAOCP 2.2.2 Exercise 7
Section 2.2.2: Sequential Allocation Exercise 7. [ 12 ] Step G4 of Algorithm G indicates a division by the quantity INC . Can INC ever be zero at that point in the algorithm? Verified: no Solve time: - Solution Step G4 of Algorithm G sets $$ \alpha \leftarrow 0.1 \times \frac{\text{SUM}}{n},\qquad \beta \leftarrow 0.9 \times \frac{\text{SUM}}{\text{INC}}. $$ The quantity INC was computed in step G2 as $$ \text{INC} \leftarrow \sum_{j=1}^{n}...
TAOCP 2.2.2 Exercise 2
Section 2.2.2: Sequential Allocation Exercise 2. [ 22 ] Generalize the method of (6a) and (7a) so that it will apply to any deque with fewer than $M$ elements. In other words, give specifications for the other two operations, "delete from rear" and "insert at front." Verified: no Solve time: - Solution In addition to the front pointer F and rear pointer R , introduce the convention that the deque...
TAOCP 1.4.4 Exercise 14
Section 1.4.4: Input and Output Exercise 14. [ 20 ] Suppose the computational program does not alternate between ASSIGN and RELEASE, but instead gives the sequence of actions ... ASSIGN ... ASSIGN ... RELEASE ... RELEASE . What effect does this have on the algorithms described in the text? Is it possibly useful? Verified: no Solve time: - Solution In the multiple-buffering scheme described in the text, the computational program...
TAOCP 2.2.1 Exercise 14
Section 2.2.1: Stacks, Queues, and Deques Exercise 14. [ 26 ] Suppose you are allowed to use only stacks as data structures. How can you implement a queue efficiently with two stacks? The simplest and most natural way to keep a linear list inside a computer is to put the list items in consecutive locations, one node after the other. Then we will have $$ \operatorname{LOC}(X[j+1]) = \operatorname{LOC}(X[j]) + c,...
TAOCP 1.4.4 Exercise 16
Section 1.4.4: Input and Output Exercise 16. [ 29 ] Formulate the "green-yellow-red-purple" algorithm, suggested by Fig. 26, in the manner of the algorithms for multiple buffering given in the text, using three coroutines (one to control the input device, one for the output device, and one for the computation). Verified: no Solve time: - Setup We are asked to formulate the "green-yellow-red-purple" algorithm, illustrated in Fig. 26, in the...
TAOCP 2.2.1 Exercise 2
Section 2.2.1: Stacks, Queues, and Deques Exercise 2. [ 15 ] Imagine four railroad cars positioned on the input side of the track in Fig. 1, numbered $1$, $2$, $3$, and $4$, from left to right. Suppose we perform the following sequence of operations (which is compatible with the direction of the arrows in the diagram and does not require cars to "jump over" other cars): (i) move car $1$...
TAOCP 2.2.1 Exercise 10
Section 2.2.1: Stacks, Queues, and Deques Exercise 10. [ M25 ] (See exercise 3.) Let S , Q , and X denote respectively the operations of inserting an element at the left, inserting an element at the right, and emitting an element from the left, of an output-restricted deque. For example, the sequence QQXSXSXX will transform the input sequence 1234 into 1342 . The sequence SXQSXSXX gives the same transformation....
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: 47s...
TAOCP 2.2.2 Exercise 3
Section 2.2.2: Sequential Allocation Exercise 3. [ 21 ] Suppose that MIX is extended as follows: The I-field of each instruction is to have the form $8I_1 + I_2$, where $0 \le I_1 \le 8$, $0 \le I_2 \le 8$. In assembly language one writes ' OP ADDRESS,I1:I2 ' or (as presently) ' OP ADDRESS,I2 ' if $I_1 = 0$. The meaning is to perform first the "address modification" $I_1$...
TAOCP 1.4.4 Exercise 11
Section 1.4.4: Input and Output Exercise 11. [ 21 ] Repeat exercise 9, except with just one buffer. Verified: no Solve time: 46s Solution We are asked to repeat exercise 9 under the assumption that there is only one buffer. Exercise 9 involves an output program whose original schedule with three buffers was as follows: assignments (A), computations (R), and output operations (OUT) with buffer-specific assignments. The output device operates...
TAOCP 2.2.2 Exercise 5
Section 2.2.2: Sequential Allocation Exercise 5. [ 35 ] The extension of MIX suggested in exercise 3 has an unfortunate restriction that " 7:7 " is not allowed in an indirectly addressed location. a) Give an example to indicate that, without this restriction, it would probably be necessary for the MIX hardware to be capable of maintaining a long internal stack of three-bit items. (This would be prohibitively expensive hardware,...
TAOCP 1.4.4 Exercise 4
Section 1.4.4: Input and Output Exercise 4. [ M20 ] Show that if a program refers to a single I/O device, we might be able to cut the running time in half by buffering the I/O, in favorable circumstances; but we can never decrease the running time by more than a factor of two, with respect to the time taken by unbuffered I/O. Verified: no Solve time: - Solution Let...
TAOCP 2.2.1 Exercise 3
Section 2.2.1: Stacks, Queues, and Deques Exercise 3. [ 25 ] The operations (i) through (viii) in the previous exercise can be much more concisely described by the code SSXSSXXX , where S stands for "move a car from the input into the stack," and X stands for "move a car from the stack into the output." Some sequences of S 's and X 's specify meaningless operations, since there...
TAOCP 2.2.1 Exercise 6
Section 2.2.1: Stacks, Queues, and Deques Exercise 6. [ 00 ] Consider the problem of exercise 2, with a queue substituted for a stack. What permutations of $12\ldots n$ can be obtained with use of a queue? Verified: no Solve time: - With a queue, the first item inserted is always the first item removed. Since the input arrives in the order $1,2,\ldots,n$, item $1$ must leave before item $2$,...
TAOCP 2.2.2 Exercise 1
Section 2.2.2: Sequential Allocation Exercise 1. [ 15 ] In the queue operations given by (6a) and (7a), how many items can be in the queue at one time without OVERFLOW occurring? Verified: no Solve time: - Solution Let $X[1], \ldots, X[M]$ be the memory locations for the queue, with F and R the front and rear pointers, respectively, as in (6a) and (7a). The queue is initially empty when...
TAOCP 1.4.4 Exercise 3
Section 1.4.4: Input and Output Exercise 3. [ 22 ] Write a buffer-swapping output subroutine analogous to (4). The subroutine, called WORDOUT , should store the word in rA as the next word of output, and if a buffer is full it should write 100 words onto tape unit V. Index register 5 should be used to refer to the current buffer position. Show the layout of buffer areas and...
TAOCP 2.2.2 Exercise 9
Section 2.2.2: Sequential Allocation Exercise 9. [ M27 ] Using the mathematical model described near the end of the text, prove that Eq. (14) is the expected number of moves. (Note that the sequence 1, 1, 4, 2, 3, 1, 2, 4, 2, 1 specifies $0 + 0 + 0 + 1 + 1 + 3 + 2 + 0 + 3 + 6 = 16$ moves.) Verified: no Solve...
TAOCP 1.4.4 Exercise 13
Section 1.4.4: Input and Output Exercise 13. [ 20 ] What instructions should be included at the end of the COMPUTE coroutine in the text, if the buffering algorithms are being applied to output, to ensure that all information has been output from the buffers? Verified: no Solve time: - Solution The COMPUTE coroutine, as described in Section 1.4.2 and extended in Section 1.4.4 for buffered input, can be adapted...
TAOCP 1.4.4 Exercise 12
Section 1.4.4: Input and Output Exercise 12. [ 24 ] Suppose that the multiple buffering algorithm in the text is being used for card input, and suppose the input is to terminate as soon as a card with "." in column 80 has been read. Show how the CONTROL coroutine (Algorithm B and Program B) should be changed so that input is shut off in this way. Verified: no Solve...
TAOCP 2.2.1 Exercise 8
Section 2.2.1: Stacks, Queues, and Deques Exercise 8. [ 22 ] Are there any permutations of $12\ldots n$ that cannot be obtained with the use of a deque that is neither input- nor output-restricted? Verified: no Solve time: - Solution Let $p = p_1 p_2 \ldots p_n$ be a permutation of $12\ldots n$, and let $D$ be a deque that is neither input- nor output-restricted. We wish to determine whether...
TAOCP 1.4.4 Exercise 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 2
Section 1.4.4: Input and Output Exercise 2. [ 10 ] The instructions OUT 1000(6); JBUS *(6) may be used to output a tape block in an unbuffered fashion, just as the instructions (1) did this for input. Give a method analogous to (2) and (3) that buffers this output, by using MOVE instructions and an auxiliary buffer in locations 2000-2099. Verified: yes Solve time: 44s The output tape is used...
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: yes Solve time: 2m06s Corrected solution We model the system as a pool of $N$ indistinguishable buffers....
TAOCP 1.3.3 Exercise 19
Section 1.3.3: Applications to Permutations Exercise 19. [ HM21 ] Show that, in the notation of Eq. (25), the number $P_{n0}$ of derangements is exactly equal to $n!/e$ rounded to the nearest integer, for all $n \ge 1$. Verified: yes Solve time: 47s Solution Equation (25) gives the number of derangements as $$ P_{n0} = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}. $$ Let $$ S_n=\sum_{k=0}^{n}\frac{(-1)^k}{k!}. $$ Since $$ e^{-1} = \sum_{k=0}^{\infty}\frac{(-1)^k}{k!}, $$ we have $$...
TAOCP 1.3.3 Exercise 37
Section 1.3.3: Applications to Permutations Exercise 37. [ M26 ] ( Even permutations. ) Let $\pi$ be a permutation of ${1,\ldots,n}$. Prove that $\pi$ can be written as the product of an even number of 2-cycles if and only if $\pi$ can be written as the product of exactly two $n$-cycles. Verified: no Solve time: 3m09s The forward direction is correct and standard. The reverse direction fails because it does...
TAOCP 1.4.3.1 Exercise 2
Section 1.4.3.1: A MIX Simulator Exercise 2. [ 20 ] Write the SHIFT routine, which is missing from the program in the text (operation code 6). Verified: no Solve time: 5m08s The missing routine must be written in the conventions of Program M. The simulator stores each MIX register as a signed word whose five magnitude bytes are individually accessible. Therefore the routine must operate directly on those stored bytes...
TAOCP 1.3.3 Exercise 13
Section 1.3.3: Applications to Permutations Exercise 13. [ M24 ] Prove that Algorithm $J$ is valid. Verified: yes Solve time: 9m43s Let the current permutation be $$ P = (a_1,a_2,\dots,a_n). $$ Algorithm $J$ constructs a new permutation $Q$ as follows: Choose the largest index $i$ such that $a_i < a_{i+1}$. Choose the largest index $j>i$ such that $a_j > a_i$. Swap $a_i$ and $a_j$. Reverse the suffix $a_{i+1},\dots,a_n$. We prove...
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: no Solve time: 4m13s Solution Yes. The identity permutation is the unique example. Let $\pi$ be a permutation of ${1,2,\ldots,n}$. Write its canonical cycle form as $$ (c_{11}\ c_{12}\ \cdots\ c_{1\ell_1}) (c_{21}\ c_{22}\ \cdots\ c_{2\ell_2}) \cdots...
TAOCP 1.3.3 Exercise 33
Section 1.3.3: Applications to Permutations Exercise 33. [ M33 ] If $m = 2^{2^l}$ and $n = 2^{2l+1}$, show how to construct sequences of permutations $(\alpha_{j1}, \alpha_{j2}, \ldots, \alpha_{jn}; \beta_{j1}, \beta_{j2}, \ldots, \beta_{jn})$ for $0 \le j < m$ with the following "orthogonality" property: $$ \alpha_{i1}\beta_{j1}\alpha_{i2}\beta_{j2}\cdots\alpha_{in}\beta_{jn} = \begin{cases} (1,2,3,4,5), & \text{if } i = j;\ (), & \text{if } i \ne j. \end{cases} $$ Each $\alpha_{jk}$ and $\beta_{jk}$ should be...
TAOCP 1.3.3 Exercise 36
Section 1.3.3: Applications to Permutations Exercise 36. [ 27 ] Write a MIX subroutine for the algorithm in the answer to exercise 35, and analyze its running time. Compare it with the simpler method that goes from $\alpha\beta\gamma$ to $(\alpha\beta\gamma)^R = \gamma^R\beta^R\alpha^R$ to $\gamma\beta\alpha$, where $\sigma^R$ denotes the left-right reversal of the string $\sigma$. Verified: yes Solve time: 3m13s Exercise 1.3.3.36, Corrected Solution Problem Let an array $X[0 \dots l+m+n-1]$...
TAOCP 1.3.3 Exercise 29
Section 1.3.3: Applications to Permutations Exercise 29. [ M25 ] Prove that the cycle form of the Josephus permutation when $m = 2$ can be obtained by first expressing the "perfect shuffle" permutation of ${1, 2, \ldots, 2n}$, which takes $(1, 2, \ldots, 2n)$ into $(2, 4, \ldots, 2n, 1, 3, \ldots, 2n - 1)$, in cycle form, then reversing left and right and erasing all the numbers greater than...
TAOCP 1.3.3 Exercise 30
Section 1.3.3: Applications to Permutations Exercise 30. [ M24 ] Use exercise 29 to show that the fixed elements of the Josephus permutation when $m = 2$ are precisely the numbers $(2^d - 1)(2n + 1)/(2^{d+1} - 1)$ for all positive integers $d$ such that this is an integer. Verified: yes Solve time: 8m56s Corrected Solution Let $n$ be a positive integer, and let $J_{2n+1}$ denote the Josephus permutation for...
TAOCP 1.3.3 Exercise 34
Section 1.3.3: Applications to Permutations Exercise 34. [ M25 ] ( Transposing blocks of data. ) One of the most common permutations needed in practice is the change from $\alpha\beta$ to $\beta\alpha$, where $\alpha$ and $\beta$ are substrings of an array. In other words, if $x_0x_1\ldots x_{m-1} = \alpha$ and $x_mx_{m+1}\ldots x_{m+n-1} = \beta$, we want to change the array $x_0x_1\ldots x_{m+n-1} = \alpha\beta$ to the array $x_mx_{m+1}\ldots x_{m+n-1}x_0x_1\ldots x_{m-1}...
TAOCP 1.4.3.2 Exercise 1
Section 1.4.3.2: Trace Routines Exercise 1. [ 22 ] Modify the trace routine of the text so that it restores register J when leaving. (You may assume that register J is not zero.) Verified: no Solve time: - Solution We are asked to modify the trace routine so that it restores register J upon leaving, assuming $rJ \neq 0$. By examining the routine in Section 1.4.3.2, we see that JREG...
TAOCP 1.3.3 Exercise 24
Section 1.3.3: Applications to Permutations Exercise 24. [ M41 ] Find the variance of the quantity $A$ that enters into the timing of Algorithm $J$. (See exercise 14.) Verified: yes Solve time: 4m38s Let the permutation be decomposed into cycles, and let $\alpha_k$ denote the number of cycles of length $k$. In the timing analysis of Algorithm $J$ from Exercise 14, the quantity $A$ is the total number of repetitions...
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: 2m07s Solution Let $$ r=\alpha_1+\alpha_2+\alpha_3+\cdots $$ be the total number of cycles of the permutation, including the one-cycles. Two independent choices determine...
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: 16m52s Setup Let $p_{nkm}$ denote the probability that a...
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: 32m02s Solution Let the initial permutation in linear notation be $1324$. This means the mapping on ${1,2,3,4}$ is $$ 1 \mapsto 1,\quad...
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.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: 1m20s Solution The subroutine FCHECK is called whenever an instruction uses a field specification. The sequence JMP FCHECK appears inside GETV...
TAOCP 1.3.3 Exercise 21
Section 1.3.3: Applications to Permutations Exercise 21. [ M22 ] What is the probability $P(n;\alpha_1,\alpha_2,\ldots)$ that a permutation of $n$ objects has exactly $\alpha_1$ one-cycles, $\alpha_2$ two-cycles, etc.? Verified: yes Solve time: 42s Solution Let a permutation of $n$ objects have exactly $\alpha_k$ cycles of length $k$ for each $k \ge 1$, with only finitely many nonzero $\alpha_k$. The constraint on the parameters is $$ \sum_{k \ge 1} k \alpha_k...
TAOCP 1.3.3 Exercise 31
Section 1.3.3: Applications to Permutations Exercise 31. [ HM38 ] Generalizing exercises 29 and 30, prove that the $j$th man to be executed, for general $m$ and $n$, is in position $x$, where $x$ may be computed as follows: Set $x \leftarrow jm$; then, while $x > n$, set $x \leftarrow \lfloor (m(x - n) - 1)/(m - 1)\rfloor$. Consequently the average number of fixed elements, for $1 \le n...
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.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 6
Section 1.4.3.1: A MIX Simulator Exercise 6. [ 28 ] Write programs for the input-output operators JBUS , IOC , IN , OUT , and JRED , which are missing from the program in the text, allowing only units 16 and 18. Assume that the operations "read-card" and "skip-to-new-page" take $T = 10000u$, while "print-line" takes $T = 7500u$. [ Note: Experience shows that the JBUS instruction should be simulated...
TAOCP 1.4.3.1 Exercise 3
Section 1.4.3.1: A MIX Simulator Exercise 3. [ 22 ] Write the MOVE routine, which is missing from the program in the text (operation code 7). Verified: yes Solve time: 2m37s The failure is entirely caused by using a non-existent MIX instruction JLE . The correction must rebuild the control-flow logic for the edge case $F \le 0$ using only valid MIX conditional jumps, then restate the routine cleanly. Correct...
TAOCP 1.4.3.2 Exercise 4
Section 1.4.3.2: Trace Routines Exercise 4. [ 25 ] What would happen if the trace routine were tracing itself ? Specifically, consider the behavior if the two instructions ENTX LEAVEX; JMP *+1 were placed just before ENTER . Verified: no Solve time: - Solution Assume that the trace routine is tracing itself, and that the two instructions ENTX LEAVEX JMP *+1 have been inserted immediately before ENTER . The purpose...
TAOCP 1.4.3.2 Exercise 3
Section 1.4.3.2: Trace Routines Exercise 3. [ 10 ] The previous exercise suggests having the trace program write its output onto tape. Discuss why this would be preferable to printing directly. Verified: no Solve time: - Writing trace output onto tape rather than printing directly is preferable because tape allows information to be recorded sequentially without interrupting the execution of the program being traced. Direct printing would require the program...
TAOCP 1.3.3 Exercise 27
Section 1.3.3: Applications to Permutations Exercise 27. [ M20 ] Use the principle of inclusion and exclusion to count the number of integers $n$ in the range $0 \le n < am_1m_2\cdots m_t$ that are not divisible by any of $m_1, m_2, \ldots, m_t$. Here $m_1, m_2, \ldots, m_t$, and $a$ are positive integers, with $m_j \perp m_k$ when $j \ne k$. Verified: yes Solve time: 42s Solution Let $$...
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: 11m45s Corrected Solution for Exercise 1.3.3.23 (Golomb, Shepp,...
TAOCP 1.4.3.1 Exercise 5
Section 1.4.3.1: A MIX Simulator Exercise 5. [ 24 ] Determine the time required to simulate the LDA and ENTA operators, compared with the actual time for MIX to execute these operators directly. Verified: no Solve time: - Solution Let $$ S=\sum_{1<k\le n}\frac{H_k}{k(k-1)}. $$ Since $$ \frac1{k(k-1)}=\frac1{k-1}-\frac1k, $$ the summand has the form $(a_{k+1}-a_k)b_k$ if we choose $$ a_k=-\frac1{k-1}, \qquad b_k=H_k $$ for $k\ge2$. Then $$ a_{k+1}-a_k =-\frac1k+\frac1{k-1} =\frac1{k(k-1)}. $$...
TAOCP 1.3.3 Exercise 11
Section 1.3.3: Applications to Permutations Exercise 11. [ 15 ] Find a simple rule for writing $\pi^{-}$ in cycle form, if the permutation $\pi$ is given in cycle form. Verified: yes Solve time: 1m28s Solution Let $\pi$ be a permutation expressed in cycle form as a product of disjoint cycles: $$ \pi = (x_{11} x_{12} \dots x_{1n_1})(x_{21} x_{22} \dots x_{2n_2})\cdots(x_{k1} x_{k2} \dots x_{kn_k}), $$ where each cycle $(x_{i1} x_{i2} \dots...
TAOCP 1.3.3 Exercise 35
Section 1.3.3: Applications to Permutations Exercise 35. [ M30 ] Continuing the previous exercise, let $x_0x_1\ldots x_{l+m+n-1} = \alpha\beta\gamma$ where $\alpha$, $\beta$, and $\gamma$ are strings of respective lengths $l$, $m$, and $n$, and suppose that we want to change $\alpha\beta\gamma$ to $\gamma\beta\alpha$. Show that the corresponding permutation has a convenient cycle structure that leads to an efficient algorithm. [Exercise 34 considered the special case $m = 0$.] Hint: Consider...
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: 5m35s Solution For $1\le k\le n$, let...
TAOCP 1.3.3 Exercise 14
Section 1.3.3: Applications to Permutations Exercise 14. [ M34 ] Find the average value of the quantity $A$ in the timing of Algorithm $J$. Verified: yes Solve time: 24m50s Let $A$ denote the total number of executions of the assignment $$ j\leftarrow i $$ in Algorithm $J$. We compute the average of $A$ over all $n!$ permutations. 1. Contribution of a single cycle Let the permutation $\pi$ contain a cycle...
TAOCP 1.3.3 Exercise 32
Section 1.3.3: Applications to Permutations Exercise 32. [ M25 ] (a) Prove that any permutation $\pi = \pi_1\pi_2\cdots\pi_{2m+1}$ of the form $$ \pi = (2,3)^{e_2}(4,5)^{e_4}\cdots(2m\ 2m!+!1)^{e_{2m}}(1,2)^{e_1}(3,4)^{e_3}\cdots(2m!-!1\ 2m)^{e_{2m-1}}, $$ where each $e_k$ is $0$ or $1$, has $|\pi_k - k| \le 2$ for $1 \le k \le 2m + 1$. (b) Given any permutation $\rho$ of ${1, 2, \ldots, n}$, construct a permutation $\pi$ of the stated form such that $\rho\pi$...
TAOCP 1.4.3.1 Exercise 8
Section 1.4.3.1: A MIX Simulator Exercise 8. [ 20 ] True or false: Whenever line 010 of the simulator program is executed, we have $0 \le rI6 < \text{BEGIN}$. Verified: no Solve time: 2m25s Exercise 1.4.3.1.8, Corrected Solution We are asked: True or false: Whenever line 010 of the simulator program is executed, we have $0 \le rI6 < \text{BEGIN}.$ We will analyze this carefully using the actual semantics of...
TAOCP 1.4.3.1 Exercise 4
Section 1.4.3.1: A MIX Simulator Exercise 4. [ 14 ] Change the program in the text so that it begins as though MIX's "GO button" had been pushed (see exercise 1.3.1-26 ). Verified: yes Solve time: 39s Solution Exercise 1.4.3.1.4 [ 14 ] asks to modify the MIX simulator program so that it begins execution as though MIX's "GO button" had been pushed. In the MIX hardware, pushing the GO...
TAOCP 1.4.3.2 Exercise 2
Section 1.4.3.2: Trace Routines Exercise 2. [ 26 ] Modify the trace routine of the text so that before executing each program step it writes the following information on tape unit 0. Word 1, (0:2) field: location. Word 1, (4:5) field: register J (before execution). Word 1, (3:3) field: 2 if comparison is greater, 1 if equal, 0 if less; plus 8 if overflow is not on before execution. Word...
TAOCP 1.3.3 Exercise 25
Section 1.3.3: Applications to Permutations Exercise 25. [ M22 ] Prove Eq. (29). Verified: yes Solve time: 27m26s Solution Let $S_n$ be the set of all permutations of ${1,2,\dots,n}$, each taken with equal probability $1/n!$. Let $X(\pi)$ denote the number of cycles in a permutation $\pi \in S_n$. Equation (29) asserts that the mean value satisfies $$ \mathbb{E}[X] = 1 + \frac{1}{2} + \cdots + \frac{1}{n}. $$ For $i \in...
TAOCP 1.3.3 Exercise 17
Section 1.3.3: Applications to Permutations Exercise 17. [ M24 ] (a) The text demonstrates that there are $n!H_n$ cycles altogether, among all the permutations on $n$ elements. If these cycles (including singleton cycles) are individually written on $n!H_n$ slips of paper, and if one of these slips of paper is chosen at random, what is the average length of the cycle that is thereby picked? (b) If we write the...
TAOCP 1.3.3 Exercise 26
Section 1.3.3: Applications to Permutations Exercise 26. [ M24 ] Extend the principle of inclusion and exclusion to obtain a formula for the number of elements that are in exactly $r$ of the subsets $S_1, S_2, \ldots, S_M$. (The text considers only the case $r = 0$.) Verified: yes Solve time: 26m57s Solution Let $S_1, S_2, \ldots, S_M$ be subsets of a fixed universe $U$. For each element $x \in...
TAOCP 1.3.1 Exercise 23
Section 1.3.1: Description of MIX Exercise 23. [ 27 ] Location 0200 contains a word + a b c d e write two programs that compute the "reflected" word + e d c b a and halt with the result in register A . One program should do this without using MIX's ability to load and store partial fields of words. Both programs should take the minimum possible number of...
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: yes Solve time: 1m22s...
TAOCP 1.3.2 Exercise 7
Section 1.3.2: The MIX Assembly Language Exercise 7. [ 10 ] (a) What is the meaning of 4B in line 34 of Program P ? (b) What effect, if any, would be caused if the location of line 15 were changed to 2H and the address of line 20 were changed to 2B ? Verified: yes Solve time: 3m03s In MIX assembly (as used in The Art of Computer Programming...
TAOCP 1.3.2 Exercise 8
Section 1.3.2: The MIX Assembly Language Exercise 8. ▶ [ 24 ] What does the following program do? (Do not run it on a computer, figure it out by hand!) * MYSTERY PROGRAM BUF ORIG *+3000 1H ENT1 1 ENT2 0 LDX 4F 2H ENT3 0,1 3H STZ BUF,2 INC2 1 DEC3 1 J3P 3B STX BUF,2 INC2 1 INC1 1 CMP1 =75= JL 2B ENN2 2400 OUT BUF+2400,2(18) INC2...
TAOCP 1.3.3 Exercise 5
Section 1.3.3: Applications to Permutations Exercise 5. [ M10 ] Equation (3) shows several equivalent ways to express the same permutation in cycle form. How many different ways of writing that permutation are possible, if all singleton cycles are suppressed? Verified: yes Solve time: 1m14s Kí hiệu phép hoán vị đã cho là $(acf)(bd)$, trong đó $e$ là điểm bất động và bị lược bỏ trong ký hiệu chu trình....
TAOCP 1.3.1 Exercise 6
Section 1.3.1: Description of MIX Exercise 6. [ 10 ] Assume that location 3000 contains + 5 1 200 15 What is the result of the following instructions? (State if any of them are undefined or only partially defined.) (a) LDAN 3000 ; (b) LD2N 3000(3:4) ; (c) LDX 3000(1:3) ; (d) LD6 3000 ; (e) LDXN 3000(0:0) . Verified: yes Solve time: 2m53s Solution to Exercise 1.3.1.6 [10] We...
TAOCP 1.3.1 Exercise 13
Section 1.3.1: Description of MIX Exercise 13. [ 10 ] Suppose location 1000 contains the instruction JOV 1001 . This instruction turns off the overflow toggle if it is on (and the next instruction executed will be in location 1001, in any case). If this instruction were changed to JNOV 1001 , would there be any difference? What if it were changed to JOV 1000 or JNOV 1000 ? Verified:...
TAOCP 1.3.1 Exercise 7
Section 1.3.1: Description of MIX Exercise 7. [ M15 ] Give a precise definition of the results of the DIV instruction for all cases in which overflow does not occur, using the algebraic operations X mod Y and \lfloor X/Y \rfloor . Verified: yes Solve time: 9m36s Let $D$ be the signed integer formed from $(rA,rX)$, and let $m\neq 0$ be the signed integer in $M$. Assume no overflow occurs,...
TAOCP 1.3.1 Exercise 11
Section 1.3.1: Description of MIX Exercise 11. [ 15 ] List all the MIX operators that can possibly affect the setting of rI1 . Verified: yes Solve time: 3m05s Solution The exercise asks for all MIX operators that can affect the setting of the index register $rI1$. To answer such a question, one must inspect the complete list of operators in §1.3.1 and determine which of them can alter the...
TAOCP 1.3.1 Exercise 12
Section 1.3.1: Description of MIX Exercise 12. [ 10 ] Find a single instruction that has the effect of multiplying the current contents of rI3 by two and leaving the result in rI3 . Verified: yes Solve time: 5m27s Corrected solution The proposed solution fails because MMIX provides no arithmetic or shift instructions that operate directly on index registers $rI_0,\dots,rI_7$. These registers are not part of the general ALU register...
TAOCP 1.3.1 Exercise 10
Section 1.3.1: Description of MIX Exercise 10. [ 15 ] List all the MIX operators that can possibly affect the setting of the comparison indicator. Verified: yes Solve time: 51s Solution The comparison indicator $\mathrm{CI}$ takes one of the three values $\mathrm{LESS}$, $\mathrm{EQUAL}$, or $\mathrm{GREATER}$. From the instruction tables in Section 1.3.1, $\mathrm{CI}$ is assigned only by instructions of the comparison family. The relevant definition is given in the instruction...
TAOCP 1.3.2 Exercise 9
Section 1.3.2: The MIX Assembly Language Exercise 9. ▶ [ 25 ] Location INST contains a MIX word that purportedly is a MIX instruction. Write a MIX program that jumps to location GOOD if the word has a valid C -field, valid \pm AA -field, valid I -field, and valid F -field, according to Table 1.3.1-1 ; your program should jump to location BAD otherwise. Remember that the test for...
TAOCP 1.2.9 Exercise 21
Section 1.2.9: Generating Functions Exercise 21. [ HM30 ] Set up the generating function for the sequence $\langle n! \rangle$ and study properties of this function. Verified: yes Solve time: 1m06s Setup Let $\langle a_n \rangle = \langle n! \rangle$. Define its ordinary generating function by $$ G(z) = \sum_{n \ge 0} n! , z^n. \tag{1} $$ The task is to construct $G(z)$ and analyze its properties using the operations...
TAOCP 1.3.3 Exercise 10
Section 1.3.3: Applications to Permutations Exercise 10. [ M28 ] Examine the timing characteristics of Program $B$, namely, the quantities $A$, $B$, $\ldots$, $Z$ shown there; express the total time in terms of the quantities $X$, $Y$, $M$, $N$, $U$, $V$ defined in (19), and of $F$. Compare the total time for Program $B$ with the total time for Program $A$ on the input (6), as computed in exercise 7....
TAOCP 1.3.1 Exercise 3
Section 1.3.1: Description of MIX Exercise 3. [ 02 ] Give the partial field specifications, (L:R) , for the (a) address field, (b) index field, (c) field field, and (d) operation code field of a MIX instruction. Verified: yes Solve time: 44s The instruction format of a MIX word places the sign in position $0$, the address in bytes $1$–$2$, the index specification in byte $3$, the field specification in...
TAOCP 1.2.9 Exercise 25
Section 1.2.9: Generating Functions Exercise 25. [ M23 ] Evaluate the sum $$ \sum_k \binom{n}{k} 2^{n-2k}(-2)^k $$ by simplifying the equivalent formula $$ \sum_k w^k ^n z^{n-k} ^{2n-2k}. $$ Verified: no Solve time: 4m29s Solution Let $$ S=\sum_k w^k ^n, z^{,n-k} ^{2n-2k}. $$ We simplify this coefficient-extraction formula directly. First observe that $$ z^{,n-k} ^{2n-2k} = z^{,n-k} ^{2(n-k)} =\binom{2(n-k)}{,n-k,}. $$ Hence $$ S=\sum_k \binom{n}{k}(-2)^k \binom{2(n-k)}{,n-k,}, $$ since $$ w^k ^n=\binom{n}{k}(-2)^k....
TAOCP 1.3.1 Exercise 1
Section 1.3.1: Description of MIX Exercise 1. [ 00 ] If MIX were a ternary (base 3) computer, how many "trits" would there be per byte? Verified: yes Solve time: 3m18s A MIX byte must be capable of representing at least $64$ distinct values and at most $100$ distinct values. On a ternary computer, a byte containing $t$ trits can represent $3^t$ distinct values. Therefore $t$ must satisfy $$ 64...
TAOCP 1.3.1 Exercise 19
Section 1.3.1: Description of MIX Exercise 19. [ 14 ] What is the execution time of the program in the preceding exercise, not counting the HLT instruction? Verified: yes Solve time: 1m05s Solution The execution times given in Section 1.3.1 are: $$ \begin{aligned} &\text{all STORE operations} &&= 2,\ &\text{all shift operations} &&= 2,\ &\text{ADD} &&= 2,\ &\text{all comparison operations} &&= 2,\ &\text{MOVE of } n \text{ words} &&= 2n+1,\ &\text{NUM,...
TAOCP 1.3.3 Exercise 3
Section 1.3.3: Applications to Permutations Exercise 3. [ 03 ] Compute the product $$ \begin{pmatrix} a & b & c & d & e & f \ b & d & c & a & f & e \end{pmatrix} \times \begin{pmatrix} a & b & c & d & e & f \ c & d & f & b & e & a \end{pmatrix}, $$ and express the answer...
TAOCP 1.3.2 Exercise 2
Section 1.3.2: The MIX Assembly Language Exercise 2. ▶ [ 10 ] Line 12 of Program M says JMP * , where * denotes the location of that line. Why doesn't the program go into an infinite loop, endlessly repeating this instruction? * The author was astonished to learn in 1971 that MIXAL is also the name of a laundry detergent in Yugoslavia, developed for use with automate [automatics]. Verified:...
TAOCP 1.3.2 Exercise 6
Section 1.3.2: The MIX Assembly Language Exercise 6. [ HM20 ] (a) Show that if n is not prime, n has a divisor d with 1 < d \le \sqrt{n} . (b) Use this fact to show that the test in step P7 of Algorithm P proves that N is prime. Verified: yes Solve time: 1m30s Solution (a) Let $n$ be a composite integer, $n > 1$. Then there exist...
TAOCP 1.2.9 Exercise 19
Section 1.2.9: Generating Functions Exercise 19. [ HM32 ] (C. F. Gauss, 1812.) The sums of the following infinite series are well known: $$ 1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+\cdots = \ln 2; \qquad 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots = \frac{\pi}{4}; $$ $$ 1-\frac{1}{4}+\frac{1}{7}-\frac{1}{10}+\cdots = \frac{\pi\sqrt{3}}{9} + \frac{1}{3}\ln 2. $$ Using the definition $$ H_x = \sum_{n \ge 1}\left(\frac{1}{n}-\frac{1}{n+x}\right) $$ found in the answer to exercise 1.2.7-24, these series may be written respectively as $$ 1 - \frac{1}{2}H_{1/2}; \qquad...
TAOCP 1.3.1 Exercise 17
Section 1.3.1: Description of MIX Exercise 17. [ 26 ] This is the same as the previous exercise, except that locations 0000 through N , inclusive, are to be set to zero, where N is the current contents of rI2 . Your programs (a) and (b) should work for any value 0 \le N \le 2999 ; they should start in location 3000 . Verified: yes Solve time: 1m55s The...
TAOCP 1.3.1 Exercise 4
Section 1.3.1: Description of MIX Exercise 4. [ 00 ] The last example in (5) is LDA -2000,4 . How can this be legitimate, in view of the fact that memory addresses should not be negative? Verified: yes Solve time: 51s The sign in the address field is part of the instruction encoding and is not constrained by the requirement on memory references. The instruction format allows the address to...
TAOCP 1.3.1 Exercise 22
Section 1.3.1: Description of MIX Exercise 22. [ 28 ] Location 2000 contains an integer number, X . Write two programs that compute X^{13} and halt with the result in register A . One program should use the minimum number of MIX memory locations; the other should require the minimum execution time possible. Assume that X^{13} fits into a single word. Verified: no Solve time: 2m49s Program 1: minimum memory...
TAOCP 1.3.1 Exercise 5
Section 1.3.1: Description of MIX Exercise 5. [ 10 ] What symbolic notation, analogous to (4), corresponds to (6) if (6) is regarded as a MIX instruction? Verified: yes Solve time: 3m09s Let (6) be a MIX word written in its physical layout as $$ (6):\quad \pm ; \text{AA} ; \text{I} ; \text{F} ; \text{C}, $$ where AA is the address field, I the index field, F the field specification,...
TAOCP 1.2.9 Exercise 22
Section 1.2.9: Generating Functions Exercise 22. [ M21 ] Find a generating function $G(z)$ for which $$ [z^n]G(z) = \sum_{k_0+2k_1+4k_2+8k_3+\cdots=n} \binom{r}{k_0}\binom{r}{k_1}\binom{r}{k_2}\binom{r}{k_3}\cdots . $$ Verified: yes Solve time: 1m05s Solution We are asked to find a generating function $G(z)$ such that the coefficient of $z^n$ is $$ [z^n]G(z) = \sum_{k_0+2k_1+4k_2+8k_3+\cdots=n} \binom{r}{k_0}\binom{r}{k_1}\binom{r}{k_2}\binom{r}{k_3}\cdots. $$ Observe that the sum runs over all sequences of nonnegative integers $(k_0,k_1,k_2,\ldots)$ such that $$ \sum_{j \ge 0} 2^j...
TAOCP 1.3.1 Exercise 24
Section 1.3.1: Description of MIX Exercise 24. [ 21 ] Assuming that registers A and X contain + 0 a b c d and + e f g h i respectively, write two programs that change the contents of these registers to + a b c d e and + 0 f g h i respectively, using (a) minimum memory space and (b) minimum execution time. Verified: yes Solve time:...
TAOCP 1.3.2 Exercise 4
Section 1.3.2: The MIX Assembly Language Exercise 4. ▶ [ 25 ] Assemble Program P by hand. (It won't take as long as you think.) What are the actual numerical contents of memory, corresponding to that symbolic program? Verified: no Solve time: 4m39s To produce a fully corrected solution for TAOCP, Exercise 1.3.2.4, part 4, we must assemble Program P by hand and produce the numerical contents of memory ....
TAOCP 1.3.1 Exercise 2
Section 1.3.1: Description of MIX Exercise 2. [ 02 ] If a value to be represented within MIX may get as large as 99999999, how many adjacent bytes should be used to contain this quantity? Verified: yes Solve time: 35s Four adjacent bytes represent integers from $0$ through $16{,}777{,}215$ by the table in Section 1.3.1. Since $$ 99{,}999{,}999 > 16{,}777{,}215, $$ no representation using four adjacent bytes can accommodate all...
TAOCP 1.2.9 Exercise 20
Section 1.2.9: Generating Functions Exercise 20. [ M21 ] For what coefficients $c_{mk}$ is $$ \sum_{n \ge 0} n^m z^n = \sum_{k=0}^{m} c_{mk}\frac{z^k}{(1-z)^{k+1}} , ? $$ Verified: yes Solve time: 1m Solution We are asked to express the generating function $$ \sum_{n \ge 0} n^m z^n $$ in the form $$ \sum_{k=0}^{m} c_{mk}\frac{z^k}{(1-z)^{k+1}}. $$ Let $G_m(z) = \sum_{n \ge 0} n^m z^n$. By Eq. (5), we have $$ \sum_{n \ge...
TAOCP 1.3.3 Exercise 9
Section 1.3.3: Applications to Permutations Exercise 9. [ 10 ] Both Programs $A$ and $B$ accept the same input and give the answer in essentially the same form. Is the output exactly the same under both programs? Verified: yes Solve time: 1m04s No. The two programs do not necessarily produce exactly the same output. Program $A$ implements Algorithm $A$, which constructs the answer by choosing the first untagged element encountered...
TAOCP 1.3.1 Exercise 15
Section 1.3.1: Description of MIX Exercise 15. [ 10 ] How many alphanumeric characters are there in a typewriter or paper-tape block? in a card-reader or card-punch block? in a line-printer block? Verified: yes Solve time: 36s In MIX, each alphanumeric character is represented by one byte, since a byte holds at least $64$ distinct values and is used for character coding in input-output operations. Thus the number of characters...
TAOCP 1.3.1 Exercise 16
Section 1.3.1: Description of MIX Exercise 16. [ 20 ] Write a program that sets memory cells 0000 - 0099 all to zero and is (a) as short a program as possible; (b) as fast a program as possible. [Hint: Consider using the MOVE command.] Verified: yes Solve time: 3m35s The previous solution failed because it imported an incorrect model of MOVE and then built optimality arguments on top of...
TAOCP 1.2.9 Exercise 18
Section 1.2.9: Generating Functions Exercise 18. [ M25 ] Given positive integers $n$ and $r$, find a simple formula for the value of the following sums: (a) $\sum_{1 \le k_1 < k_2 < \cdots < k_r \le n} k_1k_2\cdots k_r$; (b) $\sum_{1 \le k_1 \le k_2 \le \cdots \le k_r \le n} k_1k_2\cdots k_r$. (For example, when $n=3$ and $r=2$ the sums are, respectively, $1\cdot2 + 1\cdot3 + 2\cdot3$ and...
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: 1m07s Let $f(x)=2x \bmod 7$ on ${0,1,2,3,4,5,6}$. Since $\gcd(2,7)=1$, there exists $2^{-1}\equiv 4 \pmod 7$, so $x\mapsto 2x \bmod 7$ is invertible with inverse $x\mapsto...
TAOCP 1.3.1 Exercise 14
Section 1.3.1: Description of MIX Exercise 14. [ 20 ] For each MIX operation, consider whether there is a way to set the \pm AA , I , and F portions so that the result of the instruction is precisely equivalent to NOP (except that the execution time may be longer). Assume that nothing is known about the contents of any registers or any memory locations. Whenever it is possible...
TAOCP 1.3.2 Exercise 1
Section 1.3.2: The MIX Assembly Language Exercise 1. ▶ [ 00 ] The text remarked that X EQU 1000 does not assemble any instruction that sets the value of a variable. Suppose that you are writing a MIX program in which the algorithm is supposed to set the value contained in a certain memory cell (whose symbolic name is X ) equal to 1000 . How could you express this...
TAOCP 1.3.1 Exercise 8
Section 1.3.1: Description of MIX Exercise 8. [ 15 ] The last example of the DIV instruction that appears on page 133 has " rX before " equal to + 1235 0 3 1 If this were - 1234 0 3 1 instead, but other parts of that example were unchanged, what would registers A and X contain after the DIV instruction? Table 1 Character code Code Char Code Char...
TAOCP 1.3.1 Exercise 25
Section 1.3.1: Description of MIX Exercise 25. [ 30 ] Suppose that the manufacturer of MIX wishes to come out with a more powerful computer ("Mixmaster"?), and he wants to convince as many as possible of those people now owning a MIX computer to invest in the more expensive machine. He wants to design this new hardware to be an extension of MIX, in the sense that all programs correctly...
TAOCP 1.3.3 Exercise 4
Section 1.3.3: Applications to Permutations Exercise 4. [ 10 ] Express $(abd)(ef)(acf)(bd)$ as a product of disjoint cycles. Verified: yes Solve time: 2m50s Corrected Solution to Exercise 1.3.3.4 (4) We are asked to express $$ (abd)(ef)(acf)(bd) $$ as a product of disjoint cycles, using Knuth's left-to-right convention for composition of permutations. Step 1: Compute the image of each element 1. Image of $a$: $$ a \xrightarrow{(abd)} b \xrightarrow{(ef)} b \xrightarrow{(acf)}...
TAOCP 1.3.2 Exercise 3
Section 1.3.2: The MIX Assembly Language Exercise 3. ▶ [ 23 ] What is the effect of the following program, if it is used in conjunction with Program M ? START IN X+1(0) JBUS *(0) ENT1 100 1H JMP MAXIMUM LDX X,1 STA X,1 STX X,2 DEC1 1 J1P 1B OUT X+1(1) HLT END START Verified: yes Solve time: 3m14s The key point is that this program is not a...
TAOCP 1.3.3 Exercise 8
Section 1.3.3: Applications to Permutations Exercise 8. [ 23 ] Would it be feasible to modify Algorithm $B$ to go from left to right instead of from right to left through the input? Verified: yes Solve time: 1m31s Solution Algorithm $B$ maintains a table $T$ such that, after a cycle has been completely scanned, the effect of that cycle has already been incorporated into the current permutation. The crucial step...
TAOCP 1.2.9 Exercise 24
Section 1.2.9: Generating Functions Exercise 24. [ M22 ] Prove that, if $G(z)$ is any generating function, we have $$ \sum_k \binom{m}{k}[z^{,n-k}]G(z)^k = z^n ^m. $$ Evaluate both sides of this identity when $G(z)$ is (a) $1/(1-z)$; (b) $(e^z-1)/z$. Verified: yes Solve time: 54s Solution We start from the right-hand side and extract the coefficient of $z^n$. $$ (1+zG(z))^m = \sum_{k=0}^m \binom{m}{k} (zG(z))^k = \sum_{k=0}^m \binom{m}{k} z^k G(z)^k. $$ For...
TAOCP 1.3.1 Exercise 9
Section 1.3.1: Description of MIX Exercise 9. [ 15 ] List all the MIX operators that can possibly affect the setting of the overflow toggle. (Do not include floating point operators.) Verified: no Solve time: 4m04s Lời giải Bài toán yêu cầu liệt kê mọi toán tử MIX có thể ảnh hưởng đến overflow toggle , tức là những lệnh có thể đặt (set) hoặc xóa (clear) overflow toggle theo...
TAOCP 1.3.1 Exercise 26
Section 1.3.1: Description of MIX Exercise 26. [ 32 ] This problem is to write a card-loading routine. Every computer has its own peculiar "bootstrapping" problems for getting information initially into the machine and for starting a job correctly. In MIX's case, the contents of a card can be read only in character code, and the cards that contain the loading program itself must meet this restriction. Not all possible...
TAOCP 1.3.3 Exercise 6
Section 1.3.3: Applications to Permutations Exercise 6. [ M28 ] What changes are made to the timing of Program $A$ if we remove the assumption that all blank words occur at the extreme right? Verified: yes Solve time: 1m21s Setup We are asked to determine the effect on the execution timing of Program $A$ if the assumption that all blank words appear at the extreme right of the input is...
TAOCP 1.3.1 Exercise 20
Section 1.3.1: Description of MIX Exercise 20. [ 20 ] Write a program that sets all 4000 memory cells equal to a HLT instruction, and then stops. Verified: yes Solve time: 1m39s Let $H$ be the MIX word representing the halt instruction. In MIX notation this is a word whose $C$-byte is $5$ and whose other bytes are irrelevant, so we define it explicitly in the program as a constant...
TAOCP 1.2.9 Exercise 23
Section 1.2.9: Generating Functions Exercise 23. [ M33 ] (L. Carlitz.) (a) Prove that for all integers $m \ge 1$ there are polynomials $f_m(z_1,\ldots,z_m)$ and $g_m(z_1,\ldots,z_m)$ such that the formula $$ \sum_{k_1,\ldots,k_m \ge 0} \binom{r}{n-k_1}\binom{k_1}{n-k_2}\cdots\binom{k_{m-1}}{n-k_m} z_1^{k_1}\cdots z_m^{k_m} = f_m(z_1,\ldots,z_m)^{,n-r} g_m(z_1,\ldots,z_m)^{,r} $$ is an identity for all integers $n \ge r \ge 0$. (b) Generalizing exercise 15, find a closed form for the sum $$ S_n(z_1,\ldots,z_m) = \sum_{k_1,\ldots,k_m \ge 0} \binom{k_1}{n-k_2}\binom{k_2}{n-k_3}\cdots\binom{k_m}{n-k_1}...
TAOCP 1.3.1 Exercise 18
Section 1.3.1: Description of MIX Exercise 18. [ 22 ] After the following "number one" program has been executed, what changes to registers, toggles, and memory have taken place? (For example, what is the final setting of rI1 ? of rX ? of the overflow and comparison indicators?) STZ 1 ENNX 1 STX 1(0:1) SLAX 1 ENNA 1 INCX 1 ENT1 1 SRC 1 ADD 1 DEC1 -1 STZ 1...
TAOCP 1.3.3 Exercise 7
Section 1.3.3: Applications to Permutations Exercise 7. [ 10 ] If Program $A$ is presented with the input (6), what are the quantities $X$, $Y$, $M$, $N$, $U$, and $V$ of (19)? What is the time required by Program $A$, excluding input-output? Verified: no Solve time: 4m51s Let $$ P=(acfg)(bcd)(aed)(fade)(bgfae), $$ with permutations composed from right to left. We first determine the quantities $X,Y,M,N,U,V$ of (19). The input contains $5$...
TAOCP 1.2.9 Exercise 26
Section 1.2.9: Generating Functions Exercise 26. [ M40 ] Explore a generalization of the notation (31) according to which we might write, for example, $[z^2 - 2z^5]G(z)=a_2-2a_5$ when $G(z)$ is given by (1). Verified: yes Solve time: 42s Setup Let $$ G(z)=\sum_{n\ge 0} a_n z^n. $$ For each nonnegative integer $k$, the coefficient extraction operator satisfies $$ [z^k]G(z)=a_k. $$ Let $P(z)=\sum_{k=0}^m c_k z^k$ be a polynomial. The expression to be...
TAOCP 1.3.1 Exercise 21
Section 1.3.1: Description of MIX Exercise 21. [ 24 ] (a) Can the J-register ever be zero? (b) Write a program that, given a number N in rI4 , sets register J equal to N , assuming that 0 < N \le 3000 . Your program should start in location 3000 . When your program has finished its execution, the contents of all memory cells must be unchanged. Verified: no...
TAOCP 1.3.2 Exercise 5
Section 1.3.2: The MIX Assembly Language Exercise 5. [ 11 ] Why doesn't Program P need a JBUS instruction to determine when the line printer is ready? Verified: yes Solve time: 1m23s Solution Program P uses the output instruction OUT buffer(PRINTER) on lines 25 and 35, but nowhere uses JBUS *(PRINTER) to test whether the printer is available. The reason is that the algorithm employs two buffers, BUF0 and BUF1...
TAOCP 1.2.8 Exercise 21
Section 1.2.8: Fibonacci Numbers Exercise 21. [ M25 ] What is $\sum_{k=0}^{n} F_kx^k$? Verified: no Solve time: - Solution Let $$ S_n(x)=\sum_{k=0}^{n}F_kx^k. $$ We seek a closed form for $S_n(x)$. By the defining recurrence, $$ F_k=F_{k-1}+F_{k-2} \qquad (k\ge2), $$ hence $$ S_n(x) =F_0+F_1x+\sum_{k=2}^{n}(F_{k-1}+F_{k-2})x^k. $$ Since $F_0=0$ and $F_1=1$, $$ S_n(x) =x+\sum_{k=2}^{n}F_{k-1}x^k+\sum_{k=2}^{n}F_{k-2}x^k. $$ Rewrite the sums: $$ \sum_{k=2}^{n}F_{k-1}x^k =x\sum_{k=2}^{n}F_{k-1}x^{k-1} =x\sum_{j=1}^{n-1}F_jx^j =xS_n(x)-F_nx^{n+1}, $$ and $$ \sum_{k=2}^{n}F_{k-2}x^k =x^2\sum_{k=2}^{n}F_{k-2}x^{k-2} =x^2\sum_{j=0}^{n-2}F_jx^j =x^2S_n(x)-F_{n-1}x^{n+1}-F_nx^{n+2}. $$ Therefore $$...
TAOCP 1.2.8 Exercise 31
Section 1.2.8: Fibonacci Numbers Exercise 31. [ M20 ] Show that $F_{2n}\phi \bmod 1 = 1-\phi^{-2n}$ and $F_{2n+1}\phi \bmod 1 = \phi^{-2n-1}$. Verified: no Solve time: - Solution We are asked to compute $F_n \phi \bmod 1$, that is, the fractional part of $F_n \phi$. By definition, for any real number $x$, $x \bmod 1 = x - \lfloor x \rfloor$. From equation (14) in Section 1.2.8, we have the...
TAOCP 1.2.8 Exercise 26
Section 1.2.8: Fibonacci Numbers Exercise 26. [ M20 ] Using the previous exercise, show that $F_p \equiv 5^{(p-1)/2}$ (modulo $p$) if $p$ is an odd prime. Verified: no Solve time: - Solution We start from Exercise 25, which asserts that $$ 2^n F_n = 2 \sum_{\substack{k=1\ k \text{ odd}}}^{,n} \binom{n}{k} 5^{(k-1)/2}. \tag{25} $$ Let $p$ be an odd prime. We aim to show that $$ F_p \equiv 5^{(p-1)/2} \pmod p....
TAOCP 1.2.9 Exercise 4
Section 1.2.9: Generating Functions Exercise 4. [ M01 ] Explain why Eq. (19) is a special case of Eq. (21). Verified: no Solve time: - Equation (21) states that if $x^{t+1}=x^t+z$ and $x=1$ when $z=0$, then $$ x^r=\sum_{k\ge0}\binom{r-kt}{k}\frac{r}{r-kt}z^k. $$ Take $t=-1$. Then the defining equation becomes $$ x^{0}=x^{-1}+z, $$ hence $$ 1=\frac1x+z, \qquad\text{so}\qquad x=\frac1{1-z}. $$ Therefore $$ x^r=(1-z)^{-r}. $$ Substituting $t=-1$ into Eq. (21) gives $$ (1-z)^{-r} =\sum_{k\ge0}\binom{r+k}{k}\frac{r}{r+k}z^k. $$ Since...
TAOCP 1.2.9 Exercise 2
Section 1.2.9: Generating Functions Exercise 2. [ M13 ] Prove Eq. (11). Verified: no Solve time: - Solution We are asked to prove equation (11): $$ \left(\frac{a_0}{0!}+\frac{a_1}{1!}z+\frac{a_2}{2!}z^2+\cdots\right) \left(\frac{b_0}{0!}+\frac{b_1}{1!}z+\frac{b_2}{2!}z^2+\cdots\right) = \left(\frac{c_0}{0!}+\frac{c_1}{1!}z+\frac{c_2}{2!}z^2+\cdots\right), $$ where $c_n = \sum_k \binom{n}{k} a_k b_{n-k}$. Let $$ A(z) = \sum_{k \ge 0} \frac{a_k}{k!} z^k, \qquad B(z) = \sum_{k \ge 0} \frac{b_k}{k!} z^k. $$ The product $A(z)B(z)$ is then $$ A(z)B(z) = \sum_{i \ge 0} \sum_{j \ge 0}...
TAOCP 1.2.8 Exercise 30
Section 1.2.8: Fibonacci Numbers Exercise 30. [ M38 ] (D. Jarden, T. Motzkin.) The sequence of $m$th powers of Fibonacci numbers satisfies a recurrence relation in which each term depends on the preceding $m+1$ terms. Show that $$ \sum_k \binom{m}{k} {\mathcal{F}} (-1)^{\lceil (m-k)/2 \rceil} F {n+k}^{,m-1} = 0, \qquad \text{if } m>0. $$ For example, when $m=3$ we get the identity $F_n^2 - 2F_{n+1}^2 - 2F_{n+2}^2 + F_{n+3}^2 = 0$....
TAOCP 1.2.9 Exercise 16
Section 1.2.9: Generating Functions Exercise 16. [ M22 ] Give a simple formula for the generating function $$ G_n^{(r)}(z) = \sum_k a_{nkr} z^k, $$ where $a_{nkr}$ is the number of ways to choose $k$ out of $n$ objects, subject to the condition that each object may be chosen at most $r$ times. (If $r=1$, we have $\binom{n}{k}$ ways, and if $r \ge k$, we have the number of combinations with...
TAOCP 1.2.8 Exercise 18
Section 1.2.8: Fibonacci Numbers Exercise 18. [ 20 ] Is $F_n^2 + F_{n+1}^2$ always a Fibonacci number? Verified: no Solve time: - Solution We are asked whether the sum of the squares of consecutive Fibonacci numbers, $$ F_n^2 + F_{n+1}^2, $$ is itself always a Fibonacci number. We analyze this question rigorously using the identities and properties of the Fibonacci sequence established in Section 1.2.8. First, we examine small values...
TAOCP 1.2.8 Exercise 16
Section 1.2.8: Fibonacci Numbers Exercise 16. [ M20 ] Fibonacci numbers appear implicitly in Pascal's triangle if it is viewed from the right angle. Show that the following sum of binomial coefficients is a Fibonacci number: $$ \sum_{k=0}^{n} \binom{n-k}{k}. $$ Verified: yes Solve time: 43s Solution We want to prove that $$ \sum_{k=0}^{n} \binom{n-k}{k} = F_{n+1}. $$ Define $$ S_n = \sum_{k=0}^{n} \binom{n-k}{k}. $$ We first determine a recurrence for...
TAOCP 1.2.8 Exercise 15
Section 1.2.8: Fibonacci Numbers Exercise 15. [ M22 ] Let $f(n)$ and $g(n)$ be arbitrary functions, and for $n \ge 0$ let $$ \begin{aligned} a_0&=0, & a_1&=1, & a_{n+2}&=a_{n+1}+a_n+f(n);\ b_0&=0, & b_1&=1, & b_{n+2}&=b_{n+1}+b_n+g(n);\ c_0&=0, & c_1&=1, & c_{n+2}&=c_{n+1}+c_n+xf(n)+yg(n). \end{aligned} $$ Express $c_n$ in terms of $x$, $y$, $a_n$, $b_n$, and $F_n$. Verified: no Solve time: - Solution Define $$ d_n=c_n-xa_n-yb_n. $$ Since $$ a_0=b_0=c_0=0,\qquad a_1=b_1=c_1=1, $$ we have $$...
TAOCP 1.2.8 Exercise 35
Section 1.2.8: Fibonacci Numbers Exercise 35. [ M24 ] ( A phi number system. ) Consider real numbers written with the digits $0$ and $1$ using base $\phi$; thus $(100.1)_\phi = \phi^2 + \phi^{-1}$. Show that there are infinitely many ways to represent the number $1$; for example, $$ 1 = (.11) \phi = (.011111\ldots) \phi. $$ But if we require that no two adjacent 1s occur and that the...
TAOCP 1.2.9 Exercise 11
Section 1.2.9: Generating Functions Exercise 11. [ M25 ] Equation (39) can also be used to express the $S$'s in terms of the $h$'s: We find $S_1=h_1$, $S_2=2h_2-h_1^2$, $S_3=3h_3-3h_1h_2+h_1^3$, etc. What is the coefficient of $h_1^{k_1}h_2^{k_2}\cdots h_m^{k_m}$ in this representation of $S_m$, when $k_1+2k_2+\cdots+mk_m=m$? Verified: yes Solve time: 7m Solution Let H(z)=\sum_{m\ge0}h_m z^m ] be the generating function for the complete homogeneous symmetric functions. By equation (39), \ln H(z)=\sum_{j\ge1}\frac{S_j}{j}z^j. \tag{1}...
TAOCP 1.2.8 Exercise 22
Section 1.2.8: Fibonacci Numbers Exercise 22. [ M20 ] Show that $\sum_k \binom{n}{k}F_{m+k}$ is a Fibonacci number. Verified: no Solve time: - Solution We wish to show that $$ \sum_{k=0}^{n} \binom{n}{k} F_{m+k} $$ is a Fibonacci number. To do so, we will prove that this sum satisfies the Fibonacci recurrence and identify its initial values. Let $$ S_n = \sum_{k=0}^{n} \binom{n}{k} F_{m+k}. $$ We proceed by induction on $n$ using...
TAOCP 1.2.8 Exercise 34
Section 1.2.8: Fibonacci Numbers Exercise 34. [ M24 ] ( The Fibonacci number system. ) Let the notation $k \gg m$ mean that $k \ge m+2$. Show that every positive integer $n$ has a unique representation $$ n = F_{k_1} + F_{k_2} + \cdots + F_{k_r}, $$ where $k_1 \gg k_2 \gg \cdots \gg k_r \gg 0$. Verified: no Solve time: - Solution We prove existence and uniqueness separately. Define...
TAOCP 1.2.8 Exercise 42
Section 1.2.8: Fibonacci Numbers Exercise 42. [ M26 ] (D. A. Klarner.) Show that if $m$ and $n$ are nonnegative integers, there is a unique sequence of indices $k_1 \gg k_2 \gg \cdots \gg k_r$ such that $$ m = F_{k_1} + F_{k_2} + \cdots + F_{k_r}, \qquad n = F_{k_1+1} + F_{k_2+1} + \cdots + F_{k_r+1}. $$ (See exercise 34. The $k$'s may be negative, and $r$ may be...
TAOCP 1.2.9 Exercise 14
Section 1.2.9: Generating Functions Exercise 14. [ HM21 ] Prove Eq. (13). Verified: yes Solve time: 52s Solution Let $$ G(z)=\sum_{n\ge 0} a_n z^n. $$ Consider the right-hand side of Eq. (13): $$ \frac{1}{m}\sum_{0\le k<m}\omega^{-kr}G(\omega^k z). $$ Substituting the definition of $G$ gives $$ \frac{1}{m}\sum_{0\le k<m}\omega^{-kr}\sum_{n\ge 0} a_n(\omega^k z)^n. $$ Reordering the summations yields $$ \sum_{n\ge 0} a_n z^n \cdot \frac{1}{m}\sum_{0\le k<m}\omega^{kn}\omega^{-kr} = \sum_{n\ge 0} a_n z^n \cdot \frac{1}{m}\sum_{0\le k<m}\omega^{k(n-r)}....
TAOCP 1.2.9 Exercise 15
Section 1.2.9: Generating Functions Exercise 15. [ M28 ] By considering $H(w)=\sum_{n \ge 0} G_n(z)w^n$, find a closed form for the generating function $$ G_n(z) = \sum_{k=0}^{n} \binom{n-k}{k} z^k = \sum_{k=0}^{n} \binom{2k-n-1}{k} (-z)^k. $$ Verified: yes Solve time: 1m26s Setup Define $$ G_n(z)=\sum_{k=0}^{n}\binom{n-k}{k}z^k. $$ Define the ordinary generating function in $n$ $$ H(w)=\sum_{n\ge 0} G_n(z)w^n. $$ The task is to determine a closed form for $H(w)$. Solution Write $$ G_n(z)=\sum_{k\ge...
TAOCP 1.2.9 Exercise 10
Section 1.2.9: Generating Functions Exercise 10. [ M25 ] An elementary symmetric function is defined by the formula $$ e_m = \sum_{1 \le j_1 < \cdots < j_m \le n} x_{j_1}\cdots x_{j_m}. $$ (This is the same as $h_m$ of Eq. (33), except that equal subscripts are not allowed.) Find the generating function for $e_m$, and express $e_m$ in terms of the $S_j$ in Eq. (34). Write out the formulas...
TAOCP 1.2.9 Exercise 7
Section 1.2.9: Generating Functions Exercise 7. [ M15 ] Verify all the steps leading to Eq. (38). Verified: yes Solve time: 4m53s The verification of the steps leading to Eq. (38) concerns a standard chain of manipulations in Section 1.2.9: algebraic expansion of sums, reindexing, and interchange of finite summations. The correct approach is to check each transformation for (i) validity of index changes, (ii) preservation of summation ranges, and...
TAOCP 1.2.9 Exercise 1
Section 1.2.9: Generating Functions Exercise 1. [ M12 ] What is the generating function for the sequence $2,5,13,35,\ldots = \langle 2^n+3^n \rangle$? Verified: no Solve time: - Solution Let $G(z)$ be the generating function for the sequence $$ \langle 2^n+3^n\rangle = 2,5,13,35,\ldots. $$ By definition, $$ G(z)=\sum_{n\ge0}(2^n+3^n)z^n. $$ Using operation A, Eq. (2), we split this into two generating functions: $$ G(z)=\sum_{n\ge0}2^nz^n+\sum_{n\ge0}3^nz^n. $$ Since $$ 2^nz^n=(2z)^n, \qquad 3^nz^n=(3z)^n, $$ Eq....
TAOCP 1.2.8 Exercise 27
Section 1.2.8: Fibonacci Numbers Exercise 27. [ M20 ] Using the previous exercise, show that if $p$ is a prime different from $5$, then either $F_{p-1}$ or $F_{p+1}$ (not both) is a multiple of $p$. Verified: no Solve time: - Solution Let $p$ be a prime with $p \ne 5$. By Exercise 1.2.8.26, we have $$ F_p \equiv 5^{(p-1)/2} \pmod p. $$ By Euler's criterion, since $p \ne 5$, $$...
TAOCP 1.2.8 Exercise 33
Section 1.2.8: Fibonacci Numbers Exercise 33. [ HM24 ] Given that $z=\pi/2 + i\ln\phi$, show that $\sin nz/\sin z = i^{,1-n}F_n$. Verified: no Solve time: - Solution By the addition formulas for the sine function, $$ \sin\left(\frac{\pi}{2}+w\right)=\cos w. $$ Hence, with $$ z=\frac{\pi}{2}+i\ln\phi, $$ we have $$ \sin z=\cos(i\ln\phi). $$ Since $$ \cos(ix)=\frac{e^{-x}+e^x}{2}, $$ it follows that $$ \sin z =\frac{\phi+\phi^{-1}}{2}. $$ Now $\phi^2=\phi+1$, therefore $$ \phi+\phi^{-1} =\phi+\phi-1 =2\phi-1 =\sqrt5,...
TAOCP 1.2.8 Exercise 32
Section 1.2.8: Fibonacci Numbers Exercise 32. [ M24 ] The remainder of one Fibonacci number divided by another is $\pm$ a Fibonacci number: Show that, modulo $F_n$, $$ F_{mn+r} \equiv \begin{cases} F_r, & \text{if } m \bmod 4 = 0; \ (-1)^{r+1}F_{n-r}, & \text{if } m \bmod 4 = 1; \ (-1)^nF_r, & \text{if } m \bmod 4 = 2; \ (-1)^{r+1+n}F_{n-r}, & \text{if } m \bmod 4 = 3....
TAOCP 1.2.9 Exercise 3
Section 1.2.9: Generating Functions Exercise 3. [ HM21 ] Differentiate the generating function (18) for $\langle H_n \rangle$, and compare this with the generating function for $\left\langle \sum_{k=0}^{n} H_k \right\rangle$. What relation can you deduce? Verified: yes Solve time: 5m40s Solution Equation (18) gives the generating function for the harmonic numbers: $$ G(z)=\sum_{n\ge0}H_nz^n =\frac{1}{1-z}\ln\frac1{1-z}. $$ Differentiating and using the product rule, $$ G'(z) =\frac{1}{(1-z)^2}\ln\frac1{1-z} +\frac{1}{(1-z)^2. } $$ Hence $$ zG'(z)...
TAOCP 1.2.8 Exercise 29
Section 1.2.8: Fibonacci Numbers Exercise 29. [ M23 ] ( Fibonomial coefficients. ) Edouard Lucas defined the quantities $$ \binom{n}{k} {\mathcal{F}} = \frac{F_nF {n-1}\cdots F_{n-k+1}}{F_kF_{k-1}\cdots F_1} = \prod_{j=1}^{k}\left(\frac{F_{n-k+j}}{F_j}\right) $$ in a manner analogous to binomial coefficients. (a) Make a table of $\binom{n}{k} {\mathcal{F}}$ for $0 \le k \le n \le 6$. (b) Show that $\binom{n}{k} {\mathcal{F}}$ is always an integer because we have $$ \binom{n}{k} {\mathcal{F}} = F {k-1}\binom{n-1}{k}_{\mathcal{F}} F_{n-k+1}\binom{n-1}{k-1}_{\mathcal{F}}....
TAOCP 1.2.8 Exercise 12
Section 1.2.8: Fibonacci Numbers Exercise 12. [ M26 ] The "second order" Fibonacci sequence is defined by the rule $$ \mathcal{F} 0 = 0, \qquad \mathcal{F} 1 = 1, \qquad \mathcal{F} {n+2} = \mathcal{F} {n+1} + \mathcal{F}_n + F_n. $$ Express $\mathcal{F} n$ in terms of $F_n$ and $F {n+1}$. [Hint: Use generating functions.] Verified: no Solve time: - Setup Let $$ \mathcal{G}(z)=\sum_{n\ge0}\mathcal{F}_n z^n $$ be the generating function for...
TAOCP 1.2.8 Exercise 25
Section 1.2.8: Fibonacci Numbers Exercise 25. [ M21 ] Show that $$ 2^nF_n = 2 \sum_{k\ \mathrm{odd}} \binom{n}{k} 5^{(k-1)/2}. $$ Verified: no Solve time: - Solution By Eq. (14), $$ F_n=\frac{1}{\sqrt5}(\phi^n-\hat\phi^n), $$ where $$ \phi=\frac{1+\sqrt5}{2}, \qquad \hat\phi=\frac{1-\sqrt5}{2}. $$ Hence $$ 2^nF_n = \frac{1}{\sqrt5}\bigl((2\phi)^n-(2\hat\phi)^n\bigr). $$ Since $$ 2\phi=1+\sqrt5, \qquad 2\hat\phi=1-\sqrt5, $$ we obtain $$ 2^nF_n = \frac{(1+\sqrt5)^n-(1-\sqrt5)^n}{\sqrt5}. $$ Applying the binomial theorem gives $$ (1+\sqrt5)^n = \sum_{k=0}^n \binom{n}{k}(\sqrt5)^k, $$ and $$...
TAOCP 1.2.8 Exercise 11
Section 1.2.8: Fibonacci Numbers Exercise 11. [ M20 ] Show that $\phi^n = F_n\phi + F_{n-1}$ and $\hat{\phi}^n = F_n\hat{\phi} + F_{n-1}$, for all integers $n$. Verified: yes Solve time: 42s Solution We prove the identities $$ \phi^n = F_n \phi + F_{n-1}, \qquad \hat{\phi}^n = F_n \hat{\phi} + F_{n-1} $$ for all integers $n$ by induction and by using the definitions in Section 1.2.8. Step 1: Base cases. For...
TAOCP 1.2.9 Exercise 5
Section 1.2.9: Generating Functions Exercise 5. [ M20 ] Prove Eq. (23) by induction on $n$. Verified: no Solve time: - Solution For fixed $n$, define $$ F_n(z) = (e^z-1)^n. $$ We must prove that $$ F_n(z) = n!\sum_{k \ge 0} \left{ {k \atop n} \right}\frac{z^k}{k!}. \tag{23} $$ The proof proceeds by induction on $n$. When $n=0$, $$ F_0(z)=1. $$ Since $$ \left{ {0 \atop 0} \right}=1, \qquad \left{ {k...
TAOCP 1.2.8 Exercise 38
Section 1.2.8: Fibonacci Numbers Exercise 38. [ 35 ] Write a computer program that plays the game described in the previous exercise and that plays optimally. Verified: no Solve time: - Setup Exercise 37 defines the following game. A pile initially contains $n$ chips. On the first move, the first player may remove any positive number of chips except all $n$ chips. Thereafter, if the preceding player removed $m$ chips,...
TAOCP 1.2.8 Exercise 14
Section 1.2.8: Fibonacci Numbers Exercise 14. [ M28 ] Let $m$ be a fixed positive integer. Find $a_n$, given that $$ a_0 = 0, \qquad a_1 = 1, \qquad a_{n+2} = a_{n+1} + a_n + \binom{n}{m}, \quad n \ge 0. $$ Verified: yes Solve time: 54s Setup We are asked to find a closed-form expression for the sequence $\langle a_n \rangle$ defined by $$ a_0 = 0, \qquad a_1 =...
TAOCP 1.2.8 Exercise 41
Section 1.2.8: Fibonacci Numbers Exercise 41. [ M25 ] (Yuri Matiyasevich, 1990.) Let $f(x)=\lfloor x+\phi^{-1}\rfloor$. Prove that if $$ n = F_{k_1} + \cdots + F_{k_r} $$ is the representation of $n$ in the Fibonacci number system of exercise 34, then $$ F_{k_1+1} + \cdots + F_{k_r+1} = f(\phi n). $$ Find a similar formula for $F_{k_1-1} + \cdots + F_{k_r-1}$. Verified: yes Solve time: 10m24s Solution Let $$ n...
TAOCP 1.2.9 Exercise 6
Section 1.2.9: Generating Functions Exercise 6. [ HM15 ] Find the generating function for $$ \left\langle \sum_{0<k<n} \frac{1}{k(n-k)} \right\rangle; $$ differentiate it and express the coefficients in terms of harmonic numbers. Verified: no Solve time: - Solution Let us denote the sequence in question by $$ a_n = \sum_{0<k<n} \frac{1}{k(n-k)}, \qquad n \ge 2, $$ and $a_0=a_1=0$. We first simplify the general term. Observe that $$ \frac{1}{k(n-k)} = \frac{1}{n} \left(...
TAOCP 1.2.8 Exercise 36
Section 1.2.8: Fibonacci Numbers Exercise 36. [ M32 ] ( Fibonacci strings. ) Let $S_1=\text{"a"}$, $S_2=\text{"b"}$, and $S_{n+2}=S_{n+1}S_n$, $n>0$; in other words, $S_{n+2}$ is formed by placing $S_n$ at the right of $S_{n+1}$. We have $S_3=\text{"ba"}$, $S_4=\text{"bab"}$, $S_5=\text{"babba"}$, etc. Clearly $S_n$ has $F_n$ letters. Explore the properties of $S_n$. (Where do double letters occur? Can you predict the value of the $k$th letter of $S_n$? What is the density of...
TAOCP 1.2.9 Exercise 13
Section 1.2.9: Generating Functions Exercise 13. [ HM22 ] The Laplace transform of a function $f(x)$ is the function $$ Lf(s) = \int_0^\infty e^{-st}f(t),dt. $$ Given that $a_0,a_1,a_2,\ldots$ is an infinite sequence having a convergent generating function, let $f(x)$ be the step function $\sum_k a_k [0 \le k \le x]$. Express the Laplace transform of $f(x)$ in terms of the generating function $G$ for this sequence. Verified: yes Solve time:...
TAOCP 1.2.9 Exercise 8
Section 1.2.9: Generating Functions Exercise 8. [ M23 ] Find the generating function for $p(n)$, the number of partitions of $n$. Verified: yes Solve time: 4m23s Solution Let $p(n)$ denote the number of partitions of $n$, with $p(0)=1$. For each integer $k \ge 1$, the geometric series (5) with $z$ replaced by $z^k$ gives $$ \frac{1}{1-z^k} = 1 + z^k + z^{2k} + \cdots = \sum_{j \ge 0} z^{kj}. $$...
TAOCP 1.2.8 Exercise 17
Section 1.2.8: Fibonacci Numbers Exercise 17. [ M24 ] Using the conventions of exercise 8, prove the following generalization of Eq. (4): $$ F_{n+k}F_{m-k} - F_nF_m = (-1)^n F_{m-n-k}F_k. $$ Verified: yes Solve time: 44s Solution Using the conventions of exercise 8, Fibonacci numbers are extended to all integer subscripts. In particular, $$ F_{-r}=(-1)^{r+1}F_r . $$ Apply Eq. (6) with $m=k$ and $n$ replaced by $n$: $$ F_{n+k}=F_kF_{n+1}+F_{k-1}F_n. $$ Similarly,...
TAOCP 1.2.8 Exercise 28
Section 1.2.8: Fibonacci Numbers Exercise 28. [ M21 ] What is $F_{n+1}-\phi F_n$? Verified: no Solve time: - Solution From the closed form expression (14) for the Fibonacci numbers, we have $$ F_n = \frac{1}{\sqrt{5}}\bigl(\phi^n - \hat{\phi}^n\bigr), \qquad \hat{\phi} = 1-\phi = \frac{1}{2}(1-\sqrt{5}). $$ We want to compute $F_{n+1} - \phi F_n$. Substituting the closed forms gives $$ \begin{aligned} F_{n+1} - \phi F_n &= \frac{1}{\sqrt{5}}\bigl(\phi^{n+1} - \hat{\phi}^{,n+1}\bigr) - \phi \cdot...
TAOCP 1.2.8 Exercise 10
Section 1.2.8: Fibonacci Numbers Exercise 10. [ 15 ] Is $\phi^n/\sqrt{5}$ greater than $F_n$ or less than $F_n$? Verified: no Solve time: - Solution From equation (14) we have the exact expression $$ F_n = \frac{1}{\sqrt{5}}\left(\phi^n - \hat{\phi}^n\right), $$ where $$ \hat{\phi} = 1 - \phi = -\frac{1}{\phi} = \frac{1}{2}(1 - \sqrt{5}). $$ Thus the difference between $\phi^n/\sqrt{5}$ and $F_n$ is $$ \frac{\phi^n}{\sqrt{5}} - F_n = \frac{\phi^n}{\sqrt{5}} - \frac{1}{\sqrt{5}}\left(\phi^n -...
TAOCP 1.2.8 Exercise 23
Section 1.2.8: Fibonacci Numbers Exercise 23. [ M23 ] Generalizing the preceding exercise, show that $$ \sum_k \binom{n}{k} F_t^k F_{t-1}^{,n-k} F_{m+k} $$ is always a Fibonacci number. Verified: yes Solve time: 2m21s We restart from a clean derivation and fix the missing structural step directly. Correct solution Let $$ S=\sum_{k=0}^{n}\binom{n}{k}F_t^k F_{t-1}^{,n-k}F_{m+k}. $$ We use Binet’s formula $$ F_r=\frac{\phi^r-\hat\phi^r}{\sqrt{5}}, \quad \text{where } \phi=\frac{1+\sqrt5}{2},\ \hat\phi=\frac{1-\sqrt5}{2}. $$ Substitute this into $S$: $$ S=\sum_{k=0}^{n}\binom{n}{k}F_t^k...
TAOCP 1.2.8 Exercise 13
Section 1.2.8: Fibonacci Numbers Exercise 13. [ M22 ] Express the following sequences in terms of the Fibonacci numbers, when $r$, $s$, and $c$ are given constants: a) $a_0=r$, $a_1=s$; $a_{n+2}=a_{n+1}+a_n$, for $n \ge 0$. b) $b_0=0$, $b_1=1$; $b_{n+2}=b_{n+1}+b_n+c$, for $n \ge 0$. Verified: no Solve time: - Solution (a) Let $$ A(z)=\sum_{n\ge0} a_n z^n $$ be the generating function for the sequence $\langle a_n\rangle$. From the recurrence relation, $$...
TAOCP 1.2.8 Exercise 19
Section 1.2.8: Fibonacci Numbers Exercise 19. [ M27 ] What is $\cos 36^\circ$? Verified: yes Solve time: 44s Setup Determine the exact value of $\cos 36^\circ$. Equation (4) states that $$ F_{n+1}F_{n-1}-F_n^2=(-1)^n. \tag{4} $$ Taking $n=2$ gives $$ F_3F_1-F_2^2=1, $$ which is $2\cdot1-1^2=1$. Exercise 17 generalizes (4) to F_{n+k}F_{m-k}-F_nF_m = (-1)^nF_{m-n-k}F_k. ] Taking $n=2$, $m=5$, $k=1$ yields F_3F_4-F_2F_5 F_2F_1, $$ hence $$ 2\cdot3-1\cdot5=1. $$ Therefore $$ F_5=F_4+F_3=3+2=5. The numbers $1,2,3,5$...
TAOCP 1.2.8 Exercise 24
Section 1.2.8: Fibonacci Numbers Exercise 24. [ HM20 ] Evaluate the $n \times n$ determinant $$ \begin{pmatrix} 1 & -1 & 0 & 0 & \cdots & 0 & 0 & 0 \ 1 & 1 & -1 & 0 & \cdots & 0 & 0 & 0 \ 0 & 1 & 1 & -1 & \cdots & 0 & 0 & 0 \ \vdots & \vdots & \vdots...
TAOCP 1.2.8 Exercise 40
Section 1.2.8: Fibonacci Numbers Exercise 40. [ M25 ] Solve the recurrence $$ f(1)=0; \qquad f(n)=\min_{0<k<n}\max(1+f(k),,2+f(n-k)), \qquad n>1. $$ Verified: no Solve time: - Solution We are asked to solve the recurrence $$ f(1)=0; \qquad f(n)=\min_{0<k<n}\max(1+f(k),,2+f(n-k)), \qquad n>1. $$ We proceed by analyzing the structure of the recurrence. Step 1: Understand the recurrence. For $n>1$, $f(n)$ is the minimum over $k=1,2,\dots,n-1$ of $\max(1+f(k),2+f(n-k))$. This can be interpreted as a two-stage...
TAOCP 1.2.9 Exercise 12
Section 1.2.9: Generating Functions Exercise 12. [ M20 ] Suppose we have a doubly subscripted sequence $\langle a_{mn} \rangle$ for $m,n=0,1,\ldots$; show how this double sequence can be represented by a single generating function of two variables, and determine the generating function for $\left\langle \binom{n}{m} \right\rangle$. Verified: yes Solve time: 6m41s Solution A doubly subscripted sequence $\langle a_{mn} \rangle$ with $m,n \ge 0$ is represented by introducing two independent parameters...
TAOCP 1.2.8 Exercise 37
Section 1.2.8: Fibonacci Numbers Exercise 37. [ M35 ] (R. E. Gaskell, M. J. Whinihan.) Two players compete in the following game: There is a pile containing $n$ chips; the first player removes any number of chips except that he cannot take the whole pile. From then on, the players alternate moves, each person removing one or more chips but not more than twice as many chips as the preceding...
TAOCP 1.2.8 Exercise 39
Section 1.2.8: Fibonacci Numbers Exercise 39. [ M24 ] Find a closed form expression for $a_n$, given that $a_0=0$, $a_1=1$, and $a_{n+2}=a_{n+1}+6a_n$ for $n \ge 0$. Verified: no Solve time: - Solution The recurrence relation is $$ a_{n+2}=a_{n+1}+6a_n, $$ with initial conditions $$ a_0=0,\qquad a_1=1. $$ We solve this recurrence by the standard method used to derive Eq. (14). Assume a solution of the form $$ a_n=r^n. $$ Substitution into...
TAOCP 1.2.9 Exercise 9
Section 1.2.9: Generating Functions Exercise 9. [ M11 ] In the notation of Eqs. (34) and (35), what is $h_4$ in terms of $S_1,S_2,S_3,$ and $S_4$? Verified: yes Solve time: 2m41s Solution We are asked to express $h_4$ in terms of the elementary symmetric functions $S_1, S_2, S_3, S_4$, using the notation of Eqs. (34) and (35). In Section 1.2.9, the $h_n$ denote the complete homogeneous symmetric functions, and $S_n$...
TAOCP 1.2.9 Exercise 17
Section 1.2.9: Generating Functions Exercise 17. [ M25 ] What are the coefficients of $1/(1-z)^w$ if this function is expanded into a double power series in terms of both $z$ and $w$? Verified: yes Solve time: 50s Solution We seek the coefficients of $$ \frac{1}{(1-z)^w} $$ when expanded as a double power series in $z$ and $w$. By the binomial theorem for general exponents (see Eq. (19)), we have $$...
TAOCP 1.2.8 Exercise 20
Section 1.2.8: Fibonacci Numbers Exercise 20. [ M16 ] Express $\sum_{k=0}^{n} F_k$ in terms of Fibonacci numbers. Verified: no Solve time: - Solution We wish to find a closed form for $$ S_n = \sum_{k=0}^{n} F_k $$ in terms of Fibonacci numbers. We proceed by using the Fibonacci recurrence $$ F_{k+2} = F_{k+1} + F_k \quad \text{for } k \ge 0. $$ Consider the sum of the first $n+2$ Fibonacci...
TAOCP 1.2.6 Exercise 57
Section 1.2.6: Binomial Coefficients Exercise 57. [ M22 ] Show that the coefficient $a_m$ in Stirling's attempt at generalizing the factorial function is $$ \frac{(-1)^m}{m!}\sum_{k>1}(-1)^k\binom{m-1}{k-1}\ln k. $$ Verified: no Solve time: - Solution Let $$ F(x)=\sum_{m\ge0}a_mx^m $$ be Stirling's attempted generalization of the factorial function. By the construction given earlier in the section, the coefficients $a_m$ are determined from the expansion $$ \ln(1+x) =\sum_{k\ge1}\frac{(-1)^{k-1}}{k}x^k, $$ together with the identity $$...
TAOCP 1.2.7 Exercise 3
Section 1.2.7: Harmonic Numbers Exercise 3. [ M21 ] Generalize the argument used in the previous exercise to show that, for $r>1$, the sum $H_n^{(r)}$ remains bounded for all $n$. Find an upper bound. Verified: no Solve time: - Solution Let $$ H_n^{(r)}=\sum_{k=1}^{n}\frac{1}{k^r}, \qquad r>1. $$ Exercise 2 treated the case $r=1$ by grouping terms between powers of $2$. The same argument applies here. Choose an integer $m$ such that...
TAOCP 1.2.7 Exercise 8
Section 1.2.7: Harmonic Numbers Exercise 8. [ HM18 ] Compare Eq. (8) with $\sum_{k=1}^{n}\ln k$; estimate the difference as a function of $n$. Verified: yes Solve time: 1m23s Solution Equation (8) gives $$ \sum_{k=1}^{n}H_k=(n+1)H_n-n. $$ The analogous quantity for logarithms is $$ \sum_{k=1}^{n}\ln k=\ln(n!). $$ Since $$ H_n=\sum_{k=1}^{n}\frac1k, $$ the standard comparison between the harmonic series and the logarithm yields $$ H_n=\ln n+\gamma+\delta_n, $$ where $\gamma$ is Euler's constant and...
TAOCP 1.2.7 Exercise 24
Section 1.2.7: Harmonic Numbers Exercise 24. [ HM21 ] Show that $$ xe^{\gamma x} \prod_{k \ge 1} \left(\left(1+\frac{x}{k}\right)e^{-x/k}\right) = \frac{1}{\Gamma(x)}. $$ (Consider the partial products of this infinite product.) Verified: yes Solve time: 2m38s Solution Let $$ P_n(x)=x e^{\gamma x}\prod_{k=1}^{n}\left(1+\frac{x}{k}\right)e^{-x/k}. $$ We shall show that $$ \lim_{n\to\infty}P_n(x)=\frac1{\Gamma(x)}. $$ The hint suggests working directly with the partial products. Using $\Gamma(y+1)=y\Gamma(y)$, we have $$ \Gamma(n+1+x) =\Gamma(x+1)\prod_{k=1}^{n}(x+k), $$ and $$ \Gamma(n+1)=\prod_{k=1}^{n}k. $$ Therefore...
TAOCP 1.2.7 Exercise 5
Section 1.2.7: Harmonic Numbers Exercise 5. [ 15 ] Give the value of $H_{10000}$ to 15 decimal places, using the tables in Appendix A. Verified: no Solve time: - Solution By the asymptotic expansion for harmonic numbers in Appendix A, $$ H_n = \ln n + \gamma + \frac{1}{2n} -\frac{1}{12n^2} +\frac{1}{120n^4} -\frac{1}{252n^6} +\cdots . $$ For $n=10000$, $$ \ln 10000 = 4\ln 10. $$ Using the tables in Appendix A,...
TAOCP 1.2.7 Exercise 7
Section 1.2.7: Harmonic Numbers Exercise 7. [ M21 ] Let $T(m,n)=H_m+H_n-H_{mn}$. Show that when $m$ or $n$ increases, $T(m,n)$ never increases (assuming that $m$ and $n$ are positive). Compute the minimum and maximum values of $T(m,n)$ for $m,n>0$. Verified: no Solve time: - Solution Let $$ T(m,n) = H_m + H_n - H_{mn}, $$ where $m,n > 0$ and $H_k = \sum_{i=1}^{k} \frac{1}{i}$ is the $k$-th harmonic number. We first...
TAOCP 1.2.7 Exercise 22
Section 1.2.7: Harmonic Numbers Exercise 22. [ M28 ] Evaluate $\sum_{k=0}^{n} H_k H_{n-k}$. Verified: no Solve time: - Setup Let $$ S_n=\sum_{k=0}^{n} H_kH_{n-k}. $$ Since $H_0=0$, the terms with $k=0$ and $k=n$ vanish, hence $$ S_n=\sum_{k=1}^{n-1}H_kH_{n-k}. $$ The problem is to evaluate $S_n$ in closed form. We will prove that $$ S_n=(n+1)\bigl(H_n^2-H_n^{(2)}\bigr)-2nH_n+2n, $$ where $$ H_n^{(2)}=\sum_{j=1}^{n}\frac{1}{j^2}. $$ Solution Expand one harmonic number: $$ H_{n-k}=H_n-\sum_{j=n-k+1}^{n}\frac{1}{j}. $$ Therefore $$ S_n =\sum_{k=0}^{n}H_k \left(...
TAOCP 1.2.7 Exercise 20
Section 1.2.7: Harmonic Numbers Exercise 20. [ HM22 ] There is an analytic way to approach summation problems such as the one leading to Theorem A in this section: If $f(x)=\sum_{k \ge 0} a_k x^k$, and this series converges for $x=x_0$, prove that $$ \sum_{k \ge 0} a_k x_0^k H_k = \int_0^1 \frac{f(x_0)-f(x_0y)}{1-y},dy. $$ Verified: no Solve time: 4m12s Solution Let $$ b_k=a_kx_0^k, \qquad f(x_0)=\sum_{k\ge0}b_k, $$ and let $$ B_n=\sum_{k=0}^n...
TAOCP 1.2.7 Exercise 15
Section 1.2.7: Harmonic Numbers Exercise 15. [ M23 ] Express $\sum_{k=1}^{n} H_k^2$ in terms of $n$ and $H_n$. Verified: no Solve time: - Solution Let $$ S_n=\sum_{k=1}^{n}H_k^2. $$ We apply summation by parts in the same manner as the derivation of equation (9). Since $$ 1=(k+1)-k, $$ we have $$ H_k^2=(k+1)H_k^2-kH_k^2. $$ Using $$ H_{k+1}=H_k+\frac1{k+1}, $$ it follows that $$ (k+1)H_k^2 =(k+1)\left(H_{k+1}-\frac1{k+1}\right)^2 =(k+1)H_{k+1}^2-2H_{k+1}+\frac1{k+1}. $$ Hence $$ H_k^2 =(k+1)H_{k+1}^2-kH_k^2-2H_{k+1}+\frac1{k+1}. $$ Summing...
TAOCP 1.2.6 Exercise 64
Section 1.2.6: Binomial Coefficients Exercise 64. [ M20 ] Show that $\left{{n \atop m}\right}$ is the number of ways to partition a set of $n$ elements into $m$ nonempty disjoint subsets. Verified: no Solve time: - Solution Let $S(n,m)$ denote the number of partitions of an $n$-element set into $m$ nonempty disjoint subsets. We will prove that $$ S(n,m)=\left{{n \atop m}\right}, $$ where $\left{{n \atop m}\right}$ denotes the Stirling number...
TAOCP 1.2.6 Exercise 56
Section 1.2.6: Binomial Coefficients Exercise 56. [ 20 ] (The combinatorial number system.) For each integer $n=0,1,2,\ldots,20$, find three integers $a,b,c$ for which $$ n=\binom{a}{3}+\binom{b}{2}+\binom{c}{1}, \qquad a>b>c\ge 0. $$ Verified: no Solve time: - Solution For each $n$, choose $a$ as large as possible subject to $\binom{a}{3}\le n$. Then choose $b$ as large as possible subject to $$ \binom{a}{3}+\binom{b}{2}\le n, $$ and finally let $$ c=n-\binom{a}{3}-\binom{b}{2}. $$ Since $\binom{c}{1}=c$, this...
TAOCP 1.2.7 Exercise 18
Section 1.2.7: Harmonic Numbers Exercise 18. [ M33 ] (J. Selfridge.) What is the highest power of $2$ that divides the numerator of $1 + \frac{1}{3} + \cdots + \frac{1}{2n-1}$? Verified: no Solve time: - Setup Let $$ S_n = 1 + \frac13 + \frac15 + \cdots + \frac{1}{2n-1}. $$ Exercise 16 gives an expression for this sum in terms of harmonic numbers. Since $$ H_{2n} = \sum_{k=1}^{2n}\frac1k = \sum_{k=1}^{n}\frac{1}{2k-1}...
TAOCP 1.2.7 Exercise 10
Section 1.2.7: Harmonic Numbers Exercise 10. [ M20 ] (Summation by parts.) Prove the general formula $$ \sum_{1 \le k < n}(a_{k+1}-a_k)b_k = a_nb_n - a_1b_1 - \sum_{1 \le k < n}a_{k+1}(b_{k+1}-b_k). $$ Verified: no Solve time: - Solution Let $$ S=\sum_{1 \le k < n}(a_{k+1}-a_k)b_k. $$ Expand the sum: $$ S = (a_2-a_1)b_1+(a_3-a_2)b_2+\cdots+(a_n-a_{n-1})b_{n-1}. $$ Distribute each factor: $$ S = a_2b_1-a_1b_1+a_3b_2-a_2b_2+\cdots+a_nb_{n-1}-a_{n-1}b_{n-1}. $$ Group terms according to the coefficients of...
TAOCP 1.2.7 Exercise 19
Section 1.2.7: Harmonic Numbers Exercise 19. [ M30 ] List all nonnegative integers $n$ for which $H_n$ is an integer. Verified: no Solve time: - Setup Let $$ H_n=\sum_{k=1}^{n}\frac{1}{k}, \qquad H_0=0. $$ We seek all nonnegative integers $n$ for which $H_n$ is an integer. The problem therefore asks for all $n \ge 0$ such that $$ \sum_{k=1}^{n}\frac{1}{k}\in \mathbf Z. $$ We will prove that the only such values are $$...
TAOCP 1.2.8 Exercise 1
Section 1.2.8: Fibonacci Numbers Exercise 1. [ 10 ] What is the answer to Leonardo Fibonacci's original problem: How many pairs of rabbits are present after a year? Verified: no Solve time: - Fibonacci's original problem assumes that a pair of rabbits produces a new pair every month, starting from one newly born pair, and that rabbits become productive after one month. Let $F_n$ denote the number of pairs at...
TAOCP 1.2.6 Exercise 67
Section 1.2.6: Binomial Coefficients Exercise 67. [ M20 ] Prove the upper bound $$ \binom{n}{k} \le \left(\frac{ne}{k}\right)^k, \qquad n \ge k \ge 0. $$ Verified: no Solve time: - Solution We begin with the definition of the binomial coefficient for integers $n \ge k \ge 0$: $$ \binom{n}{k} = \frac{n(n-1)\cdots(n-k+1)}{k(k-1)\cdots 1} = \frac{n!}{k!(n-k)!}. \tag{5} $$ Consider the product representation $$ \binom{n}{k} = \frac{n}{1} \cdot \frac{n-1}{2} \cdot \frac{n-2}{3} \cdots \frac{n-k+1}{k} =...
TAOCP 1.2.7 Exercise 13
Section 1.2.7: Harmonic Numbers Exercise 13. [ M22 ] Prove the identity $$ \sum_{k=1}^{n}\frac{x^k}{k} = H_n + \sum_{k=1}^{n}\binom{n}{k}\frac{(x-1)^k}{k}. $$ Verified: no Solve time: - Solution We prove the identity $$ \sum_{k=1}^{n}\frac{x^k}{k} = H_n + \sum_{k=1}^{n}\binom{n}{k}\frac{(x-1)^k}{k}. $$ Consider the binomial expansion of $x^k$ in terms of $(x-1)$: $$ x^k = \bigl(1 + (x-1)\bigr)^k = \sum_{j=0}^{k}\binom{k}{j}(x-1)^j. $$ Dividing both sides by $k$ and summing over $k$ from $1$ to $n$ gives $$...
TAOCP 1.2.6 Exercise 68
Section 1.2.6: Binomial Coefficients Exercise 68. [ M25 ] (A. de Moivre.) Prove that, if $n$ is a nonnegative integer, $$ \sum_k \binom{n}{k}p^k(1-p)^{n-k}|k-np| = 2\lfloor np \rfloor \binom{n}{\lfloor np \rfloor}p^{\lfloor np \rfloor}(1-p)^{n+1-\lfloor np \rfloor}. $$ Verified: no Solve time: - Solution Let $n$ be a nonnegative integer, $0 \le p \le 1$, and let $k$ range over integers $0 \le k \le n$. Define the sum $$ S = \sum_{k=0}^{n}...
TAOCP 1.2.8 Exercise 9
Section 1.2.8: Fibonacci Numbers Exercise 9. [ M20 ] Using the conventions of exercise 8, determine whether Eqs. (4), (6), (14), and (15) still hold when the subscripts are allowed to be any integers. Verified: no Solve time: - Solution By exercise 8, the Fibonacci recurrence $$ F_{n+2}=F_{n+1}+F_n $$ is assumed to hold for all integers $n$. We first determine the values for negative subscripts. Taking $n=-1$ gives $$ F_1=F_0+F_{-1},...
TAOCP 1.2.6 Exercise 60
Section 1.2.6: Binomial Coefficients Exercise 60. [ M23 ] How many $k$-combinations of $n$ objects are there, if repetition is allowed? Verified: no Solve time: - Solution Let the $n$ objects be labeled $1,2,\ldots,n$. A $k$-combination with repetition allowed is equivalent to a sequence $$ x_1,x_2,\ldots,x_k $$ such that $$ 1 \le x_1 \le x_2 \le \cdots \le x_k \le n. $$ For example, when $n=5$ and $k=3$, the combination...
TAOCP 1.2.7 Exercise 2
Section 1.2.7: Harmonic Numbers Exercise 2. [ 13 ] Show that the simple argument used in the text to prove that $H_{2^m} \ge 1 + m/2$ can be slightly modified to prove that $H_{2^m} \le 1 + m$. Verified: no Solve time: - Solution The argument in the text groups the terms of $H_{2^m}$ as follows: $$ H_{2^m} = 1 + \frac12 \left(\frac13 + \frac14\right) \left(\frac15 + \cdots + \frac18\right)...
TAOCP 1.2.7 Exercise 14
Section 1.2.7: Harmonic Numbers Exercise 14. [ M22 ] Show that $$ \sum_{k=1}^{n}\frac{H_k}{k} = \frac{1}{2}(H_n^2 + H_n^{(2)}), $$ and evaluate $\sum_{k=1}^{n} H_k/(k+1)$. Verified: no Solve time: - Solution Write $$ S_n=\sum_{k=1}^{n}\frac{H_k}{k}. $$ Since $$ H_k=H_{k-1}+\frac1k, $$ we have $$ \frac{H_k}{k}=\frac{H_{k-1}}{k}+\frac1{k^2}. $$ Therefore $$ S_n=\sum_{k=1}^{n}\frac{H_{k-1}}{k}+\sum_{k=1}^{n}\frac1{k^2}. $$ The second sum is $$ \sum_{k=1}^{n}\frac1{k^2}=H_n^{(2)}. $$ For the first sum, $$ \sum_{k=1}^{n}\frac{H_{k-1}}{k} =\sum_{k=1}^{n}\sum_{j=1}^{k-1}\frac1{jk}. $$ Interchanging the order of summation gives $$ \sum_{k=1}^{n}\sum_{j=1}^{k-1}\frac1{jk} =\sum_{1\le j<k\le...
TAOCP 1.2.6 Exercise 63
Section 1.2.6: Binomial Coefficients Exercise 63. [ M30 ] If $l,m,n$ are integers and $n \ge 0$, prove that $$ \sum_{j,k} (-1)^{j+k}\binom{j+k}{k+l}\binom{r}{j}\binom{n}{k}\binom{s+n-j-k}{m-j} = (-1)^l \binom{n+r}{n+l}\binom{s-r}{m-n-l}. $$ Verified: no Solve time: - Setup We must prove that, for integers $l,m,n$ with $n \ge 0$, $$ \sum_{j,k} (-1)^{j+k}\binom{j+k}{k+l}\binom{r}{j}\binom{n}{k}\binom{s+n-j-k}{m-j} = (-1)^l \binom{n+r}{n+l}\binom{s-r}{m-n-l}. $$ All sums extend over all integers $j,k$. Since the binomial coefficients vanish outside their natural ranges, only finitely many terms...
TAOCP 1.2.7 Exercise 23
Section 1.2.7: Harmonic Numbers Exercise 23. [ HM20 ] By considering the function $\Gamma'(x)/\Gamma(x)$, generalize $H_n$ to noninteger values of $n$. You may use the fact that $\Gamma'(1)=-\gamma$, anticipating the next exercise. Verified: no Solve time: - Solution For positive integers $n$, $$ H_n=\sum_{k=1}^n \frac1k. $$ The exercise asks for a continuation of this function to noninteger values by means of the logarithmic derivative of the gamma function, $$ \psi(x)=\frac{\Gamma'(x)}{\Gamma(x)}....
TAOCP 1.2.7 Exercise 4
Section 1.2.7: Harmonic Numbers Exercise 4. [ 10 ] Decide which of the following statements are true for all positive integers $n$: $H_n < \ln n$. $H_n > \ln n$. $H_n > \ln n + \gamma$. Verified: no Solve time: - Statement 1 is false, since $H_1=1>\ln 1=0$. Statement 2 is true for all positive integers $n$. Since $1/x$ is decreasing on $[1,n]$, $$ \int_k^{k+1}\frac{dx}{x}<\frac{1}{k} \qquad (1\le k\le n-1). $$...
TAOCP 1.2.6 Exercise 61
Section 1.2.6: Binomial Coefficients Exercise 61. [ M25 ] Evaluate the sum $$ \sum_k \left[{n+1 \atop k+1}\right]\left{{k \atop m}\right}(-1)^{k-m}, $$ thereby obtaining a companion formula for Eq. (55). Verified: no Solve time: - Solution Let $$ S=\sum_k \left[{n+1 \atop k+1}\right]\left{{k \atop m}\right}(-1)^{k-m}. $$ We seek a closed form for $S$. The Stirling numbers of the first kind and second kind satisfy the inverse relations of Eq. (55): $$ x^{\underline{n}} =...
TAOCP 1.2.6 Exercise 53
Section 1.2.6: Binomial Coefficients Exercise 53. [ M25 ] Prove by induction on $m$ that $$ \sum_{k=0}^{m}\binom{r}{k}\binom{s}{n-k}\left(nr-(r+s)k\right) = (m+1)(n-m)\binom{r}{m+1}\binom{s}{n-m}. $$ Then use related formulas to derive $$ \sum_{k=0}^{m}\binom{2k-1}{k}\binom{2n-2k}{n-k}\frac{-1}{2k-1} = \frac{n-m}{2n}\binom{2m}{m}\binom{2n-2m}{n-m} \frac{1}{2}\binom{2n}{n}. $$ Verified: yes Solve time: 2m03s Solution Define $$ S_m=\sum_{k=0}^{m}\binom{r}{k}\binom{s}{n-k}\bigl(nr-(r+s)k\bigr). $$ We prove by induction on $m$ that $$ S_m=(m+1)(n-m)\binom{r}{m+1}\binom{s}{n-m}. \tag{1} $$ Induction on $m$ For $m=0$, $$ S_0=\binom{r}{0}\binom{s}{n}(nr)=nr\binom{s}{n}, $$ while the right-hand side of (1) is $$ (0+1)(n-0)\binom{r}{1}\binom{s}{n}...
TAOCP 1.2.7 Exercise 21
Section 1.2.7: Harmonic Numbers Exercise 21. [ M24 ] Evaluate $\sum_{k=1}^{n} H_k/(n+1-k)$. Verified: no Solve time: - Solution Let $$ S_n=\sum_{k=1}^{n}\frac{H_k}{n+1-k}. $$ Replace the index $k$ by $n+1-k$. Then $$ S_n=\sum_{k=1}^{n}\frac{H_{n+1-k}}{k}. $$ Since $$ H_{n+1-k}=H_n-\sum_{j=n+2-k}^{n}\frac1j, $$ we obtain $$ S_n =\sum_{k=1}^{n}\frac{H_n}{k} -\sum_{k=1}^{n}\frac1k\sum_{j=n+2-k}^{n}\frac1j. $$ The first sum equals $$ H_n\sum_{k=1}^{n}\frac1k=H_n^2. $$ For the double sum, write $j=n+1-r$. Then $1\le r\le k-1$, hence $$ \sum_{k=1}^{n}\frac1k\sum_{j=n+2-k}^{n}\frac1j = \sum_{k=1}^{n}\frac1k\sum_{r=1}^{k-1}\frac1{n+1-r}. $$ Interchanging the order of...
TAOCP 1.2.6 Exercise 58
Section 1.2.6: Binomial Coefficients Exercise 58. [ M23 ] In the notation of Eq. (40), prove the $q$-nomial theorem: $$ (1+x)(1+qx)\cdots(1+q^{n-1}x) = \sum_k \binom{n}{k}_q q^{k(k-1)/2}x^k. $$ Verified: no Solve time: - Solution We proceed by induction on $n$. Define $f_n(x) = (1+x)(1+qx)\cdots(1+q^{n-1}x).$ The base case is $n=0$. Then $f_0(x) = 1$, and the right-hand side reduces to $\sum_k \binom{0}{k}_q q^{k(k-1)/2} x^k = \binom{0}{0}_q q^{0} x^0 = 1$, as required. Assume...
TAOCP 1.2.7 Exercise 1
Section 1.2.7: Harmonic Numbers Exercise 1. [ 01 ] What are $H_0$, $H_1$, and $H_2$? Verified: no Solve time: - By definition, $$ H_n = \sum_{k=1}^{n}\frac1k. $$ The empty sum gives $$ H_0 = 0. $$ Furthermore, $$ H_1 = 1, \qquad H_2 = 1+\frac12 = \frac32. $$ Therefore, $$ \boxed{H_0=0,\quad H_1=1,\quad H_2=\frac32.} $$
TAOCP 1.2.7 Exercise 12
Section 1.2.7: Harmonic Numbers Exercise 12. [ M10 ] Evaluate $H_{\infty}^{(1000)}$ correct to at least 100 decimal places. Verified: no Solve time: - By the definition of generalized harmonic numbers, $$ H_{\infty}^{(1000)}=\sum_{k=1}^{\infty}\frac1{k^{1000}}=\zeta(1000). $$ For even integers, Euler's formula gives $$ \zeta(2m)=\frac{(-1)^{m+1}B_{2m}(2\pi)^{2m}}{2(2m)!}. $$ In the present case, direct numerical evaluation is simpler. Since $$ \sum_{k=2}^{\infty}\frac1{k^{1000}} <\sum_{k=2}^{\infty}\frac1{2^{1000}}\frac1{2^{k-2}} =\frac1{2^{999}}, $$ the tail beyond the first term is less than $10^{-300}$. Hence the decimal...
TAOCP 1.2.6 Exercise 62
Section 1.2.6: Binomial Coefficients Exercise 62. [ M23 ] Prove the identity $$ \sum_k (-1)^k \binom{l+m}{l+k}\binom{m+n}{m+k}\binom{n+l}{n+k} = \frac{(l+m+n)!}{l!,m!,n!}, \qquad \text{integer } l,m,n \ge 0. $$ Verified: no Solve time: - Solution Let $$ S=\sum_k (-1)^k \binom{l+m}{l+k}\binom{m+n}{m+k}\binom{n+l}{n+k}, \qquad l,m,n\ge0 . $$ We shall evaluate $S$ by extracting a coefficient from a suitable generating function. Using the definition (3), $$ \binom{l+m}{l+k}= x^{,l+k} ^{l+m}, $$ where $[x^r]$ denotes the coefficient of $x^r$. Hence...
TAOCP 1.2.8 Exercise 3
Section 1.2.8: Fibonacci Numbers Exercise 3. [ 25 ] Write a computer program that calculates and prints $F_1$ through $F_{1000}$ in decimal notation. (The previous exercise determines the size of numbers that must be handled.) Verified: no Solve time: - Solution Exercise 2 shows that $$ F_n = \phi^n/\sqrt{5} \text{ rounded to the nearest integer,} $$ by Eq. (15), and therefore $F_{1000}$ has approximately $$ 1000\log_{10}\phi-\log_{10}\sqrt{5}+1 $$ decimal digits. Since...
TAOCP 1.2.6 Exercise 66
Section 1.2.6: Binomial Coefficients Exercise 66. [ HM30 ] Suppose $x,y,z$ are real numbers satisfying $$ \binom{x}{n} = \binom{y}{n} + \binom{z}{n-1}, $$ where $x \ge n-1$, $y \ge n-1$, $z > n-2$, and $n$ is an integer $\ge 2$. Prove that $$ \binom{x}{n-1} \le \binom{y}{n-1} + \binom{z}{n-2} \quad \text{if and only if } y \ge z, $$ and $$ \binom{x}{n+1} \le \binom{y}{n+1} + \binom{z}{n} \quad \text{if and only if }...
TAOCP 1.2.8 Exercise 5
Section 1.2.8: Fibonacci Numbers Exercise 5. [ 20 ] Find all $n$ for which $F_n=n^2$. Verified: no Solve time: - Solution We are asked to find all positive integers $n$ such that $$ F_n = n^2. \tag{*} $$ The Fibonacci numbers $\langle F_n \rangle$ grow exponentially, while $n^2$ grows quadratically. We will first check small values of $n$ and then prove that no larger solutions exist. The first few Fibonacci...
TAOCP 1.2.8 Exercise 6
Section 1.2.8: Fibonacci Numbers Exercise 6. [ HM10 ] Prove Eq. (5). Verified: yes Solve time: 36s Let $$ A=\begin{pmatrix} 1&1\ 1&0 \end{pmatrix}. $$ The proof is by induction on $n$. For $n=1$, $$ A^1= \begin{pmatrix} 1&1\ 1&0 \end{pmatrix} = \begin{pmatrix} F_2&F_1\ F_1&F_0 \end{pmatrix}, $$ since $F_0=0$, $F_1=1$, and $F_2=1$. Thus Eq. (5) holds for $n=1$. Assume that $$ A^n= \begin{pmatrix} F_{n+1}&F_n\ F_n&F_{n-1} \end{pmatrix}. $$ Then $$ A^{n+1} = A^nA...
TAOCP 1.2.6 Exercise 52
Section 1.2.6: Binomial Coefficients Exercise 52. [ HM11 ] Prove that Abel's binomial formula (16) is not always valid when $n$ is not a nonnegative integer, by evaluating the right-hand side when $n=x=-1$, $y=z=1$. Verified: no Solve time: - Solution Abel's binomial formula (16) states that $$ (x+y)^n = \sum_k \binom{n}{k}x(x-kz)^{k-1}(y+kz)^{n-k}. \tag{16} $$ Take $$ n=-1,\qquad x=-1,\qquad y=1,\qquad z=1. $$ The left-hand side becomes $$ (x+y)^n = (-1+1)^{-1} = 0^{-1},...
TAOCP 1.2.6 Exercise 65
Section 1.2.6: Binomial Coefficients Exercise 65. [ HM35 ] Prove Eqs. (59) and (60). Verified: no Solve time: - I need the statements of Eqs. (59) and (60) from Section 1.2.6 to write a rigorous proof in the requested format. The excerpt you provided ends at Eq. (13), and Eqs. (59)–(60) occur later in the section. Please paste the text containing Eqs. (59) and (60), or upload the relevant page(s),...
TAOCP 1.2.6 Exercise 54
Section 1.2.6: Binomial Coefficients Exercise 54. [ M21 ] Consider Pascal's triangle as a matrix. What is the inverse of that matrix? Verified: no Solve time: - Solution Let $P$ denote the infinite lower-triangular matrix whose $(i,j)$ entry is $$ P_{i,j} = \binom{i}{j}, \qquad i,j \ge 0, $$ with the convention that $\binom{i}{j} = 0$ when $j > i$. This is the matrix form of Pascal's triangle. Our goal is...
TAOCP 1.2.7 Exercise 11
Section 1.2.7: Harmonic Numbers Exercise 11. [ M21 ] Using summation by parts, evaluate $$ \sum_{1<k \le n}\frac{1}{k(k-1)}H_k. $$ Verified: yes Solve time: 1m14s Solution Let $$ S=\sum_{1<k\le n}\frac{1}{k(k-1)}H_k. $$ To apply summation by parts in the form of Exercise 10, choose $$ a_k=-\frac1{k-1}\qquad (k\ge2), $$ so that $$ a_{k+1}-a_k =-\frac1k+\frac1{k-1} =\frac1{k(k-1)}. $$ Also take $$ b_k=H_k. $$ Then $$ S=\sum_{1<k\le n}(a_{k+1}-a_k)b_k =\sum_{2\le k\le n}(a_{k+1}-a_k)b_k. $$ Applying the summation-by-parts formula...
TAOCP 1.2.8 Exercise 7
Section 1.2.8: Fibonacci Numbers Exercise 7. [ 15 ] If $n$ is not a prime number, $F_n$ is not a prime number (with one exception). Prove this and find the exception. Verified: no Solve time: - Solution Assume that $n$ is composite. Then there exist integers $a,b$ such that $$ n=ab, \qquad a>1, \qquad b>1. $$ By the remark following Eq. (6), if $m$ is a multiple of $n$, then...
TAOCP 1.2.7 Exercise 6
Section 1.2.7: Harmonic Numbers Exercise 6. [ M15 ] Prove that the harmonic numbers are directly related to Stirling's numbers, which were introduced in the previous section; in fact, $$ H_n = \left[{n+1 \atop 2}\right] / n!. $$ Verified: no Solve time: - Solution We wish to prove that $$ H_n = \frac{\left[{,n+1 \atop 2,}\right]}{n!}, $$ where $\left[{n \atop k}\right]$ denotes the Stirling numbers of the first kind, as defined...
TAOCP 1.2.6 Exercise 55
Section 1.2.6: Binomial Coefficients Exercise 55. [ M21 ] Considering each of Stirling's triangles as matrices, determine their inverses. Verified: yes Solve time: 1m33s Solution Let $$ P=\bigl(\binom{n}{k}\bigr)_{n,k\ge 0} $$ be Pascal's triangle regarded as an infinite lower-triangular matrix. Exercise 54 asks for the inverse of $P$, namely $$ P^{-1}=\bigl((-1)^{,n-k}\binom{n}{k}\bigr)_{n,k\ge0}. \tag{1} $$ The two Stirling triangles are the matrices $$ S=\bigl[{n\atop k}\bigr] {n,k\ge0}, \qquad s=\bigl[{n\atop k}\bigr] {!!*,,n,k\ge0}, $$ where $\bigl[{n\atop...
TAOCP 1.2.8 Exercise 2
Section 1.2.8: Fibonacci Numbers Exercise 2. [ 20 ] In view of Eq. (15), what is the approximate value of $F_{1000}$? (Use logarithms found in Appendix A.) Verified: no Solve time: - Solution Equation (15) gives $$ F_n = \frac{\phi^n}{\sqrt{5}} \text{ rounded to the nearest integer,} \qquad \phi = \frac{1+\sqrt{5}}{2}. $$ We want $F_{1000}$. First compute $$ \log_{10} F_{1000} \approx \log_{10} \frac{\phi^{1000}}{\sqrt{5}} = 1000 \log_{10} \phi - \frac{1}{2} \log_{10} 5....
TAOCP 1.2.7 Exercise 25
Section 1.2.7: Harmonic Numbers Exercise 25. [ M21 ] Let $$ H_n^{(u,v)} = \sum_{1 \le j \le k \le n} \frac{1}{j^u k^v}. $$ What are $H_n^{(0,v)}$ and $H_n^{(u,0)}$? Prove the general identity $$ H_n^{(u,v)} + H_n^{(v,u)} = H_n^{(u)}H_n^{(v)} + H_n^{(u+v)}. $$ Verified: no Solve time: - Solution By definition, $$ H_n^{(u,v)}=\sum_{1\le j\le k\le n}\frac1{j^uk^v}. $$ When $u=0$, $$ H_n^{(0,v)} =\sum_{1\le j\le k\le n}\frac1{k^v} =\sum_{k=1}^n\sum_{j=1}^k\frac1{k^v} =\sum_{k=1}^n\frac{k}{k^v} =\sum_{k=1}^n k^{1-v}. $$ Hence $$...
TAOCP 1.2.8 Exercise 8
Section 1.2.8: Fibonacci Numbers Exercise 8. [ 15 ] In many cases it is convenient to define $F_n$ for negative $n$, by assuming that $F_{n+2}=F_{n+1}+F_n$ for all integers $n$. Explore this possibility: What is $F_{-1}$? What is $F_{-2}$? Can $F_{-n}$ be expressed in a simple way in terms of $F_n$? Verified: no Solve time: - Solution We extend the Fibonacci sequence to negative indices by the recurrence $$ F_{n+2} =...
TAOCP 1.2.7 Exercise 16
Section 1.2.7: Harmonic Numbers Exercise 16. [ 18 ] Express the sum $1 + \frac{1}{3} + \cdots + \frac{1}{2n-1}$ in terms of harmonic numbers. Verified: no Solve time: - Solution Let $$ S_n = 1 + \frac{1}{3} + \cdots + \frac{1}{2n-1} = \sum_{k=1}^{n}\frac{1}{2k-1}. $$ The harmonic number $H_{2n}$ contains all reciprocals from $1$ to $2n$: $$ H_{2n} = \sum_{k=1}^{2n}\frac{1}{k} = \sum_{k=1}^{n}\frac{1}{2k-1} \sum_{k=1}^{n}\frac{1}{2k}. $$ The second sum is $$ \sum_{k=1}^{n}\frac{1}{2k} =...
TAOCP 1.2.6 Exercise 59
Section 1.2.6: Binomial Coefficients Exercise 59. [ M25 ] A sequence of numbers $A_{nk}$ satisfies $$ A_{n0}=1,\qquad A_{0k}=\delta_{0k},\qquad A_{nk}=A_{n-1,k}+A_{n-1,k-1}+\binom{n}{k}. $$ Find $A_{nk}$. Verified: no Solve time: - Solution Define $$ B_{nk}=A_{nk}-\binom{n+1}{k}. $$ Then $$ B_{n0}=A_{n0}-\binom{n+1}{0}=1-1=0, $$ and $$ B_{0k}=A_{0k}-\binom{1}{k} =\delta_{0k}-\binom{1}{k}=0 \qquad (k\ge0), $$ since $$ \binom{1}{0}=1,\qquad \binom{1}{1}=1,\qquad \binom{1}{k}=0\quad(k>1). $$ Using the addition formula (9), $$ \binom{n+1}{k} = \binom{n}{k}+\binom{n}{k-1}. $$ Hence \begin{align*} B_{nk} &=A_{nk}-\binom{n+1}{k} \ &=A_{n-1,k}+A_{n-1,k-1}+\binom{n}{k} -\binom{n}{k}-\binom{n}{k-1} \ &=\bigl(A_{n-1,k}-\binom{n}{k}\bigr) +\bigl(A_{n-1,k-1}-\binom{n}{k-1}\bigr) \...
TAOCP 1.2.8 Exercise 4
Section 1.2.8: Fibonacci Numbers Exercise 4. [ 14 ] Find all $n$ for which $F_n=n$. Verified: yes Solve time: 38s Solution By Eq. (14), $$ F_n=\frac{1}{\sqrt5}(\phi^n-\hat\phi^n), $$ where $$ \phi=\frac{1+\sqrt5}{2}, \qquad \hat\phi=\frac{1-\sqrt5}{2}. $$ The values $$ F_0=0,\qquad F_1=1,\qquad F_5=5 $$ show that $$ n=0,;1,;5 $$ are solutions of the equation $F_n=n$. It remains to prove that no other solutions exist. For $n\ge 6$, Section 1.2.1 gives $$ F_n\ge \phi^{,n-2}. $$...
TAOCP 1.2.7 Exercise 17
Section 1.2.7: Harmonic Numbers Exercise 17. [ M24 ] (E. Waring, 1782.) Let $p$ be an odd prime. Show that the numerator of $H_{p-1}$ is divisible by $p$. Verified: no Solve time: - Solution Since $p$ is an odd prime, the integers $$ 1,2,\ldots,p-1 $$ can be paired as $$ k \leftrightarrow p-k, \qquad 1 \le k \le p-1. $$ Therefore $$ H_{p-1} = \sum_{k=1}^{p-1}\frac1k = \frac12\sum_{k=1}^{p-1}\left(\frac1k+\frac1{p-k}\right). $$ Now $$...
TAOCP 1.2.7 Exercise 9
Section 1.2.7: Harmonic Numbers Exercise 9. [ M18 ] Theorem A applies only when $x>0$; what is the value of the sum considered when $x=-1$? Verified: no Solve time: - Solution We are asked to evaluate $$ \sum_{k=1}^{n} \binom{n}{k}(-1)^k H_k. $$ Let $$ S_n = \sum_{k=1}^{n} \binom{n}{k} (-1)^k H_k. $$ We begin by observing that $$ (1 + (-1))^n = 0^n. $$ Hence, for $n \ge 1$, $$ \sum_{k=0}^{n} \binom{n}{k}...
TAOCP 1.2.6 Exercise 46
Section 1.2.6: Binomial Coefficients Exercise 46. [ M21 ] Using Stirling's approximation, Eq. 1.2.5--(7), find an approximate value of $\binom{x+y}{y}$, assuming that both $x$ and $y$ are large. In particular, find the approximate size of $\binom{2n}{n}$ when $n$ is large. Verified: no Solve time: - Solution By Eq. (5), $$ \binom{x+y}{y}=\frac{(x+y)!}{x!,y!}. $$ Stirling's approximation, Eq. 1.2.5--(7), states that $$ n!\sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^n $$ when $n$ is large. Applying this approximation...
TAOCP 1.2.6 Exercise 9
Section 1.2.6: Binomial Coefficients Exercise 9. [ 01 ] What is the value of $\binom{n}{n}$? (Consider all integers $n$.) Verified: yes Solve time: 39s For every integer $n \ge 0$, Eq. (5) gives $$ \binom{n}{n} = \frac{n!}{n!(n-n)!} = \frac{n!}{n!,0!}. $$ Since $\binom{r}{0}=1$ by Eq. (4), Eq. (5) with $n=0$ implies $0!=1$; hence $$ \binom{n}{n}=1. $$ If $n<0$, Eq. (3) defines $\binom{r}{k}=0$ whenever the lower argument $k$ is a negative integer....
TAOCP 1.2.6 Exercise 11
Section 1.2.6: Binomial Coefficients Exercise 11. [ M20 ] (E. Kummer, 1852.) Let $p$ be prime. Show that if $p^n$ divides $\binom{a+b}{a}$ but $p^{n+1}$ does not, then $n$ is equal to the number of carries that occur when $a$ is added to $b$ in the $p$-ary number system. Verified: yes Solve time: 2m16s Solution Let $$ v_p(m) $$ denote the exponent of the highest power of $p$ dividing $m$, and...
TAOCP 1.2.6 Exercise 51
Section 1.2.6: Binomial Coefficients Exercise 51. [ M21 ] Prove Abel's formula, Eq. (16), by writing $y=(x+y)-x$, expanding the right-hand side in powers of $(x+y)$, and applying the result of the previous exercise. Verified: no Solve time: - Solution We are asked to prove Abel's formula, Eq. (16), using the hint: write $y=(x+y)-x$, expand in powers of $(x+y)$, and apply the result of Exercise 50. Abel's formula states, for arbitrary...
TAOCP 1.2.6 Exercise 8
Section 1.2.6: Binomial Coefficients Exercise 8. [ 00 ] What property of Pascal's triangle is reflected in the symmetry condition, Eq. (6)? Verified: yes Solve time: 35s The symmetry condition, Eq. (6), states that $$ \binom{n}{k} = \binom{n}{n-k}, \qquad \text{integer } n \ge 0, \ \text{integer } k. $$ This property is reflected in Pascal's triangle by the fact that each row is symmetric about its middle. Specifically, for row...
TAOCP 1.2.6 Exercise 41
Section 1.2.6: Binomial Coefficients Exercise 41. [ HM22 ] Prove that $$ B(x,y)=\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}. $$ Verified: no Solve time: - Solution Define $$ I(x,y)=\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}. $$ We will show that $I(x,y)$ satisfies the same defining relations as $B(x,y)$ in Exercise 40. From the definition of the gamma function, $$ \Gamma(x+1)=x\Gamma(x), \qquad x>0. $$ Hence $$ I(x,1) = \frac{\Gamma(x)\Gamma(1)}{\Gamma(x+1)}. $$ Since $$ \Gamma(1)=\int_0^\infty e^{-t},dt=1, $$ and since $\Gamma(x+1)=x\Gamma(x)$, $$ I(x,1) = \frac{\Gamma(x)}{x\Gamma(x)} =...
TAOCP 1.2.6 Exercise 18
Section 1.2.6: Binomial Coefficients Exercise 18. [ M15 ] Prove Eq. (22) using Eqs. (21) and (6). Verified: yes Solve time: 34s Solution Equation (22) states that for integers $n \ge 0$ and $m \ge 0$: $$ \sum_{k=0}^{n} \binom{k}{m} = \binom{n+1}{m+1}. $$ We are to prove this using Eq. (21) and the symmetry relation (6). Equation (21) is the more general summation formula $$ \sum_{k=0}^{n} \binom{r+k}{k} = \binom{r+n+1}{n}, \qquad \text{integer...
TAOCP 1.2.6 Exercise 31
Section 1.2.6: Binomial Coefficients Exercise 31. [ M20 ] Evaluate $$ \sum_k \binom{m-r+s}{k}\binom{n+r-s}{n-k}\binom{r+k}{m+n} $$ in terms of $r$, $s$, $m$, and $n$, given that $m$ and $n$ are integers. Begin by replacing $$ \binom{r+k}{m+n} $$ by $\sum_j \binom{r}{m+n-j}\binom{k}{j}$. Verified: no Solve time: - Solution Let $$ S=\sum_k \binom{m-r+s}{k}\binom{n+r-s}{n-k}\binom{r+k}{m+n}. $$ The exercise directs us to expand the last factor by Vandermonde's convolution: $$ \binom{r+k}{m+n} = \sum_j \binom{r}{m+n-j}\binom{k}{j}. $$ Hence $$ S...
TAOCP 1.2.6 Exercise 43
Section 1.2.6: Binomial Coefficients Exercise 43. [ HM20 ] Show that $B(1/2,1/2)=\pi$. Verified: no Solve time: - Solution We are asked to evaluate the beta function $$ B!\left(\frac{1}{2},\frac{1}{2}\right) = \int_0^1 t^{-1/2} (1-t)^{-1/2}, dt. $$ We perform the substitution $t = \sin^2\theta$, so that $dt = 2\sin\theta\cos\theta, d\theta$. Then $$ t^{-1/2} = (\sin^2\theta)^{-1/2} = \frac{1}{\sin\theta}, \qquad (1-t)^{-1/2} = (\cos^2\theta)^{-1/2} = \frac{1}{\cos\theta}. $$ Hence the integrand transforms as $$ t^{-1/2} (1-t)^{-1/2} ,...
TAOCP 1.2.6 Exercise 22
Section 1.2.6: Binomial Coefficients Exercise 22. [ M20 ] Prove Eq. (26) for the special case $s=(n-1)r+nt$. Verified: yes Solve time: 4m15s Let $$ S=\sum_{i=0}^{s}\binom{r+i}{r}\binom{t+s-i}{t}, \qquad s=(n-1)r+nt. $$ We shall prove, using only identity (11), $$ \sum_{m=0}^{M}\binom{p+m}{p} =\binom{p+M+1}{p+1}, \tag{11} $$ that $$ S=\binom{r+t+s+1}{r+t+1}. $$ Substituting $s=(n-1)r+nt$ will then give the required special case of Eq. (26). From (11), $$ \binom{r+i}{r} =\sum_{k=0}^{i}\binom{r-1+k}{r-1}. $$ Substituting into $S$ and reversing the order of...
TAOCP 1.2.6 Exercise 15
Section 1.2.6: Binomial Coefficients Exercise 15. [ M15 ] Prove the binomial formula, Eq. (13). Verified: yes Solve time: 42s Solution We prove Eq. (13) by induction on $r$. When $r=0$, the right-hand side contains only the term with $k=0$, since $\binom{0}{k}=0$ for $k\ne0$. Therefore $$ \sum_k \binom{0}{k}x^ky^{-k} = \binom{0}{0} = 1 = (x+y)^0. $$ Hence Eq. (13) is true for $r=0$. Assume that $$ (x+y)^r=\sum_k\binom{r}{k}x^ky^{r-k}, \qquad r\ge0. $$ Then...
TAOCP 1.2.6 Exercise 23
Section 1.2.6: Binomial Coefficients Exercise 23. [ M13 ] Assuming that Eq. (26) holds for $(r,s,t,n)$ and $(r,s-t,t,n-1)$, prove it for $(r,s+1,t,n)$. Verified: yes Solve time: 33s Solution Equation (26) is the identity $$ \sum_k \binom{r}{m+k}\binom{s}{n-k}\binom{k}{t} = \binom{r+s-t}{m+n-t}\binom{r}{m+t}, $$ where the summation extends over all integers $k$. Assume that Eq. (26) holds for $(r,s,t,n)$ and also for $(r,s-t,t,n-1)$. We must prove it for $(r,s+1,t,n)$. Consider $$ \sum_k \binom{r}{m+k}\binom{s+1}{n-k}\binom{k}{t}. $$ By...
TAOCP 1.2.6 Exercise 13
Section 1.2.6: Binomial Coefficients Exercise 13. [ M13 ] Prove the summation formula, Eq. (10). Verified: yes Solve time: 38s Solution Equation (10) states that $$ \sum_{k=0}^{n}\binom{r+k}{k} = \binom{r+n+1}{n}, \qquad \text{integer } n\ge 0. $$ We prove this formula by induction on $n$. When $n=0$, the left-hand side is $$ \sum_{k=0}^{0}\binom{r+k}{k} = \binom{r}{0} = 1 $$ by equation (4). The right-hand side is $$ \binom{r+0+1}{0} = \binom{r+1}{0} = 1. $$...
TAOCP 1.2.6 Exercise 14
Section 1.2.6: Binomial Coefficients Exercise 14. [ M21 ] Evaluate $\sum_{k=0}^{n} k^4$. Verified: yes Solve time: 29s Solution We seek a closed form for the sum $$ \sum_{k=0}^{n} k^4. $$ By the argument in Section 1.2.6.E, any power $k^m$ can be expressed as a linear combination of binomial coefficients $\binom{k}{j}$ with $0 \le j \le m$. We first determine the coefficients for $k^4$. We look for constants $c_0, c_1, c_2,...
TAOCP 1.2.6 Exercise 35
Section 1.2.6: Binomial Coefficients Exercise 35. [ M23 ] Prove the addition formulas (46) for Stirling numbers directly from the definitions, Eqs. (44) and (45). Verified: no Solve time: - Solution We are asked to prove the addition formulas (46) for Stirling numbers directly from their definitions, Eqs. (44) and (45). For clarity, we restate these definitions. The Stirling numbers of the first kind , $c(n,k)$, are defined by $$...
TAOCP 1.2.6 Exercise 20
Section 1.2.6: Binomial Coefficients Exercise 20. [ M20 ] Prove Eq. (24) by using Eqs. (21) and (19), then show that another use of Eq. (19) yields Eq. (25). Verified: no Solve time: 3m17s We use the identities $$ \sum_k \binom{r}{k}\binom{s}{n-k}=\binom{r+s}{n} \tag{21} $$ and $$ \sum_{k=0}^n \binom{k}{m}=\binom{n+1}{m+1}. \tag{19} $$ We first prove Eq. (24), $$ \sum_{k=0}^n \binom{r+k}{k}=\binom{r+n+1}{n}, \tag{24} $$ and then derive Eq. (25) by another application of Eq. (19)....
TAOCP 1.2.6 Exercise 39
Section 1.2.6: Binomial Coefficients Exercise 39. [ M10 ] What is the sum $\sum_k \left[{n \atop k}\right]$ of the numbers in each row of Stirling's first triangle? What is the sum of these numbers with alternating signs? Verified: no Solve time: - Let $\left[{n \atop k}\right]$ denote the numbers in Stirling's first triangle. By the interpretation of $\left[{n \atop k}\right]$ as the number of permutations of $n$ elements having exactly...
TAOCP 1.2.6 Exercise 12
Section 1.2.6: Binomial Coefficients Exercise 12. [ M22 ] Are there any positive integers $n$ for which all the nonzero entries in the $n$th row of Pascal's triangle are odd? If so, find all such $n$. Verified: yes Solve time: 44s Solution Let $$ P(n):\qquad \binom{n}{k}\equiv 1 \pmod 2 \quad\text{for every }k\text{ with }0\le k\le n\text{ and }\binom{n}{k}\ne0. $$ Since $n$ is a positive integer, the nonzero entries in the...
TAOCP 1.2.6 Exercise 34
Section 1.2.6: Binomial Coefficients Exercise 34. [ M23 ] Show that Abel's generalization, Eq. (16), of the binomial formula is true also for rising powers: $$ (x+y)^{\overline{n}} = \sum_k \binom{n}{k}x(x-kz+1)^{\overline{k-1}}(y+kz)^{\overline{n-k}}. $$ Verified: no Solve time: - Solution We prove the stated identity by induction on $n$. The identity to prove is $$ (x+y)^{\overline{n}} = \sum_{k=0}^{n} \binom{n}{k} x(x-kz+1)^{\overline{k-1}} (y+kz)^{\overline{n-k}}. \tag{*} $$ Base case : $n=0$. The left-hand side is $(x+y)^{\overline{0}} =...
TAOCP 1.2.6 Exercise 10
Section 1.2.6: Binomial Coefficients Exercise 10. [ M25 ] If $p$ is prime, show that: $\binom{n}{p} \equiv \lfloor n/p \rfloor \pmod p$. $\binom{p}{k} \equiv 0 \pmod p$, for $1 \le k \le p-1$. $\binom{p-1}{k} \equiv (-1)^k \pmod p$, for $0 \le k \le p-1$. $\binom{p+1}{k} \equiv 0 \pmod p$, for $2 \le k \le p-1$. (E. Lucas, 1877.) $$ \binom{n}{k} \equiv \binom{\lfloor n/p \rfloor}{\lfloor k/p \rfloor} \binom{n \bmod p}{k \bmod...
TAOCP 1.2.6 Exercise 37
Section 1.2.6: Binomial Coefficients Exercise 37. [ M10 ] From the answers to the preceding exercise, deduce the value of the sum of every other entry in a row, $\binom{n}{0} + \binom{n}{2} + \binom{n}{4} + \cdots$. Verified: no Solve time: - By Exercise 36 and the binomial theorem (13), $$ \sum_k \binom{n}{k} = (1+1)^n = 2^n, $$ and $$ \sum_k \binom{n}{k}(-1)^k = (1-1)^n = 0 \qquad (n>0). $$ Let $$...
TAOCP 1.2.6 Exercise 33
Section 1.2.6: Binomial Coefficients Exercise 33. [ M20 ] Show that the binomial formula is valid also when it involves factorial powers instead of the ordinary powers. In other words, prove that $$ (x+y)^{\underline{n}} = \sum_k \binom{n}{k}x^{\underline{k}}y^{\underline{n-k}}, \qquad (x+y)^{\overline{n}} = \sum_k \binom{n}{k}x^{\overline{k}}y^{\overline{n-k}}. $$ Verified: no Solve time: - Solution We prove the identities by induction on $n$, using the definitions of falling and rising factorial powers and the standard binomial...
TAOCP 1.2.6 Exercise 7
Section 1.2.6: Binomial Coefficients Exercise 7. [ 12 ] If $n$ is a fixed positive integer, what value of $k$ makes $\binom{n}{k}$ a maximum? Verified: yes Solve time: 37s Solution By Eq. (5), $$ \binom{n}{k}=\frac{n!}{k!(n-k)!}, \qquad 0\le k\le n. $$ To determine where this quantity is maximal, compare consecutive terms. For $0\le k<n$, $$ \frac{\binom{n}{k+1}}{\binom{n}{k}} = \frac{n!}{(k+1)!(n-k-1)!} \frac{k!(n-k)!}{n!} = \frac{n-k}{k+1}. $$ Hence $$ \binom{n}{k+1}>\binom{n}{k} \iff \frac{n-k}{k+1}>1 \iff n-k>k+1 \iff n>2k+1,...
TAOCP 1.2.6 Exercise 42
Section 1.2.6: Binomial Coefficients Exercise 42. [ HM10 ] Express the binomial coefficient $\binom{r}{k}$ in terms of the beta function. Verified: no Solve time: - By Exercise 40(3), $$ B(k+1,r-k+1) =\frac{(r+1)!}{k!(r-k)!},B(r+1,1). $$ Since Exercise 40(1) gives $B(r+1,1)=1/(r+1)$, we obtain $$ B(k+1,r-k+1) =\frac{r!}{k!(r-k)!}. $$ Therefore, by Eq. (5), $$ \binom{r}{k} =\frac{r!}{k!(r-k)!} =\frac{1}{B(k+1,r-k+1)}, \qquad \text{integer } r \ge k \ge 0. $$ Hence $$ \boxed{\binom{r}{k}=\frac{1}{B(k+1,r-k+1)}}. $$
TAOCP 1.2.6 Exercise 49
Section 1.2.6: Binomial Coefficients Exercise 49. [ M20 ] Show that the identity $(1+x)^r = (1-x^2)^r(1-x)^{-r}$ implies a relation on binomial coefficients. Verified: no Solve time: - Solution By the binomial theorem (13), $$ (1+x)^r=\sum_{k\ge0}\binom{r}{k}x^k, $$ since the terms with $k<0$ vanish by definition (3). Similarly, $$ (1-x^2)^r=\sum_{j\ge0}\binom{r}{j}(-1)^j x^{2j}, $$ and $$ (1-x)^{-r}=\sum_{m\ge0}\binom{-r}{m}(-1)^m x^m. $$ Hence $$ (1-x^2)^r(1-x)^{-r} = \left(\sum_{j\ge0}\binom{r}{j}(-1)^j x^{2j}\right) \left(\sum_{m\ge0}\binom{-r}{m}(-1)^m x^m\right). $$ Multiplying the series and collecting the...
TAOCP 1.2.6 Exercise 6
Section 1.2.6: Binomial Coefficients Exercise 6. [ 10 ] Pascal's triangle (Table 1) can be extended in all directions by use of the addition formula, Eq. (9). Find the three rows that go on top of Table 1 (i.e., for $r=-1$, $-2$, and $-3$). Verified: yes Solve time: 41s Using Eq. (9), $$ \binom{r}{k}=\binom{r-1}{k}+\binom{r-1}{k-1}, $$ the rows above Table 1 are determined uniquely by working upward. Since every entry equals...
TAOCP 1.2.6 Exercise 48
Section 1.2.6: Binomial Coefficients Exercise 48. [ M25 ] Show that $$ \sum_{k \ge 0}\binom{n}{k}\frac{(-1)^k}{k+x} = \frac{n!}{x(x+1)\cdots(x+n)} = \frac{1}{x\binom{n+x}{n}}, $$ if the denominators are not zero. Verified: no Solve time: - Solution We are asked to evaluate $$ \sum_{k \ge 0} \binom{n}{k} \frac{(-1)^k}{k+x}. $$ Consider the function $$ f(x) = \int_0^1 t^{x-1} (1-t)^n , dt, $$ which is well defined for $x \notin {0,-1,-2,\dots,-n}$. Expanding $(1-t)^n$ by the binomial theorem,...
TAOCP 1.2.6 Exercise 38
Section 1.2.6: Binomial Coefficients Exercise 38. [ HM30 ] (C. Ramus, 1834.) Generalizing the result of the preceding exercise, show that we have the formula $$ \binom{n}{k} + \binom{n}{m+k} + \binom{n}{2m+k} + \cdots = \frac{1}{m}\sum_{0 \le j < m}\left(2\cos\frac{j\pi}{m}\right)^n \cos\frac{j(n-2k)\pi}{m}. $$ Verified: no Solve time: - Setup Let $n$ and $m$ be integers with $m \ge 1$, and let $k$ be an integer. We are asked to prove the identity...
TAOCP 1.2.6 Exercise 19
Section 1.2.6: Binomial Coefficients Exercise 19. [ M18 ] Prove Eq. (23) by induction. Verified: yes Solve time: 38s Solution Equation (23) is $$ \sum_k (-1)^{r-k}\binom{r}{k}\binom{s+k}{n} = \binom{s}{n-r}, \qquad \text{integer } r \ge 0. \tag{23} $$ We prove this identity by induction on $r$. For the basis, let $r=0$. Since $\binom{0}{k}=0$ for $k\ne 0$ and $\binom{0}{0}=1$, the sum reduces to a single term: $$ \sum_k (-1)^{-k}\binom{0}{k}\binom{s+k}{n} = \binom{0}{0}\binom{s}{n} = \binom{s}{n}....
TAOCP 1.2.6 Exercise 21
Section 1.2.6: Binomial Coefficients Exercise 21. [ M05 ] Both sides of Eq. (25) are polynomials in $s$; why isn't that equation an identity in $s$? Verified: yes Solve time: 34s In Eq. (25), if $m$, $n$, and $r$ are regarded as fixed nonnegative integers, each term $\binom{s+k}{n}$ is a polynomial in $s$ of degree $n$ by Eq. (3). Hence the left-hand side is a polynomial in $s$ of degree...
TAOCP 1.2.6 Exercise 47
Section 1.2.6: Binomial Coefficients Exercise 47. [ M21 ] Given that $k$ is an integer, show that $$ \binom{r}{k}\binom{r-1/2}{k} = \binom{2r}{k}\binom{2r-k}{k}/4^k = \binom{2r}{2k}\binom{2k}{k}/4^k. $$ Give a simpler formula for the special case $r=-1/2$. Verified: no Solve time: - Solution Let $k$ be an integer. From the definition (3) of generalized binomial coefficients, we have $$ \binom{r}{k} = \frac{r^{\underline{k}}}{k!}, \qquad \binom{r-1/2}{k} = \frac{(r-1/2)^{\underline{k}}}{k!}, $$ where $r^{\underline{k}} = r(r-1)\cdots(r-k+1)$ denotes the falling...
TAOCP 1.2.6 Exercise 45
Section 1.2.6: Binomial Coefficients Exercise 45. [ HM21 ] Using the generalized binomial coefficient suggested in exercise 42, find $$ \lim_{r \to \infty}\frac{\binom{r}{k}}{r^k}. $$ Verified: no Solve time: - Solution By equation (3), $$ \binom{r}{k} = \frac{r(r-1)\cdots(r-k+1)}{k!}, \qquad \text{integer } k \ge 0. $$ Hence $$ \frac{\binom{r}{k}}{r^k} = \frac{r(r-1)\cdots(r-k+1)}{k!,r^k} = \frac1{k!} \prod_{j=0}^{k-1}\left(1-\frac{j}{r}\right). $$ Here $k$ is fixed. For each $j$ with $0\le j\le k-1$, $$ \lim_{r\to\infty}\left(1-\frac{j}{r}\right)=1. $$ Since the product...
TAOCP 1.2.6 Exercise 26
Section 1.2.6: Binomial Coefficients Exercise 26. [ HM25 ] Using the assumptions of the previous exercise, prove that $$ \sum_k \binom{r-tk}{k} z^k = \frac{x^{r+1}}{(t+1)x-t}. $$ Verified: no Solve time: - Solution Exercise 25 established that, under the assumptions of Example 4, $$ \sum_k A_k(r,t) z^k = x^r, \qquad z=x^{t+1}-x^t, $$ provided that $x$ is sufficiently close to $1$. By the definition of $A_k(r,t)$ in Example 4, $$ A_k(r,t)=\frac{r-tk}{r}\binom{r}{k}. $$ Using...
TAOCP 1.2.6 Exercise 50
Section 1.2.6: Binomial Coefficients Exercise 50. [ M20 ] Prove Abel's formula, Eq. (16), in the special case $x+y=0$. Verified: no Solve time: - Solution Let $$ A_n(x,y)=\sum_{k=0}^n \binom{n}{k}(x+k)^{k-1}(y+n-k)^{n-k}. $$ Abel's formula, Eq. (16), asserts that $$ A_n(x,y)=\frac{(x+y+n)^n}{x}, $$ provided that $x\ne0$. The exercise asks for a proof in the special case $x+y=0$. Put $y=-x$. Then $$ A_n(x,-x) =\sum_{k=0}^n \binom{n}{k}(x+k)^{k-1}(n-k-x)^{n-k}. $$ We must prove that $$ A_n(x,-x)=\frac{n^n}{x}. \tag{1} $$ Multiply...
TAOCP 1.2.6 Exercise 25
Section 1.2.6: Binomial Coefficients Exercise 25. [ HM30 ] Let the polynomial $A_n(x,t)$ be defined as in Example 4. Let $z=x^{t+1}-x^t$. Prove that $\sum_k A_k(r,t)z^k = x^r$, provided that $x$ is close enough to 1. Verified: no Solve time: - Setup Let $A_n(x,t)$ be the polynomial defined in Example 4 of Section 1.2.6. We are given $$ z = x^{t+1} - x^t $$ and are asked to prove that $$...
TAOCP 1.2.6 Exercise 36
Section 1.2.6: Binomial Coefficients Exercise 36. [ M10 ] What is the sum $\sum_k \binom{n}{k}$ of the numbers in each row of Pascal's triangle? What is the sum of these numbers with alternating signs, $\sum_k \binom{n}{k}(-1)^k$? Verified: no Solve time: - By the binomial theorem (13), with $x=y=1$, $$ (1+1)^n = \sum_k \binom{n}{k}1^k1^{n-k} = \sum_k \binom{n}{k}. $$ Hence $$ \sum_k \binom{n}{k} = 2^n. $$ Similarly, with $x=-1$ and $y=1$, $$...
TAOCP 1.2.6 Exercise 44
Section 1.2.6: Binomial Coefficients Exercise 44. [ HM20 ] Using the generalized binomial coefficient suggested in exercise 42, show that $$ \binom{r}{1/2} = \frac{2^{2r+1}}{\binom{2r}{r}\pi}. $$ Verified: no Solve time: - Solution Exercise 42 suggests extending the binomial coefficient to arbitrary arguments by means of the beta function. Since exercise 41 established $$ B(x,y)=\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}, $$ and equation (3) gives $$ \binom{r}{k} = \frac{r(r-1)\cdots(r-k+1)}{k!}, $$ the corresponding gamma-function form is $$ \binom{r}{k}...
TAOCP 1.2.6 Exercise 24
Section 1.2.6: Binomial Coefficients Exercise 24. [ M15 ] Explain why the results of the previous two exercises combine to give a proof of Eq. (26). Verified: no Solve time: 1m30s Solution Exercise 22 proves Eq. (26) for all quadruples $(r,s,t,n)$ satisfying $$ s=(n-1)r+nt. $$ Exercise 23 proves the following induction step: If Eq. (26) holds for $(r,s,t,n)$ and for $(r,s-t,t,n-1)$, then it also holds for $(r,s+1,t,n)$. Fix $r,t,n$, and...
TAOCP 1.2.6 Exercise 29
Section 1.2.6: Binomial Coefficients Exercise 29. [ M20 ] Show that Eq. (34) is just a special case of the general identity proved in exercise 1.2.3--33. Verified: no Solve time: - Solution Equation (34) states that for integers $r \ge 0$ and $m$, $$ \sum_k \binom{r}{k}(-1)^{r-k}k^m = \begin{cases} 0, & 0 \le m < r,\ r!, & m=r. \end{cases} \tag{34} $$ The general identity proved in exercise 1.2.3, 33 is...
TAOCP 1.2.6 Exercise 30
Section 1.2.6: Binomial Coefficients Exercise 30. [ M24 ] Show that there is a better way to solve Example 3 than the way used in the text, by manipulating the sum so that Eq. (26) applies. Verified: no Solve time: - Solution Example 3 in Section 1.2.6 considers the sum $$ S = \sum_{k=0}^{n} \binom{r+k}{k} \binom{s-k}{n-k}, $$ and in the text, it is evaluated by direct manipulation. We are asked...
TAOCP 1.2.6 Exercise 16
Section 1.2.6: Binomial Coefficients Exercise 16. [ M15 ] Given that $n$ and $k$ are positive integers, prove the symmetrical identity $$ (-1)^n\binom{-n}{k-1} = (-1)^k\binom{-k}{n-1}. $$ Verified: yes Solve time: 34s Solution We begin with the left-hand side of the identity and apply the definition (3) of binomial coefficients for general integers $r$: $$ \binom{-n}{k-1} = \frac{(-n)(-n-1)\cdots(-n-(k-2))}{(k-1)!}. $$ Expanding the numerator, we have $$ (-n)(-n-1)\cdots(-n-(k-2)) = (-1)^{k-1} n(n+1)\cdots(n+k-2), $$ because...
TAOCP 1.2.6 Exercise 32
Section 1.2.6: Binomial Coefficients Exercise 32. [ M20 ] Show that $\sum_k \binom{n}{k}x^k = x^{\overline{n}}$, where $x^{\overline{n}}$ is the rising factorial power defined in Eq. 1.2.5--(19). Verified: no Solve time: - Solution By Eq. 1.2.5--(19), the rising factorial power is $$ x^{\overline{n}} = x(x+1)\cdots(x+n-1). $$ We prove that $$ \sum_k \binom{n}{k}x^k = x^{\overline{n}}. $$ Since $\binom{n}{k}=0$ for $k<0$ or $k>n$ by Eq. (3), the sum is finite: $$ \sum_k \binom{n}{k}x^k...
TAOCP 1.2.6 Exercise 28
Section 1.2.6: Binomial Coefficients Exercise 28. [ M25 ] Prove that $$ \sum_k \binom{r+tk}{k}\binom{s-tk}{n-k} = \sum_{k \ge 0} \binom{r+s-k}{n-k} t^k, $$ if $n$ is a nonnegative integer. Verified: no Solve time: - Solution Let $$ S_n=\sum_k \binom{r+tk}{k}\binom{s-tk}{n-k}, $$ where $n$ is a nonnegative integer. Since $\binom{s-tk}{n-k}=0$ when $k>n$, the sum contains only finitely many nonzero terms. We apply the binomial theorem (13). The coefficient of $x^ny^{r+s-n}$ in $$ (x+y)^{r+s-k}(x+ty)^k $$...
TAOCP 1.2.6 Exercise 17
Section 1.2.6: Binomial Coefficients Exercise 17. [ M18 ] Prove the Chu--Vandermonde formula (21) from Eq. (15), using the idea that $(1+x)^{r+s} = (1+x)^r(1+x)^s$. Verified: yes Solve time: 1m08s Solution Equation (15) is the generalized binomial theorem, $$ (1+x)^r=\sum_{j\ge0}\binom{r}{j}x^j. \tag{15} $$ We compute $(1+x)^{r+s}$ in two ways. First, by Eq. (15), $$ (1+x)^{r+s} =\sum_{n\ge0}\binom{r+s}{n}x^n. \tag{1} $$ Second, $$ (1+x)^{r+s} =(1+x)^r(1+x)^s. $$ Applying Eq. (15) to each factor gives $$ (1+x)^r(1+x)^s...
TAOCP 1.2.6 Exercise 27
Section 1.2.6: Binomial Coefficients Exercise 27. [ HM21 ] Solve Example 4 in the text by using the result of exercise 25; and prove Eq. (26) from the preceding two exercises. Verified: no Solve time: - Solution We begin with the setup from Exercise 25. Let $A_n(x,t)$ be defined as in Example 4, and let $z = x^{t+1} - x^t$. Exercise 25 established that, for $x$ sufficiently close to 1,...
TAOCP 1.2.6 Exercise 40
Section 1.2.6: Binomial Coefficients Exercise 40. [ HM17 ] The beta function $B(x,y)$ is defined for positive real numbers $x,y$ by $$ B(x,y) = \int_0^1 t^{x-1}(1-t)^{y-1},dt. $$ Show that: $B(x,1)=B(1,x)=1/x$. $B(x+1,y)+B(x,y+1)=B(x,y)$. $B(x,y)=((x+y)/y)B(x,y+1)$. Verified: no Solve time: - Solution By definition, $$ B(x,1) = \int_0^1 t^{x-1} (1-t)^{1-1}, dt = \int_0^1 t^{x-1}, dt. $$ Since $x>0$, the integral converges and evaluates to $$ \int_0^1 t^{x-1}, dt = \left[ \frac{t^x}{x} \right]_0^1 = \frac{1}{x}....
TAOCP 1.2.4 Exercise 30
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 30. [ M30 ] Prove that the function $\varphi(n)$ of exercise 27 is multiplicative. Using this fact, evaluate $\varphi(1000000)$, and give a method for evaluating $\varphi(n)$ in a simple way once $n$ has been factored into primes. Verified: yes Solve time: 36s Setup Let $n$ be a positive integer and let $\varphi(n)$ denote the number of integers in ${0,1,\ldots,n-1}$ that are...
TAOCP 1.2.4 Exercise 45
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 45. ▶ [ M28 ] The result of exercise 37 is somewhat surprising, since it implies that $$ \sum_{0 \le k < n} \left\lfloor \frac{mk+x}{n} \right\rfloor = \sum_{0 \le k < m} \left\lfloor \frac{nk+x}{m} \right\rfloor $$ when $m$ and $n$ are positive integers and $x$ is arbitrary. This “reciprocity relationship” is one of many similar formulas (see Section 3.3.3). Show that...
TAOCP 1.2.4 Exercise 35
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 35. ▶ [ M20 ] Given that $m$ and $n$ are integers and $n>0$, prove that $$\left\lfloor \frac{x+m}{n} \right\rfloor = \left\lfloor \frac{\lfloor x \rfloor + m}{n} \right\rfloor$$ for all real $x$. (When $m=0$, we have an important special case.) Does an analogous result hold for the ceiling function? Verified: yes Solve time: 1m18s Solution Let $x$ be a real number, and...
TAOCP 1.2.4 Exercise 42
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 42. [ M24 ] (a) Prove that $$\sum_{k=1}^n a_k = na_n - \sum_{k=1}^{n-1} k(a_{k+1}-a_k),\qquad \text{if } n>0.$$ (b) The preceding formula is useful for evaluating certain sums involving the floor function. Prove that, if $b$ is an integer $\ge 2$, $$ \sum_{k=1}^n \lfloor \log_b k \rfloor = (n+1)\lfloor \log_b n \rfloor - \frac{b^{\lfloor \log_b n \rfloor +1}-b}{b-1}. $$ Verified: no Solve...
TAOCP 1.2.6 Exercise 5
Section 1.2.6: Binomial Coefficients Exercise 5. [ 05 ] Use Pascal's triangle to explain the fact that $11^4 = 14641$. Verified: yes Solve time: 41s The rows of Pascal's triangle give the coefficients of the expansion of $(x+y)^n$ according to the binomial theorem, Eq. (13). The fourth row (corresponding to $n=4$) is $1, 4, 6, 4, 1$. Substituting $x = 10$ and $y = 1$, we have = \binom{4}{0}10^0 +...
TAOCP 1.2.5 Exercise 16
Section 1.2.5: Permutations and Factorials Exercise 16. [**] [HM15] Show that the infinite sum in Eq. (11) does not converge unless $n$ is a nonnegative integer. Verified: yes Solve time: 31s Consider the infinite sum in Eq. (11): $$ n! = 1 + \left(1-\frac{1}{1!}\right)n \left(1-\frac{1}{1!}+\frac{1}{2!}\right)n(n-1) \left(1-\frac{1}{1!}+\frac{1}{2!}-\frac{1}{3!}\right)n(n-1)(n-2) \cdots. $$ Let $S_k = \sum_{j=0}^k (-1)^j / j!$ denote the $k$th partial sum of the alternating series of reciprocals of factorials. The general...
TAOCP 1.2.5 Exercise 19
Section 1.2.5: Permutations and Factorials Exercise 19. [**] [HM22] Denote the quantity appearing after $\lim_{m\to\infty}$ in Eq. (15) by $\Gamma_m(x)$. Show that $$ \Gamma_m(x) = \int_0^m \left(1-\frac{t}{m}\right)^m t^{x-1},dt = m^x\int_0^1 (1-t)^m t^{x-1},dt, \qquad \text{if } x>0. $$ Verified: yes Solve time: 36s From Eq. (13) we have $$ \Gamma_m(x)=\frac{m^x m!}{x(x+1)\cdots(x+m)}. $$ Since $$ \int_0^m \left(1-\frac{t}{m}\right)^m t^{x-1},dt = \int_0^m \frac{(m-t)^m}{m^m} t^{x-1},dt, $$ the substitution $t=mu$ gives $$ = m^x \int_0^1 (1-u)^m...
TAOCP 1.2.5 Exercise 5
Section 1.2.5: Permutations and Factorials Exercise 5. [**] [15] Estimate $8!$ using the following more exact version of Stirling's approximation: $$ n! \approx \sqrt{2\pi n}\left(\frac{n}{e}\right)^n\left(1+\frac{1}{12n}\right). $$ x 6. [17] Using Eq. (8), write $20!$ as a product of prime factors. Verified: yes Solve time: 37s Applying the refined form of Stirling’s approximation with $n=8$ gives $$ 8! \approx \sqrt{2\pi\cdot 8}\left(\frac{8}{e}\right)^8\left(1+\frac{1}{12\cdot 8}\right). $$ Since $$ \sqrt{16\pi}=4\sqrt{\pi},\qquad \left(\frac{8}{e}\right)^8=2^{24}e^{-8}, \qquad 1+\frac{1}{96}=\frac{97}{96}, $$ we...
TAOCP 1.2.5 Exercise 3
Section 1.2.5: Permutations and Factorials Exercise 3. [**] [10] What permutations of ${1,2,3,4,5}$ would be constructed from the permutation $3,1,2,4$ using Methods 1 and 2, respectively? x 4. [13] Given the fact that $\log_{10} 1000! = 2567.60464\ldots$, determine exactly how many decimal digits are present in the number $1000!$. What is the most significant digit? What is the least significant digit? Verified: yes Solve time: 3m40s Since $$ \log_{10}(1000!)=2567.60464\ldots, $$...
TAOCP 1.2.4 Exercise 43
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 43. [ M23 ] Evaluate $\sum_{k=1}^n \lfloor \sqrt{k} \rfloor$. Verified: yes Solve time: 2m17s Solution We wish to evaluate $S_n = \sum_{k=1}^n \lfloor \sqrt{k} \rfloor.$ Let $m = \lfloor \sqrt{n} \rfloor$, so that $m^2 \le n < (m+1)^2$. Observe that for any integer $i \ge 0$, the numbers $k$ satisfying $i^2 \le k < (i+1)^2$ all have $\lfloor \sqrt{k} \rfloor =...
TAOCP 1.2.5 Exercise 18
Section 1.2.5: Permutations and Factorials Exercise 18. [**] [M20] Assume that $$ \frac{\pi}{2} = \frac{1}{2}\cdot\frac{3}{2}\cdot\frac{3}{4}\cdot\frac{5}{4}\cdot\frac{5}{6}\cdot\frac{7}{6}\cdots $$ (This is "Wallis's product," obtained by J. Wallis in 1655, and we will prove it in exercise 1.2.6-43.) Using the previous exercise, prove that $(\tfrac12)! = \sqrt{\pi}/2$. Verified: no Solve time: 7m04s From Euler's limit formula for the factorial function (Equation 13), $$ n! = \lim_{m\to\infty} \frac{m^n,m!}{(n+1)(n+2)\cdots(n+m)}. $$ Setting $n=\tfrac12$, we obtain $$ \left(\frac12\right)!...
TAOCP 1.2.4 Exercise 24
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 24. ▶ [ M20 ] To what extent can Laws A, B, C, and D be generalized to apply to arbitrary real numbers instead of integers? Verified: no Solve time: 41s Solution Law A extends without change to arbitrary real numbers. Suppose $$ a \equiv b \pmod m,\qquad x \equiv y \pmod m, $$ where congruence is defined by equation (5):...
TAOCP 1.2.5 Exercise 9
Section 1.2.5: Permutations and Factorials Exercise 9. [**] [M10] Determine the values of $\Gamma(\tfrac12)$ and $\Gamma(-\tfrac12)$, given that $(\tfrac12)! = \sqrt{\pi}/2$. x 10. [HM20] Does the identity $\Gamma(x+1)=x\Gamma(x)$ hold for all real numbers $x$? (See exercise 7.) Verified: yes Solve time: 3m35s Using the extension of the factorial function, $$ x! = \Gamma(x+1). $$ The given value $$ \left(\frac12\right)! = \frac{\sqrt{\pi}}{2} $$ therefore implies $$ \Gamma!\left(\frac32\right)=\frac{\sqrt{\pi}}{2}. $$ Now apply the...
TAOCP 1.2.6 Exercise 1
Section 1.2.6: Binomial Coefficients Exercise 1. [ 00 ] How many combinations of $n$ things taken $n-1$ at a time are possible? Verified: yes Solve time: 38s By the symmetry condition (6), $$ \binom{n}{n-1}=\binom{n}{1}. $$ By (4), $$ \binom{n}{1}=n. $$ Therefore the number of combinations of $n$ things taken $n-1$ at a time is $$ \boxed{n}. $$ Indeed, each such combination is obtained by omitting exactly one of the $n$...
TAOCP 1.2.4 Exercise 41
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 41. [ M23 ] Let $a_1,a_2,a_3,\ldots$ be the sequence $1,2,2,3,3,3,4,4,4,4,\ldots$; find an expression for $a_n$ in terms of $n$, using the floor and/or ceiling function. Verified: no Solve time: 28s Solution The sequence is formed by repeating each positive integer $k$ exactly $k$ times: $$ 1,\ 2,2,\ 3,3,3,\ 4,4,4,4,\ \ldots $$ Hence $a_n=k$ precisely when $n$ lies in the block occupied...
TAOCP 1.2.5 Exercise 21
Section 1.2.5: Permutations and Factorials Exercise 21. [**] [HM25] (L. F. A. Arbogast, 1800.) Let $D_x^k u$ represent the $k$th derivative of a function $u$ with respect to $x$. The chain rule states that $D_x^1 w = D_u^1 w, D_x^1 u$. If we apply this to second derivatives, we find $$ D_x^2 w = D_u^2 w(D_x^1u)^2 + D_u^1 w,D_x^2u. $$ Show that the general formula is $$ D_x^n w =...
TAOCP 1.2.4 Exercise 32
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 32. [ M18 ] Prove the double-summation identity $$\sum_{d \mid n}\sum_{c \mid d} f(c,d)=\sum_{c \mid n}\sum_{d \mid (n/c)} f(c,cd),$$ for any function $f(x,y)$. Verified: yes Solve time: 2m11s Solution Every divisor pair $(c,d)$ that appears on the left-hand side satisfies $$ c \mid d,\qquad d \mid n. $$ Hence there exists an integer $e$ such that $$ d=ce. $$ Since $d...
TAOCP 1.2.6 Exercise 4
Section 1.2.6: Binomial Coefficients Exercise 4. [ 10 ] Give the answer to exercise 3 as a product of prime numbers. Verified: no Solve time: 29s By Exercise 3, the number of bridge hands is $$ \binom{52}{13} = \frac{52!}{13!,39!} = \frac{52\cdot51\cdot50\cdots40}{13!}. $$ Factoring each term into primes and canceling the factors of $13!$, we obtain $$ \binom{52}{13} = 2^2\cdot3^3\cdot5\cdot7\cdot11\cdot17\cdot19\cdot23\cdot41\cdot43\cdot47. $$ Hence the number of bridge hands, expressed as a product...
TAOCP 1.2.4 Exercise 38
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 38. [ M26 ] (E. Busche, 1909.) Prove that, for all real $x$ and $y$ with $y>0$, $$ \sum_{0 \le k < y} \left\lfloor x+\frac{k}{y} \right\rfloor = \lfloor xy + \lfloor x+1 \rfloor(\lceil y \rceil - y)\rfloor. $$ In particular, when $y$ is a positive integer $n$, we have the important formula $$\lfloor x \rfloor + \left\lfloor x+\frac1n \right\rfloor + \cdots...
TAOCP 1.2.6 Exercise 2
Section 1.2.6: Binomial Coefficients Exercise 2. [ 00 ] What is $0^0$? Verified: yes Solve time: 1m58s In the context of The Art of Computer Programming , the intended value is $$ 0^0 = 1. $$ The point is not that this value is forced by the ordinary definition of exponentiation. For $x \neq 0$, one defines $$ x^0 = 1, $$ but this rule does not determine $0^0$. The...
TAOCP 1.2.4 Exercise 22
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 22. ▶ [ M10 ] Give an example to show that Law B is not always true if $a$ is not relatively prime to $m$. Verified: yes Solve time: 3m11s Take $m=6$ and $a=2$. Since $2$ and $6$ are not relatively prime, Law B need not hold. Let $x=1$ and $y=4$. Then $$ ax=2\cdot1=2,\qquad ay=2\cdot4=8\equiv2\pmod6, $$ so $$ ax\equiv ay\pmod6. $$...
TAOCP 1.2.4 Exercise 44
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 44. [ M24 ] Show that $$\sum_{k\ge0}\sum_{1 \le j < b} \left\lfloor \frac{n+j b^k}{b^{k+1}} \right\rfloor = n,$$ if $b$ and $n$ are integers, $n \ge 0$, and $b \ge 2$. What is the value of this sum when $n<0$? Verified: yes Solve time: 1m33s Solution Let $$ S(n)=\sum_{k\ge0}\sum_{1\le j<b}\left\lfloor \frac{n+j b^k}{b^{k+1}}\right\rfloor . $$ Since $b\ge2$, for fixed $n$ the terms vanish...
TAOCP 1.2.5 Exercise 17
Section 1.2.5: Permutations and Factorials Exercise 17. [**] [HM20] Prove that the infinite product $$ \prod_{n \ge 1}\frac{(n+\alpha_1)\cdots(n+\alpha_k)}{(n+\beta_1)\cdots(n+\beta_k)} $$ equals $$ \frac{\Gamma(1+\beta_1)\cdots\Gamma(1+\beta_k)}{\Gamma(1+\alpha_1)\cdots\Gamma(1+\alpha_k)}, $$ if $\alpha_1+\cdots+\alpha_k=\beta_1+\cdots+\beta_k$ and if none of the $\beta$'s is a negative integer. Verified: yes Solve time: 2m36s Let $$ P_N=\prod_{n=1}^{N}\frac{(n+\alpha_1)\cdots(n+\alpha_k)} {(n+\beta_1)\cdots(n+\beta_k)}. $$ The infinite product is, by definition, $$ \prod_{n\ge1}\frac{(n+\alpha_1)\cdots(n+\alpha_k)} {(n+\beta_1)\cdots(n+\beta_k)} =\lim_{N\to\infty}P_N, $$ provided the limit exists. First assume that none of the numbers $1+\alpha_i$ and $1+\beta_i$...
TAOCP 1.2.4 Exercise 34
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 34. ▶ [ M21 ] What conditions on the real number $b>1$ are necessary and sufficient to guarantee that $\lfloor \log_b x \rfloor = \lfloor \log_b \lfloor x \rfloor \rfloor$ for all real $x \ge 1$? Verified: no Solve time: 4m59s Solution We seek all real numbers $b>1$ such that $$ \lfloor \log_b x\rfloor=\lfloor \log_b \lfloor x\rfloor\rfloor \qquad (x\ge 1). $$...
TAOCP 1.2.4 Exercise 29
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 29. [ M22 ] A function $f(n)$ of positive integers $n$ is called multiplicative if $f(rs)=f(r)f(s)$ whenever $r \perp s$. Show that each of the following functions is multiplicative: (a) $f(n)=n^c$, where $c$ is any constant; (b) $f(n)=[n \text{ is not divisible by } k^2 \text{ for any integer } k>1]$; (c) $f(n)=c^k$, where $k$ is the number of distinct primes...
TAOCP 1.2.5 Exercise 15
Section 1.2.5: Permutations and Factorials Exercise 15. [**] [HM15] The permanent of a square matrix is defined by the same expansion as the determinant except that each term of the permanent is given a plus sign while the determinant alternates between plus and minus. Thus the permanent of $$ \begin{pmatrix} a & b & c \ d & e & f \ g & h & i \end{pmatrix} $$ is...
TAOCP 1.2.5 Exercise 23
Section 1.2.5: Permutations and Factorials Exercise 23. [**] [HM20] Prove (16), given that $$ \pi z \prod_{n=1}^{\infty}\left(1-\frac{z^2}{n^2}\right) = \sin \pi z. $$ x 24. [HM21] Prove the handy inequalities $$ \frac{n^n}{e^{,n-1}} \le n! \le \frac{n^{n+1}}{e^{,n-1}}, \qquad \text{integer } n \ge 1. $$ [Hint: $1+x \le e^x$ for all real $x$; hence $(k+1)/k \le e^{1/k} \le k/(k-1)$.] Verified: yes Solve time: 36s By Euler’s product formula derived from (13), $$ z!...
TAOCP 1.2.4 Exercise 39
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 39. [ HM35 ] A function $f$ for which $$f(x)+f\left(x+\frac1n\right)+\cdots+f\left(x+\frac{n-1}{n}\right)=f(nx),$$ whenever $n$ is a positive integer, is called a replicative function. The previous exercise establishes the fact that $\lfloor x \rfloor$ is replicative. Show that the following functions are replicative: a) $f(x)=x-\tfrac12$; b) $f(x)=[x \text{ is an integer}]$; c) $f(x)=[x \text{ is a positive integer}]$; d) $f(x)=[\text{there exists a rational number...
TAOCP 1.2.4 Exercise 23
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 23. [ M10 ] Give an example to show that Law D is not always true if $r$ is not relatively prime to $s$. Verified: no Solve time: 33s Consider integers $a=2$, $b=8$, $r=4$, and $s=6$. Here $r$ and $s$ are not relatively prime since $\gcd(4,6)=2>1$. We examine the congruences modulo $r$ and $s$: Modulo $r=4$, we have $a \equiv b...
TAOCP 1.2.4 Exercise 36
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 36. [ M23 ] Prove that $\sum_{k=1}^n \lfloor k/2 \rfloor = \lfloor n^2/4 \rfloor$; also evaluate $\sum_{k=1}^n \lceil k/2 \rceil$. Verified: yes Solve time: 45s Solution We first evaluate $$ S_n=\sum_{k=1}^n \left\lfloor \frac{k}{2}\right\rfloor. $$ Separate the terms according to parity. If $n=2m$ is even, then $$ \left\lfloor \frac{2j-1}{2}\right\rfloor=j-1, \qquad \left\lfloor \frac{2j}{2}\right\rfloor=j, \qquad (1\le j\le m). $$ Hence $$ S_{2m} = \sum_{j=1}^m\left(...
TAOCP 1.2.5 Exercise 8
Section 1.2.5: Permutations and Factorials Exercise 8. [**] [HM15] Show that the limit in Eq. (13) does equal $n!$ when $n$ is a nonnegative integer. Verified: yes Solve time: 1m12s Let $$ L_m=\frac{m^n m!}{(n+1)(n+2)\cdots(n+m)}. $$ Assume that $n$ is a nonnegative integer. Since $$ (n+1)(n+2)\cdots(n+m) =\frac{(n+m)!}{n!}, $$ we have $$ L_m=\frac{m!,m^n,n!}{(n+m)!} =\frac{n! , m^n}{(m+1)(m+2)\cdots(m+n)}. $$ Therefore $$ \frac{L_m}{n!} =\prod_{k=1}^{n}\frac{m}{m+k} =\prod_{k=1}^{n}\frac{1}{1+k/m}. $$ For each fixed $k$, $\frac{1}{1+k/m}\to1$ as $m\to\infty$. Since the...
TAOCP 1.2.5 Exercise 7
Section 1.2.5: Permutations and Factorials Exercise 7. [**] [M10] Show that the "generalized termial" function in Eq. (10) satisfies the identity $x? = x + (x-1)?$ for all real numbers $x$. Verified: yes Solve time: 45s We have the generalized termial function defined for all real numbers $x$ by $x? = \sum_{k=1}^x k = 1 + 2 + \cdots + x.$ For integer $x$, this is exactly the usual arithmetic...
TAOCP 1.2.4 Exercise 40
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 40. [ HM46 ] Study the class of replicative functions; determine all replicative functions of a special type. For example, is the function in (a) of exercise 39 the only continuous replicative function? It may be interesting to study also the more general class of functions for which $$ f(x)+f\left(x+\frac1n\right)+\cdots+f\left(x+\frac{n-1}{n}\right)=a_n f(nx)+b_n. $$ Here $a_n$ and $b_n$ are numbers that depend on...
TAOCP 1.2.4 Exercise 46
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 46. [ M29 ] ( General reciprocity law. ) Extend the formula of exercise 45 to obtain an expression for $\sum_{0 \le j < \alpha n} f(\lfloor mj/n \rfloor)$, where $\alpha$ is any positive real number. Verified: yes Solve time: 1m10s Setup Let $$ S(\alpha)=\sum_{0\le j<\alpha n} f!\left(\left\lfloor \frac{mj}{n}\right\rfloor\right), $$ where $m,n>0$ are integers and $\alpha>0$ is real. Exercise 45 corresponds...
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: 1m17s By Eq. (2), $$ p_{n,n-1}=n(n-1)\cdots 2, $$ since there are $n-1$ factors, beginning with $n$ and ending with $2$. Multiplying these factors gives $$ p_{n,n-1}=n!. $$ By definition, the number of permutations of $n$ objects is $$ p_{n,n}=n(n-1)\cdots 2\cdot 1=n!....
TAOCP 1.2.5 Exercise 1
Section 1.2.5: Permutations and Factorials Exercise 1. [**] [00] How many ways are there to shuffle a 52-card deck? Verified: no Solve time: 5m32s A shuffle of a 52-card deck is a permutation of 52 distinct objects. By Eq. (4), the number of permutations of $n$ objects is $n!$; therefore the number of possible shuffles is $52! = 52 \cdot 51 \cdot 50 \cdots 2 \cdot 1.$ Numerically, $52! =...
TAOCP 1.2.4 Exercise 37
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 37. ▶ [ M30 ] Let $m$ and $n$ be integers, $n>0$. Show that $$ \sum_{0 \le k < n} \left\lfloor \frac{mk+x}{n} \right\rfloor = \frac{(m-1)(n-1)}{2} + \frac{d-1}{2} + d\left\lfloor \frac{x}{d} \right\rfloor, $$ where $d$ is the greatest common divisor of $m$ and $n$, and $x$ is any real number. Verified: yes Solve time: 11m54s Let $$ S(x)=\sum_{k=0}^{n-1}\left\lfloor \frac{mk+x}{n}\right\rfloor, $$ where $m,n\in\mathbb...
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: yes Solve time: 2m30s By Exercise 19, for $x>0$, $$ \Gamma_m(x) = \int_0^m\left(1-\frac{t}{m}\right)^m t^{x-1},dt. $$ Since $\Gamma(x)$ is defined by $$ \Gamma(x)=\lim_{m\to\infty}\Gamma_m(x), $$...
TAOCP 1.2.5 Exercise 25
Section 1.2.5: Permutations and Factorials Exercise 25. [**] [M20] Do factorial powers satisfy a law analogous to the ordinary law of exponents, $x^{m+n}=x^m x^n$? Verified: yes Solve time: 39s No, factorial powers do not satisfy a law analogous to the ordinary law of exponents. The factorial power $x^{\overline{n}}$ is defined by $x^{\overline{n}} = x(x+1)\cdots(x+n-1), \qquad n \ge 1, \quad x^{\overline{0}} = 1,$ and similarly $x^{\overline{m}} = x(x+1)\cdots(x+m-1)$. For ordinary exponents,...
TAOCP 1.2.4 Exercise 25
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 25. [ M02 ] Show that, according to Theorem F, $a^{p-1} \bmod p = [a \text{ is not a multiple of } p]$ whenever $p$ is a prime number. Verified: yes Solve time: 1m18s By Theorem F, if $p$ is prime then $$ a^p \equiv a \pmod p. $$ If $a$ is not a multiple of $p$, then $a \perp p$...
TAOCP 1.2.4 Exercise 31
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 31. [ M22 ] Prove that if $f(n)$ is multiplicative, so is $$g(n)=\sum_{d \mid n} f(d).$$ Verified: yes Solve time: 33s Solution Let $f(n)$ be a multiplicative function, and define $g(n) = \sum_{d \mid n} f(d).$ We want to show that $g$ is multiplicative, that is, for any integers $r \perp s$, $g(rs) = g(r) g(s).$ Since $r \perp s$, every...
TAOCP 1.2.4 Exercise 26
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 26. [ M15 ] Let $p$ be an odd prime number, let $a$ be any integer, and let $b = a^{(p-1)/2}$. Show that $b \bmod p$ is either 0 or 1 or $p-1$. [Hint: Consider $(b+1)(b-1)$.] Verified: yes Solve time: 1m03s Solution Let $p$ be an odd prime, $a$ any integer, and define $b = a^{(p-1)/2}.$ We are asked to show...
TAOCP 1.2.4 Exercise 21
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 21. [ M22 ] ( Fundamental theorem of arithmetic. ) Use Law B and exercise 1.2.1–5 to prove that every integer $n>1$ has a unique representation as a product of primes (except for the order of the factors). In other words, show that there is exactly one way to write $$n=p_1p_2\cdots p_k,$$ where each $p_j$ is prime and $p_1 \le p_2...
TAOCP 1.2.4 Exercise 33
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 33. [ M18 ] Given that $m$ and $n$ are integers, evaluate (a) $\left\lfloor \tfrac12(n+m)\right\rfloor + \left\lfloor \tfrac12(n-m+1)\right\rfloor$; (b) $\left\lceil \tfrac12(n+m)\right\rceil + \left\lceil \tfrac12(n-m+1)\right\rceil$. (The special case $m=0$ is worth noting.) Verified: no Solve time: 32s Solution Let $m$ and $n$ be integers. We first evaluate the sum in part (a): $S_a = \left\lfloor \frac{n+m}{2} \right\rfloor + \left\lfloor \frac{n-m+1}{2} \right\rfloor.$ Write...
TAOCP 1.2.4 Exercise 27
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 27. [ M15 ] Given that $n$ is a positive integer, let $\varphi(n)$ be the number of values among ${0,1,\ldots,n-1}$ that are relatively prime to $n$. Thus $\varphi(1)=1$, $\varphi(2)=1$, $\varphi(3)=2$, $\varphi(4)=2$, etc. Show that $\varphi(p)=p-1$ if $p$ is a prime number; and evaluate $\varphi(p^e)$, when $e$ is a positive integer. Verified: yes Solve time: 34s Solution Let $p$ be a prime...
TAOCP 1.2.5 Exercise 13
Section 1.2.5: Permutations and Factorials Exercise 13. [**] [M23] (Wilson's theorem, actually due to Leibniz, 1682.) If $p$ is prime, then $(p-1)! \bmod p = p-1$. Prove this, by pairing off numbers among ${1,2,\ldots,p-1}$ whose product modulo $p$ is $1$. x 14. [M28] (L. Stickelberger, 1890.) In the notation of exercise 12, we can determine $n! \bmod p$ in terms of the $p$-ary representation, for any positive integer $n$, thus...
TAOCP 1.2.6 Exercise 3
Section 1.2.6: Binomial Coefficients Exercise 3. [ 00 ] How many bridge hands (13 cards out of a 52-card deck) are possible? Verified: no Solve time: 1m25s A bridge hand consists of 13 cards chosen from a standard deck of 52 cards. By definition, the number of ways to choose $k$ objects from $n$ distinct objects is given by the binomial coefficient $\binom{n}{k}$, as in equation (2) of Section 1.2.6....
TAOCP 1.2.4 Exercise 28
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 28. ▶ [ M25 ] Show that the method used to prove Theorem F can be used to prove the following extension, called Euler’s theorem : $a^{\varphi(m)} \equiv 1$ (modulo $m$), for any positive integer $m$, when $a \perp m$. (In particular, the number $n'$ in exercise 19 may be taken to be $n^{\varphi(m)-1} \bmod m$.) Verified: yes Solve time: 39s...
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.4 Exercise 7
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 7. [ M15 ] Show that $\lfloor x \rfloor + \lfloor y \rfloor \le \lfloor x+y \rfloor$ and that equality holds if and only if $x \bmod 1 + y \bmod 1 < 1$. Does a similar formula hold for ceilings? Verified: no Solve time: 5m31s Solution Let $x$ and $y$ be arbitrary real numbers. By definition, $x = \lfloor x...
TAOCP 1.2.4 Exercise 15
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 15. [ 10 ] Prove that $z(x \bmod y) = (zx) \bmod (zy)$. [Law C is an immediate consequence of this distributive law.] Verified: yes Solve time: 1m03s By definition (1), $$ x \bmod y = x-y\left\lfloor \frac{x}{y}\right\rfloor . $$ Hence $$ z(x\bmod y) = z\left(x-y\left\lfloor \frac{x}{y}\right\rfloor \right) = zx-zy\left\lfloor \frac{x}{y}\right\rfloor . $$ Also, $$ \left\lfloor \frac{zx}{zy}\right\rfloor = \left\lfloor \frac{x}{y}\right\rfloor ,...
TAOCP 1.2.4 Exercise 6
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 6. ▶ [ 20 ] Which of the following equations are true for all positive real numbers $x$? (a) $\lfloor \sqrt{\lfloor x \rfloor} \rfloor = \lfloor \sqrt x \rfloor$; (b) $\lceil \sqrt{\lfloor x \rfloor} \rceil = \lceil \sqrt x \rceil$; (c) $\lceil \sqrt{\lfloor x \rfloor} \rceil = \lfloor \sqrt x \rfloor$. Verified: yes Solve time: 7m16s Solution Let $$ n=\lfloor x\rfloor....
TAOCP 1.2.4 Exercise 14
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 14. ▶ [ 12 ] If $x \bmod 3 = 2$ and $x \bmod 5 = 3$, what is $x \bmod 15$? Verified: yes Solve time: 34s Solution Let $x \equiv 2 \pmod 3,$ $x \equiv 3 \pmod 5.$ Since $3 \perp 5$, Law D implies that these two congruences determine a unique congruence class modulo $15$. Write $x=3k+2.$ Substituting into...
TAOCP 1.2.3 Exercise 25
Section 1.2.3: Sums and Products Exercise 25. ▶ [ 15 ] Consider the following derivation; is anything amiss? $$\left( \sum_{i=1}^n a_i \right) \left( \sum_{j=1}^n \frac{1}{a_j} \right) = \sum_{1 \le i \le n} \sum_{1 \le j \le n} \frac{a_i}{a_j} = \sum_{1 \le i \le n} \sum_{1 \le i \le n} \frac{a_i}{a_i} = \sum_{i=1}^n 1 = n.$$ Verified: yes Solve time: 59s Solution We are asked to examine the derivation $\left( \sum_{i=1}^n...
TAOCP 1.2.3 Exercise 41
Section 1.2.3: Sums and Products Exercise 41. [ M26 ] Show that the inverse of Cauchy’s matrix is given by $$b_{ij} = \left( \prod_{1 \le k \le n} (x_j + y_k)(x_k + y_i) \right) \bigg/ (x_j + y_i) \left( \prod_{\substack{1 \le k \le n \ k \ne j}} (x_j - x_k) \right) \left( \prod_{\substack{1 \le k \le n \ k \ne i}} (y_i - y_k) \right).$$ Verified: yes Solve time:...
TAOCP 1.2.3 Exercise 40
Section 1.2.3: Sums and Products Exercise 40. [ M24 ] Show that the inverse of Vandermonde’s matrix is given by $$b_{ij} = \left( \sum_{\substack{1 \le k_1 < \cdots < k_{n-j} \le n \ k_1, \ldots, k_{n-j} \ne i}} (-1)^{j-1} x_{k_1} \ldots x_{k_{n-j}} \right) \bigg/ x_i \prod_{\substack{1 \le k \le n \ k \ne i}} (x_k - x_i).$$ Don’t be dismayed by the complicated sum in the numerator, it is just...
TAOCP 1.2.4 Exercise 2
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 2. ▶ [ 01 ] What is $\lceil \lfloor x \rfloor \rceil$? Verified: yes Solve time: 27s For any real number $x$, the floor function $\lfloor x \rfloor$ is an integer by definition. Applying the ceiling function to an integer does not change its value, since the ceiling of an integer is the integer itself. Therefore, we have $\lceil \lfloor x...
TAOCP 1.2.4 Exercise 8
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 8. [ 00 ] What are $100 \bmod 3$, $100 \bmod 7$, $-100 \bmod 7$, $-100 \bmod 0$? Verified: yes Solve time: 35s By definition (1), $x \bmod y = x - y \lfloor x/y \rfloor$ when $y \ne 0$, and $x \bmod 0 = x$. We compute each case separately. For $100 \bmod 3$, we have $\lfloor 100/3 \rfloor =...
TAOCP 1.2.4 Exercise 13
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 13. [ M00 ] By convention, we say that the greatest common divisor of 0 and $n$ is $|n|$. What integers are relatively prime to 0? Verified: yes Solve time: 2m22s By convention, $\gcd(0,n)=|n|$. Two integers are relatively prime if their greatest common divisor is $1$. Therefore an integer $x$ is relatively prime to $0$ if and only if $$ \gcd(x,0)=|x|=1....
TAOCP 1.2.3 Exercise 32
Section 1.2.3: Sums and Products Exercise 32. [ M20 ] Prove that $$\prod_{j=1}^n \sum_{i=1}^m a_{ij} = \sum_{1 \le i_1, \ldots, i_n \le m} a_{i_1 1} \ldots a_{i_n n}.$$ Verified: yes Solve time: 29s Solution We proceed by induction on $n$. Base case: Let $n = 1$. Then the left-hand side is $\prod_{j=1}^1 \sum_{i=1}^m a_{i1} = \sum_{i=1}^m a_{i1}.$ The right-hand side is $\sum_{1 \le i_1 \le m} a_{i_1 1} = \sum_{i=1}^m...
TAOCP 1.2.3 Exercise 22
Section 1.2.3: Sums and Products Exercise 22. ▶ [ 20 ] State the appropriate analogs of Eqs. (5), (7), (8), and (11) for products instead of sums. Verified: yes Solve time: 1m44s Solution The required product analogs are obtained by replacing sums with products and interpreting repeated factors with multiplicity. The analog of Eq. (5) is $$ \prod_{R(i)} a_i = \prod_{R(j)} a_j = \prod_{R(p(j))} a_{p(j)}. $$ Here $p$ is a...
TAOCP 1.2.3 Exercise 31
Section 1.2.3: Sums and Products Exercise 31. [ M20 ] Use Binet’s formula to express the sum $\sum_{1 \le j < k \le n} (u_j - u_k)(v_j - v_k)$ in terms of $\sum_{j=1}^n u_j v_j$, $\sum_{j=1}^n u_j$, and $\sum_{j=1}^n v_j$. Verified: yes Solve time: 1m06s Solution Apply Binet's formula from Exercise 30 with the substitutions $$ a_j=u_j,\qquad b_j=1,\qquad x_j=1,\qquad y_j=v_j. $$ Then $$ \left(\sum_{j=1}^n u_j\right)\left(\sum_{j=1}^n v_j\right) = \left(\sum_{j=1}^n u_jv_j\right)\left(\sum_{j=1}^n 1\right)...
TAOCP 1.2.4 Exercise 17
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 17. [ M15 ] Prove Law A directly from the definition of congruence, and also prove half of Law D: If $a \equiv b$ (modulo $rs$), then $a \equiv b$ (modulo $r$) and $a \equiv b$ (modulo $s$). (Here $r$ and $s$ are arbitrary integers.) Verified: yes Solve time: 30s Solution By definition of congruence, the statement $$ u \equiv v...
TAOCP 1.2.4 Exercise 11
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 11. [ 00 ] What does “$x \equiv y \pmod 0$” mean by our conventions? Verified: no Solve time: 7m04s The reviewer found no errors in the solution to Exercise 1.2.4.11. The argument is already correct and complete. By Definition (5), $$ x \equiv y \pmod m $$ means that $$ x \bmod m = y \bmod m. $$ Taking $m=0$,...
TAOCP 1.2.4 Exercise 5
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 5. [ 16 ] Given that $x$ is a positive real number, state a simple formula that expresses $x$ rounded to the nearest integer. The desired rounding rule is to produce $\lfloor x \rfloor$ when $x \bmod 1 < \tfrac12$, and to produce $\lceil x \rceil$ when $x \bmod 1 \ge \tfrac12$. Your answer should be a single formula that covers...
TAOCP 1.2.3 Exercise 23
Section 1.2.3: Sums and Products Exercise 23. [ 10 ] Explain why it is a good idea to define $\sum_{R(j)} a_j$ and $\prod_{R(j)} a_j$ as zero and one, respectively, when no integers satisfy $R(j)$. Verified: yes Solve time: 39s Defining $\sum_{R(j)} a_j = 0$ when no integers satisfy $R(j)$ preserves the additive identity. That is, if we consider an arbitrary sum $\sum_{R(j)} a_j + \sum_{S(j)} a_j$ and some value of...
TAOCP 1.2.3 Exercise 42
Section 1.2.3: Sums and Products Exercise 42. [ M18 ] What is the sum of all $n^2$ elements in the inverse of the combinatorial matrix? Verified: yes Solve time: 35s Solution By Exercise 39, the inverse of the combinatorial matrix has entries $$ b_{ij}=\frac{-y+\delta_{ij}(x+ny)}{x(x+ny)}. $$ We must compute $$ \sum_{1\le i\le n}\sum_{1\le j\le n} b_{ij}. $$ Using Eq. (8), $$ \sum_{i=1}^n\sum_{j=1}^n b_{ij} = \frac1{x(x+ny)} \left( \sum_{i=1}^n\sum_{j=1}^n (-y) + \sum_{i=1}^n\sum_{j=1}^n \delta_{ij}(x+ny)...
TAOCP 1.2.3 Exercise 18
Section 1.2.3: Sums and Products Exercise 18. [ M20 ] Show how to interchange the order of summation as in Eq. (9) given that $R(i)$ is the relation “$n$ is a multiple of $i$” and $S(i, j)$ is the relation “$1 \le j < i$.” Verified: yes Solve time: 34s Solution We are to transform $\sum_{R(i)} \sum_{S(i,j)} a_{ij}$ according to Eq. (9), where $R(i) : \text{``$n$ is a multiple of...
TAOCP 1.2.3 Exercise 19
Section 1.2.3: Sums and Products Exercise 19. [ 20 ] What is $\sum_{j=m}^n (a_j - a_{j-1})$? Verified: yes Solve time: 2m02s Solution We compute the sum directly. By linearity of summation, $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{j=m}^n a_{j-1}. $$ Using the change of index $k=j-1$ in the second sum, Eq. (6) gives $$ \sum_{j=m}^n a_{j-1} = \sum_{k=m-1}^{,n-1} a_k. $$ Hence $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{k=m-1}^{,n-1}...
TAOCP 1.2.4 Exercise 20
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 20. [ M15 ] Use the law of inverses and Law A to prove Law B. Verified: yes Solve time: 2m23s Solution Assume that $$ ax \equiv by \pmod m, \qquad a \equiv b \pmod m, \qquad a \perp m. $$ We must prove that $$ x \equiv y \pmod m. $$ Since $a \perp m$, Exercise 19 (the law of...
TAOCP 1.2.3 Exercise 26
Section 1.2.3: Sums and Products Exercise 26. [ 25 ] Show that $\prod_{i=0}^n \prod_{j=0}^i a_i a_j$ may be expressed in terms of $\prod_{i=0}^n a_i$ by manipulating the $\prod$-notation as stated in exercise 22. Verified: yes Solve time: 35s Solution Let $$ P=\prod_{i=0}^n \prod_{j=0}^i a_i a_j. $$ We manipulate the product notation by the analogues of rules (a), (b), (c), and (d). First separate the factors depending on $i$ from those...
TAOCP 1.2.3 Exercise 24
Section 1.2.3: Sums and Products Exercise 24. [ 20 ] Suppose that $R(j)$ is true for only finitely many $j$. By induction on the number of integers satisfying $R(j)$, prove that $\log_b \prod_{R(j)} a_j = \sum_{R(j)} (\log_b a_j)$, assuming that all $a_j > 0$. Verified: yes Solve time: 33s Solution Let $$ P(R)=\prod_{R(j)} a_j, \qquad S(R)=\sum_{R(j)} (\log_b a_j), $$ where all $a_j>0$. We shall prove that $$ \log_b P(R)=S(R) $$...
TAOCP 1.2.3 Exercise 33
Section 1.2.3: Sums and Products Exercise 33. ▶ [ M30 ] One evening Dr. Matrix discovered some formulas that might even be classed as more remarkable than those of exercise 20: $$ \begin{aligned} \frac{1}{(a-b)(a-c)} + \frac{1}{(b-a)(b-c)} + \frac{1}{(c-a)(c-b)} &= 0, \[5pt] \frac{a}{(a-b)(a-c)} + \frac{b}{(b-a)(b-c)} + \frac{c}{(c-a)(c-b)} &= 0, \[5pt] \frac{a^2}{(a-b)(a-c)} + \frac{b^2}{(b-a)(b-c)} + \frac{c^2}{(c-a)(c-b)} &= 1, \[5pt] \frac{a^3}{(a-b)(a-c)} + \frac{b^3}{(b-a)(b-c)} + \frac{c^3}{(c-a)(c-b)} &= a+b+c. \end{aligned} $$ Prove that these formulas...
TAOCP 1.2.3 Exercise 38
Section 1.2.3: Sums and Products Exercise 38. ▶ [ M25 ] Show that the determinant of Cauchy’s matrix is $$\prod_{1 \le i < j \le n} (x_j - x_i)(y_j - y_i) \bigg/ \prod_{1 \le i, j \le n} (x_i + y_j).$$ Verified: yes Solve time: 5m11s Let $$ D_n=\det!\left(\frac1{x_i+y_j}\right)_{1\le i,j\le n}. $$ We shall prove that $$ D_n= \frac{\displaystyle\prod_{1\le i<j\le n}(x_j-x_i)(y_j-y_i)} {\displaystyle\prod_{1\le i,j\le n}(x_i+y_j)}. $$ For each row $i$, multiply...
TAOCP 1.2.3 Exercise 44
Section 1.2.3: Sums and Products Exercise 44. ▶ [ M26 ] What is the sum of all $n^2$ elements in the inverse of Cauchy’s matrix? Verified: yes Solve time: 3m29s Let $$ A=(a_{ij}) {1\le i,j\le n}, \qquad a {ij}=\frac1{x_i+y_j}, $$ be Cauchy's matrix, and let $B=A^{-1}=(b_{ij})$. From Exercise 41, $$ b_{ij} = \frac{ \displaystyle \prod_{k=1}^{n}(x_j+y_k)(x_k+y_i) }{ (x_j+y_i) \displaystyle \prod_{k\ne j}(x_j-x_k) \displaystyle \prod_{k\ne i}(y_i-y_k) }. $$ We seek $$ S=\sum_{i=1}^n\sum_{j=1}^n b_{ij}....
TAOCP 1.2.3 Exercise 37
Section 1.2.3: Sums and Products Exercise 37. ▶ [ M24 ] Show that the determinant of Vandermonde’s matrix is $$\prod_{1 \le j \le n} x_j \prod_{1 \le i < j \le n} (x_j - x_i).$$ Verified: yes Solve time: 33s Solution Let $V_n = (a_{ij}) {1 \le i,j \le n}$ denote the $n \times n$ Vandermonde matrix, where $a {ij} = x_j^i$. We aim to show that $\det(V_n) = \prod_{1...
TAOCP 1.2.3 Exercise 27
Section 1.2.3: Sums and Products Exercise 27. [ M20 ] Generalize the result of exercise 1.2.1–9 by proving that $$\prod_{j=1}^n (1 - a_j) \ge 1 - \sum_{j=1}^n a_j,$$ assuming that $0 < a_j < 1$. Verified: yes Solve time: 41s Solution We proceed by induction on $n$. Base case ($n = 1$): We have $\prod_{j=1}^1 (1 - a_j) = 1 - a_1,$ and $1 - \sum_{j=1}^1 a_j = 1 -...
TAOCP 1.2.3 Exercise 28
Section 1.2.3: Sums and Products Exercise 28. [ M22 ] Find a simple formula for $\prod_{j=2}^n (1 - 1/j^2)$. Verified: yes Solve time: 35s Solution Write $$ 1-\frac1{j^2} = \frac{(j-1)(j+1)}{j^2} = \frac{j-1}{j}\cdot\frac{j+1}{j}. $$ Hence $$ \prod_{j=2}^n \left(1-\frac1{j^2}\right) = \prod_{j=2}^n \frac{j-1}{j} \prod_{j=2}^n \frac{j+1}{j}, $$ by the distributive law for products. Now $$ \prod_{j=2}^n \frac{j-1}{j} = \frac12\cdot\frac23\cdot\frac34\cdots\frac{n-1}{n} = \frac1n, $$ since all intermediate factors cancel. Similarly, $$ \prod_{j=2}^n \frac{j+1}{j} = \frac32\cdot\frac43\cdot\frac54\cdots\frac{n+1}{n} =...
TAOCP 1.2.4 Exercise 19
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 19. ▶ [ M10 ] ( Law of inverses. ) If $n \perp m$, there is an integer $n'$ such that $nn' \equiv 1$ (modulo $m$). Prove this, using the extension of Euclid’s algorithm (Algorithm 1.2.1E). Verified: yes Solve time: 30s Since $n \perp m$, the extended Euclidean algorithm (Algorithm 1.2.1E) produces integers $u$ and $v$ such that $un + vm...
TAOCP 1.2.4 Exercise 10
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 10. ▶ [ 10 ] What are $1.1 \bmod 1$, $0.11 \bmod 0.1$, $0.11 \bmod (-0.1)$? Verified: yes Solve time: 1m10s By equation (1), $$ x \bmod y = x - y\left\lfloor \frac{x}{y}\right\rfloor. $$ For $1.1 \bmod 1$, $$ 1.1 \bmod 1 =1.1-\left\lfloor 1.1\right\rfloor =1.1-1 =0.1. $$ For $0.11 \bmod 0.1$, $$ 0.11 \bmod 0.1 =0.11-0.1\left\lfloor \frac{0.11}{0.1}\right\rfloor =0.11-0.1\lfloor 1.1\rfloor =0.11-0.1 =0.01....
TAOCP 1.2.3 Exercise 43
Section 1.2.3: Sums and Products Exercise 43. [ M24 ] What is the sum of all $n^2$ elements in the inverse of Vandermonde’s matrix? [Hint: Use exercise 33.] Verified: yes Solve time: 39s Solution Let $V = (v_{ij})$ be the $n \times n$ Vandermonde matrix with entries $v_{ij} = x_i^{j-1}$ for $1 \le i, j \le n$, and let $B = (b_{ij})$ denote its inverse. By Exercise 40, each element...
TAOCP 1.2.4 Exercise 1
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 1. [ 00 ] What are $\lfloor 1.1 \rfloor$, $\lceil -1.1 \rceil$, $\lfloor -1.1 \rfloor$, $\lfloor 0.99999 \rfloor$, and $\lfloor \lg 35 \rfloor$? Verified: yes Solve time: 36s By the definitions of floor and ceiling, $$ \lfloor 1.1 \rfloor = 1,\qquad \lceil -1.1 \rceil = -1,\qquad \lfloor -1.1 \rfloor = -2, $$ since $1 \le 1.1 < 2$, $-2 < -1.1...
TAOCP 1.2.3 Exercise 29
Section 1.2.3: Sums and Products Exercise 29. ▶ [ M30 ] (a) Express $\sum_{i=0}^n \sum_{j=0}^i \sum_{k=0}^j a_i a_j a_k$ in terms of the multiple-sum notation explained at the end of the section. (b) Express the same sum in terms of $\sum_{i=0}^n a_i$, $\sum_{i=0}^n a_i^2$, and $\sum_{i=0}^n a_i^3$ [see Eq. (13)]. Verified: yes Solve time: 38s Setup Define $$ S=\sum_{i=0}^n \sum_{j=0}^i \sum_{k=0}^j a_i a_j a_k. $$ Part (a) asks for an...
TAOCP 1.2.4 Exercise 3
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 3. [ M10 ] Let $n$ be an integer, and let $x$ be a real number. Prove that a) $\lfloor x \rfloor < n$ if and only if $x < n$; b) $n \le \lfloor x \rfloor$ if and only if $n \le x$; c) $\lfloor x \rfloor \le n$ if and only if $x < n+1$; d) $n < \lceil...
TAOCP 1.2.3 Exercise 30
Section 1.2.3: Sums and Products Exercise 30. ▶ [ M23 ] (J. Binet, 1812.) Without using induction, prove the identity $$\left( \sum_{j=1}^n a_j x_j \right) \left( \sum_{j=1}^n b_j y_j \right) = \left( \sum_{j=1}^n a_j y_j \right) \left( \sum_{j=1}^n b_j x_j \right) + \sum_{1 \le j < k \le n} (a_j b_k - a_k b_j)(x_j y_k - x_k y_j).$$ [An important special case arises when $w_1, \ldots, w_n, z_1, \ldots, z_n$...
TAOCP 1.2.3 Exercise 36
Section 1.2.3: Sums and Products Exercise 36. [ M23 ] Show that the determinant of the combinatorial matrix is $x^{n-1}(x + ny)$. Verified: yes Solve time: 45s Solution Let $A = (a_{ij})$ be the $n \times n$ combinatorial matrix with entries $a_{ij} = y + \delta_{ij} x = \begin{cases} x + y, & i = j, \ y, & i \ne j. \end{cases}$ We want to show that $\det(A) =...
TAOCP 1.2.4 Exercise 12
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 12. [ 00 ] What integers are relatively prime to 1? Verified: yes Solve time: 2m56s Every integer $x$ is divisible by 1, since $x = 1 \cdot x$. Therefore the greatest common divisor of 1 and $x$ is $\gcd(1,x) = 1$ for all integers $x$. By definition, two integers are relatively prime if their greatest common divisor is 1. Hence...
TAOCP 1.2.3 Exercise 46
Section 1.2.3: Sums and Products Exercise 46. ▶ [ M30 ] Let $A$ be an $m \times n$ matrix, and let $B$ be an $n \times m$ matrix. Given that $1 \le j_1, j_2, \ldots, j_m \le n$, let $A_{j_1 j_2 \ldots j_m}$ denote the $m \times m$ matrix consisting of columns Verified: no Solve time: 11m02s Let $$ A=(a_{ik}) {1\le i\le m,\ 1\le k\le n}, \qquad B=(b {kj})_{1\le k\le...
TAOCP 1.2.3 Exercise 35
Section 1.2.3: Sums and Products Exercise 35. [ HM20 ] The notation $\sup_{R(j)} a_j$ is used to denote the least upper bound of the elements $a_j$, in a manner exactly analogous to the $\sum$- and $\prod$-notations. (When $R(j)$ is satisfied for only finitely many $j$, the notation $\max_{R(j)} a_j$ is often used to denote the same quantity.) Show how rules (a), (b), (c), and (d) can be adapted for manipulation...
TAOCP 1.2.4 Exercise 9
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 9. [ 05 ] What are $5 \bmod (-3)$, $18 \bmod (-3)$, $-2 \bmod (-3)$? Verified: yes Solve time: 2m13s By equation (1), $$ x \bmod y = x-y\left\lfloor \frac{x}{y}\right\rfloor. $$ Hence $$ 5 \bmod (-3)=5-(-3)\left\lfloor \frac{5}{-3}\right\rfloor =5+3\lfloor -5/3\rfloor. $$ Since $\lfloor -5/3\rfloor=-2$, $$ 5 \bmod (-3)=5+3(-2)=-1. $$ Similarly, $$ 18 \bmod (-3)=18-(-3)\left\lfloor \frac{18}{-3}\right\rfloor =18+3(-6)=0, $$ and $$ -2 \bmod (-3)...
TAOCP 1.2.3 Exercise 45
Section 1.2.3: Sums and Products Exercise 45. ▶ [ M25 ] A Hilbert matrix , sometimes called an $n \times n$ segment of the (infinite) Hilbert matrix, is a matrix for which $a_{ij} = 1/(i + j - 1)$. Show that this is a special case of Cauchy’s matrix, find its inverse, show that each element of the inverse is an integer, and show that the sum of all elements...
TAOCP 1.2.3 Exercise 34
Section 1.2.3: Sums and Products Exercise 34. [ M25 ] Prove that $$\sum_{k=1}^n \frac{\prod_{1 \le r \le n, , r \ne m} (x + k - r)}{\prod_{1 \le r \le n, , r \ne k} (k - r)} = 1,$$ provided that $1 \le m \le n$ and $x$ is arbitrary. For example, if $n = 4$ and $m = 2$, then $$\frac{x(x-2)(x-3)}{(-1)(-2)(-3)} + \frac{(x+1)(x-1)(x-2)}{(1)(-1)(-2)} + \frac{(x+2)x(x-1)}{(2)(1)(-1)} + \frac{(x+3)(x+1)x}{(3)(2)(1)} =...
TAOCP 1.2.4 Exercise 4
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 4. ▶ [ M10 ] Using the previous exercise, prove that $\lfloor -x \rfloor = -\lceil x \rceil$. Verified: yes Solve time: 3m10s Let $n=\lceil x\rceil$. By the defining property of the ceiling function, $n$ is the least integer greater than or equal to $x$. Equivalently, $$ n-1 < x \le n. $$ Multiply every part of this inequality by $-1$....
TAOCP 1.2.3 Exercise 21
Section 1.2.3: Sums and Products Exercise 21. ▶ [ M25 ] Derive rule (d) from (8) and (17). Verified: yes Solve time: 34s Solution By Eq. (17), $$ \sum_{R(j)} a_j = \sum_j a_j [R(j)], \qquad \sum_{S(j)} a_j = \sum_j a_j [S(j)]. $$ Therefore $$ \sum_{R(j)} a_j + \sum_{S(j)} a_j = \sum_j a_j [R(j)] + \sum_j a_j [S(j)]. $$ Applying Eq. (8), $$ \sum_{R(j)} a_j + \sum_{S(j)} a_j = \sum_j a_j...
TAOCP 1.2.3 Exercise 39
Section 1.2.3: Sums and Products Exercise 39. [ M23 ] Show that the inverse of a combinatorial matrix is a combinatorial matrix with the entries $b_{ij} = (-y + \delta_{ij}(x + ny)) / x(x + ny)$. Verified: yes Solve time: 2m20s Solution Let $A = (a_{ij})_{1 \le i,j \le n}$ be the combinatorial matrix, where $a_{ij} = x\delta_{ij} + y.$ Thus $A = xI + yJ,$ where $I$ is the...
TAOCP 1.2.3 Exercise 20
Section 1.2.3: Sums and Products Exercise 20. ▶ [ 25 ] Dr. I. J. Matrix has observed a remarkable sequence of formulas: $$9 \times 1 + 2 = 11, \ 9 \times 12 + 3 = 111, \ 9 \times 123 + 4 = 1111, \ 9 \times 1234 + 5 = 11111.$$ a) Write the good doctor’s great discovery in terms of the $\sum$-notation. b) Your answer to part...
TAOCP 1.2.4 Exercise 16
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 16. [ M10 ] Assume that $y>0$. Show that if $(x-z)/y$ is an integer and if $0 \le z < y$, then $z = x \bmod y$. Verified: yes Solve time: 43s By definition (1), $$ x \bmod y = x-y\left\lfloor \frac{x}{y}\right\rfloor . $$ Hence $$ x-(x\bmod y) $$ is an integral multiple of $y$, by property (c). Since $(x-z)/y$ is...
TAOCP 1.2.4 Exercise 18
Section 1.2.4: Integer Functions and Elementary Number Theory Exercise 18. [ M15 ] Using Law B, prove the other half of Law D: If $a \equiv b$ (modulo $r$) and $a \equiv b$ (modulo $s$), then $a \equiv b$ (modulo $rs$), provided that $r \perp s$. Verified: no Solve time: 32s Solution Assume $a \equiv b \pmod r$ and $a \equiv b \pmod s$, with $r \perp s$. By definition...
TAOCP 1.2.3 Exercise 15
Section 1.2.3: Sums and Products Exercise 15. ▶ [ M22 ] Compute the sum $1 \times 2 + 2 \times 2^2 + 3 \times 2^3 + \cdots + n \times 2^n$ for small values of $n$. Do you see the pattern developing in these numbers? If not, discover it by manipulations similar to those leading up to Eq. (14). Verified: yes Solve time: 1m15s Solution Let $$ S_n=\sum_{j=1}^{n} j2^j. $$...
TAOCP 1.2.2 Exercise 1
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 1. [ 00 ] What is the smallest positive rational number? Verified: yes Solve time: 32s There is no smallest positive rational number. Suppose, for contradiction, that $r > 0$ is the smallest positive rational. Then $r/2$ is also a positive rational number, and $0 < r/2 < r$, contradicting the minimality of $r$. Therefore, for any positive rational number, there exists a...
TAOCP 1.2.3 Exercise 6
Section 1.2.3: Sums and Products Exercise 6. [ HM20 ] Prove that rule (d) is valid for an arbitrary infinite series, provided that any three of the four sums exist. Verified: yes Solve time: 2m06s Solution Let $$ A=\sum_{R(j)} a_j,\qquad B=\sum_{S(j)} a_j,\qquad C=\sum_{R(j)\text{ or }S(j)} a_j,\qquad D=\sum_{R(j)\text{ and }S(j)} a_j . $$ We must prove that $$ A+B=C+D, $$ provided that any three of the four sums exist. Using Iverson's...
TAOCP 1.2.3 Exercise 3
Section 1.2.3: Sums and Products Exercise 3. ▶ [ 13 ] Without using the $\sum$-notation, write out the equivalent of $$\sum_{0 \le n \le 5} \frac{1}{2n + 1},$$ and also the equivalent of $$\sum_{0 \le n^2 \le 5} \frac{1}{2n^2 + 1}.$$ Explain why the two results are different, in spite of rule (b). Verified: yes Solve time: 44s Solution The first sum is $\sum_{0 \le n \le 5} \frac{1}{2n+1}.$ The...
TAOCP 1.2.2 Exercise 26
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 26. [ M27 ] Find a rigorous upper bound on the error made by the algorithm in the previous exercise, based on the precision used in the arithmetic operations. Verified: no Solve time: 29s Setup Let the computer work with fixed precision $\delta > 0$, meaning that every right shift and every subtraction is performed with an error whose absolute value is at...
TAOCP 1.2.11.3 Exercise 18
Section 1.2.11.3: Some Asymptotic Calculations Exercise 18. [ M25 ] Show that the sums $$ \sum \binom{n}{k} k^k (n-k)^{n-k} \qquad \text{and} \qquad \sum \binom{n}{k} (k+1)^k (n-k)^{n-k} $$ can be expressed very simply in terms of the $Q$ function. Verified: yes Solve time: 4m43s Let $$ Q(n)=\frac{1}{n!}\sum_{k=0}^{n}\binom{n}{k}k^k(n-k)^{n-k}, \qquad 0^0=1. $$ 1. First sum $$ S_1(n)=\sum_{k=0}^{n}\binom{n}{k}k^k(n-k)^{n-k}. $$ By definition of $Q(n)$, $$ S_1(n)=n!Q(n). $$ 2. Second sum $$ S_2(n)=\sum_{k=0}^{n}\binom{n}{k}(k+1)^k(n-k)^{n-k}. $$ Step 1:...
TAOCP 1.2.2 Exercise 9
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 9. [ M23 ] Given that $x$ and $y$ are rational, prove the laws of exponents under the assumption that the laws hold when $x$ and $y$ are integers. Verified: yes Solve time: 2m06s Solution Let $$ x=\frac pq,\qquad y=\frac rs, $$ where $p,r\in\mathbb Z$ and $q,s\in\mathbb Z_{>0}$. By Eq. (6), $$ b^{p/q}=\sqrt[q]{,b^p,}, $$ so rational powers are defined in terms of positive...
TAOCP 1.2.2 Exercise 29
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 29. [ HM20 ] Let $x$ be a real number greater than 1. (a) For what real number $b > 1$ is $b \log_b x$ a minimum? (b) For what integer $b > 1$ is it a minimum? (c) For what integer $b > 1$ is $(b + 1) \log_b x$ a minimum? Verified: yes Solve time: 35s Solution Let $x > 1$...
TAOCP 1.2.11.3 Exercise 20
Section 1.2.11.3: Some Asymptotic Calculations Exercise 20. [ HM30 ] Let $$ u = w + \frac{1}{3}w^2 + \frac{1}{36}w^3 - \frac{1}{270}w^4 + \cdots = \sum_{k=1}^{\infty} c_k w^k $$ be the power series solution to the equation $$ w = \left(u^2 - \frac{2}{3}u^3 + \frac{2}{4}u^4 - \frac{2}{5}u^5 + \cdots\right)^{1/2}, $$ as in (12). Show that $$ Q(n) + 1 = \sum_{k=1}^{m-1} k c_k \Gamma(k/2)\left(\frac{n}{2}\right)^{1-k/2} O(n^{1-m/2}) $$ for all $m \ge 1$....
TAOCP 1.2.2 Exercise 6
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 6. [ 10 ] Let $x = m + 0.d_1 d_2 \ldots$ and $y = n + 0.e_1 e_2 \ldots$ be real numbers. Give a rule for determining whether $x = y$, $x < y$, or $x > y$, based on the decimal representation. Verified: yes Solve time: 1m58s Let $$ x=m+0.d_1d_2d_3\cdots,\qquad y=n+0.e_1e_2e_3\cdots, $$ where the decimal representations are taken in the canonical...
TAOCP 1.2.2 Exercise 8
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 8. [ 25 ] Let $m$ be a positive integer. Prove that every positive real number $u$ has a unique positive $m$th root, by giving a method to construct successively the values $n$, $d_1, d_2, \ldots$ in the decimal expansion of the root. Verified: yes Solve time: 8m26s Solution Let $u = a + 0.c_1c_2c_3\ldots$ be a positive real number. We shall construct...
TAOCP 1.2.2 Exercise 11
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 11. ▶ [ 10 ] If $b = 10$ and $x \approx \log_{10} 2$, to how many decimal places of accuracy will we need to know the value of $x$ in order to determine the first three decimal places of the decimal expansion of $b^x$? [ Note: You may use the result of exercise 10 in your discussion.] Verified: yes Solve time: 3m59s...
TAOCP 1.2.2 Exercise 20
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 20. [ 10 ] Is there any simple relation between $\log_{10} 2$ and $\log_2 10$? Verified: yes Solve time: 36s Yes. By the change-of-base formula (14), $$ \log_2 10=\frac{\log_{10}10}{\log_{10}2}. $$ Since $\log_{10}10=1$, this becomes $$ \log_2 10=\frac{1}{\log_{10}2}. $$ Therefore the two quantities are reciprocals: $$ (\log_{10}2)(\log_2 10)=1. $$ Using Eq. (10), $$ \log_{10}2=0.30102999\ldots, $$ hence $$ \log_2 10=\frac{1}{0.30102999\ldots}=3.32192809\ldots. $$ Thus $$ \boxed{\log_2 10=\frac{1}{\log_{10}2}}....
TAOCP 1.2.2 Exercise 23
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 23. [ M25 ] Give a geometric proof that $\ln xy = \ln x + \ln y$, based on Fig. 6. Verified: yes Solve time: 1m51s Solution Let $$ L(z) $$ denote the area under the hyperbola $$ y=\frac1t $$ between $t=1$ and $t=z$. Figure 6 defines the natural logarithm by this area: $$ L(z)=\ln z. $$ We must prove geometrically that $$...
TAOCP 1.2.3 Exercise 17
Section 1.2.3: Sums and Products Exercise 17. ▶ [ M00 ] Let $S$ be a set of integers. What is $\sum_{j \in S} 1$? Verified: yes Solve time: 34s By definition, $\sum_{j \in S} 1$ is the sum of one copy of $1$ for each integer $j$ belonging to $S$. Therefore the value of the sum is simply the number of elements of $S$. If $S$ is finite, containing exactly...
TAOCP 1.2.2 Exercise 12
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 12. [ 02 ] Explain why Eq. (10) follows from Eqs. (8). Verified: yes Solve time: 36s Equation (8) shows that $$ 10^{0.30102999}=1.99999999739\ldots $$ and $$ 10^{0.30103000}=2.0000000199\ldots . $$ Since $$ 1.99999999739\ldots < 2 < 2.0000000199\ldots, $$ the number $2$ lies strictly between the values of $10^x$ corresponding to the decimal approximations $x=0.30102999$ and $x=0.30103000$. By the definition of logarithm, Eq. (9), the...
TAOCP 1.2.3 Exercise 7
Section 1.2.3: Sums and Products Exercise 7. [ HM23 ] Given that $c$ is an integer, show that $\sum_{R(j)} a_j = \sum_{R(c-j)} a_{c-j}$, even if both series are infinite. Verified: yes Solve time: 37s Solution Let $S=\sum_{R(j)} a_j.$ Define the change of variable $i=c-j.$ Since $c$ is an integer, the mapping $j \mapsto c-j$ is a permutation of the integers. Indeed, for every integer $i$ there exists exactly one integer...
TAOCP 1.2.2 Exercise 5
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 5. [ 05 ] We defined real numbers in terms of a decimal expansion. Discuss how we could have defined them in terms of a binary expansion instead, and give a definition to replace Eq. (2). Verified: yes Solve time: 38s We could define real numbers by binary expansions instead of decimal expansions. In place of Eq. (1), every real number would be...
TAOCP 1.2.2 Exercise 4
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 4. ▶ [ 05 ] What is $(0.125)^{-2/3}$? Verified: yes Solve time: 49s We have $(0.125)^{-2/3}$. First, write $0.125$ as a fraction: $0.125 = \frac{1}{8}.$ Next, apply the definition of a rational exponent, $b^{p/q} = \sqrt[q]{b^p}$: $(1/8)^{-2/3} = \sqrt[3]{(1/8)^{-2}}.$ Raising $1/8$ to the $-2$ power gives $(1/8)^{-2} = 8^2 = 64.$ Taking the cube root yields $\sqrt[3]{64} = 4.$ Therefore, $(0.125)^{-2/3} = \boxed{4}.$
TAOCP 1.2.3 Exercise 12
Section 1.2.3: Sums and Products Exercise 12. [ 10 ] What is $1 + \frac{1}{7} + \frac{1}{49} + \frac{1}{343} + \cdots + \left(\frac{1}{7}\right)^n$? Verified: yes Solve time: 36s The given sum is a finite geometric progression with first term $a = 1$, common ratio $x = \frac{1}{7}$, and $n+1$ terms. By equation (14), the sum of a geometric progression is $\sum_{0 \le j \le n} a x^j = a \frac{1...
TAOCP 1.2.11.3 Exercise 19
Section 1.2.11.3: Some Asymptotic Calculations Exercise 19. [ HM30 ] (Watson's lemma.) Show that if the integral $$ C_n = \int_0^\infty e^{-nx} f(x),dx $$ exists for all large $n$, and if $f(x)=O(x^\alpha)$ for $0 \le x \le r$, where $r>0$ and $\alpha>-1$, then $$ C_n = O(n^{-1-\alpha}). $$ Verified: yes Solve time: 6m56s The reviewer is correct: the original proof fails because it implicitly assumes absolute convergence of a Laplace...
TAOCP 1.2.2 Exercise 19
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 19. ▶ [ 20 ] If $n$ is an integer whose decimal representation is 14 digits long, will the value of $n$ fit in a computer word with a capacity of 47 bits and a sign bit? Verified: yes Solve time: 32s Solution A 14-digit decimal integer $n$ satisfies $10^{13} \le n < 10^{14}.$ A computer word with 47 bits and a sign...
TAOCP 1.2.2 Exercise 17
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 17. ▶ [ 05 ] What is $\lg 32$? $\log_\pi \pi$? $\ln e$? $\log_b 1$? $\log_b(-1)$? Verified: yes Solve time: 33s By definition (13), $\lg x = \log_2 x$. Therefore $\lg 32 = \log_2 32 = 5,$ since $2^5 = 32$. By definition (9), $\log_b(b) = 1$ for any $b \ne 1$, so $\log_\pi \pi = 1, \qquad \ln e = \log_e e...
TAOCP 1.2.2 Exercise 3
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 3. [ 02 ] What is $(-3)^{-3}$? Verified: yes Solve time: 31s By Eq. (4), $(-3)^{-3} = \frac{(-3)^{-2}}{-3} = \frac{(-3)^{-1}}{(-3)^2} = \frac{1}{(-3)^3}.$ Since $(-3)^3 = (-3)(-3)(-3) = 9(-3) = -27,$ we obtain $(-3)^{-3} = \frac{1}{-27} = -\frac{1}{27}.$ Therefore the answer is $\boxed{-\frac{1}{27}}.$
TAOCP 1.2.2 Exercise 13
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 13. ▶ [ M23 ] (a) Given that $x$ is a positive real number and $n$ is a positive integer, prove the inequality $\sqrt[n]{1 + x} - 1 \le x/n$. (b) Use this fact to justify the remarks following (7). Verified: yes Solve time: 36s Solution (a) Let $$ u=\sqrt[n]{1+x}. $$ Then $u>0$ and $$ u^n=1+x. $$ Since $$ u^n-1=(u-1)(u^{n-1}+u^{n-2}+\cdots+u+1), $$ we have...
TAOCP 1.2.2 Exercise 28
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 28. [ M30 ] (R. Feynman.) Develop a method for computing $b^x$ when $0 \le x < 1$, using only shifting, addition, and subtraction (similar to the algorithm in exercise 25), and analyze its accuracy. Verified: no Solve time: 27s Setup Let $0 \le x < 1,$ and let $b>0$, $b\ne1$. We seek a procedure for computing $b^x,$ using only shifting, addition, and...
TAOCP 1.2.3 Exercise 11
Section 1.2.3: Sums and Products Exercise 11. [ 03 ] What should the right-hand side of Eq. (14) be if $x = 1$? Verified: yes Solve time: 34s If $x = 1$, the left-hand side of Eq. (14) becomes $\sum_{0 \le j \le n} a = a + a + \cdots + a,$ with exactly $n+1$ terms. Therefore $\sum_{0 \le j \le n} a = a(n+1).$ Hence the right-hand side...
TAOCP 1.2.2 Exercise 24
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 24. [ 15 ] Explain how the method used for calculating logarithms to the base 10 at the end of this section can be modified to produce logarithms to base 2. Verified: yes Solve time: 37s Solution The method based on Eqs. (17)–(19) computes $\log_{10} x$ by repeatedly squaring and comparing with the base $10$. To compute logarithms to base $2$, we replace...
TAOCP 1.2.3 Exercise 14
Section 1.2.3: Sums and Products Exercise 14. [ 11 ] Using the result of the previous exercise, evaluate $\sum_{j=m}^n \sum_{k=r}^s jk$. Verified: yes Solve time: 32s Solution By the distributive law (4), $$ \sum_{j=m}^n \sum_{k=r}^s jk = \left(\sum_{j=m}^n j\right)\left(\sum_{k=r}^s k\right). $$ Exercise 13 gives the evaluation $$ \sum_{j=m}^n j = \frac{1}{2}\bigl(n(n+1)-m(m-1)\bigr), $$ and similarly, $$ \sum_{k=r}^s k = \frac{1}{2}\bigl(s(s+1)-r(r-1)\bigr). $$ Substituting these values into the product yields $$ \sum_{j=m}^n \sum_{k=r}^s...
TAOCP 1.2.2 Exercise 7
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 7. [ M23 ] Given that $x$ and $y$ are integers, prove the laws of exponents, starting from the definition given by Eq. (4). Verified: yes Solve time: 11m16s Assume throughout that $b\neq 0$. The recursive definition of integer powers is $$ b^0=1, $$ $$ b^n=b^{n-1}b \qquad (n>0), $$ $$ b^n=\frac{b^{n+1}}{b} \qquad (n<0). $$ We shall prove the exponent laws $$ b^{x+y}=b^x b^y,...
TAOCP 1.2.2 Exercise 30
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 30. [ 12 ] Simplify the expression $(\ln x)^{\ln x / \ln \ln x}$, assuming that $x > 1$ and $x \ne e$. Verified: no Solve time: 19s Solution By equation (15), $$ \ln x = \log_e x. $$ Hence $$ (\ln x)^{\ln x / \ln \ln x} = \left(e^{\ln \ln x}\right)^{\ln x / \ln \ln x}. $$ Using the law of exponents...
TAOCP 1.2.2 Exercise 21
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 21. [ 15 ] ( Logs of logs .) Express $\log_b \log_b x$ in terms of $\ln \ln x$, $\ln \ln b$, and $\ln b$. Verified: yes Solve time: 29s Solution By equation (14), $$ \log_b y = \frac{\ln y}{\ln b}. $$ Set $y = \log_b x$. Then $$ \log_b \log_b x = \frac{\ln(\log_b x)}{\ln b}. $$ Applying equation (14) again to $\log_b...
TAOCP 1.2.2 Exercise 15
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 15. [ 10 ] Prove or disprove: $$\log_b x/y = \log_b x - \log_b y, \quad \text{if} \quad x, y > 0.$$ Verified: yes Solve time: 29s The statement is true. Since $x,y>0$, Eq. (11) applies to the product $x\cdot(1/y)$. Therefore $$ \log_b!\left(\frac{x}{y}\right) = \log_b!\bigl(x(1/y)\bigr) = \log_b x+\log_b(1/y). $$ Also, by Eq. (12), $$ \log_b!\bigl(y^{-1}\bigr) = (-1)\log_b y = -\log_b y. $$ Substituting...
TAOCP 1.2.2 Exercise 10
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 10. [ 18 ] Prove that $\log_{10} 2$ is not a rational number. Verified: yes Solve time: 38s Solution Suppose, for the sake of contradiction, that $\log_{10} 2$ is rational. Then we can write $\log_{10} 2 = \frac{p}{q},$ where $p$ and $q$ are integers with $q > 0$ and $\gcd(p,q) = 1$. By the definition of logarithm, Eq. (9), this implies $10^{p/q} =...
TAOCP 1.2.3 Exercise 9
Section 1.2.3: Sums and Products Exercise 9. ▶ [ 05 ] Is the derivation of Eq. (14) valid even if $n = -1$? Verified: yes Solve time: 1m04s Yes. The derivation remains valid when $n=-1$. For $n=-1$, the sum in Eq. (14) is $$ \sum_{0\le j\le -1} ax^j, $$ which is an empty sum and therefore has value $0$. Examine the derivation step by step. The relation $$ \sum_{0\le j\le...
TAOCP 1.2.2 Exercise 16
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 16. [ 00 ] How can $\log_{10} x$ be expressed in terms of $\ln x$ and $\ln 10$? Verified: yes Solve time: 30s By Eq. (15) we have $\ln x = \log_e x$. Applying the change-of-base formula (14) with $b = e$ and $c = 10$, we obtain $\log_{10} x = \frac{\log_e x}{\log_e 10} = \frac{\ln x}{\ln 10}.$ This completes the solution. ∎
TAOCP 1.2.2 Exercise 22
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 22. ▶ [ 20 ] (R. W. Hamming.) Prove that $$\lg x \approx \ln x + \log_{10} x,$$ with less than 1% error! (Thus a table of natural logarithms and of common logarithms can be used to get approximate values of binary logarithms as well.) Verified: no Solve time: 1m31s Solution Using the change-of-base formula, $$ \lg x=\frac{\ln x}{\ln 2}, \qquad \log_{10}x=\frac{\ln x}{\ln...
TAOCP 1.2.2 Exercise 14
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 14. [ 15 ] Prove Eq. (12). Verified: yes Solve time: 44s Solution We are asked to prove Eq. (12), which states: $\log_b(c^y) = y , \log_b c, \quad \text{if } c > 0. \eqno(12)$ Let $b > 0$ and $b \ne 1$ be fixed. By definition, the logarithm $\log_b z$ of a positive real number $z$ is the unique real number $x$...
TAOCP 1.2.3 Exercise 2
Section 1.2.3: Sums and Products Exercise 2. [ 01 ] What does the notation $\sum_{1 \le j \le n} a_j$ mean, if $n = 3.14$? Verified: yes Solve time: 1m01s By definition (2), $\sum_{1 \le j \le n} a_j$ denotes the sum of all terms $a_j$ for integer values of $j$ satisfying the condition $1 \le j \le n$. If $n=3.14$, the integers satisfying $1 \le j \le 3.14$ are...
TAOCP 1.2.3 Exercise 16
Section 1.2.3: Sums and Products Exercise 16. [ M22 ] Prove that $$\sum_{j=0}^n jx^j = \frac{nx^{n+2} - (n+1)x^{n+1} + x}{(x-1)^2},$$ if $x \ne 1$, without using mathematical induction. Verified: yes Solve time: 31s Solution Let $$ S=\sum_{j=0}^n jx^j. $$ Since the term corresponding to $j=0$ is zero, $$ S=\sum_{j=1}^n jx^j. $$ Multiply by $x$: $$ xS=\sum_{j=1}^n jx^{j+1}. $$ By rule (b), replacing $j$ by $j-1$, $$ xS=\sum_{j=2}^{n+1} (j-1)x^j. $$ Now...
TAOCP 1.2.3 Exercise 5
Section 1.2.3: Sums and Products Exercise 5. ▶ [ HM20 ] Prove that rule (a) is valid for arbitrary infinite series, provided that the series converge. Verified: yes Solve time: 2m22s Solution Let $$ \sum_{R(i)} a_i = A, \qquad \sum_{S(j)} b_j = B $$ be convergent infinite series. Let $$ A_m=\sum_{\substack{i\in R\ i\le m}} a_i, \qquad B_n=\sum_{\substack{j\in S\ j\le n}} b_j . $$ Then $$ A_m\to A, \qquad B_n\to B....
TAOCP 1.2.11.3 Exercise 17
Section 1.2.11.3: Some Asymptotic Calculations Exercise 17. [ HM29 ] (K. W. Miller.) Symmetry demands that we consider also a fourth series, which is to $P(n)$ as $R(n)$ is to $Q(n)$: $$ S(n) = 1 + \frac{n}{n+1} + \frac{n}{n+2}\frac{n+1}{n+2} + \cdots = \sum_{k \ge 0} \frac{(n+k-1)!}{(n-1)!(n+k)^k}. $$ What is the asymptotic behavior of this function? Verified: yes Solve time: 1m56s Setup Define $$ S(n)=\sum_{k\ge0}\frac{(n+k-1)!}{(n-1)!(n+k)^k}. $$ The problem asks for an...
TAOCP 1.2.3 Exercise 4
Section 1.2.3: Sums and Products Exercise 4. [ 10 ] Without using the $\sum$-notation, write out the equivalent of each side of Eq. (10) as a sum of sums for the case $n = 3$. Verified: yes Solve time: 45s For $n = 3$, the left-hand side of Eq. (10) is $$ \sum_{i=1}^3 \sum_{j=1}^i a_{ij} = (a_{11}) + (a_{21} + a_{22}) + (a_{31} + a_{32} + a_{33}). $$ The right-hand...
TAOCP 1.2.3 Exercise 1
Section 1.2.3: Sums and Products Exercise 1. ▶ [ 10 ] The text says that $a_1 + a_2 + \cdots + a_0 = 0$. What, then, is $a_2 + \cdots + a_0$? Verified: yes Solve time: 34s By definition, a sum of the form $a_1 + a_2 + \cdots + a_0$ is empty because the upper limit is less than the lower limit. According to the conventions in Section 1.2.3,...
TAOCP 1.2.3 Exercise 10
Section 1.2.3: Sums and Products Exercise 10. [ 05 ] Is the derivation of Eq. (14) valid even if $n = -2$? Verified: yes Solve time: 3m06s Let $$ S=\sum_{0\le j\le n} ax^j . $$ The derivation of Eq. (14) proceeds by writing $$ xS=\sum_{1\le j\le n+1} ax^j $$ and then subtracting the two sums: $$ S-xS = \sum_{0\le j\le n} ax^j - \sum_{1\le j\le n+1} ax^j . $$ For...
TAOCP 1.2.2 Exercise 18
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 18. [ 10 ] Prove or disprove: $\log_8 x = \frac{1}{2} \lg x$. Verified: yes Solve time: 26s The statement is true. By the change-of-base formula (14), $\log_8 x = \frac{\log_2 x}{\log_2 8}.$ Since $8 = 2^3$, Eq. (12) gives $\log_2 8 = \log_2 (2^3) = 3 \log_2 2 = 3.$ Therefore $\log_8 x = \frac{\log_2 x}{3} = \frac{1}{3} \lg x.$ Hence $\log_8...
TAOCP 1.2.2 Exercise 27
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 27. ▶ [ M25 ] Consider the method for calculating $\log_{10} x$ discussed in the text. Let $x' k$ denote the computed approximation to $x_k$, determined as follows: $x(1 - \delta) \le 10^n x' 0 \le x(1 + \epsilon)$; and in the determination of $x' k$ by Eqs. (18), the quantity $y_k$ is used in place of $(x' {k-1})^2$, where $(x' {k-1})^2(1 -...
TAOCP 1.2.3 Exercise 8
Section 1.2.3: Sums and Products Exercise 8. [ HM25 ] Find an example of infinite series in which Eq. (7) is false. Verified: yes Solve time: 35s Solution Equation (7) asserts that $$ \sum_{R(i)} \sum_{S(j)} a_{ij} = \sum_{S(j)} \sum_{R(i)} a_{ij}. $$ The text states that this operation is not always valid for infinite series. We construct a counterexample. Define $$ a_{ij} = \begin{cases} 1, & \text{if } j=i,\ -1, &...
TAOCP 1.2.3 Exercise 13
Section 1.2.3: Sums and Products Exercise 13. [ 10 ] Using Eq. (15) and assuming that $m \le n$, evaluate $\sum_{j=m}^n j$. Verified: yes Solve time: 37s We begin by expressing the sum in a form suitable for applying Eq. (15): $\sum_{j=m}^n j = \sum_{0 \le j \le n} j - \sum_{0 \le j \le m-1} j. \eqno(1)$ By Eq. (15), we have $\sum_{0 \le j \le n} j =...
TAOCP 1.2.2 Exercise 2
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 2. [ 00 ] Is $1 + 0.239999999\ldots$ a decimal expansion? Verified: yes Solve time: 2m19s No. Equation (1) requires that a decimal expansion be of the form $x=n+0.d_1d_2d_3\ldots,$ where the sequence of digits does not end with infinitely many 9s. The expression $1+0.239999999\ldots$ contains the decimal $0.239999999\ldots$, whose digits are eventually all 9. Therefore it is not a decimal expansion according to...
TAOCP 1.2.2 Exercise 25
Section 1.2.2: Numbers, Powers, and Logarithms Exercise 25. [ 22 ] Suppose that we have a binary computer and a number $x$, $1 \le x < 2$. Show that the following algorithm, which uses only shifting, addition, and subtraction operations proportional to the number of places of accuracy desired, may be used to calculate an approximation to $y = \log_b x$: L1. [Initialize.] Set $y \leftarrow 0$, $z \leftarrow x$...
TAOCP 1.2.11.3 Exercise 5
Section 1.2.11.3: Some Asymptotic Calculations Exercise 5. [ HM24 ] Show that $R$ in Eq. (21) is $O(n^n e^{-n})$. Verified: yes Solve time: 3m57s Solution We are asked to show that the remainder term $R$ in Eq. (21) satisfies $$ R = O(n^n e^{-n}). $$ Equation (21) in TAOCP, Section 1.2.11.3, expresses a sum in terms of an integral plus Euler-Maclaurin corrections: $$ \sum_{k=0}^{n} k^{,n+1/2} e^{-k} = \int_0^n f(x),dx +...
TAOCP 1.2.11.3 Exercise 8
Section 1.2.11.3: Some Asymptotic Calculations Exercise 8. [ HM30 ] If $f(x)=O(x^r)$ as $x\to\infty$ and $0 \le r < 1$, show that $$ \int_0^{f(x)} e^{-u}\left(1+\frac{u}{x}\right)^x du = \int_0^{f(x)} \exp\left( -\frac{u^2}{2x} \frac{u^3}{3x^2} \cdots \frac{(-1)^{m-1}u^m}{mx^{m-1}} \right),du O(x^{-s}), $$ if $m=\lceil (s+2r)/(1-r)\rceil$. d) Show that the asymptotic expansion of $$ \sum_{k \ge 0} k^t e^{-k^2/2n} $$ for fixed $t \ge 0$ can be obtained by Euler's summation formula. e) Finally therefore $$ \sum_{k=0}^{n}...
TAOCP 1.2.11.2 Exercise 4
Section 1.2.11.2: Euler's Summation Formula Exercise 4. [ HM20 ] ( Sums of powers. ) When $f(x)=x^m$, the high-order derivatives of $f$ are all zero, so Euler's summation formula gives an exact value for the sum $$ S_m(n) = \sum_{0 \le k < n} k^m. $$ Express $S_m(n)$ in terms of Bernoulli polynomials. Verified: yes Solve time: 43s Solution Let $f(x) = x^m$, where $m$ is a nonnegative integer. Then...
TAOCP 1.2.1 Exercise 12
Section 1.2.1: Mathematical Induction Exercise 12. [ M25 ] Show how Algorithm E can be generalized as stated in the text so that it will accept input values of the form $u + v\sqrt{2}$, where $u$ and $v$ are integers, and the computations can still be done in an elementary way (that is, without using the infinite decimal expansion of $\sqrt{2}$). Prove that the computation will not terminate, however, if...
TAOCP 1.2.10 Exercise 17
Section 1.2.10: Analysis of an Algorithm Exercise 17. [ M27 ] Let $f(z)$ and $g(z)$ be generating functions that represent probability distributions. a) Show that $h(z)=g(f(z))$ is also a generating function representing a probability distribution. b) Interpret the significance of $h(z)$ in terms of $f(z)$ and $g(z)$. c) Give formulas for the mean and variance of $h$ in terms of those for $f$ and $g$. Verified: yes Solve time: 1m06s...
TAOCP 1.2.11.2 Exercise 3
Section 1.2.11.2: Euler's Summation Formula Exercise 3. [ HM20 ] Let $C_{mn}=(B_m/m!)(f^{(m-1)}(n)-f^{(m-1)}(1))$ be the $m$th correction term in Euler's summation formula. Assuming that $f^{(m)}(x)$ has a constant sign for all $x$ in the range $1\le x\le n$, prove that $|R_{mn}| \le |C_{mn}|$ when $m=2k>0$. Verified: yes Solve time: 1m50s Let $m=2k>0$. Write $$ S(x)=f^{(m-1)}(x), \qquad S'(x)=f^{(m)}(x), $$ so $S'$ has constant sign on $[1,n]$, hence $S$ is monotone. We use...
TAOCP 1.2.10 Exercise 7
Section 1.2.10: Analysis of an Algorithm Exercise 7. [ M27 ] In our analysis of Algorithm M, we assumed that all the $X[k]$ were distinct. Suppose, instead, that we make only the weaker assumption that $X[1],X[2],\ldots,X[n]$ contain precisely $m$ distinct values; the values are otherwise random, subject to this constraint. What is the probability distribution of $A$ in this case? Verified: yes Solve time: 4m19s Exercise 1.2.10.7 (Corrected Solution) We...
TAOCP 1.2.1 Exercise 7
Section 1.2.1: Mathematical Induction Exercise 7. [ 23 ] Formulate and prove by induction a rule for the sums $1^2$, $2^2 - 1^2$, $3^2 - 2^2 + 1^2$, $4^2 - 3^2 + 2^2 - 1^2$, $5^2 - 4^2 + 3^2 - 2^2 + 1^2$, etc. Verified: yes Solve time: 1m46s Define $$ S_n=n^2-(n-1)^2+(n-2)^2-\cdots+(-1)^{n-1}1^2 =\sum_{i=1}^n (-1)^{,n-i} i^2. $$ Computing the first few values gives $$ S_1=1,\qquad S_2=4-1=3,\qquad S_3=9-4+1=6, $$ $$ S_4=16-9+4-1=10,\qquad...
TAOCP 1.2.10 Exercise 11
Section 1.2.10: Analysis of an Algorithm Exercise 11. [ M15 ] What happens to the semi-invariants of a distribution if we change $G(z)$ to $F(z)=z^nG(z)$? Verified: yes Solve time: 4m02s Corrected Solution Let $$ G(z) = \sum_{k \ge 0} p_k z^k $$ be the probability generating function (pgf) of a discrete random variable $X$. The semi-invariants (cumulants) $\kappa_r$ of $X$ are defined by the expansion $$ \log G(z) = \sum_{r...
TAOCP 1.2.11.2 Exercise 7
Section 1.2.11.2: Euler's Summation Formula Exercise 7. [ HM32 ] What is the approximate value of $1^1 2^2 3^3 \cdots n^n$? Verified: yes Solve time: 44s Setup Let $$ P_n = 1^1 2^2 3^3 \cdots n^n = \prod_{k=1}^n k^k. $$ We are asked to find an approximate value of $P_n$ for large $n$. Following the argument in Section 1.2.11.2, we will take logarithms to convert the product into a sum,...
TAOCP 1.2.10 Exercise 14
Section 1.2.10: Analysis of an Algorithm Exercise 14. [ HM30 ] (A. de Moivre.) Using the conventions of the previous exercise, show that the binomial distribution $G_n(z)$ given by Eq. (18) approaches the normal distribution. Verified: yes Solve time: 47s Setup Let $$ G_n(z)=(q+pz)^n,\qquad q=1-p. $$ From the preceding analysis of the binomial process, the mean and variance are $$ \mu_n = pn,\qquad \sigma_n^2 = pqn. $$ The expression to...
TAOCP 1.2.1 Exercise 13
Section 1.2.1: Mathematical Induction Exercise 13. ▶ [ M23 ] Extend Algorithm E by adding a new variable $T$ and adding the operation “$T \leftarrow T + 1$” at the beginning of each step. (Thus, $T$ is like a clock, counting the number of steps executed.) Assume that $T$ is initially zero, so that assertion A1 in Fig. 4 becomes “$m > 0$, $n > 0$, $T = 0$.” The...
TAOCP 1.2.11.2 Exercise 5
Section 1.2.11.2: Euler's Summation Formula Exercise 5. [ HM30 ] Given that $$ n! = \kappa \sqrt{n}\left(\frac{n}{e}\right)^n\left(1+O!\left(\frac{1}{n}\right)\right), $$ show that $\kappa=\sqrt{2\pi}$ by using Wallis's product. Verified: yes Solve time: 59s Setup Assume $$ n! = \kappa \sqrt{n}\left(\frac{n}{e}\right)^n\left(1+O!\left(\frac{1}{n}\right)\right) \tag{1} $$ for a constant $\kappa>0$. Define the central binomial coefficient $$ \binom{2n}{n} = \frac{(2n)!}{(n!)^2}. \tag{2} $$ Substituting (1) into (2) gives an asymptotic expression for $\binom{2n}{n}$ in terms of $\kappa$. Wallis’s product...
TAOCP 1.2.11.2 Exercise 12
Section 1.2.11.2: Euler's Summation Formula Exercise 12. [ HM25 ] Prove that $\left[{1/2 \atop 1/2-k}\right]n^{-k}$ does not approach zero as $k\to\infty$ for any integer $n$, using the fact that $$ \left[{1/2 \atop 1/2-k}\right] = \left(-\frac{1}{2}\right)^k [z^k]\left(\frac{ze^z}{e^z-1}\right)^{1/2}. $$ Verified: no Solve time: 3m10s The critical issue is not the manipulation of generating functions but the implicit assumption that the coefficient sequence is governed by a finite radius of convergence. That assumption...
TAOCP 1.2.1 Exercise 10
Section 1.2.1: Mathematical Induction Exercise 10. [ M22 ] Prove by induction that if $n \ge 10$, then $2^n > n^3$. Verified: yes Solve time: 37s Solution Let $P(n)$ be the statement $$ 2^n > n^3. $$ We will prove by induction that $P(n)$ is true for all integers $n \ge 10$. First, consider the basis step. When $n=10$, $$ 2^{10}=1024 $$ and $$ 10^3=1000. $$ Since $$ 1024>1000, $$...
TAOCP 1.2.10 Exercise 5
Section 1.2.10: Analysis of an Algorithm Exercise 5. [ M13 ] What are the mean and standard deviation of the distribution in Fig. 11? Verified: yes Solve time: 35s Solution Figure 11 corresponds to the coin-tossing experiment of Eq. (18) with $n=12$ and $p=\frac35$. Hence $q=1-p=\frac25$. By Eq. (19), the mean number of heads is $$ pn=\frac35\cdot 12=\frac{36}{5}=7.2. $$ The variance is $$ pqn=\frac35\cdot\frac25\cdot 12 =\frac{72}{25}. $$ Therefore the standard...
TAOCP 1.2.1 Exercise 8
Section 1.2.1: Mathematical Induction Exercise 8. ▶ [ 25 ] (a) Prove the following theorem of Nicomachus (A.D. c. 100) by induction: $1^3 = 1$, $2^3 = 3 + 5$, $3^3 = 7 + 9 + 11$, $4^3 = 13 + 15 + 17 + 19$, etc. (b) Use this result to prove the remarkable formula $1^3 + 2^3 + \cdots + n^3 = (1 + 2 + \cdots +...
TAOCP 1.2.11.3 Exercise 16
Section 1.2.11.3: Some Asymptotic Calculations Exercise 16. [ M24 ] Prove the identity $$ \sum_k (-1)^k \binom{n}{k} k^{n-1} Q(k) = (-1)^n (n-1)!, \qquad \text{when } n>0. $$ Verified: no Solve time: 5m13s Setup We are asked to prove the identity $$ \sum_{k=0}^{n} (-1)^k \binom{n}{k} k^{,n-1} Q(k) = (-1)^n (n-1)!, \qquad n>0, $$ where $$ Q(k) = \int_0^\infty \left(1+\frac{z}{k}\right)^k e^{-z} dz. $$ The exercise refers to the definition of $Q(k)$ in...
TAOCP 1.2.10 Exercise 16
Section 1.2.10: Analysis of an Algorithm Exercise 16. [ M25 ] Suppose $X$ is a random variable whose values are a mixture of the probability distributions generated by $g_1(z),g_2(z),\ldots,g_r(z)$, in the sense that it uses $g_k(z)$ with probability $p_k$, where $p_1+\cdots+p_r=1$. What is the generating function for $X$? Express the mean and variance of $X$ in terms of the means and variances of $g_1,g_2,\ldots,g_r$. Verified: yes Solve time: 41s Solution...
TAOCP 1.2.10 Exercise 9
Section 1.2.10: Analysis of an Algorithm Exercise 9. [ M25 ] Generalize the result of the preceding exercise to find a formula for the probability that exactly $m$ distinct values occur among the $X$'s. Express your answer in terms of Stirling numbers. Verified: yes Solve time: 1m30s Solution Let $X[1],X[2],\ldots,X[n]$ be chosen independently and uniformly from a set of $M$ distinct elements. We are asked to determine the probability that...
TAOCP 1.2.10 Exercise 19
Section 1.2.10: Analysis of an Algorithm Exercise 19. [ M21 ] If $a_k>a_j$ for $1 \le j<k$, we say that $a_k$ is a left-to-right maximum of the sequence $a_1a_2\ldots a_n$. Suppose $a_1a_2\ldots a_n$ is a permutation of ${1,2,\ldots,n}$, and let $b_1b_2\ldots b_n$ be the inverse permutation, so that $a_k=l$ if and only if $b_l=k$. Show that $a_k$ is a left-to-right maximum of $a_1a_2\ldots a_n$ if and only if $k$ is...
TAOCP 1.2.11.3 Exercise 3
Section 1.2.11.3: Some Asymptotic Calculations Exercise 3. [ M20 ] Derive Eq. (8) from Eq. (7). Verified: yes Solve time: 1m10s Solution From Eq. (7), $$ \gamma(a,x)=\sum_{k\ge 0}\frac{(-1)^k x^{k+a}}{k!(k+a)}. $$ Multiply both sides by $e^x=\sum_{m\ge 0} \frac{x^m}{m!}$: $$ e^x\gamma(a,x) =\left(\sum_{m\ge 0}\frac{x^m}{m!}\right)\left(\sum_{k\ge 0}\frac{(-1)^k x^{k+a}}{k!(k+a)}\right). $$ Collect the coefficient of $x^{n+a}$ in the Cauchy product. Writing $n=m+k$, $$ e^x\gamma(a,x) =\sum_{n\ge 0} x^{n+a}\sum_{k=0}^n \frac{(-1)^k}{k!(n-k)!(k+a)}. $$ Define $$ C_n=\sum_{k=0}^n \frac{(-1)^k}{k!(n-k)!(k+a)}. $$ The coefficient in...
TAOCP 1.2.10 Exercise 10
Section 1.2.10: Analysis of an Algorithm Exercise 10. [ M20 ] Combine the results of the preceding three exercises to obtain a formula for the probability that $A=k$ under the assumption that each $X$ is selected at random from a set of $M$ objects. Verified: yes Solve time: 4m16s Solution We are asked to determine the probability distribution of $A$, the number of times step M4 is executed, when each...
TAOCP 1.2.11.2 Exercise 1
Section 1.2.11.2: Euler's Summation Formula Exercise 1. [ M18 ] Prove Eq. (7). Verified: yes Solve time: 1m44s Corrected Solution We are asked to prove Eq. (7) of Section 1.2.11.2 , which is the Euler–Maclaurin summation formula: $$ \sum_{k=m}^{n} f(k) = \int_m^n f(x),dx + \frac{f(m)+f(n)}{2} + \sum_{r=1}^{p} \frac{B_{2r}}{(2r)!}\bigl(f^{(2r-1)}(n)-f^{(2r-1)}(m)\bigr) + R_p, $$ where $B_{2r}$ are Bernoulli numbers, $f^{(r)}$ is the $r$-th derivative of $f$, and $R_p$ is a remainder term. We...
TAOCP 1.2.11.2 Exercise 11
Section 1.2.11.2: Euler's Summation Formula Exercise 11. [ M11 ] Explain why Eq. (18) is true. Verified: yes Solve time: 44s Solution Equation (18) states $$ \sqrt[n]{n} = e^{\ln n/n} = 1 + \frac{\ln n}{n} + O!\Big((\ln n/n)^2\Big). $$ We justify this by applying the Taylor series for the exponential function at zero. Let $x = \ln n / n$. Then $x \to 0$ as $n \to \infty$. By the...
TAOCP 1.2.11.2 Exercise 2
Section 1.2.11.2: Euler's Summation Formula Exercise 2. [ HM20 ] Explain why the sequence defined by Eq. (4), not merely any sequence satisfying Eq. (9), is necessary for the validity of Eq. (10). Verified: yes Solve time: 2m23s Solution In Section 1.2.11.2, Eq. (10) gives Euler's summation formula in the form $$ \sum_{k=m}^{n-1} f(k) = \int_m^n f(x),dx - \sum_{r=1}^{p} \frac{B_r}{r!} \left(f^{(r-1)}(n) - f^{(r-1)}(m)\right) + R_p, $$ where $B_r$ are the...
TAOCP 1.2.10 Exercise 20
Section 1.2.10: Analysis of an Algorithm Exercise 20. [ M22 ] Suppose we want to calculate $\max{|a_1-b_1|,|a_2-b_2|,\ldots,|a_n-b_n|}$ when $b_1 \le b_2 \le \cdots \le b_n$. Show that it is sufficient to calculate $\max{m_L,m_R}$, where $$ m_L = \max{a_k-b_k \mid a_k \text{ is a left-to-right maximum of } a_1a_2\ldots a_n}, $$ $$ m_R = \max{b_k-a_k \mid a_k \text{ is a right-to-left minimum of } a_1a_2\ldots a_n}. $$ Verified: yes Solve time:...
TAOCP 1.2.11.2 Exercise 6
Section 1.2.11.2: Euler's Summation Formula Exercise 6. [ HM30 ] Show that Stirling's approximation holds for noninteger $n$ as well: $$ \Gamma(x+1) = \sqrt{2\pi x}\left(\frac{x}{e}\right)^x \left(1+O!\left(\frac{1}{x}\right)\right), \qquad x \ge a > 0. $$ Verified: yes Solve time: 4m03s Let $$ S(x)=\sqrt{2\pi x}\left(\frac{x}{e}\right)^x . $$ We must prove that for every fixed $a>0$, $$ \Gamma(x+1)=S(x)\left(1+O!\left(\frac1x\right)\right), \qquad x\ge a. $$ The corresponding formula for integers is already known from Exercise 5: $$...
TAOCP 1.2.11.2 Exercise 8
Section 1.2.11.2: Euler's Summation Formula Exercise 8. [ M23 ] Find the asymptotic value of $\ln((an^2+bn)!)$ with absolute error $O(n^{-2})$. Verified: yes Solve time: 45s Solution Let $$ m = an^2 + bn, \qquad a>0. $$ By Stirling’s formula in logarithmic form, $$ \ln(m!) = m\ln m - m + \frac{1}{2}\ln(2\pi m) + \frac{1}{12m} + O!\left(\frac{1}{m^3}\right). \tag{22} $$ Since $m = \Theta(n^2)$, the remainder term satisfies $O(1/m^3)=O(n^{-6})$, which is absorbed...
TAOCP 1.2.10 Exercise 22
Section 1.2.10: Analysis of an Algorithm Exercise 22. [ HM22 ] Suppose $X$ has the generating function $(q_1+p_1z)(q_2+p_2z)\cdots(q_n+p_nz)$, where $p_k+q_k=1$ for $1 \le k \le n$. Let $\mu=EX=p_1+\cdots+p_n$. a) Prove that $$ \Pr(X \le \mu r) \le (r^{-r}e^{r-1})^\mu, \qquad 0<r\le 1; $$ $$ \Pr(X \ge \mu r) \le (r^{-r}e^{r-1})^\mu, \qquad r\ge 1. $$ Verified: yes Solve time: 1m09s Solution Let $X$ be a sum of independent Bernoulli random variables $X_1,X_2,\ldots,X_n$,...
TAOCP 1.2.10 Exercise 4
Section 1.2.10: Analysis of an Algorithm Exercise 4. [ M10 ] Give an explicit, closed formula for the values of $p_{nk}$ in the coin-tossing experiment, Eq. (17). Verified: yes Solve time: 1m10s From Eq. (17), the recurrence for the probabilities is $$ p_{nk}=p,p_{n-1,k-1}+q,p_{n-1,k}, $$ where $q=1-p$. The generating function therefore satisfies $$ G_n(z)=(q+pz)^n $$ by Eq. (18). Expanding this by the binomial theorem gives $$ G_n(z)=\sum_{k=0}^n \binom{n}{k} q^{,n-k}p^k z^k. $$...
TAOCP 1.2.10 Exercise 3
Section 1.2.10: Analysis of an Algorithm Exercise 3. [ M15 ] What are the minimum, maximum, average, and standard deviation of the number of times step M4 is executed, if we are using Algorithm M to find the maximum of 1000 randomly ordered, distinct items? (Give your answer as decimal approximations to these quantities.) Verified: yes Solve time: 4m35s Let $X$ be the number of times step M4 is executed...
TAOCP 1.2.11.2 Exercise 9
Section 1.2.11.2: Euler's Summation Formula Exercise 9. [ M25 ] Find the asymptotic value of $\binom{2n}{n}$ with a relative error of $O(n^{-3})$, in two ways. 1.2.11.3. Some asymptotic calculations. In this subsection we shall investigate the following three intriguing sums, in order to deduce their approximate values: $$ P(n) = 1 + \frac{n-1}{n} + \frac{(n-2)^2}{n(n-1)} + \cdots = \sum_{k=0}^{n} \frac{(n-k)^k (n-k)!}{n!}, \tag{1} $$ $$ Q(n) = 1 + \frac{n-1}{n} +...
TAOCP 1.2.1 Exercise 11
Section 1.2.1: Mathematical Induction Exercise 11. [ M30 ] Find and prove a simple formula for the sum $$ \frac{1^3}{1^4 + 4} - \frac{3^3}{3^4 + 4} + \frac{5^3}{5^4 + 4} - \cdots + \frac{(-1)^n(2n + 1)^3}{(2n + 1)^4 + 4}. $$ Verified: no Solve time: 4m32s Let $$ S_n=\sum_{k=0}^{n}(-1)^k\frac{(2k+1)^3}{(2k+1)^4+4}. $$ We seek a closed form for $S_n$. The reviewer correctly pointed out that the previous partial-fraction decomposition was false. Therefore...
TAOCP 1.2.11.3 Exercise 1
Section 1.2.11.3: Some Asymptotic Calculations Exercise 1. [ HM20 ] Prove Eq. (5) by induction on $n$. Verified: yes Solve time: 1m21s Solution Equation (5) states that $$ f(x) = \sum_{k=0}^{n}\frac{f^{(k)}(0)}{k!}x^k + \frac1{n!}\int_0^x f^{(n+1)}(t)(x-t)^n,dt. \tag{5} $$ We prove this formula by induction on $n$. For $n=0$, $$ f(x) = f(0)+\int_0^x f'(t),dt, $$ by the fundamental theorem of calculus. This is exactly (5) when $n=0$. Assume that (5) holds for some...
TAOCP 1.2.1 Exercise 14
Section 1.2.1: Mathematical Induction Exercise 14. [ 50 ] (R. W. Floyd.) Prepare a computer program that accepts, as input, programs in some programming language together with optional assertions, and that attempts to fill in the remaining assertions necessary to make a proof that the computer program is valid. (For example, strive to get a program that is able to prove the validity of Algorithm E, given only assertions A1...
TAOCP 1.2.1 Exercise 9
Section 1.2.1: Mathematical Induction Exercise 9. [ 20 ] Prove by induction that if $0 < a < 1$, then $(1 - a)^n \ge 1 - na$. Verified: yes Solve time: 1m28s We prove by induction on $n$. For the base case $n=1$, $$ (1-a)^1 = 1-a = 1-1\cdot a, $$ so the statement is true. Now assume that for some integer $n \ge 1$, $$ (1-a)^n \ge 1-na. $$...
TAOCP 1.2.10 Exercise 1
Section 1.2.10: Analysis of an Algorithm Exercise 1. [ 10 ] Determine the value of $p_{n0}$ from Eqs. (4) and (5) and interpret this result from the standpoint of Algorithm M. Verified: yes Solve time: 40s From (4) with $k=0$ and using $p_{n-1,-1}=0$ from (5), $$ p_{n0}=\frac{1}{n}p_{n-1,-1}+\frac{n-1}{n}p_{n-1,0}=\frac{n-1}{n}p_{n-1,0}. $$ From (5), $p_{10}=1$. Iteration yields $$ p_{n0}=\prod_{j=2}^{n}\frac{j-1}{j} =\frac{1}{n}. $$ The event $A=0$ occurs exactly when no new maximum is encountered in steps...
TAOCP 1.2.10 Exercise 12
Section 1.2.10: Analysis of an Algorithm Exercise 12. [ HM21 ] When $G(z)=p_0+p_1z+p_2z^2+\cdots$ represents a probability distribution, the quantities $$ M_n = \sum_k k^n p_k \qquad\text{and}\qquad m_n = \sum_k (k-M_1)^n p_k $$ are called the "$n$th moment" and "$n$th central moment," respectively. Show that $$ G(e^t) = 1 + M_1t + M_2t^2/2! + \cdots; $$ then use Arbogast's formula (exercise 1.2.5-21) to show that $$ \kappa_n = \sum_{k_1,k_2,\ldots,k_n \ge 0}...
TAOCP 1.2.10 Exercise 6
Section 1.2.10: Analysis of an Algorithm Exercise 6. [ HM27 ] We've computed the mean and the variance of the important probability distributions (8), (18), (20). What is the third semi-invariant, $\kappa_3$, in each of those cases? Verified: yes Solve time: 2m03s Setup For a probability generating function $$ G(z)=\sum_{k\ge0}p_k z^k, $$ the third semi-invariant (third cumulant) is $$ \kappa_3 = \left.\frac{d^3}{dt^3}\log G(e^t)\right|_{t=0}. $$ Since $$ K(t)=\log G(e^t), $$ we...
TAOCP 1.2.10 Exercise 18
Section 1.2.10: Analysis of an Algorithm Exercise 18. [ M28 ] Suppose that the values taken on by $X[1],X[2],\ldots,X[n]$ in Algorithm M include exactly $k_1$ ones, $k_2$ twos, $\ldots$, $k_n$ $n$'s, arranged in random order, where $$ k_1+k_2+\cdots+k_n = n. $$ Show that in this generalized situation, the generating function (8) becomes $$ \left(\frac{k_n z}{k_n}\right) \left(\frac{k_{n-1}z+k_n}{k_{n-1}+k_n}\right) \left(\frac{k_{n-2}z+k_{n-1}+k_n}{k_{n-2}+k_{n-1}+k_n}\right)\cdots \left(\frac{k_1z+k_2+\cdots+k_n}{k_1+k_2+\cdots+k_n}\right)z, $$ using the convention $0/0=1$. Verified: yes Solve time: 2m32s The critical...
TAOCP 1.2.11.3 Exercise 7
Section 1.2.11.3: Some Asymptotic Calculations Exercise 7. [ HM30 ] In the evaluation of $I_2$, we had to consider $$ \int_0^y e^{-u}\left(1+\frac{u}{x}\right)^x du. $$ Give an asymptotic representation of $$ \int_0^{yx^{1/4}} e^{-u}\left(1+\frac{u}{x}\right)^x du $$ to terms of order $O(x^{-2})$, when $y$ is fixed and $x$ is large. Verified: no Solve time: 6m22s Let $$ J(x)=\int_0^{y x^{1/4}} e^{-u}\left(1+\frac ux\right)^x,du, $$ where $y$ is fixed and $x\to\infty$. We seek an asymptotic expansion...
TAOCP 1.2.11.3 Exercise 6
Section 1.2.11.3: Some Asymptotic Calculations Exercise 6. [ HM20 ] Prove Eq. (23). Verified: no Solve time: 1m55s Solution Write $$ \frac{(n+\alpha)^n}{n^{,n+\beta}} = n^{\alpha-\beta}\left(1+\frac{\alpha}{n}\right)^n. $$ Take logarithms of the second factor: $$ \ln\left(1+\frac{\alpha}{n}\right) = \frac{\alpha}{n} - \frac{\alpha^2}{2n^2} + O(n^{-3}). $$ Multiplying by $n$ gives $$ n\ln\left(1+\frac{\alpha}{n}\right) = \alpha - \frac{\alpha^2}{2n} + O(n^{-2}). $$ Exponentiation yields $$ \left(1+\frac{\alpha}{n}\right)^n = \exp\left(\alpha - \frac{\alpha^2}{2n} + O(n^{-2})\right) = e^\alpha \exp\left(-\frac{\alpha^2}{2n} + O(n^{-2})\right). $$ Expanding...
TAOCP 1.2.11.2 Exercise 10
Section 1.2.11.2: Euler's Summation Formula Exercise 10. [ HM22 ] Make a statement similar to that in exercise 9 about $\ln(1+O(z^m))$. Verified: yes Solve time: 45s Solution Let $u(z)=O(z^m)$ as $z\to 0$, with $m>0$. Then there exist positive constants $L$ and $z_0$ such that $|u(z)| \le L|z|^m$ for all $|z|\le z_0$, and hence $u(z)\to 0$ as $z\to 0$. For $|u|<1$, the expansion $$ \ln(1+u)=u+O(u^2) $$ holds, since $\ln(1+u)$ has a...
TAOCP 1.2.11.3 Exercise 15
Section 1.2.11.3: Some Asymptotic Calculations Exercise 15. [ HM20 ] Show that the following integral is related to $Q(n)$: $$ \int_0^\infty \left(1+\frac{z}{n}\right)^n e^{-z},dz. $$ Verified: no Solve time: 2m08s Let $$ I_n=\int_0^\infty \left(1+\frac{z}{n}\right)^n e^{-z},dz . $$ We first evaluate $I_n$ exactly. Using the binomial theorem, $$ \left(1+\frac{z}{n}\right)^n =\sum_{k=0}^{n}\binom{n}{k}\left(\frac{z}{n}\right)^k, $$ hence $$ I_n =\sum_{k=0}^{n}\binom{n}{k}\frac1{n^k} \int_0^\infty z^k e^{-z},dz . $$ Since $$ \int_0^\infty z^k e^{-z},dz=\Gamma(k+1)=k!, $$ we obtain $$ I_n =\sum_{k=0}^{n}\binom{n}{k}\frac{k!}{n^k}. $$...
TAOCP 1.2.11.3 Exercise 2
Section 1.2.11.3: Some Asymptotic Calculations Exercise 2. [ HM20 ] Obtain Eq. (7) from Eq. (6). Verified: yes Solve time: 58s Solution We are asked to derive the series expansion $$ \gamma(a,x) = \sum_{k\ge 0} \frac{(-1)^k x^{k+a}}{k!(k+a)} \tag{7} $$ starting from the definition of the incomplete gamma function $$ \gamma(a,x) = \int_0^x e^{-t} t^{a-1}, dt, \qquad a>0. \tag{6} $$ We begin by expanding the exponential function in its Maclaurin series:...
TAOCP 1.2.10 Exercise 2
Section 1.2.10: Analysis of an Algorithm Exercise 2. [ HM16 ] Derive Eq. (13) from Eq. (10). Verified: yes Solve time: 47s Solution We are asked to derive Eq. (13) from Eq. (10). Begin by recalling the definitions in Eq. (10). Let $$ G(z) = p_0 + p_1 z + p_2 z^2 + \cdots $$ be a generating function with $p_k$ the probability that an event takes value $k$. Then...
TAOCP 1.2.11.2 Exercise 13
Section 1.2.11.2: Euler's Summation Formula Exercise 13. [ M10 ] Prove or disprove: $g(n)=\Omega(f(n))$ if and only if $f(n)=O(g(n))$. 1.2.11.2. Euler's summation formula. One of the most useful ways to obtain good approximations to a sum is an approach due to Leonhard Euler. His method approximates a finite sum by an integral, and gives us a means to get better and better approximations in many cases. Figure 12 shows a...
TAOCP 1.2.1 Exercise 15
Section 1.2.1: Mathematical Induction Exercise 15. ▶ [ HM28 ] ( Generalized induction .) The text shows how to prove statements $P(n)$ that depend on a single integer $n$, but it does not describe how to prove statements $P(m, n)$ depending on two integers. In these circumstances a proof is often given by some sort of “double induction,” which frequently seems confusing. Actually, there is an important principle more general...
TAOCP 1.2.10 Exercise 21
Section 1.2.10: Analysis of an Algorithm Exercise 21. [ HM21 ] Let $X$ be the number of heads that occur when a random coin is flipped $n$ times, with generating function (18). Use (25) to prove that $$ \Pr(X \ge n(p+\epsilon)) \le e^{-\epsilon^2 n/(2q)} $$ when $\epsilon \ge 0$, and obtain a similar estimate for $\Pr(X \le n(p-\epsilon))$. Verified: yes Solve time: 2m25s Let $X\sim \mathrm{Bin}(n,p)$, $q=1-p$, and let $0\le...
TAOCP 1.2.11.3 Exercise 4
Section 1.2.11.3: Some Asymptotic Calculations Exercise 4. [ HM10 ] Prove Eq. (13). Verified: yes Solve time: 1m21s Corrected Solution to Exercise 1.2.11.3.4, Part 4 We are asked to prove Eq. (13), which is $$ v = u - \ln(1+u). \tag{13} $$ Solution Define the function $$ f(u) = u - \ln(1+u). $$ We aim to show that this function has the properties asserted in Eq. (13). In particular, Eq....
TAOCP 1.2.10 Exercise 8
Section 1.2.10: Analysis of an Algorithm Exercise 8. [ M20 ] Suppose that each $X[k]$ is taken at random from a set of $M$ distinct elements, so that each of the $M^n$ possible choices for $X[1],X[2],\ldots,X[n]$ is considered equally likely. What is the probability that all the $X[k]$ will be distinct? Verified: yes Solve time: 4m36s Solution Let $E$ be the event that all of the values $X[1],X[2],\ldots,X[n]$ are distinct....
TAOCP 1.2.10 Exercise 15
Section 1.2.10: Analysis of an Algorithm Exercise 15. [ HM23 ] When the probability that some quantity has the value $k$ is $e^{-\mu}(\mu^k/k!)$, it is said to have the Poisson distribution with mean $\mu$. a) What is the generating function for this set of probabilities? b) What are the values of the semi-invariants? c) Show that as $n\to\infty$ the Poisson distribution with mean $np$ approaches the normal distribution in the...
TAOCP 1.2.10 Exercise 13
Section 1.2.10: Analysis of an Algorithm Exercise 13. [ HM38 ] A sequence of probability generating functions $G_n(z)$ with means $\mu_n$ and deviations $\sigma_n$ is said to approach a normal distribution if $$ \lim_{n\to\infty} e^{-it\mu_n/\sigma_n}G_n(e^{it/\sigma_n}) = e^{-t^2/2} $$ for all real values of $t$. Using $G_n(z)$ as given by Eq. (8), show that $G_n(z)$ approaches a normal distribution. Verified: yes Solve time: 7m56s Setup Let $$ G_n(z)=\frac1{n!}(z+1)(z+2)\cdots(z+n-1), $$ as given...
TAOCP 1.2.1 Exercise 3
Section 1.2.1: Mathematical Induction Exercise 3. [ 18 ] The following proof by induction seems correct, but for some reason the equation for $n = 6$ gives $\frac{1}{2} + \frac{1}{6} + \frac{1}{12} + \frac{1}{20} + \frac{1}{30} = \frac{5}{6}$ on the left-hand side, and $\frac{3}{2} - \frac{1}{6} = \frac{4}{3}$ on the right-hand side. Can you find a mistake? “ Theorem. $$\frac{1}{1 \times 2} + \frac{1}{2 \times 3} + \cdots + \frac{1}{(n...
TAOCP 1.2.1 Exercise 1
Section 1.2.1: Mathematical Induction Exercise 1. [ 05 ] Explain how to modify the idea of proof by mathematical induction, in case we want to prove some statement $P(n)$ for all nonnegative integers, that is, for $n = 0$, $1$, $2$, $\ldots$ instead of for $n = 1, 2, 3, \ldots,$. Verified: yes Solve time: 31s To prove a statement $P(n)$ for all nonnegative integers $n = 0, 1, 2,...
TAOCP 1.2.1 Exercise 2
Section 1.2.1: Mathematical Induction Exercise 2. ▶ [ 15 ] There must be something wrong with the following proof. What is it? “ Theorem. Let $a$ be any positive number. For all positive integers $n$ we have $a^{n-1} = 1$. Proof. If $n = 1$, $a^{n-1} = a^{1-1} = a^0 = 1$. And by induction, assuming that the theorem is true for $1, 2, \ldots, n$, we have $$a^{(n+1)-1} =...
TAOCP 1.2.1 Exercise 4
Section 1.2.1: Mathematical Induction Exercise 4. [ 20 ] Prove that, in addition to Eq. (3), Fibonacci numbers satisfy $F_n \ge \phi^{n-2}$ for all positive integers $n$. Verified: yes Solve time: 27s Let $$ P(n): \qquad F_n \ge \phi^{,n-2}, $$ where $\phi=(1+\sqrt5)/2$ and $n$ is a positive integer. We prove $P(n)$ for all positive integers $n$ by mathematical induction. For the basis, when $n=1$, $$ F_1=1, $$ and since $\phi<2$,...
TAOCP 1.2.1 Exercise 5
Section 1.2.1: Mathematical Induction Exercise 5. [ 21 ] A prime number is an integer $> 1$ that has no positive integer divisors other than 1 and itself. Using this definition and mathematical induction, prove that every integer $> 1$ may be written as a product of one or more prime numbers. (A prime number is considered to be the “product” of a single prime, namely itself.) Verified: yes Solve...
TAOCP 1.2.1 Exercise 6
Section 1.2.1: Mathematical Induction Exercise 6. [ 20 ] Prove that if Eqs. (6) hold just before step E4, they hold afterwards also. Verified: no Solve time: 3m38s Let the values immediately before step E4 be $$ a,b,a',b',c,d, $$ and assume that the invariants $$ ac+bd=m,\qquad a'c+b'd=n \tag{6} $$ hold. From step E2, we have $$ c=qd+r. $$ Step E4 replaces the variables by $$ c^ =d,\qquad d^ =r, $$...
68. Computer Science
This volume studies theoretical and practical foundations of computation. It connects algorithms, systems, and formal models with mathematical rigor. Part I. Foundations of Computation Chapter 1. Models of Computation 1.1 Finite automata 1.2 Turing machines 1.3 Computability 1.4 Church–Turing thesis 1.5 Examples Chapter 2. Formal Languages 2.1 Alphabets and strings 2.2 Regular languages 2.3 Context-free languages 2.4 Grammars 2.5 Applications Chapter 3. Logic and Proof Systems 3.1 Propositional logic 3.2...
3.2 Functional Data Structures
3.2 Functional data structures, 35 index slug name 1 functional-data-structure Functional Data Structure 2 persistent-list Persistent List 3 functional-stack Functional Stack 4 functional-queue Functional Queue 5 banker's-queue Bankers Queue 6 real-time-queue Real Time Queue 7 functional-deque Functional Deque 8 finger-tree-functional Finger Tree 9 functional-heap Functional Heap 10 binomial-heap-functional Functional Binomial Heap 11 pairing-heap-functional Functional Pairing Heap 12 skew-binomial-heap Skew Binomial Heap 13 functional-set Functional Set 14 functional-map Functional Map 15...
SIMD Linear Search
SIMD Linear Search SIMD linear search is a linear search variant that compares several values at the same time using vector instructions. SIMD means single instruction, multiple data. Instead of comparing one array element per loop iteration, the algorithm loads a block of elements into a vector register and compares all lanes against the target in one operation. The logical algorithm remains linear search. The speedup comes from processing multiple...
Finger Search
Finger Search Finger search exploits locality. Instead of searching from the root or beginning, it starts from a known position called a finger and moves toward the target. The cost depends on the distance between the finger and the target, not the total size of the structure. This method appears in ordered data structures such as balanced trees, skip lists, and sorted arrays with auxiliary links. Problem Given an ordered...
LeetCode
LeetCode Solutions to LeetCode problems with comprehensive analysis, Python and Go implementations. Problems # Problem Difficulty Time Notes 1 Two Sum 🟢 Easy 4m The problem gives us an integer array called nums and another integ… 2 Add Two Numbers 🟡 Medium 2m 35s The problem gives us two non empty singly linked lists. Each linked… 3 Longest Substring Without Repeating Characters 🟡 Medium 1m 9s The problem gives a...
Eytzinger Layout Search
Eytzinger Layout Search Eytzinger layout search reorganizes a sorted array into a breadth first tree layout, also known as heap order. The goal is to improve cache locality and make memory access patterns more predictable. Instead of storing data in sorted order, the array is rearranged so that binary search follows a sequential memory pattern. This reduces cache misses and improves throughput on modern CPUs. Problem Given a sorted array...
Galloping Intersection Search
Galloping Intersection Search Galloping intersection search computes the intersection of two sorted sequences by combining linear scanning with exponential jumps. When one sequence advances much faster than the other, the algorithm uses exponential search to skip ahead, then refines with binary search. This reduces unnecessary comparisons when the sequences differ significantly in size or distribution. Problem Given two sorted arrays $A$ and $B$, compute all elements that appear in both:...
3.3 Succinct and Compressed Structures
3.3 Succinct and compressed structures, 45 index slug name 1 succinct-data-structure Succinct Structure 2 bit-vector Bit Vector 3 rank-select Rank Select 4 succinct-rank Succinct Rank 5 succinct-select Succinct Select 6 wavelet-tree-succinct Succinct Wavelet Tree 7 wavelet-matrix-succinct Succinct Wavelet Matrix 8 compressed-trie-succinct Succinct Trie 9 compressed-suffix-array Compressed Suffix Array 10 fm-index FM Index 11 burrows-wheeler-transform BWT 12 run-length-encoding RLE 13 delta-encoding Delta Encoding 14 gamma-coding Gamma Coding 15 elias-delta-coding Elias Delta...
Interpolation Sequential Search
Interpolation Sequential Search Interpolation sequential search combines estimation and local scanning. It first predicts where the target should lie using interpolation, then performs a short sequential search around that estimate. This method is effective when values are approximately uniformly distributed and when exact positioning may be noisy or approximate. Problem Given a sorted array $A$ and a target $x$, find an index $i$ such that $$ A[i] = x $$...
Learned Index Search
Learned Index Search Learned index search treats an ordered index as a prediction problem. Given a key, a model estimates the position where that key should appear in sorted data. The algorithm then corrects the estimate by searching a small range around the predicted position. The main idea is that a sorted array defines a cumulative distribution function from key to position. If this function is predictable, a model can...
3.6 External-Memory and Database Structures
3.6 External-memory and database structures, 35 index slug name 1 external-memory-structure External Memory Structure 2 block-model Block Model 3 buffer-pool Buffer Pool 4 page-cache Page Cache 5 slotted-page Slotted Page 6 heap-file Heap File 7 sorted-file Sorted File 8 clustered-index Clustered Index 9 secondary-index Secondary Index 10 b-tree-index B Tree Index 11 b-plus-tree-index B Plus Tree Index 12 lsm-tree LSM Tree 13 memtable Memtable 14 sstable SSTable 15 bloom-filter-index Bloom...
Interpolation Search with Fallback
Interpolation Search with Fallback Interpolation search with fallback combines the speed of interpolation search on well distributed numeric keys with the reliability of binary search. It estimates where the target should be by value, but falls back to ordinary binary search when the estimate becomes poor, unstable, or unsafe. This gives good performance on near uniform data while preserving the worst case behavior of binary search. Problem Given a sorted...
3.7 Probabilistic Data Structures
3.7 Probabilistic data structures, 15 index slug name 1 probabilistic-data-structure Probabilistic Data Structure 2 bloom-filter-probabilistic Bloom Filter 3 counting-bloom-filter-probabilistic Counting Bloom Filter 4 cuckoo-filter-probabilistic Cuckoo Filter 5 quotient-filter-probabilistic Quotient Filter 6 xor-filter-probabilistic XOR Filter 7 count-min-sketch Count Min Sketch 8 count-sketch Count Sketch 9 hyperloglog HyperLogLog 10 minhash MinHash 11 reservoir-sampling Reservoir Sampling 12 skip-list Skip List 13 treap-probabilistic Treap 14 randomized-meldable-heap Randomized Meldable Heap 15 probabilistic-invariant-check Probabilistic Invariant Check
Branchless Binary Search
Branchless Binary Search Branchless binary search is a binary search variant designed to reduce branch mispredictions. A normal binary search repeatedly branches on whether the middle value is less than the target. On modern CPUs, this branch can be hard to predict because the search direction depends on the data. A branchless version keeps the same comparison logic but updates the search position using conditional moves, masks, or arithmetic expressions....
3.4 Geometric Data Structures
3.4 Geometric data structures, 40 index slug name 1 geometric-data-structure Geometric Data Structure 2 point-set Point Set 3 line-segment-set Line Segment Set 4 interval-tree-geometric Interval Tree 5 segment-tree-geometric Geometric Segment Tree 6 range-tree-geometric Range Tree 7 two-dimensional-range-tree-geometric 2D Range Tree 8 kd-tree KD Tree 9 kd-tree-nearest-neighbor KD Tree Nearest Neighbor 10 ball-tree Ball Tree 11 vp-tree Vantage Point Tree 12 cover-tree Cover Tree 13 quadtree Quadtree 14 octree Octree 15...
3.1 Persistent Data Structures
3.1 Persistent data structures, 45 index slug name 1 persistent-data-structure Persistent Data Structure 2 partial-persistence Partial Persistence 3 full-persistence Full Persistence 4 confluent-persistence Confluent Persistence 5 path-copying Path Copying 6 fat-node Fat Node 7 persistent-array Persistent Array 8 persistent-segment-tree Persistent Segment Tree 9 persistent-fenwick Persistent Fenwick Tree 10 persistent-bst Persistent BST 11 persistent-treap Persistent Treap 12 persistent-heap Persistent Heap 13 persistent-union-find Persistent Union Find 14 persistent-stack Persistent Stack 15 persistent-queue...
Cache Aware Binary Search
Cache Aware Binary Search Cache aware binary search modifies ordinary binary search to account for the memory hierarchy. The algorithm still uses ordering and comparisons, but the data layout or search strategy is chosen with explicit knowledge of cache line size, block size, or page size. The goal is to reduce cache misses. On modern hardware, a comparison is cheap, while an unpredictable memory load can be expensive. Problem Given...
3.5 Concurrent and Lock-Free Structures
3.5 Concurrent and lock-free structures, 35 index slug name 1 concurrent-data-structure Concurrent Data Structure 2 thread-safe-wrapper Thread Safe Wrapper 3 mutex-protected-map Mutex Protected Map 4 read-write-lock-map Read Write Lock Map 5 striped-locking Striped Locking 6 concurrent-queue Concurrent Queue 7 mpsc-queue MPSC Queue 8 spmc-queue SPMC Queue 9 mpmc-queue MPMC Queue 10 bounded-concurrent-queue Bounded Concurrent Queue 11 unbounded-concurrent-queue Unbounded Concurrent Queue 12 lock-free-stack Lock Free Stack 13 lock-free-queue Lock Free Queue...
Merge Path Search
Merge Path Search Merge path search finds partition points in two sorted arrays so that a merge can be split into independent chunks. It is mainly used for parallel merge, GPU merge, and distributed sorted data processing. The method views merging as a path through a grid. Each step consumes one element from either the first array or the second array. A diagonal in this grid represents a fixed number...
Van Emde Boas Layout Search
Van Emde Boas Layout Search Van Emde Boas layout search stores a binary search tree in recursive memory order. The layout is cache oblivious, which means it does not need to know the cache line size or memory block size in advance. The main idea is to split the tree into a top subtree and several bottom subtrees, then store each part recursively. Nodes that are close in the search...
Branchless Lower Bound
Branchless Lower Bound Branchless lower bound computes the first position where a value is greater than or equal to a target, without using unpredictable branches. It follows the same logical structure as binary search but replaces control flow with arithmetic updates or conditional moves. The result is the same as standard lower bound. The benefit is improved performance on modern CPUs when branch misprediction is expensive. Problem Given a sorted...
Exponential Backoff Search
Exponential Backoff Search Exponential backoff search finds a target in a sorted or monotone structure by expanding the search interval geometrically. Instead of scanning linearly, it probes positions at increasing distances until it either finds the target or bounds it inside an interval. A secondary search then refines the result. This method is useful when the size of the data is unknown, unbounded, or expensive to traverse fully. Problem Given...
Recursive Model Index Search
Recursive Model Index Search Recursive model index search uses several learned models arranged as a hierarchy. A top model selects a lower model, and the lower model predicts the likely position of a key in sorted data. The final prediction is then corrected by searching inside a bounded local range. This is the main search procedure behind a recursive model index, often shortened to RMI. It replaces part of a...
SIMD Binary Search
SIMD Binary Search SIMD binary search accelerates binary search by evaluating multiple candidate comparisons in parallel. Instead of checking a single midpoint per step, the algorithm probes several positions using vector instructions and narrows the search range based on the combined results. The structure still follows binary search, but each iteration performs more comparisons per memory access. Problem Given a sorted array $A$ of length $n$ and a target value...