Download presentation
Presentation is loading. Please wait.
Published byClara Hart Modified over 9 years ago
1
October 3, 2001CSE 373, Autumn 20011 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.
2
October 3, 2001CSE 373, Autumn 20012 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.
3
October 3, 2001CSE 373, Autumn 20013 Logarithms, Series log A/B = log A – log B log (A B ) = B log A Series binary representation of numbers
4
October 3, 2001CSE 373, Autumn 20014 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).
5
October 3, 2001CSE 373, Autumn 20015 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.)
6
October 3, 2001CSE 373, Autumn 20016 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)
7
October 3, 2001CSE 373, Autumn 20017 Proof that 1.P(N) is 2.P(1): 3.Suppose P(m) is true for all 1 m N, N 1.
8
October 3, 2001CSE 373, Autumn 20018 P(N) implies P(N+1)
9
October 3, 2001CSE 373, Autumn 20019 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.)
10
October 3, 2001CSE 373, Autumn 200110 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.
11
October 3, 2001CSE 373, Autumn 200111 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.
12
October 3, 2001CSE 373, Autumn 200112 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)); }
13
October 3, 2001CSE 373, Autumn 200113 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));
14
October 3, 2001CSE 373, Autumn 200114 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.
15
October 3, 2001CSE 373, Autumn 200115 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?
16
October 3, 2001CSE 373, Autumn 200116 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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.