October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1 Logarithms Definition: X A = B if and only if log X B = A.
October 3, 2001CSE 373, Autumn Logarithms, continued log AB = log A + log B Proof: Let X = log A, Y = log B, and Z = log AB. Then 2 X = A, 2 Y = B, and 2 Z = AB. So, 2 X 2 Y = AB = 2 Z. Therefore, X + Y = Z.
October 3, 2001CSE 373, Autumn Logarithms, Series log A/B = log A – log B log (A B ) = B log A Series binary representation of numbers
October 3, 2001CSE 373, Autumn Boolean Logic Let P and Q be statements. “not P” is true if P is false. “P and Q” is true if both P and Q are true. “P or Q” is true if one of or both P or Q are true. “P implies Q” is true if P is false or Q is true (or both).
October 3, 2001CSE 373, Autumn Proof by Induction 1.Formulate a statement P(n) that depends on n. 2.Prove P(1). (Basis Step) 3.Prove that for any n 1, if P(m) for all 1 m n, then P(n+1). (Induction Step) (The red statement is called the induction hypothesis.)
October 3, 2001CSE 373, Autumn Proof by induction Why does it work? We don’t actually prove, for example, P(503). P(1)P(2)P(3)P(4)P(503)
October 3, 2001CSE 373, Autumn Proof that 1.P(N) is 2.P(1): 3.Suppose P(m) is true for all 1 m N, N 1.
October 3, 2001CSE 373, Autumn P(N) implies P(N+1)
October 3, 2001CSE 373, Autumn log N < N For all integers N > 0, log N < N. P(N) is log N < N. P(1): log 1 = 0 < 1. Suppose P(m) is true for all 1 m N, N 1. log (N+1) log (N+N) = log 2N = (log N) + 1 < N + 1(ind. hyp.)
October 3, 2001CSE 373, Autumn Proof by Contradiction Assume the theorem is false and show that that assumption leads to a known falsehood. This shows that the original assumption was false. The only way the whole line of reasoning is true is if is false.
October 3, 2001CSE 373, Autumn Proof by Counterexample To show a general statement to be false, you need only exhibit one specific instance of the statement to be false. “For all N > 1, N > N 2 ” is false. For N=5, 5 < 5 2.
October 3, 2001CSE 373, Autumn Recursion A recursive function is one that calls itself. n! = n (n-1) (n-2) … 1 int factorial (const int n) { if (n == 1) return 1; else return (n * factorial(n-1)); }
October 3, 2001CSE 373, Autumn Anatomy of a Recursive Function Base case: does not use recursion. if (n == 1) return 1; Making progress: the recursive call needs to get “closer” to a base case. else return (n * factorial(n-1));
October 3, 2001CSE 373, Autumn Recursion and Induction Prove that factorial(n) returns n! P(n) is P(1): factorial(1) follows the if part of the if statement and returns 1 = 1! So, P(1) is true. Suppose P(m) is true for all 1 m n, n 1. Then factorial(n+1) follows the else part of the if statement and returns (n+1)*factorial(n). So, factorial(n+1) returns (n+1)*n! = (n+1)! P(n+1) is true.
October 3, 2001CSE 373, Autumn Iterative factorial int factorial_iterative(const int n) { int i; int product = 1; for (i = 1; i <= n; i++) product *= i; return product; } How do we prove that this function computes factorial correctly?
October 3, 2001CSE 373, Autumn Loop invariants Loop invariant: a statement that is true after each iteration of a loop. After the kth iteration of the loop (before i++ ), i=k and product = k! If the loop invariant is true, then after the nth iteration, the loop will end with i=n and the function will return product = n! Exercise: Prove that factorial_iterative(n) returns n!