=========================================================================== CSC 165H Lecture Summary for Week 8 Winter 2003/4 =========================================================================== -------------- Proof by cases -------------- To prove A -> B, it can help to treat some As differently than others, e.g., to prove that for all integers x, x^2 + x is even. Outline of proof: Let x in Z. ... Therefore x^2 + x is even, and since x was an arbitrary member of Z, \-/ x in Z, x^2 + x is even. How to show a number is even? One way is to show that one of the factors of the number is even: this suggests factoring. Let x in Z. ... So x(x+1) is even. Therefore x^2 + x is even, and since x was an arbitrary member of Z, \-/ x in Z, x^2 + x is even. But neither x nor (x+1) is always even: it depends on whether x is even or not. So we introduce cases. Let x in Z. Case 1: x is even. Then x(x+1) is even. Case 2: x is odd. Then x+1 is even. So x(x+1) is even. Since x is even \/ x is odd, x(x+1) is even in all cases. Therefore x^2 + x is even, and since x was an arbitrary member of Z, \-/ x in Z, x^2 + x is even. This is a common case analysis: rewrite A as (A /\ C) \/ (A /\ ~C). The case part is sometimes written using "if ... else/otherwise ..." style: If x is even, then x(x+1) is even. Otherwise, x is odd, so x+1 is even, so x(x+1) is even. The proof structure is a special case of the general proof structure for hypotheses involving \/. To prove A1 \/ A2 -> B: Suppose A1. ... Then B. Suppose A2. ... Then B. Thus A1 \/ A2 -> B. --------------------------- More general proof by cases --------------------------- We can rewrite A as A1 \/ A2 \/ ... \/ An -> B, and then prove A1 -> B, A2 -> B, ..., and An -> B. In other words, we can have more than 2 cases. It's not required that A1, A2, ..., An be mutually exclusive (cases can overlap). And it's sufficient that A -> A1 \/ A2 \/ ... \/ An, i.e., that the cases cover A (instead of requiring equivalence). One way of generating cases is by breaking up the domain, and then using that to break up A. If C1 \/ C2 \/ ... \/ Cn is true, then (A /\ C1) \/ ... \/ (A /\ Cn) <-> A. -------------- Indirect proof -------------- Because P -> Q is equivalent to its contrapositive ~Q -> ~P, proving ~Q -> ~P proves P -> Q. This is called an "indirect proof". The outline of such a proof of \-/ x in D, p(x) -> q(x) is: Let x in D. Suppose ~q(x). ... Then ~p(x). So p(x) -> q(x). Since x is an arbitrary element of D, \-/ x in D, p(x) -> q(x). For example: For each integer x, if x^2 is even then x is even. What's wrong with: Let x be an integer. Suppose x is even. Then x^2 = x * x, which is even. ... [It's a proof of the converse!] Here's a correct indirect proof: Let x be an integer. Suppose x is not even. Then x is odd (i.e., x = 2k+1 for some integer k). So x^2 = x * x is odd (since x^2 = (2k+1)^2 = 4k^2+4k+1 = 2(2k^2+2k)+1 = 2k'+1 for the integer k' = 2k^2+2k). So x^2 is not even. Hence, if x^2 is even then x is even. Since x is an arbitrary integer, for each integer x, if x^2 is even then x is even. Exercise: Consider For each integer x, if x^2 = 3n + 1 for some integer n, then x = 3n + 1 for some integer n. Is it true? How about its converse? --------------------------------------------------------------------------- ------------------------ Binary (Base 2) notation ------------------------ We're used to writing natural numbers in decimal (base 10) notation. The sequence of digits 20395 represents: 5 + 9 * 10 + 3 * 100 + 0 * 1000 + 2 * 10000 = 5 + 9 * 10 + 3 * 10^2 + 0 * 10^3 + 2 * 10^4 = 2 * 10^4 + 0 * 10^3 + 3 * 10^2 + 9 *10^1 + 5 * 10^0. Each position represents a power of 10, and 10 is called the base. Each position has a digit 0 to 9 representing how many of that power of 10 to include. Why do we use 10? Probably because we have ten fingers to count with. But other than that it's arbitrary. Let's consider using 2 as a base for a number system. What digits should we use? In analogy with base 10: 0 up to one less than 2. That's just 0 and 1! We don't need 2 or higher, because that's taken care of by choosing a different position (just like in base 10 there is no "digit" for the number 10 itself: we write it using two digits). Let's look at some binary numbers: (10011)_2 represents 1 * 2^4 + 0 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0 = 1 * 16 + 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 16 + 2 + 1 = 19 (101)_2 represents ? How do you multiply a number by 10 in base 10? What's analogous in binary? What does "shifting right" (eliminating rightmost digit) do in binary? (What does it do in base 10?) What does the rightmost digit tell us in base 10? What does the rightmost digit tell us in binary? Let's convert some numbers from decimal to binary notation. Consider 57. We want to build it by adding 0 or 1 of each of 1, 2, 4, 8, 16, 32, etc. 57 = 32 + 16 + 8 + 1 We can also do this bottom up (where 'x % y' is the remainder of x divided by y, just like in Java): 57 % 2 = 1, so (??????1)_2 (57 - 1) / 2 = 28 28 % 2 = 0, so (?????01)_2 28 / 2 = 14 14 % 2 = 0, so (????001)_2 14 / 2 = 7 7 % 2 = 1, so (???1001)_2 (7 - 1) / 2 = 3 3 % 2 = 1, so (??11001)_2 (3 - 1) / 2 = 1 1 % 2 = 1, so (?111001)_2 (1 - 1) / 2 = 0 We can add number in binary directly, if we remember that "1+1=2" becomes (1)_2 + (1)_2 = (10)_2 in binary: 1 11 1011 + 1011 ------ 10110 ------------------------------------ Multiplication by shift and addition ------------------------------------ Usually represent integers in binary (base 2) on a computer. Easy & quick to divide or multiply by 2 (via shift), and to determine even vs. odd. (We'll examine all this in more detail later, but it justifies why we would consider the following algorithm). Here's a multiplication algorithm using these fast operations: // Precondition: m >= 0. MULT(m, n) x = m y = n z = 0 // z = mn - xy while x != 0 if x odd then z = z + y x = x div 2 // div is integer division that rounds down y = y * 2 // z = mn when done, since x == 0 return z We will show that going through one iteration of the loop preserves the claimed relationship between the variables, i.e., If z = mn - xy, and x != 0, then after executing if x odd then z = z + y x = x div 2 y = y * 2, z = mn - xy is still true.