3.review (evens)

# 3.review (evens) - Supplementary Exercises 87 SUPPLEMENTARY...

This preview shows pages 1–6. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Supplementary Exercises 87 SUPPLEMENTARY EXERCISES FOR CHAPTER 3 2. a) We need to keep track of the ﬁrst and second largest elements as we go along. updating as we look at the elements in the list. procedure lopi-wo(a1,a2. . . . ,0," : integers) [repeat 1: (:1 second :: —eo for 2' r: 2 to n begin if a. > second then second :: aI if a, > largest then begin second : largest : largest, (1i end end b) The loop is executed n — 1 times, and there are 2 comparisons per iteration. Therefore (ignoring book- keeping) there are 211 * ‘2 comparisons. 4. a) Since the list is in order, all the occurrences appear consecutively. Thus the output of our algorithm will be a pair of numbers, ﬁrst. and test, which give the ﬁrst location and the last. location of occurrences of :r, respectiver All the numbers between ﬁrst and last are also locations of appearances of Jr. If there are no appearances of :13, we set ﬁrst, equal to 0 to indicate this fact. procedure ﬂ-U(I,ﬂ1,(t2, . . . .an : integers, with a] 2 a2 2 r H 2 an) 7': 1 while 7'. S n. and e-,_ < :1: i 2: i + 1 if 'i. : 11 +1 then ﬁrst :: 0 else if a.- > .r then ﬁrst 2: 0 else begin ﬁrst :2 i, i := i+ 1 while i g n and (2,; 2 cc 1' :2 i + 1 [est :: 'i. — 1 end b) The number of comparisons depends on the data. Roughly speaking, in the worst case we have to go all the way through the list. This requires that a: be compared with each of the elements, a total of n. comparisons (not including bookkeeping). The situation is really a bit more complicated than this, but in any case the answer is O(n). 6. a) We follow the instructions given. If n is odd then we start the loop at ’i 2 2, and if n is even then we start the loop at i = 3. Within the loop, we compare the next two elements to see which is larger and which is smaller. The larger is possiny the new maximum, and the smaller is possibly the new minimum. b) procedure clever smallest and lergesﬁal, 02, . . . 41.” : integers) if n. is odd then begin min. := (11 THO-LE :2 a; end else if 01 < a; then 88 10. Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices begin min := (11 mar :2 0.2 end else begin min :2 02 mars .': 01 end if n. is odd then 17:: 2 else i:: 3 while 'i <11 begin if (Ii < (1H,]. then begin smaller :: a,- blgger :2 (n+1 end else begin smaller := {n+1 bigger :: a,- end if smaller < min then min. :: smaller if bigger > mm; then mam :: bigger i z: 1' + 2 end { min is the smallest integer among the input, and mom is the largest} c) If n is even, then pairs of elements are compared {ﬁrst with second, third with fourth, and so on), which accounts for “ll/2 comparisons, and there are an additional 2((n/2) — 1) : n. 7 2 comparisons to determine whether to update min and 11103:. This gives a total of (3a. A 4)/2 comparisons. If n is odd, then there are (n. e 1]/2 pairs to compare and 2((71 — ])/2) : n — 1 comparisons for the updates, for a total of (372 — 3)/2. Note that in either case, this total is [Ba/2i — 2 (see Exercise 7). . The naivc approach would be. to keep track of the largest element found so far and the second largest element found so far. Each new element is compared against the largest, and if it is smaller also compared against the second largest, and the “best-so-far” values are updated if necessary. This would require about 211 comparisons in all. we can do it more efﬁcientlyr by taking Exercise 6 as a hint. If n. is odd, set l to be the ﬁrst element in the list, and set 3 to be —00. If n is even, set l to be the larger of the ﬁrst two elements and s to be the smaller. At each stage, l will be the largest clement seen so far, and s the second largest. Now consider the remaining elements two by two. Compare them and set a to be the larger and b the smaller. Compare a with l. If a > l, then a, will be the new largest element seen so far, and thc second largest element will be either l or ('9; compare them to ﬁnd out which. If a < l, then l is still the largest element, and we can compare a and s to determine the second largest. Thus it takes only three comparisons for every pair of elements, rather than the four needed with the naive approach. The counting of comparisons is exactly the same as in ,Exercise 6: [311/21 * 2. We start with the solution to Exercise 37 in Section 3.1 and modif)r it to alternately examine the list from the front and from the back. The variables front and back will show what portion of the list still needs work. (After the lat“ pass from front to back, we know that the ﬁnal ls elements are in their correct positions, and alter the it” pass from back to front, we know that the ﬁrst is elements are in their correct positions.) The outer if statement takes care of changing directions each pass. Snppiementer}r Exercises 12. 14. 16. 18. 20. 22. 89 procedure shake-rer rt(a1, . . . , on) front :: 1 back :: n stilLi-ntei'changing 1: true while front. < back and stilLintercimnging if n + back + front is odd then begin {process from front to back} .stiiLinierenaming :2 false for j :: front to back e 1 if aj > 0.3:“ then begin sit/Linterchanging :: true interchange (r,- and of,“ end bac”. :2 back i 1 end else {process from back to front} begin stitLinte-rchangéng :: false for j 2: back down to front + 1 if 0.3;} > aj then begin stilL-interehanging :: true interchange aj_1 and aj end front := front + 1 end {a}, . . . , an is in nondecreasing order} Lists that are already in close to the correct order will have few items out of place. One pass through the shaker sort will then have a good chance of moving these items to their correct positions. If We are lucky, significantly fewer than n — 1 passes through the list will be needed. Since 8:33 + 12:5 + 100 log :L' g SIB + 12.1:3 + 100213 2 120553 for all :r > 1, the conclusion follows by deﬁnition. This is a sum of n things, each of which is no larger than 217.2. Therefore the sum is 0(2n3), or more simply, 0(n3). This is the “best” possible answer. Let us look at the ratio nn/nl. we can write this as n n n n. n. n — 1 n — 2 2 Each factor is greater than or equal to 1, and the last factor is it. Therefore the ratio is greater than or equal T1. 1. to n. In particular, it cannot be bounded above by a constant C. Therefore the defining condition for n.” being 0(11!) cannot be met. . 1 Let q I — and r : a—dq. Then we have forced a. : dq+r, so it remains to prove that *d/Q < 7' S d/2. . a 1 . . . d (I Now since (1— l < H — 5 E q, we have (by multiplying through by d and adding d/Q) dq — 5 < a S dq + 5, cl (1‘? so —E < a —dq S E, as desired. There is one 0 at the end of this number for every factor of 2 in all of the numbers from 1 to 100. We count them as follows. All the even numbers have a factor of 2 , and there are 100/2 : 50 of these. All the multiples of 4 have another factor of 2, and there are 100/4 : 25 of these. All the multiples of 8 have another factor of 2, and there are [100/8] 2 12 of these, and so on. Thus the answer is 50 + 25 + 12 + 6 + 3 + 1 = 97. 90 24. 26. 28. 30. 32. 34. Chapter 3 The Fundamentals: Algorithms, the Integers, and rMatrices We need to divide successively by 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, and 1, a total of 12 divisions. a) The ﬁrst statement is clear. For the second, if a, and b are both even, then certainly 2 is a factor of their greatest common divisor, and the complementary factor must be the greatest common divisor of the numbers obtained by dividing out this 2. For the third statement, if a is even and b is odd, then the factor of 2 in a. will not appear in the greatest common divisor, so we can ignore it. Finally, the last statement follows from Lemma 1 in Section 3.5, taking q = 1 (despite the notation, nothing in Lemma 1 required 0 to be the quotient). b) All the steps involved in implementing part (a) as an algorithm require only comparisons, subtractions, and divisions of even numbers by 2. Since division. by 2 is a shift of one bit to the right, only the operations mentioned here are used. (Note that the algorithm needs two more reductions: if a. is odd and l) is even, then gcd(a,b) : gcd(a,b/2), and if a < b, then interchange a and b.) c) We show the operation of the algorithm as a string of equalities; each equation is one step. gcd(1202, 4848) : gcd(4848, 1202) : 2gcd(2424, 601) = 2gcd(1212, 601) : 2gcd(606,601) = 2 gcd(303, 601) 2 2 gcd(601, 303) = 2 gcd(298, 303) 2 2 gcd(303, 298) : 2gcd(303,149) : 2gcd(15r-l,149) = 2gcd(77,149) : 2gcd(149, 77) = 2gcd(72, 77) : 2gcd(77, 72) : 2gcd(77, 36) = 2gcd(77, 18) = 2gcd(77,9) 2 2gcd(68,9) : 2gcd(34, 9) : 2gcd(17, 9) : 2gcd(8,9) : 2ged(9,8) : 2gcd(9, 4) : 2gcd(9, 2) = 2gcd(9, 1) : 2gcd(8, 1) :— 2gcd(4, 1) : 2gcd(2, 1) : 2gcd(1, 1) : 2 We can give a nice proof by contraposition here, by showing that if n is not prime, then the sum of its divisors is not n, + 1. If n = 1, then the sum of the divisors is 1 % 1 + 1. Otherwise n is composite, so can be written as n : ab, where both a and b are divisors of n different from 1 and from 71 There are two cases. (although it might happen that a = b). Then n has at least the three distinct divisors 1, a, and n, and their sum is clearly not equal to n + 1. This completes the proof by contraposition. One should also observe that the converse of this statement is also true: if n. is prime, then the sum of its divisors is n + 1 (since its Only divisors are 1 and itself). a) Each week consists of seven days. Therefore to find how many (whole) weeks there are in n days, we need to see how many 7’s there are in 71. That is exactly what It div 7 tells us. b) Each day consists of 24 hours. Therefore to ﬁnd how many (whole) days there are in n hours, we need to see how many 24’s there are in 71. That is exactly what n. div 24 tells us. We need to arrange that every pair of the four numbers has a factor in common. There are six such pairs, so let us use the ﬁrst six prime numbers as the common factors. Call the numbers a, b, c, and d. We will give a and b a common factor of 2; a and c a common factor of 3; e and d a common factor of 5; b and c a common factor of 7; b and d a common factor of 11; and c and d a common factor of 13. The simplest. way to accomplish this is to let a=2‘3-5 :30; b: 2-7-11 :154; c: 3-7-13 :273; and d : 5-11-13: 715. The numbers are mutually relatively prime, since no number is a factor of all of them (indeed, each prime is a factor of only two of them). Many other examples are possible, of course. 1f cc E 3 (mod 9), then at : 3 + St for some integer t. In particular this equation tells us that. 3 j On the other hand the ﬁrst congruence says that .7: : 2 + 63 = 2 + 3- (23) for some integer s, which implies that the remainder when 3; is divided by 3 is 2. Obviously these two conclusions are inconsistent, so there is no simultaneous solution to the two congruences. Supplementary Exercises 91 36. 38. 40. 42. 44. a) There are two things to prove here. First suppose that gcd(m1,mg) lei Eng; say a] —e.2 = k-gcd(ml,m2). By Theorem 1 in Section 3.7 there are integers s and t such that gcd(m1, T112) : sml +tmg. Multiplying both Sides by lat and substituting into our ﬁrst equation we have 01 — (IQ = its-m] —l— ktmg, which can be rewritten as 01 * L‘s-ml = a2 + ktmg. This common value is clearly congruent to (11 modulo rm and congruent to (2-2 modulo 1112, so it is a solution to the given system. Conversely, suppose that there is a solution a: to the system. Then .1: : (11 —l— 37111 = a2 + £7112 for some integers s and t. This says that a] — (1-3 : U712 — sml. But gcd(m1,mg) divides both 1711 and 7112 and therefore divides the right-hand side of this last equation. Therefore it also divides the left-hand side, a] — 0,2, as desired. b) We follow the idea sketched in Exercises 23 and 24 of Section 3.7. First we show that if a E (1 (mod on) and u E b (mod 7112), then a E (1 (mod lcn'1(-m.1,m2)}. The first hypothesis says that m1 la 7 b; the second says that rug | a. E b. Therefore a — b is a common multiple of m; and mg. If a e b were not also a multiple of lcm(m1, m2), then (a —b) mod lcm(m1, 1722) would be a common multiple as well, contradicting the definition of iCITl(TTI.1,m.2). Therefore a. — b is a multiple of lc1’n(ml,mg), i.e., a E b (mod lcm(m1,m2)). Now suppose that there were two solutions to the given system of congruem-es. By what we have just proved, since these two solutions are congruent modulo ml (since they are both congruent to (11) and congruent modulo n12 (since they are both congruent to 02), they must be congruent to each other modulo lcrr1(m1,m.2_). That is precisely what we wanted to prove. First note that since both a and i) must be greater than 1, the sequences Lira] and Llrbj do not list any positive integer twice. The issue is whether any positive integer is listed in both sequences, 01' whether some positive integer is omitted altogether. Let N(m, n) denote the number of positive integers in the set { Lire] | i." is a positive integer} that are less than or equal to n . Then it is enough to prove that N(a, n) +N(b, n.) : n for all positive integers in. (That way no positive integer could be left out or appear twice 'When we consider all the numbers [he] and MM.) Now N(o,n) is the number of positive integers t‘ for which [he] 3 n, which is just the number of positive integers in for which ice. < n. —l— 1, since a is irrational, and this is clearly [01+ 1)/aj. We have a similar result for I). Let denote the fractional part of 3: (Le, f(.'E) z a; 7 [ml ). Then we have n -l— l n + 1 J7 b _ f ( b ) ' -l- 1 n + 1 -l— 1 n. —l— 1 N(a,n) +N(b,n) : in J + i J : Ti _ ‘f( ) But the sum of the ﬁrst and third terms of the rightehand side here is n —l— 1, since we are given that a b (r r) (l/a) + (1/1)) : 1. The second and fourth terms are each fractions strictly between 0 and 1, and the entire expression is an integer, so they must sum to 1. Therefore. the diaplayed value is 11+ 1 — l 2 u, as desired. if there were integer solutions to this equation, then by deﬁnition we would have 2:2 E (mod 5). However we easily compute (as in Exercise 2:1 in Section 3.4) that the square of an integer of the form 5!: is congruent to U modulo 5; the square of an integer of the form 51: + 'l is congruent to 1 modulo 5; the square of an integer of the form 5t". + 2 is congruent to 4 modulo 5; the square of an integer of the form 5!: + 3 is congr'Uent to 4 modulo 5; and the square of an integer of the form 5k+cl is congruent to 1 modulo 5. This is a contradiction, so no solutions exist. Since A is the matrix deﬁned by (1,,- : c and 0,3,- : 0 for 1'. 75 3', it is easy to see from the deﬁnition of multiplication that AB and BA are both the same as B except that every entry has been multiplied by c. Therefore these two matrices are equal. \Vejust use Algorithm 1 in Section 3.8, where A and B are now n x at upper triangular matrices, by replacing m. by n in line 1, and having q iterate only from 1" to j, rather than from 1 to k. (Actually even more efﬁciency is possible if we store only the nonzero portion 01' the matricesE-see solution to Exercise 45.) 92 46. 48. 50. 52. Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices See the solution to Exercise 45. Looking at the nested loops, we see that the number of multiplications is given by the following expression: [1 + 2 + A - ' + 711+ [1 + 2 + - - - (n ~ 1)] + - - - + To simplify this, we need some results from Section 4.1, namely Example 1, which says that the sum of the ﬁrst is positive integers is Mk + l)/2, and Exercise 15, which says that the sum 1.2 + 2 - 3 + 3 - 4 + ~-- +12“: +1): (:0: +1)(l€ +1)/3. Doing the algebra, we obtain um + 1)(n. + 2)/6 as the total number of multiplications. There are ﬁve ways to parenthesize the calculation ABCD, namely as (AB)(CD), as ((AB)C)D, as (A(BC))D, as A((BC)D), and as A(B(CD)). We can compute the number of multiplications for each and compare. These numbers are (in thousands) 108, 117, 80, 44, and 81. Thus the most efﬁcient method is the fourth, using 44,000 multiplications. The greedy algorithm in this case will produce the base e expansion for the number of cents required (except “+1 , the ck coins must be used rather than nonexistent cl coins for that for amounts greater than or equal to c :i. > k). Since such expansions are unique if each digit (other than the digit in the 0‘“ place) is less than c, the only other ways to make change would involve using c or more coins of a. given denomination, and this would obviously not be minimal, since 6 coins of denomination ci could be replaced by one (2011’: of dei'iornination 1 63+ . As we see from Exercise 51, at most 1?. questions (guesses) are needed. Furthermore, at least this many yes/no questions are needed as well, since if we asked fewer questions, then by the pigeonhole principle, two numbers would produce the same set of answers and we would be unable to guess the number accurately. Thus the complexity is n questions. (The case n = 0 is not included, since in that case no questions are needed.) We are assuming throughout this exercise and the previous one that the inclusive sense of “between” was intended. ...
View Full Document

## This note was uploaded on 02/18/2012 for the course MATH 640:244 taught by Professor Ming during the Spring '09 term at Rutgers.

### Page1 / 6

3.review (evens) - Supplementary Exercises 87 SUPPLEMENTARY...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online