#volume-1
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.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.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.5 Exercise 6
Section 2.5: Dynamic Storage Allocation Exercise 6. [**] [23] ( Next fit. ) After Algorithm A has been used repeatedly, there will be a strong tendency for blocks of small SIZE to remain at the front of the AVAIL list, so that it will often be necessary to search quite far into the list before finding a block of length N or more. For example, notice how the size of...
TAOCP 2.5 Exercise 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 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 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 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 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.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.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.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.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 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.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.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 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 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.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 3
Section 2.4: Multilinked Structures Exercise 3. [**] [22] The PL/I language accepts data structures like those in COBOL, except that any sequence of level numbers is possible. For example, the sequence 1 A 1 A 3 B 2 B 5 C is equivalent to 3 C 4 D 3 D 2 E 2 E In general, rule (a) is modified to read, "The items of a group must have a...
TAOCP 2.4 Exercise 5
Section 2.4: Multilinked Structures Exercise 5. [**] [20] In practice, Algorithm B may be given a linked list of Symbol Table references as input, instead of what we called " P0, P1, ..., Pn ." Let T be a pointer variable such that $$ \operatorname{INFO}(T) = P_0,\quad \operatorname{INFO}(\operatorname{RLINK}(T)) = P_1,\quad \ldots,\quad \operatorname{INFO}(\operatorname{RLINK}^{[n]}(T)) = P_n,\quad \operatorname{RLINK}^{[n+1]}(T) = \Lambda. $$ Show how to modify Algorithm B so that it uses such a...
TAOCP 2.4 Exercise 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 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.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.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.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.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 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 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 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.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 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.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.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 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 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.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.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.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.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.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.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.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 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 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.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.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.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.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 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.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 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 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 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.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 4
Section 2.3.4.6: History and Bibliography Exercise 4. [**] [M38] (Conway and Coxeter.) A frieze pattern of order $n$ is an infinite array of positive integers such as in which the top and bottom rows consist entirely of 1s, and each diamond of adjacent values $$ \begin{matrix} & b & \ a && d\ & c & \end{matrix} $$ satisfies $ad-bc=1$. Find a one-to-one correspondence between $n$-node binary trees and $(n+1)$-rowed...
TAOCP 2.3.4.6 Exercise 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.4.4 Exercise 27
Section 2.3.4.4: Enumeration of Trees Exercise 27. [**] [M28] Let $U_1,U_2,\ldots,U_p,\ldots,U_q;\ V_1,V_2,\ldots,V_r$ be vertices of a directed graph, where $1\le p\le q$. Let $f$ be any function from the set ${p+1,\ldots,q}$ into the set ${1,2,\ldots,r}$, and let the directed graph contain exactly $q-p$ arcs, from $U_k$ to $V_{f(k)}$ for $p<k\le q$. Show that the number of ways to add $r$ additional arcs, one from each of the $V$'s to one...
TAOCP 2.3.4.4 Exercise 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 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 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 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.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 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.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.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.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 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 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 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 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 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 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.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 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 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 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 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 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.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 11
Section 2.3.4.2: Oriented Trees Exercise 11. [**] $$28$$ Using the assumptions of exercise 2.3.4.1-6, but with $(a_k,b_k)$ representing an arc from $V_{a_k}$ to $V_{b_k}$, design an algorithm that not only prints out a free subtree as in that algorithm, but also prints out the fundamental cycles. [ Hint: The algorithm given in the solution to exercise 2.3.4.1-6 can be combined with the algorithm in the preceding exercise.] Verified: yes Solve...
TAOCP 2.3.4.2 Exercise 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 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 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.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 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.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.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.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 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 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 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 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 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.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.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 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.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.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 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 18
Section 2.3.4.2: Oriented Trees Exercise 18. [**] $$M30$$ Let $G$ be a graph with $n+1$ vertices $V_0,V_1,\ldots,V_n$ and $m$ edges $e_1,\ldots,e_m$. Make $G$ into a directed graph by assigning an arbitrary orientation to each edge; then construct the $m\times(n+1)$ matrix $A$ with $$ a_{ij}= \begin{cases} +1, & \text{if } \operatorname{init}(e_i)=V_j;\ -1, & \text{if } \operatorname{fin}(e_i)=V_j;\ 0, & \text{otherwise.} \end{cases} $$ Let $A_0$ be the $m\times n$ matrix $A$ with column...
TAOCP 2.3.4.2 Exercise 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.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.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.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 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.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 1
Section 2.3.3: Other Representations of Trees Exercise 1. [ 20 ] If we had only LTAG , INFO , and RTAG fields (not LLINK ) in a level order sequential representation like (8) , would it be possible to reconstruct the LLINK s? (In other words, are the LLINK s redundant in (8) , as the RLINK s are in (3) ?) Verified: no Solve time: 4m54s Solution We are...
TAOCP 2.3.2 Exercise 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 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 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 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 17
Section 2.3.3: Other Representations of Trees Exercise 17. [ 25 ] Algorithm F evaluates a "bottom-up" locally defined function, namely, one that should be evaluated at the children of a node before it is evaluated at the node. A "top-down" locally defined function f is one in which the value of f at a node x depends only on x and the value of f at the parent of x...
TAOCP 2.3.2 Exercise 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 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 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.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 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 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 21
Section 2.3.2: Binary Tree Representation of Trees Exercise 21. [ 25 ] Algorithm D controls the differentiation activity for binary operators, unary operators, and nullary operators, thus for trees whose nodes have degree 2, 1, and 0; but it does not indicate explicitly how the control would be handled for ternary operators and nodes of higher degree. (For example, exercise 17 suggests making addition and multiplication into operators with any...
TAOCP 2.3.3 Exercise 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.3 Exercise 6
Section 2.3.3: Other Representations of Trees Exercise 6. [ 24 ] Suppose that the nodes of an oriented forest have three link fields, PARENT , LCHILD , and RLINK , but only the PARENT link has been set up to indicate the tree structure. The LCHILD field of each node is Λ and the RLINK fields are set as a linear list that simply links the nodes together in some...
TAOCP 2.3.3 Exercise 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 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.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.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.2 Exercise 15
Section 2.3.2: Binary Tree Representation of Trees Exercise 15. [ 23 ] Write a MIX program for the DIV routine, corresponding to DIFF[7] as specified in the text. (This routine should be added to the program in the text after line 217 .) Verified: yes Solve time: 1m21s Solution The routine DIV computes the derivative of a formula of the form $u / v$ with respect to the variable $x$,...
TAOCP 2.3.3 Exercise 4
Section 2.3.3: Other Representations of Trees Exercise 4. [ 18 ] The trees (2) contain 10 nodes, five of which are terminal. Representation of these trees in the normal binary-tree fashion involves 10 LLINK fields and 10 RLINK fields (one for each node). Representation of these trees in the form (10) , where LLINK and INFO share the same space in a node, requires 5 LLINK s and 15 RLINK...
TAOCP 2.3.3 Exercise 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.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.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.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 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 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.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 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 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 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.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 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 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 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 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 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.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 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 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.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.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.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 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 18
Section 2.3.1: Traversing Binary Trees Exercise 18. [ 24 ] Many algorithms dealing with trees like to visit each node twice instead of once, using a combination of preorder and inorder that we might call double order . Traversal of a binary tree in double order is defined as follows: If the binary tree is empty, do nothing; otherwise a) visit the root, for the first time; b) traverse the...
TAOCP 2.3.1 Exercise 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.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 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 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 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.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.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.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 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 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.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.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 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 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 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.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.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.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 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 12
Section 2.3.1: Traversing Binary Trees Exercise 12. [ 22 ] Design an algorithm analogous to Algorithm T that traverses a binary tree in preorder , and prove that your algorithm is correct. Verified: no Solve time: 37s Solution We aim to construct an algorithm analogous to Algorithm T that traverses a binary tree in preorder , visiting each node exactly once, and then prove its correctness by induction on the...
TAOCP 2.3.1 Exercise 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 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.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.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.2 Exercise 3
Section 2.3.2: Binary Tree Representation of Trees Exercise 3. [ 22 ] What is the relation between Dewey decimal notation for the nodes of a forest and the preorder and postorder of those nodes? Verified: yes Solve time: 1m35s Solution Let the Dewey decimal notation of a node be $$ d_1.d_2.\cdots.d_k, $$ where $d_1$ gives the position of the node among the trees of the forest, $d_2$ gives its position...
TAOCP 2.3.1 Exercise 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 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 31
Section 2.3.1: Traversing Binary Trees Exercise 31. [ 23 ] Design an algorithm that "erases" a right-threaded binary tree. Your algorithm should return all of the tree nodes except the list head to the AVAIL list, and it should make the list head signify an empty binary tree. Assume that each node has the fields LLINK, RLINK, RTAG; use no auxiliary stack. Verified: no Solve time: 56s Solution Let T...
TAOCP 2.3.2 Exercise 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.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.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 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.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.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.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.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.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 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 7
Section 2.2.5: Doubly Linked Lists Exercise 7. [ 25 ] Although bugs in programs are often embarrassing to a programmer, if we are to learn from our mistakes we should record them and tell other people about them instead of forgetting them. The following error (among others) was made by the author when he first wrote the program in this section: Line 154 said JANZ CYCLE instead of JANZ U4A...
TAOCP 2.2.4 Exercise 18
Section 2.2.4: Circular Lists Exercise 18. [ 25 ] Devise a way to represent circular lists inside a computer in such a way that the list can be traversed efficiently in both directions, yet only one link field is used per node. [ Hint: If we are given two pointers, to two successive nodes $x_{i-1}$ and $x_i$, it should be possible to locate both $x_{i+1}$ and $x_{i-2}$.] Verified: no Solve...
TAOCP 2.2.5 Exercise 2
Section 2.2.5: Doubly Linked Lists Exercise 2. [ 22 ] Explain why a list that is singly linked cannot allow efficient operation as a general deque; the deletion of items can be done efficiently at only one end of a singly linked list. Verified: no Solve time: 46s Solution Let a general deque be a data structure that allows insertion and deletion at both ends. Suppose we represent it as...
TAOCP 2.2.5 Exercise 3
Section 2.2.5: Doubly Linked Lists Exercise 3. [ 22 ] The elevator system described in the text uses three call variables, CALLUP , CALLCAR , and CALLDOWN , for each floor, representing buttons that have been pushed by the users in the system. It is conceivable that the elevator actually needs only one or two binary variables for the call buttons on each floor, instead of three. Explain how an...
TAOCP 2.2.5 Exercise 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 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 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.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 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 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 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.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.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 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.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 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.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.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 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 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 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 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 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 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.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.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 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 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.4 Exercise 9
Section 2.2.4: Circular Lists Exercise 9. [ 23 ] Would Algorithm A work properly if P = Q (i.e., both pointer variables point at the same polynomial)? Would Algorithm M work properly if P = M , if P = Q , or if M = Q ? Verified: no Solve time: 5m33s Solution Algorithm A does not work properly when P = Q . At step A1 , both...
TAOCP 2.2.4 Exercise 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 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 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 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.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.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.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.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.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.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 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 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 3
Section 2.2.3: Linked Allocation Exercise 3. [ 22 ] Write a "general purpose" MIX subroutine to do the deletion operation, (11). This subroutine should have the following specifications: Calling sequence: JMP DELETE Jump to subroutine. NOP T Location of pointer variable JMP UNDERFLOW First exit, if UNDERFLOW sensed Entry conditions: None Exit conditions: If the stack whose pointer is the link variable T is empty, the first exit is taken;...
TAOCP 2.2.4 Exercise 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 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.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 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 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.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.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.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.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.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 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 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 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 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 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 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 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.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.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.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.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.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.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 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.4 Exercise 10
Section 2.2.4: Circular Lists Exercise 10. [ 20 ] The algorithms in this section assume that we are using three variables $x$, $y$, and $z$ in the polynomials, and that their exponents individually never exceed $b - 1$ (where $b$ is the byte size in MIX's case). Suppose instead that we want to do addition and multiplication of polynomials in only one variable, $x$, and to let its exponent take...
TAOCP 2.2.4 Exercise 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 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 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.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.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 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.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.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.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.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.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 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.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 2
Section 2.2.3: Linked Allocation Exercise 2. [ 22 ] Write a "general purpose" MIX subroutine to do the insertion operation, (10). This subroutine should have the following specifications (as in Section 1.4.1): Calling sequence: JMP INSERT Jump to subroutine. NOP T Location of pointer variable Entry conditions: rA = information to be put into the INFO field of a new node. Exit conditions: The stack whose pointer is the link...
TAOCP 2.2.3 Exercise 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.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.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 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.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 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 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 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 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 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 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.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.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 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 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 2.2.1 Exercise 3
Section 2.2.1: Stacks, Queues, and Deques Exercise 3. [ 25 ] The operations (i) through (viii) in the previous exercise can be much more concisely described by the code SSXSSXXX , where S stands for "move a car from the input into the stack," and X stands for "move a car from the stack into the output." Some sequences of S 's and X 's specify meaningless operations, since there...
TAOCP 1.4.4 Exercise 15
Section 1.4.4: Input and Output Exercise 15. [ 22 ] Write a complete MIX program that copies 100 blocks from tape unit 0 to tape unit 1, using just three buffers. The program should be as fast as possible. Verified: no Solve time: - Solution We are asked to write a complete MIX program to copy 100 blocks from tape unit 0 to tape unit 1 using three buffers, with...
TAOCP 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 1.4.4 Exercise 14
Section 1.4.4: Input and Output Exercise 14. [ 20 ] Suppose the computational program does not alternate between ASSIGN and RELEASE, but instead gives the sequence of actions ... ASSIGN ... ASSIGN ... RELEASE ... RELEASE . What effect does this have on the algorithms described in the text? Is it possibly useful? Verified: no Solve time: - Solution In the multiple-buffering scheme described in the text, the computational program...
TAOCP 1.4.4 Exercise 19
Section 1.4.4: Input and Output Exercise 19. [ M28 ] Special considerations arise when input or output involves short blocks on a rotating device like a magnetic disk. Suppose a program works with $n \geq 2$ consecutive blocks of information in the following way: Block $k$ begins to be input at time $t_k$, where $t_1 = 0$. It is assigned for processing at time $u_k \geq t_k + T$ and...
TAOCP 1.4.4 Exercise 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.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 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 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 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 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 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 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 1.4.4 Exercise 12
Section 1.4.4: Input and Output Exercise 12. [ 24 ] Suppose that the multiple buffering algorithm in the text is being used for card input, and suppose the input is to terminate as soon as a card with "." in column 80 has been read. Show how the CONTROL coroutine (Algorithm B and Program B) should be changed so that input is shut off in this way. Verified: no Solve...
TAOCP 1.4.4 Exercise 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 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 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 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.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 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.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 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.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.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 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 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 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 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.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 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 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 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 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.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 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 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.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 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.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.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.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 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 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 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 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 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.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.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 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 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.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.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.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.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 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.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 2
Section 1.4.3.1: A MIX Simulator Exercise 2. [ 20 ] Write the SHIFT routine, which is missing from the program in the text (operation code 6). Verified: no Solve time: 5m08s The missing routine must be written in the conventions of Program M. The simulator stores each MIX register as a signed word whose five magnitude bytes are individually accessible. Therefore the routine must operate directly on those stored bytes...
TAOCP 1.4.3.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.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 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 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 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.3.3 Exercise 12
Section 1.3.3: Applications to Permutations Exercise 12. [ M27 ] ( Transposing a rectangular matrix .) Suppose an $m \times n$ matrix $(a_{ij})$, $m \ne n$, is stored in memory in a fashion like that of exercise 1.3.2-10, so that the value of $a_{ij}$ appears in location $L + n(i - 1) + (j - 1)$, where $L$ is the location of $a_{11}$. The problem is to find a way...
TAOCP 1.3.3 Exercise 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 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.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.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.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.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.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.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.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.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.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.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.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 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.1 Exercise 21
Section 1.3.1: Description of MIX Exercise 21. [ 24 ] (a) Can the J-register ever be zero? (b) Write a program that, given a number N in rI4 , sets register J equal to N , assuming that 0 < N \le 3000 . Your program should start in location 3000 . When your program has finished its execution, the contents of all memory cells must be unchanged. Verified: no...
TAOCP 1.3.1 Exercise 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 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 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.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.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.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.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.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.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.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 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 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.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 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.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.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.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.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.3 Exercise 8
Section 1.3.3: Applications to Permutations Exercise 8. [ 23 ] Would it be feasible to modify Algorithm $B$ to go from left to right instead of from right to left through the input? Verified: yes Solve time: 1m31s Solution Algorithm $B$ maintains a table $T$ such that, after a cycle has been completely scanned, the effect of that cycle has already been incorporated into the current permutation. The crucial step...
TAOCP 1.3.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.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 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.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.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.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.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 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 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 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.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 16
Section 1.3.1: Description of MIX Exercise 16. [ 20 ] Write a program that sets memory cells 0000 - 0099 all to zero and is (a) as short a program as possible; (b) as fast a program as possible. [Hint: Consider using the MOVE command.] Verified: yes Solve time: 3m35s The previous solution failed because it imported an incorrect model of MOVE and then built optimality arguments on top of...
TAOCP 1.3.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 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.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 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 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 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.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.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.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.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.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.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 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.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 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 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 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.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.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 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 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.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.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 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 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 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.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 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 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.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 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 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.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.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.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.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 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.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.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 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.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 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 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.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.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.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 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 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 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 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 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.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.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.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 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 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.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 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.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 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 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.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.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.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 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.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.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.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.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.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 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.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 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 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 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 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.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.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.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.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.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 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.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.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.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 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.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.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.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 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.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.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.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 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.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 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.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.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.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.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.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 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 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 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.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.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.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 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.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.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.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.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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.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 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 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.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.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.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.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 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.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 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.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.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.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.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 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.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.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.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 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.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 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.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 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.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.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 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 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.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 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 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.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.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 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.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.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.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.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 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.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 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 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 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 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 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.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 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 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.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 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 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.3 Exercise 19
Section 1.2.3: Sums and Products Exercise 19. [ 20 ] What is $\sum_{j=m}^n (a_j - a_{j-1})$? Verified: yes Solve time: 2m02s Solution We compute the sum directly. By linearity of summation, $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{j=m}^n a_{j-1}. $$ Using the change of index $k=j-1$ in the second sum, Eq. (6) gives $$ \sum_{j=m}^n a_{j-1} = \sum_{k=m-1}^{,n-1} a_k. $$ Hence $$ \sum_{j=m}^n (a_j-a_{j-1}) = \sum_{j=m}^n a_j - \sum_{k=m-1}^{,n-1}...
TAOCP 1.2.3 Exercise 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 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 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 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 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.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 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 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 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 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.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 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.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.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.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.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.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.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.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 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.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 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.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.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 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 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 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 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.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.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 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 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.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.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.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 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.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.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 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.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 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 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 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.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.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 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.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 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 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.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 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.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.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 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 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.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.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 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.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 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.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.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 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.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.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 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.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 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 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 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.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.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.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 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.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 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.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.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 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 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.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.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 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.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 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 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 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.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 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 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 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 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.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.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 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 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 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.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.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.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.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.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 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.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.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.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.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.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.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.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.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.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.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.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 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.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 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.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.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 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 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 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.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 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.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.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.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.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.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.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.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.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.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 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 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.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.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.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.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 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 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.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.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 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 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.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.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 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 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 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.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 6
Section 1.2.1: Mathematical Induction Exercise 6. [ 20 ] Prove that if Eqs. (6) hold just before step E4, they hold afterwards also. Verified: no Solve time: 3m38s Let the values immediately before step E4 be $$ a,b,a',b',c,d, $$ and assume that the invariants $$ ac+bd=m,\qquad a'c+b'd=n \tag{6} $$ hold. From step E2, we have $$ c=qd+r. $$ Step E4 replaces the variables by $$ c^ =d,\qquad d^ =r, $$...
TAOCP 1.2.1 Exercise 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 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$,...