MAT 24 Test 2 SOLUTIONS, FORM A 1. [1 points] Prove the following using Mathematical Induction. L 2 i = L n L n+1 + 2 where L is the Lucas sequence: L 0 = 2 L 1 = 1 L n = L n 1 + L n 2, n 2 Solution: Let P (n) denote the predicate L 2 i = L n L n+1 + 2 (not just the right-hand side). First, P (0) is true, because the left-hand side is L 0 2 = 2 2 = 4 and the right-hand side is L 0 L 1 + 2 = 2 1 + 2 = 4. Now, assume that P (k) is true, that is The goal is to prove P (k + 1), which is This is true, because k+1 L 2 i = k L 2 i = L k L k+1 + 2. k+1 L 2 i = L k+1 L k+2 + 2. k L 2 i + L 2 k+1 = (L k L k+1 + 2) + L 2 k+1 = L k+1 (L k + L k+1 ) + 2 = L k+1 L k+2 + 2. Hence, by the Principle of Mathematical Induction, L 2 i = L n L n+1 + 2 is true for all n. Grading: + points for the base case, + points for setting up the inductive case, + points for proving the inductive case. 2. [10 points] Find a simple function g(x) such that the following function is O(g(x)). x ( 12 2 {{ x + 2 {{ 4 ) 2 x 4 x (8 x x + {{ 2 ) x 2 {{ x max( x, 2 x ) = x max(2 x, 4 x ) = 4 x 4 x x = 12 x + ( 4 + 2 (ln x) 2 ) ( 9 2 x + x ) 1 (ln x) 2 2 x x {{ max(1, (ln x) 2 ) = (ln x) 2 max(2 x, x ) = 2 x 2 x (ln x) 2 {{ max(12 x, 2 x (ln x) 2 ) = 12 x Solution: The big-o estimates for each piece of the formula above are given. The final answer is 12 x. Grading: Done on a 0///7/10 point basis. 2
MAT 24 Test 2 SOLUTIONS, FORM A. Evaluate the following. a. [ points] 6 mod 10 Solution: 6 divided by 10 gives a quotient of 6 and a remainder of. Thus 6 mod 10 =. b. [ points] 6 div 10 Solution: 6 divided by 10 gives a quotient of 6 and a remainder of. Thus 6 div 10 = 6. c. [ points] (67 div 9) mod (67 mod 9) Solution: 67 divided by 9 gives a quotient of 7 and a remainder of 4. Thus (67 div 9) mod (67 mod 9) = 7 mod 4 = because the remainder when you divide 7 by 4 is. 4. [10 points] Find real numbers C and k such that x + x 4 x + 8x 2 4x 7 C x whenever x k and show that they work. Solution: The easiest way to get an upper bound on x + x 4 x + 8x 2 4x 7 is to write x + x 4 x + 8x 2 4x 7 x + x 4 + x + 8x 2 + 4x + 7 Note the change in signs to + s! = x + x 4 + x + 8 x 2 + 4 x + 7 x 0 Now, if x 1, then x x n for all n <. In other words, you can replace all the x n terms with x : = x + x + x + 8 x + 4 x + 7 x = 28 x Thus, you can let C = 28 and k = 1. (The proof that they work is the scratch work above.) Other answers were possible. In that case, the scratch work was analyzed to make sure that the choice of C and k really did work. Grading: On a 0///7/10 point basis. Grading for common mistakes: 2 points for breaking up Y Z into Y Z.
MAT 24 Test 2 SOLUTIONS, FORM A. [10 points] Find a big-o estimate for the running time of the following pseudocode. s 0 t 1 i 1 do for j from n + n 2 to n + n s s t t s t end for i 2i until i 4n Solution: Start on the inside of the loops and work your way outwards. Inside of the for loop, there are two assignment statements (for s and t). Each of these takes time O(1), so the running time for one iteration of the for loop is O(max(1, 1)) = O(1). The number of iterations of the for loop is O(( n + n ) ( n + n 2 )) = O(n n 2 ) = O(n ). Hence, the running time for the for loop is O(1 n ) = O(n ). Now for the do loop; one iteration consists of the for loop (running time: O(n )) followed by an assignment statement (running time: O(1)). Hence the running time of one iteration of the do loop is O(max(n, 1)) = O(n ). Now you need to find the number of iterations of the do loop. Let k be the number of times the do loop is executed. The value of i after k iterations can be calculated by considering some small values and looking for a pattern: k i, after k loops 1 2 2 4 8 So it appears that i = 2 k after k loops. The equation which tells us how many iterations will occur is i = 4n, which becomes i = 4n (2 k ) = 4n 8 k = 4n k ln 8 = ln(8 k ) = ln(4n) = ln 4 + ln n k = ln 4 ln 8 + 1 ln n = O(ln n) ln 8 Thus, the running time for the do loop is O(ln n n ) = O(n ln n). Finally, there are three assignment statements before the loop, which all take time O(1). The overall running time is thus O(max(1, 1, 1, n ln n)) = O(n ln n). Grading: Done on a 0///7/10 point basis. 6. [10 points] Use the Euclidean Algorithm to find gcd(1892, 090). Solution: 1892 = 6 090 + 412 090 = 7 412 + 206 412 = 2 206 + 0 The gcd is the last nonzero remainder, so gcd(1892, 090) = 206. Grading for common mistakes: 7 points for no work, points for not using the Euclidean Algorithm. 4
7. [10 points] Convert 201 to base 9. MAT 24 Test 2 SOLUTIONS, FORM A Solution: Repeatedly divide quotients by 9: 201 = 22 9 + 8 22 = 24 9 + 7 24 = 2 9 + 6 2 = 0 9 + 2 Reading from bottom to top yields (2678) 9. Grading for common mistakes: 7 points for no work; 2 points for reading off the remainders backwards; 7 points for converting from base 9 to base 10. 8. Let S be the set defined as follows: (i) S. (ii) If x S, then x 2 S and x S. (iii) The only elements of S are those which can be obtained from condition (i) by applying condition (ii) a finite number of times. a. [10 points] Find S 0, S 1, and S 2. Solution: S n is the set of all elements of S which are obtained from the base case(s) by applying { the recursive part(s) exactly n times. Hence S 0 = is the set of base cases. Then { S 1 = 2, = S 2 = { 7 { 7, 9 7 2,, 9 2, 9 = { 17, 21, 1, 27 Grading: +2 points for S 0, +4 points for S 1, and +4 points for S 2. Grading for common mistakes: 2 points for including the base case(s) in S 1, including S 1 in S 2. b. [10 points] Use Structural Induction to prove that for all x S, x is a fraction whose numerator is odd and whose denominator is (when reduced to lowest terms). Solution: There are two parts to a structural induction proof: showing that the property is true for the base case(s), and showing that the property is preserved when the recursive rule(s) are applied. First the base case: is clearly an odd number divided by. Now, suppose x S, and that x is an odd number divided by ; i.e. x = n, where n is odd. Then: x 2 = n 2 = n 10 is an odd number divided by ; the numerator is odd, because it is the sum of an odd number and an even number. x = n = n is an odd number divided by ; the numerator is odd, because it is the product of two odd numbers. Hence, by Structural Induction, every element of S is an odd number divided by. Grading for common mistakes: points for only considering x = in the recursive part.
MAT 24 Test 2 SOLUTIONS, FORM B 1. [1 points] Prove the following using Mathematical Induction. F 2 i = F n F n+1 where F is the Fibonacci sequence: F 0 = 0 F 1 = 1 F n = F n 1 + F n 2, n 2 Solution: Let P (n) denote the predicate F 2 i = F n F n+1 (not just the right-hand side). First, P (0) is true, because the left-hand side is F 2 0 = 0 2 = 0 and the right-hand side is F 0 F 1 = 0 1 = 0. Now, assume that P (k) is true, that is The goal is to prove P (k + 1), which is This is true, because k+1 F 2 i = k F 2 i = F k F k+1. k+1 F 2 i = F k+1 F k+2. k F 2 i + F 2 k+1 = F k F k+1 + F 2 k+1 = F k+1 (F k + F k+1 ) = F k+1 F k+2. Hence, by the Principle of Mathematical Induction, F 2 i = F n F n+1 is true for all n. Grading: + points for the base case, + points for setting up the inductive case, + points for proving the inductive case. 2. [10 points] Find a simple function g(x) such that the following function is O(g(x)). 10 ( 9 2 x + x ) x + ( 9 + 2 (ln x) 2 4 {{ x! ) ( 8x ) 1 1 x x {{ x 1 (ln x) 2 x! 1 x max(1, x, x ) = x max(1, (ln x) 2 max(1, x) = x, x!) = x! 1 x x = 9 x x! x max(9 x, x! x) = x! x Solution: The big-o estimates for each piece of the formula above are given. The final answer is x! x. Grading: Done on a 0///7/10 point basis. 2
MAT 24 Test 2 SOLUTIONS, FORM B. Evaluate the following. a. [ points] 49 mod 6 Solution: 49 divided by 6 gives a quotient of 8 and a remainder of 1. Thus 49 mod 6 = 1. b. [ points] 49 div 6 Solution: 49 divided by 6 gives a quotient of 8 and a remainder of 1. Thus 49 mod 6 = 8. c. [ points] (1 div 8) mod (1 mod 8) Solution: 1 divided by 8 gives a quotient of 6 and a remainder of. Thus (1 div 8) mod (1 mod 8) = 6 mod = 0 because the remainder when you divide 6 by is zero. 4. [10 points] Find real numbers C and k such that x 6 4x 4 4x 2 8x + 7 C x 6 whenever x k and show that they work. Solution: The easiest way to get an upper bound on x 6 4x 4 4x 2 8x + 7 is to write x 6 4x 4 4x 2 8x + 7 x 6 + 4x 4 + 4x 2 + 8x + 7x 0 Note the change in signs to + s! = x 6 + 4 x 4 + 4 x 2 + 8 x + 7 x 0 Now, if x 1, then x 6 x n for all n < 6. In other words, you can replace all the x n terms with x 6 : = x 6 + 4 x 6 + 4 x 6 + 8 x 6 + 7 x 6 = 24 x 6 Thus, you can let C = 24 and k = 1. (The proof that they work is the scratch work above.) Other answers were possible. In that case, the scratch work was analyzed to make sure that the choice of C and k really did work. Grading: On a 0///7/10 point basis. Grading for common mistakes: 2 points for breaking up Y Z into Y Z.
MAT 24 Test 2 SOLUTIONS, FORM B. [10 points] Find a big-o estimate for the running time of the following pseudocode. s 0 t 0 for i from 2 n + n to n + 2 n j 0 while j 2 < n s 2s 4t s s + t j j + 4 end while if i 2 9s then s 2s n 2 end if end for Solution: Start on the inside of the loops and work your way outwards. When the if statement is executed, a condition is checked (which takes time O(1)), and if that statement is true, then an assignment statement is made (which takes time O(1)); thus, the overall running time of the if statement is O(max(1, 1)) = O(1). Now for the while loop. Every iteration of the while loop consists of three assignment statements, each of which takes time O(1), so each iteration takes time O(max(1, 1, 1)) = O(1). Now you need to calculate the number of iterations of the while loop. To do this, you need to consider the value of j after k iterations. Constructing a small table (shown below), it appears that j = 4k after k iterations. The equation which tells you how many iterations will actually occur is j 2 = n, which becomes k j, after k iterations 0 0 1 4 2 8 12 (4k) 2 = n 16k 2 = n k 2 = n 16 n k = 16 = 1 4 n/2 = O(n /2 ). Thus, the running time of the while loop is O(n /2 1) = O(n /2 ). Now for the for loop; one iteration of the for loop involves an assignment, the while loop, and the if statement. The total running time of one iteration is then O(max(1, n /2, 1)) = O(n /2 ). The number of iterations of the for loop is O(( n + 2 n ) (2 n + n)) = O( n n) = O( n ), and the total running time of the for loop is O(n /2 n ). Finally, two assignment statements are executed, in addition to the for loop. The overall running time is O(max(1, 1, n /2 n )) = O(n /2 n ). Grading: Done on a 0///7/10 point basis. 6. [10 points] Use the Euclidean Algorithm to find gcd(4189, 6860). Solution: 4189 = 6 6860 + 7 6860 = 9 7 + 24 7 = 24 + 0 The gcd is the last nonzero remainder, so gcd(4189, 6860) = 24. Grading for common mistakes: 7 points for no work, points for not using the Euclidean Algorithm. 4
7. [10 points] Convert 201 to base 6. MAT 24 Test 2 SOLUTIONS, FORM B Solution: Repeatedly divide quotients by 6: 201 = 6 + = 6 + = 9 6 + 1 9 = 1 6 + 1 = 0 6 + 1 Reading from bottom to top yields (11) 6. Grading for common mistakes: 7 points for no work; 2 points for reading off the remainders backwards; 7 points for converting from base 6 to base 10. 8. Let S be the set defined as follows: (i) 1 S. (ii) If x S, then x + 2 S and 7x S. (iii) The only elements of S are those which can be obtained from condition (i) by applying condition (ii) a finite number of times. a. [10 points] Find S 0, S 1, and S 2. Solution: S n is the set of all elements of S which are obtained from the base case(s) by applying { 1 the recursive part(s) exactly n times. Hence S 0 = is the set of base cases. Then { 1 S 1 = + 2, 1 { 7 7 = { 7 S 2 = + 2, 7 { 1 7 =, 49 Grading: +2 points for S 0, +4 points for S 1, and +4 points for S 2. Grading for common mistakes: 2 points for including the base case(s) in S 1, including S 1 in S 2. b. [10 points] Use Structural Induction to prove that for all x S, x is a fraction whose numerator is odd and whose denominator is (when reduced to lowest terms). Solution: There are two parts to a structural induction proof: showing that the property is true for the base case(s), and showing that the property is preserved when the recursive rule(s) are applied. 1 First the base case: is clearly an odd number divided by. Now, suppose x S, and that x is an odd number divided by ; i.e. x = n, where n is odd. Then: x + 2 = n + 2 = n + 6 is an odd number divided by ; the numerator is odd, because it is the sum of an odd number and an even number. 7x = 7 n = 7n is an odd number divided by ; the numerator is odd, because it is the product of two odd numbers. Hence, by Structural Induction, every element of S is an odd number divided by. Grading for common mistakes: points for only considering x = 1 in the recursive part.