Mohamed G. Gouda CS 313K Fall 2012 Exercise 4 1. Give a direct inference proof to prove the predicate: (All n, (n is odd) => (Exist k,l, n = k^2 - l^2)) where the domains of n, k, and l are the set of all positive integers. Sol: n is odd => {definition of odd} n = 2m + 1, for some integer m => {add m^2 - m^2 to right side of "="} n = (m^2 + 2m + 1) - m^2, for some integer m => {arithmetics} n = (m+1)^2 - m^2, for some integer m => {rename m+1 and m} n = k^2 - l^2, for some integers k and l 2. Give a two-sided inference proof to prove the two-sided inference: (All m, n, (m^2 = n^2) <=> (m=n or m=-n)) where the domains of m and n are the set of all real numbers. Sol: m^2 = n^2 <=> {subtract n^2 from both sides of "="} m^2 - n^2 = 0 <=> {arithmetics} (m-n)(m+n) = 0 <=> {arithmetics} m-n = 0 or m+n = 0 <=> {arithmetics} m = n or m = -n 3. Give a two-sided inference proof to prove the two-sided inference: (All n, n is even <=> (-1)^n = 1) where the domain of n is the set of all non-negative integers. Sol: n is even <=> {definition of even} n = 2k, for some integer k, and (-1)^n = (-1)^(2k) <=> {x^(2k) = (x^2)^k} n = 2k, for some integer k, and (-1)^n = ((-1)^2)^k <=> {(-1)^2 = 1} n = 2k, for some integer k, and (-1)^n = 1^k <=> {1^k = 1} n = 2k, for some integer k, and (-1)^n = 1 4. Give a direct inference proof to prove: (All x, (x is rational and x != 0) => (1/x is rational)) where the domain of x is the set of all rational numbers. Sol: (x is rational and x != 0) => {definition of rational} x = a/b, for some integers a and b where b != 0 and a and b share no factor and x != 0 => {arithmetics} x = a/b, for some integers a and b where b != 0 and a and b share no factor and a != 0 => {arithmetics} 1/x = b/a for some integers a and b where a != 0 and a and b share no factor => {definition of rational} 1/x is rational 5. Give an induction proof to prove that, for every non-negative integer n, (All n, n >= 0, 7^0 + 7^1 + ... + 7^n = (7^(n+1) - 1)/6) Sol: Let P(n) be the predicate 7^0 + 7^1 + ... + 7^n = (7^(n+1) - 1)/6 Base Case: n=0: P(0) <=> 7^0 = (7^1 - 1)/6 <=> 1 = 1 <=> T Induction Step: (All n, n >= 0, P(n) => P(n+1)) P(n) => {definition of P(n)} 7^0 + ... + 7^n = (7^(n+1) - 1)/6 => {add 7^(n+1) to both sides of "="} 7^0 + ... + 7^(n+1) = (7^(n+1) - 1)/6 + 7^(n+1) => {arithmetics} 7^0 + ... + 7^(n+1) = (7^(n+1) - 1)/6 + 7^(n+1) => {arithmetics} 7^0 + ... + 7^(n+1) = (7^(n+2) - 1)/6 => {definition of P(n+1)} P(n+1) 6. Give an induction proof to prove the quantified predicate, (All n, n >= 1, 1/2 + 1/2^2 + ... + 1/2^n = (2^n - 1)/2^n) Sol: Let P(n) be the predicate 1/2 + 1/2^2 + ... + 1/2^n = (2^n - 1)/2^n Base Case: n=1: P(1) <=> 1/2 = (2-1)/2 <=> 1/2 = 1/2 <=> T Induction Step: (All n, n >= 1, P(n) => P(n+1)) P(n) => {definition of P(n)} 1/2 + 1/2^2 + ... + 1/2^n = (2^n - 1)/2^n => {add 1/2^(n+1) to both sides of "="} 1/2 + 1/2^2 + ... + 1/2^(n+1) = (2^n - 1)/2^n + 1/2^(n+1) => {arithmetic} 1/2 + 1/2^2 + ... + 1/2^(n+1) = (2*(2^n - 1) + 1)/2^(n+1) => {arithmetic} 1/2 + 1/2^2 + ... + 1/2^(n+1) = (2^(n+1) - 1)/2^(n+1) => {definition of P(n+1)} P(n+1) 7. Give an induction proof to prove the quantified predicate, (All n, n >= 1, (2*1 - 1) + (2*2 - 1) + ... + (2*n - 1) = n^2) Sol: Let P(n) be the predicate: (2*1 - 1) + (2*2 - 1) + ... + (2*n - 1) = n^2 Base Case: n=1: P(1) <=> (2*1 - 1 = 1^2) <=> (1=1) <=> T Induction Step: (All n, n >= 1, P(n) => P(n+1)) P(n) => {definition of P(n)} (2*1-1) + (2*2-1) + ... + (2*n-1) = n^2 => {add 2*(n+1)-1} to both sides of "="} (2*1-1) + ... + (2*(n+1)-1) = n^2 + (2*(n+1)-1) => {arithmetics} (2*1-1) + ... + (2*(n+1)-1) = (n^2 + 2n + 1) => {arithmetics} (2*1-1) + ... + (2*(n+1)-1) = (n+1)^2 => {definition of P(n+1)} P(n+1) 8. Give an inducton proof to prove that, for every positive integer n, (5^n - 1) is divisible by 4. Sol: Let P(n) be the predicate: (5^n - 1) is divisible by 4 Base Case: n = 1: P(1) <=> (5 - 1 is divisible by 4) <=> T Induction Step: for every n > 0, P(n) => P(n+1) P(n) => {definitions of P(n) and of divisible by 4} 5^n - 1 = 4k, for some integer k => {arithmetics} 5^n = 4k+1, for some integer k => {multiply 5 to both sides of "="} 5^(n+1) = 5*(4k+1), for some integer k => {arithmetics} (5^(n+1) - 1) = (5*(4k+1) - 1), for some integer k => {arithmetics} (5^(n+1) - 1) = 4*(5k+1), for some integer k => {definition of divisible by 4} (5^(n+1) - 1) is divisible by 4 => {definition of P(n+1)} P(n+1) 9. Give an induction proof to prove that, for every non-negative integer n, (n^3 - n) is divisible by 3. Sol: Let P(n) be the predicate: (n^3 - n) is divisible by 3 Base Case: n=0: P(0) <=> 0 is divisible by 3 <=> T Induction Step: for every n >= 0, P(n) => P(n+1) P(n) => {definitions of P(n) and divisible by 3} (n^3 - n) = 3k, for some integer k => {add (3n^2 + 3n) to both sides of "="} (n^3 - n) + (3n^2 + 3n) + 1 - 1 = 3k + (3n^2 + 3n) => {arithmetics} (n+1)^3 - (n+1) = 3k + 3n^2 + 3n => {(k + n^2 + n) is an integer} (n+1)^3 - (n+1) = 3m, for some integer m => {definitions of P(n+1) and of divisible by 3} P(n+1) 10. Give an induction proof to prove that, for every integer n > 10, 2^n > n^3. Sol: Let P(n) be the predicate 2^n > n^3. Base Case: n=10: P(10) <=> (2^10 > 10^3) <=> (1024 > 1000) <=> T Induction Step: for every n >= 10, P(n) => P(n+1) P(n) => 2^n > n^3 => {multiply both sidex by 2} 2^(n+1) > n^3*2 => {arithmetics and n^3 >= 10*n^2 since n >= 10} 2^(n+1) > (n^3 + 10*n^2) => {arithmetics and 7*n^2 >= 70*n since n >= 10} 2^(n+1) > (n^3 + 3*n^2 + 70*n) => {arithmetics} 2^(n+1) > (n^3 + 3*n^2 + 3*n + 67*n) => {arithmetics and 67*n >= 1 since n >= 10} 2^(n+1) > (n^3 + 3*n^2 + 3*n + 1) => {arithmetics} 2^(n+1) > (n+1)^3 => {definition of P(n+1)} P(n+1) 11. Give an induction proof to prove that, for every positive integer n, there are n(n-1)/2 item pairs in any set of n items. Sol: Let P(n) be the predicate: (there are n(n-1)/2 item pairs in any set of n items) Base Case: n=1: P(1) <=> (there are 0 item pairs in any set of 1 item) <=> T Induction Step: for every n >= 1, P(n) => P(n+1) P(n) => {definition of P(n)} (there are n(n-1)/2 item pairs in any set of n items) => {add a new item t to any set of n items} (there are n(n-1)/2 item pairs that do not involve t in the set) + (there are n item pairs that involve t in the set) => {(n(n-1)/2 + n) = (n+1)n/2} (there are (n+1)n/2 item pairs in any set of n+1 items) => {definition of P(n+1)} P(n+1) 12. Define (n!) to be the multiplication (1*2*...*n). Give an induction proof to prove that, for every integer n >= 4, (2^n =< n!) Sol: Let P(n) be the predicate (2^n =< n!). Base Case: n=4: P(4) <=> (2^4 =< 4!) <=> (16 =< 24) <=> T Induction Step: for every n >= 4, P(n) => P(n+1) P(n) => {definition of P(n)} (2^n =< n!) => {multiply 2 to both sides of "="} (2^(n+1) =< 2*n!) => {(n >= 4) => n+1 >= 2} (2^(n+1) =< (n+1)*n!) => {arithmetics} (2^(n+1) =< (n+1)!) => {definition of P(n+1)} P(n+1) 13. Verfiy that the following program specification holds: (x=X and y=Y) y := x+y (x=X and y=(X+Y)) Sol: We need to prove the following predicate (All x,y, (x=X and y=Y) => (x=X and (x+y)=(X+Y))) (x=X and y=Y) => {Arithmetics} (x=X and y=Y and (x+y)=(X+Y)) => {(P and Q and R) => (P and R)} (x=X and (x+y)=(X+Y)) 14. Verify that the following program specification holds: (T) x:=5; y:=7 (x=5 and y=7) Sol: We need to verify the following two program specifications: (T) x:=5 (x=5) (x=5) y:=7 (x=5 and y=7) Thus we need to prove the following two predicates: (All x,y, (T) => (5=5)) (All x,y, (x=5) => (x=5 and 7=7)) Proof of First Predicate: (T) => {5=5 is clearly T} (5=5) Proof of Second Predicate: (x=5) => {P => (P and T)} (x=5 and T) => {7=7 is clearly T} (x=5 and 7+7) 15. Verify that the following program specification holds: (T) if x =< y then x := y else skip fi (x >= y) Sol: We need to verify the following two program specifications: (T and (x =< y)) x := y (x >= y) (T and (x > y)) skip (x >= y) Thus we need to prove the following two predicates: (All x,y, (T and (x =< y)) => (y >= y)) (All x,y, (T and (x > y)) => (x >= y)) Proof of First Predicate: (T and (x =< y)) => {(P and Q) => P} (T) => {T => (y >= y)} (y >= y) Proof of Second Predicate: (T and (x > y)) => {(P and Q) => Q} (x > y) => {Arithmetics} (x >= y) 16. Verfiy that the following program specification holds: (x > 10) skip (x >= 10) Sol: We need to prove the following predicate (All x,y, (x > 10) => (x >= 10)) (x >10) => {P => (P or Q)} (x >10 or x =10) => {arithmetics} (x >= 10) 17. Verfiy that the following program specification holds: (x >= 0) x := x+1 (x > 0) Sol: We need to prove the following predicate (All x,y, (x >= 0) => ((x+1) > 0)) x >= 0 => {adding 1 to both sides of >= maintains >=} (x+1) >= 1 => {arithmetics} (x+1) > 0 18. Verfiy that the following program specification holds: (y = 10) x := y (y > 5) Sol: We need to prove the following predicate (All x,y, (y=10) => (y>5)) y = 10 => {arithmetics} y > 5 19. Verfiy that the following program specification holds: (T) x := x+y (x >= y) Sol: We need to prove the following predicate (All x,y, (T) => ((x+y) >= y)) T => {(x+y) >= y is clearly true} (x+y) >= y 20. Verfiy that the following program specification holds: (x=5 and y=2) y := x+1 (y > 5) Sol: We need to prove the following predicate (All x,y, (x=5 and y=2) => ((x+1) > 5) x=5 and y=2 => {(P and Q) => P} x=5 => {arithmetics} (x+1) > 5 21. Verify that the following program specification holds: (y = Y) x := (y+1); x := (y+2) (x = (Y+2)) Sol: We need to verify the following two program specifications: (y = Y) x := y+1 (y = Y) (y = Y) x := y+2 (x = (Y+2)) Thus we need to prove the following two predicates: (All x,y, (y = Y) => (y = Y)) (All x,y, (y = Y) => ((y+2) = (Y+2)) Proof of First Predicate: (y = Y) => {P => P} (y = Y) Proof of Second Predicate: (y = Y)) => {adding 2 to both sides of = maintains =} ((y+2) = (Y+2)) 22. Verify that the following program specification holds: (T) if x =! y then y := x else skip fi (x = y) Sol: We need to verify the following two program specifications: (T and (x =! y)) y := x (x = y) (T and not(x =! y)) skip (x = y) Thus we need to prove the following two predicates: (All x,y, (T and (x =! y)) => (x = x)) (All x,y, (T and not(x =! y)) => (x = y)) Proof of First Predicate: (T and (x =! y)) => {(P and Q) => P} (T) => {(x=x) is clearly true} (x = x) Proof of Second Predicate: (T and not(x =! y)) => {(arithmetics} (T and (x = y)) => {(P and Q) => Q} (x = y)