Upload
clara-hart
View
229
Download
0
Embed Size (px)
Citation preview
October 3, 2001 CSE 373, Autumn 2001 1
Mathematical Background
• Exponents
XA XB = XA+B
XA / XB = XA-B
(XA)B = XAB
XN+XN = 2XN
2N+2N = 2N+1
• Logarithms
Definition: XA = B if and only if logX B = A.
October 3, 2001 CSE 373, Autumn 2001 2
Logarithms, continued
• log AB = log A + log B
Proof: Let X = log A, Y = log B, and Z = log AB. Then 2X = A, 2Y = B, and 2Z = AB.
So, 2X 2Y = AB = 2Z.
Therefore, X + Y = Z.
• 1,0,,log
loglog ACBA
A
BB
C
CA
October 3, 2001 CSE 373, Autumn 2001 3
Logarithms, Series
• log A/B = log A – log B
• log (AB) = B log A
Series
•
• binary representation of numbers
•
122 1
0
N
N
i
i
2
)1(
1
NNi
N
i
October 3, 2001 CSE 373, Autumn 2001 4
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).
P
QP
QP
QPQP
October 3, 2001 CSE 373, Autumn 2001 5
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, 2001 CSE 373, Autumn 2001 6
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, 2001 CSE 373, Autumn 2001 7
Proof that
1. P(N) is
2. P(1):
3. Suppose P(m) is true for all
1 m N, N 1.
2
)1(
1
NNi
N
i
2
)1(
1
NNi
N
i
2
)11(11
1
1
i
i
October 3, 2001 CSE 373, Autumn 2001 8
P(N) implies P(N+1)
2
)2)(1(2
)1)(2(2
)1(
2
)1(2
.)hypind.(2
)1()1(
)1(1
1
1
NN
NN
NNN
NNN
iNiN
i
N
i
October 3, 2001 CSE 373, Autumn 2001 9
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, 2001 CSE 373, Autumn 2001 10
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.
FalseP
P
October 3, 2001 CSE 373, Autumn 2001 11
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 > N2 ” is false.
For N=5, 5 < 52 .
October 3, 2001 CSE 373, Autumn 2001 12
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, 2001 CSE 373, Autumn 2001 13
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, 2001 CSE 373, Autumn 2001 14
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, 2001 CSE 373, Autumn 2001 15
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?
n
i
in1
!
October 3, 2001 CSE 373, Autumn 2001 16
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!