{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

3.review (odds) - 112 Chapter 3 The Fundamentals Algorithms...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 8
Background image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 112 Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices GUIDE TO REVIEW QUESTIONS FOR CHAPTER 3 1. a) See p. 168. b) in English, in a computer language, in pseudocode c) An algorithm is more of an abstract ideaea method in theory that will solve a problem. A computer program is the implementation of that idea into a specific syntactically correct set of instructions that areal computer can use to solve the problem. It is rather like the difference between a dollar (a certain amount of money, capable in theory of purchasing a certain quantity of goods and services) and a dollar bill. 2. a) See first three lines of text following Algorithm 1 on p. 169. b) See. Algorithm 1 in Section 3.1. 0) See Example 1 in Section 3.3. 3. a) Seep. 180. b) n2 +18n+107 S n3 + n3 + n3 : 371.3 for all n > 107. c) n3 is not less than a constant times n2 + 1811+ 107, since their ratio exceeds raj/(371.2) : 11/3 for all in. > 107. 4. a) For the sum, take the largest term; for the product. multiply the factors together. 10. 11. 12. b) 9(a) : n”_22" : (2n)”/n2 . a) the largest, average, and smallest number of comparisons used by the algorithm before it stops, among all lists of n. integers b) all are n e 1 . a) See pp. 170-172. b) See pp. 194195. c) No-wit depends on the lists involved. (However, the worst case complexity for binary search is always better than that for linear search for lists of any given size. except for very short lists.) . a) See p. 173. b) On the first pass, the 5 bubbles down to the end, producing 24135. On the next pass, the 4 bubbles down to the end, producing 2 1 34 5. On the next pass, the 1 and the 2 are swapped. No further changes are made on the fourth pass. c) 0012); see Example 5 in Section 3.3 . a) Seep. 174. b) On the first pass, the 5 is inserted into its correct position relative to the 2, producing 25143. On the next pass, the 1 is inserted into its correct position relative to 2 5, producing 12-54 3. On the next. pass, the 4 is inserted into its correct position relative to 12 5, producing 1 2 45 3. On the final pass, the 3 is inserted, producing the sorted list. c) O(-n2); see Example 6 in Section 3.3 . a) See pp. 1747175. b) See Example 6 and Theorem 1 in Section 3.1. (3) See Exercise 55b in Section 3.1. See p. 211. a) See the bottom of p. 211. b) 112 - 23 - 29 a) See p. 215. b) find all the common factors (not a good algorithm unless the numbers are really small); find the prime factorization of each integer (works well if the numbers aren’t too big and therefore can be easily factored); use the Euclidean algorithm (really the best method) c) 1 (use the Euclidean algorithm) .5) 23355573 Supplementary Exercises 1 13 13. a)7la—b b)OE-7;—13—8;3217_=_—11 0) (10a. + 13) — (—45) + 20) : 3((1 — b} + 7(a + b 1 1}; note that 7 divides both terms 14. Sec Algorithm 1 in Section 3.6 with l) = 16. Use “A” for a remainder of 10. “B” for a remainder of 11, and so on. See also Example 4 in that section. 15. a) Use the Euclidean algorithm; see Example 1 in Section 3.7. b) 725-11977-84 16. a) afi E 1 (mod in} b) Express 1 as so. + Em (see Review Question 15). Then .3 is the inverse of (I modulo m. c) 11 17. a) Multiply each side by the inverse of a. modulo m. b) { 10 + 1.9.1;- | k E Z} 18. a) See p. 236. b) {17 +140K1Jk E Z} 19. No mu, could be a pseudoprime such as 341. 20. a) See p. 241. b) The amount of shift. it. is kept secret. It is needed both to encode and to decode messages. c) Although the key for decoding, d. is kept secret, the keys for encoding, rt and e, are published. 21. See p. 248. The number of columns of A must equal the number of rows of B. '22. a) 5 b) (A1A2}(A;;A4); see Exercise '25 in Section 3.8 SUPPLEMENTARY EXERCISES FOR CHAPTER 3 1. a) This algorithm will be identical to the algorithm first largest for Exercise 17 of Section 3.1, except that we want. to change the value 01“ location each time we lind another element in the list that is equal to the current value 01' mar. Therefore we simply change the strict less than (<) in the comparison 712.03: < o.- to a less than or (Equal to (g), rendering the fifth line of that procedure “if may: :1 a1- then.” b) The number of comparisons used by this algorithm can be computed as follows. There are a 1 1 passes through the for 1001)., each one requiring a comparison of rear: with a; . In addition] n. comparisons are needed for bookkeeping for the loop (comparison of'i. with in.? as i assumes the values 2, 3, ..., n + 1). Therefore 2n — l comparisons are needed altogether; which is O(n). 3. a) \Ve will try to write an algorithm sophisticated enough to avoid unnecessary checking. The answer——true or false— will be placed in a variable called zeros, the name of the procedure. (This approach is used by function subprograms in some computer languages although it is not always permitted to use the procedure name in an expression as we do here.) 114 Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices procedure Z€T08(01(12 . . i an : bit string) 1'. :2 l zeros :2 false {110 pair of zeros found yet} while 2'. < o and fizeros if as: 1 then i::t+1 else if oi+1 = 1 then 'i := i+ 2 else zeros :: true { zeros was set to true if and only if there were a pair of consecutive zeros] b) The number of comparisons depends on whether a pair of 0’s is found and also depends on the pattern of increments of the looping variable i. Without getting into the intricate details of exactly which is the worst case, we note that at worst there are approximately it. passes through the loop, each requiring one comparison of a,- with 1 (there may be two comparisons on some passes, but then there will be fewer passes). In addition, it bookkeeping comparisons of i. with n are needed {we are ignoring the testing of the logical variable zeros). Thus a total of approximately 2n. comparisons are used, which is 0(a). . a) and b). “is have a variable min to keep track of the minimum as well as a variable man: to keep track of the maximum. procedure smallest and iargest(a.1,a2, . . . ,an : integers) min :2 all min: :2 Ct] for i :2 2 to n. begin if ai < min then min. := a,- if a, > mass then mar: z: (1,- end { min. is the smallest integer among the input, and max is the largest} c) There are two comparisons for each iteration of the loop, and there are n r 1 iterations, so there are 277 i 2 comparisons in all. . We think of ourselves as Observers as some a1 "orithm for solvincr this iroblem is executed. We do not care 3 c: what the algorithm’s strategy is, but we view it along the following lines, in effect taking notes as to what is happening and what we know as it proceeds. Before any comparisons are done, there is a possibility that each element could be the maximum and a possibility that it. could be the minimum. This means that there are 21?. different possibilities, and 211— 2 of them have to be eliminated through comparisons of elements, since we need to find the unique maximum and the unique minimum. We classify comparisons of two elements as “nonvirgin” or “virgin," depending on whether or not both elements being compared have been in any previous comparison. A virgin comparison, between two elements that have not yet been involved in any comparison, eliminates the possibility that the larger one is the minimum and that the smaller one is the maximum; thus each virgin comparison eliminates two possibilities, but it clearly cannot do more. A nonvirgin comparison, one involving at least one element that has been compared before, must be between two elements that are still in the running to be the maximum or two elements that are still in the running to be the minimum, and at least one of these elements must not be in the running for the other category. For example, we might be comparing 3; and y, where all we know is that a; has been eliminated as the minimum. If we find that .1‘ > y in this case, then only one possibility has been ruled out we now know that y is not the maximum. Thus in the worst case, a nonvirgin comparison eliminates only one possibility. (The cases of other nonvirgin comparisons are similar.) Now there are at most [ii/2] comparisons of elements that have never been compared before, each removing two possibilities; they remove 2Ln/2j possibilities altogether. Therefore we need 2n — 2 — flirt/2] more comparisons that, as we have argued, can remove only one possibility each, in order to find the answers in the worst case, since 217 — 2 possibilities have to be eliminated. This gives us a total of 2n—‘2 — 2Ln/2] + [rt/2] comparisons in all. But 2-n.—2——2Ln./2j+Ln./2j = 2u—27Ln/2j : fan-2+ f~n/2l : f2n—n/2] —2 = [371/21—2, as desired, Supplementary Exercises 1 1 5 11. 13. 15. 17. 19. 21. 23. 25. 27. Note that this gives us a lower bound on the number of comparisons used in an algoritlnn to find the minimum and the maximum. On the other hand, Exercise 6 gave us an upper bound of the same size. Thus the algorithm in Exercise 6 is the most efficient algorithm possible for solving this problem. . After the comparison and possible exchange of adjacent elements on the first pass, from front to back, the list is 3,1,4,5,2,(5, where the 6 is known to be in its correct position. After the comparison and possible exchange of adjacent elements 011 the second pass, from back to front, the list. is 1,3,2, 4, E). (j, where the 6 and the 1 are known to be in their correct positions. After the next pass, the result is 1,2,3,4,5,U. One more pass finds nothing to exchange, and the algorithm terminates. There are possibly as many as n —- 1 passes through the list (or parts of ita-it depends on the particular way it is implemented), and each pass uses 0(a) comparisons. Thus there are Otnz) comparisons in all. Since log n < n, we have (niogn + 71.2)?“ g (n2 + :12)3 g (2112)"{ : 8n” for all n > (1. This proves that (nlog-n + 71.2)3 is 0(116), with witnesses C = 8 and k = 0. In the first factor the m2 term dominates the other term, since (logz)3 is 0(a) Therefore by Theorem ‘2 in Section 3.2, this term is O(;i32). Similarly, in the second factor, the 2“" term dominates. Tlms by Theorem 3 of Section 3.2, the product. is O(:i:22“‘). Let us look at the ratio a! n-(ni1)'(-n.i2)---3-2-1 n rial 11—2 3 2 1 5‘ 2-2-2..-2-2-2 2' 2 ' 2 "‘2'5'2' Each of the fractions in the final expressiou is greater than 1 except the last one, so the entire expression is at least (’ft/2)/2 : 11/4. Since iii/2"" increases without bound as n, increases, it! cannot be bounded by a constant times ‘2'” which tells us that n! is not 0 2” . 3 Obviously there are an infinite number of possible answers. The numbers congruent to :3 modulo 1? include 5, 22, 39, 56, as well as +12, ‘29, —46, From the hypothesis are E be (mod m) we know that ac ~ be 2 km for some integer iii. Divide both sides by c to obtain the equation a i b = (km.)/c. Now the leftrhand side is an integer, and so the right-hand side must be an integer as well. In other words, cl km. Letting d. 2 getl(m,e), we write (3 : dc. Then the way that c divides km. is that d] m, and e I it (sincc no factor of e divides fit/d). Thus our equation reduces to a — b = (k/e)(m/d), where both factors on the right are integers. By definition. this means that a E b (mod m/d). gcd(10223,33341) : gcd(10223,2672) = ged(2672, 2207) = gC(l(2207,465) : gcd(465,347) = gcd(347,118) : gedfllB, 111) = gcd{111, 7') = ged(7,b‘) 2 gcd(b‘, 1) = gcd(1,0) :1 By Lemma. 1 in Section 3.6, gcd(2-ri + 1,311+ 2) : gcdt2n + 1,17. + 1), since 271+ 1 goes once into 3n + 2 with a remainder of n + 1. Now if we divide it + 1 into 2?? + 1 , we get a remainder of n, so the answer must equal gerl(n. + 1,n). At this point, the remainder when dividing it into n. + 1 is 1, so the answer n'mst equal gcdfn, 1), which is clearly I . Thus the answer is 1. This problem is similar to Exercise 7 in Section 3.5. Without loss of generality, we may assume that the given integer is positive (since n, l u if and only if n Men), and the case a : U is trivial). Let the decimal expansion of the integer n be given by a : (melons-2 . . 41100)”). Thus a. = 1.0“"1n.,,-1 +10“_2e,,..2 ~1~ - . - + 100.1 + (10. Since 10 E 1 (mod 9), we have a E a.,,_1 +G,,_§g +- - - +a.1 +a0 (mod ,9). Therefore a :—" 0 (mod .9) if and only 116 Chapter 3 The Fundamentals: Algorithms, the Integers, and rlrlatrices if the sum of the digits is congruent to 0 (mod 9). Since being divisible by 9 is the same as being congruent to 0 (mod 9), we have proved that an integer is divisible by 9 if and only if the sum of its decimal digits is divisible by 91 29. It might be helpful to read the solution to Exercise 35 in Section 3.5 to see the philosophy behind this approach. Suppose by way of contradiction that 91, gig, q” are all the primes of the form 61:. + 5. Thus q1 = 5, Q2 : 11, and so on. Let Q : 6q1q2 - - - (1,, ~ 1. We note that Q is of the form 6k+5, where l: 2 (11qu - - - q” — 1. Now Q has a prime factorization Q = p1])2"'pg. Clearly no p,- is 2, 3, or any qj, because the remainder when (2 is divided by 2 is 1, by 3 is 2, and by q] is q} — 1. All odd primes other than 3 are of the form 6t: + 1 or 6k + 5, and the product of primes of the form 6!; + l is also of this form. Therefore at least one of the p,‘s must be of the form 6k + 5, a contradiction. 31. a) Since 2 is a factor of all three of these integers, this set is not mutually relatively prime. b) Since 1‘2 and 25 share no common factors, this set has greatest common divisor 1, so it is mutually relatively prime. (It is possible for every pair of integers in a set of mutually relatively prime integers to have a nontrivial common factor (see Exercise 32), but certainly if two of the integers in a set are relatively prime, then the set is automatically mutually relatively prime.) (3) Since 15 and 28 share no common factors, this set has greatest common divisor 1, so it is mutually relatively prime. (1) Since 21 and 32 share no common factors, this set has greatest common divisor 1, so it is mutually relatively prime. 33. a) \Ve need to find the inverse function. In other words, given (up + 5) mod 26, how does one recover p? “forking modulo 26, if we subtract 1), then we will have rip. If we then multiply by an inverse of rt (which must exist since we are assuming that gcd(e, 26) = 1), we will have p back. Therefore the decryption function is gfq) : E(q -— l2) mod 26, where E is an inverse of a modulo 26. b) Translating the letters into numbers, we see that the message is 11 9—12 10 6 12 6—l2 23 5 16 fl" 23—12fi22. By part. (a) the decryption function is g(q) = 15(q * 10) mod 26. (This required computing the inverse of 7' modulo 26, using the technique shown in the solution to Exercise 7 in Section 3.7; it is easy to sec that 7 . 15 = 105 E 1 (mod 26).) Applying this function, we obtain the decrypted message 15—11~4—Uw18 4 18 4—13 3 12 14 13*4 24. This translates into PLEAS ESEND MONEY, which, after correcting the spacing, is “please send money.“ 35. The least common multiple of 6 and 15 is 30, so the set of solutions will be given modulo 30 (see Exercise 36), Since the numbers involved here are so small, it is probably best simply to write down the solutions of : E 4 (mod 6) and then see which, if any, of them are also solutions of :r E 13 (mod 15). The solutions of the first congruence, up to 30, are 4, 10, 16, 22, and 28. Only 28 is congruent to 13 modulo 15. Therefore the general solution is all numbers congruent to 28 modulo 30, i.e., . . . , —32, —2, 28, 58, 37. we give a proof by contradiction. For this proof we need a fact about polynomials, namely that a nonconstant polynomial can take on the same value only a finite number of times. (Think about its graph—a polynomial of degree n, has at most a “wiggles" and so a horizontal line can intersect it at most n times. Alternatively, this statement follows from the Fundamental Theorem of Algebra, which guarantees that a polynomial of degree n. has at most a 0‘5; if f(:r:) : a, then 3: is a zero of the polynomial fffl'i) — a.) Our given polynomial f can take on the values 0 and :|:1 only finitely many times, so if there is not some 1; such that fly) is compOsite, then there must be some :L'O such that d:f(;rr0) is prime, say p. Now look at f(.7:0 + lap). When we plug :30 + lap in for as in the polynomial and multiply it out, every term will contain a factor of p except for the terms that form ffflfl'g). Therefore flare + kp) = f(:I:O) + mp = (m :t 1);) for some integer in. As it varies, Supplementary Exercises 1 1 7 this value can be 0 or p or —p only finitely many times; therefore it must be a different multiple of p and therefore a composite number for some values of A", and our proof is complete. 39. Assume that every even integer greater than 2 is the sum of two primes, and let n be an integer greater than 5. If n. is odd, then we can write 1:. 2 3 + (n — 3), decompose n — 3 2 p + q into the sum of two primes (since n — 3 is an even integer greater than 2), and therefore have written a 2 3 + p + q as the sum of three primes. If n is even, then we can write 11 2 2 + (n 2 2), decompose n 2 2 2 p + q into the sum of two primes (since 72. 2 2 is an even integer greater than 2), and therefore have written a 2 2 + p + q as the sum of three primes. For the converse, assume that every integer greater than 5 is the sum of three primes, and let n. be an even integer greater than 2. By our assumption we can write 7?. + 2 as the sum of three primes. Since n. + 2 is even, these three primes cannot all be odd, so we have n. + 2 2 2 + p + q, where p and q are primes, whence it 2 p + q, as desired. 41. Let us begin by computing A" for the first few values of n. 1_012_210 3go—1 hie Ail—1 olA-lo ale-l1 olA-lo 1i- Sinee A“1 2 I, the pattern will repeat from here: A5 2 A4A 2 IA 2 A. A6 2 A2, A7 2 A3, and so on. Thus for all n 2 0 we have 0 1 —l 0 0 21 . 1 0 «in-r1 _ 4n+2 _ 4114-3 _ Ila-+4 _ A -l—1olA ~lo—1laA -l1 olA inl- 43. (The notation CI means the identity matrix I with each entry multiplied by the real number (3; thus this U. 'U m a; what these entries have to be by using the fact that AB 2 BA for a few judiciously chosen matrices B. First matrix consists of c’s along the main diagonal and 0’s elsewhere.) Let A 2 [ J . We will determine let B 2 0 1 . Then AB 2 0 n , and BA 2 w a:- . Since these two must be equal, we know that 0 0 0 u: 0 O O 2 w and u 21. Next choose B 2 [(1) 3] . Then we get [2 g] 2 {g g] , whence 1.: 2 0. Therefore the 'U 0 0 y] , which is just Tl. times the identity matrix, as desired. matrix A must be in the form [ 45. In an ax a upper-triangular matrix, all entries (1;; j are zero unless '1' g j. Therefore we can store such matrices in about half the space that would he required to store an ordinary n X 12. matrix. In implementing something like Algorithm 1 in Section 3.8, then, we need only do the computations for those values of the indices that can produce nonzero entries. The following algorithm does this. We follow the usual notation: A 2 [aw] and B = lbijl . procedure triangular matrix m-ult-iplieaiion(A, B : upper—triangular matrices) for i' :2 1 to n for j :2 17 to it {since we want 3' 2 i} begin czj :2 0 for k :2 i to j {the only relevant part} 0,3 I: Ci} + agkaA-j end {the upper—triangular matrix C 2 {653'} is the product of A and B } 47. We simply need to show that the alleged inverse of AB has the correct defining property2that its product with AB (on either side) is the identity. Thus we compute (AB)(B“A‘1) = A(BB‘-‘)A'1 = AIA“ = AA“ = I, 118 49. Chapter 3 The Fundamentals: Algorithms. the Integers, and Matrices and similarly (B‘IA‘IMAB) : I. Therefore (AB)’1 : B‘lA‘l. (Note that the indicated matrix multi— plications were all defined, since the hypotheses implied that both A and B were n x n. matrices for some (and the same) a.) a) The (i,j)th entry of A G 0 is by definition the Boolean sum (V) of some Boolean product...
View Full Document

{[ snackBarMessage ]}