SolHW6f07 - HW6F07 CS336 Exercises 32-36 from Rosen: (We...

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

View Full Document Right Arrow Icon
HW6F07 CS336 Exercises 32-36 from Rosen: (We only give solutions for even-numbered exercises, because the solutions for odd-numbered exercises are at the back of the book.) 18. Let k be a positive integer. Show that 1 k + 2 k + ... + n k is O ( n k +1 ). Solution: f ( n ) = 1 k + 2 k + ... + n k < arith > n k + n k + ... + n k < arith > = n * n k < arith > = n k +1 So, letting C = 1 ,k = 0, we get | f ( n ) | ≤ C | n k +1 | , for n > k . 32. Give a big- O estimate of the product of the first n odd positive integers. Solution: f ( n ) = 1 * 3 * 5 * ... * (2 n - 1) < arith > 2 * 4 * 6 * ... * 2 n < arith > = (2 · 1) * (2 · 2) * (2 · 3) * ... * (2 · n ) < arith > = 2 n (1 * 2 * 3 * ... * n ) < arith > = 2 n · n ! So a big- O estimate of this function would be O (2 n · n !). 34. Show that for all real numbers a and b with a > 1 and b > 1, if f ( x ) is O (log b x ), then f ( x ) is O (log a x ). Solution: Since f ( x ) is O (log b x ), there exist constants C,k such that | f ( x ) | ≤ C | log b x | 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
We know (from previous knowledge of logarithms) that log b x = log a x/ log a b = log b a log a x (1) Therefore, | f ( x ) | < given > C | log b x | , x > k < from(1) > = C | log b a log a x | , x > k < arith > = C | log b a || log a x | , x > k < let C 0 = C | log b a | > = C 0 | log a x | , x > k 36. Suppose that f ( x ) is O ( g ( x )). Does it follow that 2 f ( x ) is O (2 g ( x ) )? Solution: No. Here is a counterexample: Let f ( x ) = 0 and g ( x ) = - x . If we take C = 1 and k = 0, then | f ( x ) | ≤ C | g ( x ) | for x > k . But 2 f ( x ) = 2 0 = 1 is not O (2 g ( x ) ) = O (2 - x ) = O (1 / 2 x ). We will show this using a proof by contradiction. Assume that there exist C 0 ,k 0 such that 1 C 0 | 1 / 2 x | , for x > k 0 . Then C 0 2 x for x > k 0 . By taking the logarithm of each side, we see that this is equivalent to log 2 C 0 x for x > k 0 . This is a contradiction because the constant log 2 C 0 cannot be greater than all x > k 0 . Therefore, we concude that it is impossible to find C 0 ,k 0 such that 1 C 0 | 1 / 2 x | , for x > k 0 and hence 1 is not O (2 - x ). Now we move on to the problems on the homework sheet. 1. Determine the weakest precondition with respect to the postcondition z = 6” for the following (assume z , y , and x are integer variables): x := 2 z := x - y if y > 0 z := z + 1 y 0 z := 0 Solution: 2
Background image of page 2
First we compute the weakest precondition before the IF: wp ( IF,z = 6) < IF > = ( y > 0 y 0) ( y > 0 wp (” z := z + 1” ,z = 6)) ( y 0 wp (” z := 0” ,z = 6)) < excluded middle , wp , arith > = ( y > 0 z = 5) ( y 0 0 = 6) < arith > = ( y > 0 z = 5) ( y 0 F ) The following computes the weakest precondition for the whole program: wp (” x := 2; z := x - y , ( y > 0 z = 5) ( y 0 F )) < wp > = ( y > 0 2 - y = 5) ( y 0 F )) < arith > = ( y > 0 y = - 3) y > 0 < modus ponens > = ( y = - 3) y > 0 < arith > = F 2. Prove the following code is partially correct with respect to precondition
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/20/2008 for the course CS 336 taught by Professor Myers during the Spring '08 term at University of Texas at Austin.

Page1 / 11

SolHW6f07 - HW6F07 CS336 Exercises 32-36 from Rosen: (We...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online