COT 3100 Spring 2010 Midterm 2 For the first two questions #1 and #2, do ONLY ONE of them. If you do both, only question #1 will be graded. 1. (20 pts) Give iterative and recursive algorithms for finding the n th term of the sequence {a n defined by a 0 = 2, a 1 = 2 and a n+1 = a n (a n-1 ) 2. Which algorithm is more efficient and why? : + Input: n, a 0 = 2, a 1 = 2, a n+1 = a n (a n-1 ) 2 ; + Output: The n th term of the sequence {a n. Iterative algorithm: for (int i = 2; i <=n; i++) { result = a 1 * a 0 * a 0 ; a 0 = a 1 ; a 1 = result; return result; Recursive algorithm: int a(int k) { if (k <= 1 ) return 2; else return a(k-1)*a(k-2)*a(k-2); return result = a(n); Which algorithm is more efficient and why? Iterative algorithm performs (n-2) iterations and in each iteration, updates values of 3 variables, thus its time complexity is O(n). The recursive algorithm, in order to find a(n), loops into itself 3 times to find a(n-1) and two a(n-2). To find a(n-1), it again loops into itself 3 times to find a(n-2) and two a(n-2), so on and so forth. Thus, its creates a complete 3-leaf tree with height n. Thus, the total time complexity is O(3 n ). Therefore, the iterative algorithm is more efficient. 1
2. (20 pts) Give iterative and recursive algorithms for determining whether a string is a palindrome or not. (Note: A string is a palindrome if it can be read the same way in either direction, e.g., ABBA, madam, level ). + Input: A string w of length n; + Output: YES if w is a palindrome, NO otherwise. Iterative algorithm w is a palindrome iff (w[i] == w[n+1-i]) for any i th index from 1 to n. Therefore, we test this property on every i th position and stop when the index i hits the middle of the string w. int middle = n/2; for(int i = 1; i<=middle; i++) { if ( w[i]!= w[n+1-i] ) return NO; return YES; Recursive algorithm A string w of length k is a palindrome iff (w[1] == w[k]) and the substring w[2...k-1] is also a palindrome. This observation suggests the following recursive algorithm boolean ispal(w, k) { if (k==0 k==1) return YES; return (w[1] == w[k]) && ispal(w[2...k-1], k-2); 2
3. (10 pts) Prove by induction that (x - y) divides x n - y n for any integer n 1. + Let P(n) be the statement: (x - y) divides x n - y n. + Basis step: n = 1. It is clear that (x - y) divides x 1 - y 1 (= x - y). + Inductive step: - Assume P(n) is true. We will prove that P(n+1) is also true, i.e., (x-y) divides x n+1 - y n+1. - Now: x n+1 - y n+1 = x n+1 - xy n + xy n - y n+1 = x(x n - y n ) + y n (x - y) - Since P(n) is true, we have (x - y) divides x n - y n, which implies (x-y) divides x(x n - y n ). In addition, (x - y) also divides y n (x - y). Therefore, (x - y) divides x(x n - y n ) + y n (x - y), which means P(n+1) is also true. 4. (20 pts) Let S be the subset of the set of ordered pairs of integers defined recursively by: Basis step: (0,0) S. Recursive step: If (a,b) S then (a+4, b+5) S and (a-2, b-7) S. a. Prove by strong induction on the number of applications of recursive steps that a b is even for any (a,b) S. Let n be the number of applications of recursive steps. We will show that for any (a,b) S, a is even which implies a b is even for any (a,b) S Basis step n = 0. It is clear that 0 is even. Thus the basis step is true. Inductive step + Assume that for all 0 < n N, a is even for any (a,b) S. We will show that a' is also an even number for any (a',b') generated in step (N+1). + Consider the two following cases. Case #1: a' is derived by (a+4) for some number a in step N and based on the assumption, we have a is even. Thus a' = a + 4 is also even. Case #2: a' is derived by (a-2) for some number a in step N and based on the assumption, we have a is even. Thus a' = a - 2 is also even. The inductive step is complete. a. Prove by structural induction that 9 a+ b for any (a,b) S Basis step It is clear that 9 0 + 0. The basis step is true. Inductive step + We will prove that, in Recursive Step, any (a', b') obtained from (a,b) S satisfies: 9 (a' + b'). Since (a', b') is derived from (a,b), it must be obtained by either (a+4,b+5) or (a-2,b-7). Thus, (a' + b') equals either (a+b+9) or (a+b-9) (*). + Now, because (a,b) is previously in S, we have 9 (a + b) which implies 9 divides both (a+b+9) and (a+b-9). This fact also implies that 9 (a' + b') due to (*). 3
5. (10 pts) Give the recursive definition of the sequence {a n, n = 1,2,3, if a. a n = 4n - 9 a 1 = -5; a n+1 = 4(n+1) - 9 = 4n - 9 + 4 = a n + 4; b. a n = n 2 2n a 1 = -1; a n+1 = (n+1) 2-2(n+1) = n 2 + 2n + 1-2n - 2 = n 2-2n + 2n - 1 = a n + 2n - 1 For questions #6 and #7, please justify your answers. Answers with only numbers will result in no point. 6. (10 pts) How many positive integers from 1000 to 9999 inclusive are divisible by 9 but not by 4? "A number divisible by 9 but not by 4" means a number divisible by 9 but not 36(=4*9). Therefore, we just need to find the number of integers divisible by 9 and subtract the number of those divisible by 36. + Number of integers divisible by 9 + Number of integers divisible by 36 + Thus, the answer is 1000-250 = 750. 7. a. (10 pts) A coin is flipped 20 times where each flip comes up either heads or tails. How many possible outcomes contain at most 5 tails? + Number of outcomes contain 0 tails: C(20,0) + Number of outcomes contain 1 tails: C(20,1) + Number of outcomes contain 2 tails: C(20,2) + Number of outcomes contain 3 tails: C(20,3) + Number of outcomes contain 4 tails: C(20,4) + Number of outcomes contain 5 tails: C(20,5) Total possible outcomes = C(20,0) + C(20,1) + C(20,2) + C(20,3) + C(20,4) + C(20,5) 4
b. (10 pts) What is the least number of people needed to form a group in which we can surely conclude that there are at least 3 people having the same birthday? Assume that there are 365 days in a year. Based on Pigeonhole Principle, the least number of people we need to choose is one more than the number of days in (3-1) years. Answer = 365*(3-1) + 1 8. (10 pts) Find the error in this "proof" of the clearly false claim: "Every set of lines in the plane, no two of which are parallel, meet in a common point". "Proof": Let P(n) be the statement that "Every set of n lines in the plane, no two of which are parallel, meet in a common point". We will try to prove that P(n) is true for n 2. 1. Basis step: P(2) is true due to the definition of parallel lines. 2. Inductive step: Assume that P(k) is true, i.e., every set of k lines in the plane, no two of which are parallel, meet in a common point. We will try to prove that P(k+1) is also true. 3. Consider the set S of (k+1) distinct lines in the plane, no two of which are parallel. Since P(k) is true, the first k lines of S must meet in a point p 1, and the last k lines of S must meet in a point p 2. 4. If p 1 is different from p 2, then all lines containing both of them must be exactly the same since two distinct points determine a line. This implies the second line, the third line,..., and the k th lines of S are the same, contradicting to the fact that they are all distinct. 5. Therefore, p 1 and p 2 must be the same point, i.e., (k+1) distinct lines in the plane, no two of which are parallel, meet in a common point, which means P(k+1) is true. The problem is at step #4. We cannot show that P(2) implies P(3). When k = 2, the first two lines must meet in a common point p1 and the last two lines must meet in a common point p2. But in this case, p 1 and p 2 do not have to be the same, because only the second line is common to both sets of lines. 5
Bonus questions: 1. (5pts) Show that is Θ( ) for any positive integer k > 1. Choose c 1 = and c 2 =, we have = Thus, = Θ( ) c 1 c 2. 2. (5 pts) Prove that if five points are selected from the interior of a 1 1 square, then there are two points whose distance is less than. We first divide the square into 4 smaller squares (by connecting the corresponding middle points) and then select 5 points from the interior of the "big" square. By Pigeonhole Principle, there must be a "small" square containing at least 2 points. In addition, the maximum distance in this "small" square cannot exceed the length of the diagonal, which is. Thus, any two points inside this "small" square has distance less than. 6