Chapter 1 Preliminaries: Answers to Exercises 1. The required algorithm is shown below: procedure M ULTIPLY(m, n) p←0 while n > 0 do p←p+m n←n−1 6: return p 1: 2: 3: 4: 5: 2. The answers are given below: a. ⌊9.47⌋ = 9 b. ⌊−9.47⌋ = −10 c. ⌊75⌋ = 75 d. ⌊−227.9⌋ = −228 e. ⌊24.5 + 35.5⌋ = 60 f. ⌊24.5⌋ + ⌊35.5⌋ = 59 3. Compute each of the following: a. b. c. d. e. f. ⌈9.47⌉ = 10 ⌈−9.47⌉ = −9 ⌈75⌉ = 75 ⌈−227.9⌉ = −227 ⌈29.5 + 35.5⌉ = 65 ⌈29.5⌉ + ⌈35.5⌉ = 66 4. If n is an odd integer, show that n2 4 = n−1 2 1 n+1 2 . 2 CHAPTER 1. PRELIMINARIES: ANSWERS TO EXERCISES [Hint: Let n = 2k − 1] 1 n2 = k 2 − k + , which yields With n = 2k − 1, we have n2 = 4k 2 − 4k + 1. Therefore, 4 4 2 n n+1 n−1 2 = k − k = k(k − 1). But, n = 2k − 1 yields k = and k − 1 = , from 4 2 2 which we conclude that 2 n n−1 n+1 = . 4 2 2 5. If n is an odd integer, show that n2 4 = n2 + 3 . 4 [Hint: Let n = 2k − 1] n2 1 With n = 2k − 1, we have we have n2 = 4k 2 − 4k + 1. Therefore, = k 2 − k + , which 4 4 2 2 n n + 3 1 3 yields . = k2 − k + 1 = k2 − k + + = 4 4 4 4 6. Compute each of the following: [You may use a calculator] a. 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 = 5040 b. 8! + 3! = 8 · 7 · 5 · 4 · 3 · 2 · 1 + 3 · 2 · 1 = 40320 + 6 = 40326 c. (4 + 3)! = 7! = 5040 7. Compute each of the following: 11! = 11 · 10 · 9 · 8 · 7 · 6 · 5 · 4 · 3 = 6652800 3! 9! 9·8·7·6 b. = = 126 4!5! 4·3·2·1 11 · 10 · 9 990 11! = = = 165 c. (11 − 8)!8! 3·2·1 6 a. 8. Simplify each of the following expressions (your final answer should only contain positive exponents): x−1 y 4 y6 a. 5 −2 = 6 xy x 20 −1/2 (x ) 1 b. = 14 4 x x 9. Compute each of the following: [Do not use a calculator] a. log2 128 = log2 = 27 = 7 b. log2 4096 = log2 212 = 12 10. Compute each of the following: [Do not use a calculator] a. log3 243 = log3 35 = 5 3 b. log5 15625 = log5 56 = 6 c. log10 1000000 = log10 106 = 6 11. Compute each of the following: [Do not use a calculator] a. ⌊log2 75⌋ Answer: 26 < 75 < 27 ⇒ ⌊log2 75⌋ = 6 b. ⌈log3 89⌉ Answer: 34 < 89 < 35 ⇒ ⌈log3 89⌉ = 5 c. ⌊log4 500⌋ Answer: 44 < 500 < 45 ⇒ ⌊log4 500⌋ = 4 12. Compute each of the following: [Do not use a calculator] a. ⌈log10 65536⌉ Answer: 105 < 65536 < 106 ⇒ ⌈log10 65536⌉ = 6 b. ⌈log3 225⌉ Answer: 34 < 225 < 35 ⇒ ⌈log3 225⌉ = 5 c. ⌈log4 3500⌉ Answer: 45 < 3500 < 46 ⇒ ⌈log4 3500⌉ = 6 13. Simplify each of the following expressions. [Do not use a calculator] 2 a. 2log2 4 = 2log2 2 = 22 log2 2 = 22 =4 30 30 b. log2 30 − log2 5 − log2 3 = log2 = log2 = log2 2 = 1 5·3 15 14. Determine mathematically the final value of the variable count in each of the following systems of loops. Assume that n has been initialized in a calling procedure appropriately. Do not use any asymptotic notations. Express your answer in terms of n. a. count = 0 for i = 1 to n do for j = n downto i do count = count + 1 end for end for Answer: The desired answer is obtained by simplifying the following summation: 2 2 n X n n n n n X X X X X n +n n +n 2 +n = 1= (n−i+1) = n− i+ 1=n − 2 2 i=1 j=i i=1 i=1 i=1 i=1 Another way to simplify: n X n X i=1 j=i 1= n X (n − i + 1) = i=1 which is obtained by letting k = n − i + 1. count = 0 b. for (i = 1; i < n; i = 2*i) count = count + 1 for j = i+10 to i+17 do count = count + 1 n X k=1 k= n2 + n 2 4 CHAPTER 1. PRELIMINARIES: ANSWERS TO EXERCISES The desired answer is obtained by first observing that the i-loop is executed for values of i = 1, 2, 22 , · · · , 2k as long as 2k is smaller than n. Therefore, we obtain the desired answer by simplifying the following summation: X i+17 X 1+ 1 j=i+10 1≤2k <n k=⌊log2 n⌋ ! = X (1 + 8) = 9 1≤2k <n X 1≤2k <n 1=9 X 1 = 9(⌊log2 n⌋ + 1) k=0 15. For each pair of functions given below, indicate which function, f (n), or g(n) grows asymptotically faster. √ n+1 Answer: f (n) grows faster n+2 b. f (n) = n1.59 , g(n) = n2 Answer: g(n) grows faster c. f (n) = n3 log n + n2 , g(n) = n4 Answer: g(n) grows faster a. f (n) = n, g(n) = 16. Rewrite the following functions in the order of increasing growth rate from left to right. 0.01n2 log n, 2n3 , 0.0000001n!, 2000n log n, 10100 , 3n , 50n2 + n5 . The rearranged list is shown below: 10100 , 2000n log n, 0.01n2 log n, 2n3 , 50n2 + n5 , 3n , 0.0000001n!.