Escolar Documentos
Profissional Documentos
Cultura Documentos
Course Policy
Syllabus Grading Labs always in C programming language Each assignment starts and ends in the same Lab session. Late assignments will not be accepted. Study hard!
Izmir University of Economics 2
Introduction
See that how a program performs for reasonably large input is just as important as its performance on moderate amounts of input Summarize basic mathematical background needed Review recursion
Izmir University of Economics 3
Proof: Let X=logCB, Y=logCA, Z=logAB CX=B, CY=A, AZ=B by Definition 1.1. B=CX=(CY)Z.Therefore, X=YZ Theorem 1.2. log AB = logA + logB where A, B > 0 Proof: X=logA, Y=logB, and Z=logAB, 2X=A, 2Y=B, and 2Z=AB, 2X2Y=AB=2Z. Therefore, X+Y=Z
Izmir University of Economics 7
If 0 < A < 1, then and as N tends to g, the sum approaches 1/(1-A) S=1+A+A2+A3+A4... AS=A+A2+A3+A4+A5... S-AS= 1 which implies S=1/(1-A)
Izmir University of Economics 9
N i 1 A e 1 A i !0
Arithmetic Series
N N ( N 1) N 2 (Gauss Method ) } i ! 2 2 i !1 2 3 ... N S ! 1 S ! N N 1 N 2 ... 1 __________ __________ __________ _ 2S ! N 1 N 1 N 1 ... N 1 S ! N ( N 1) / 2
Izmir University of Economics
11
14
Inductive
Hypothesis
Proof by Counterexample
Proof by Counterexample: Best way for proving that a statement is false. Example: The statement Fk k2 is false. The easiest way to prove this is to compute F11 = 144 > 112 = 121
16
Proof by Contradiction
It proceeds by assuming that the theorem is false and showing that this assumption implies that some known property is false, and hence the original assumption is erroneous. Example: Prove that there is an infinite number of primes. Proof: Assume the theorem is false, so that there is some largest prime Pk. Let P1, P2, ..., Pk be all the primes in order and consider N = P1P2...Pk + 1. Clearly, N > Pk, so by assumption N can not be prime. However, none of P1, P2, ..., Pk divides N exactly, because remainders are all 1. This is a contradiction: numbers are either prime or a product of primes. Hence the original assumption is false implying that the theorem is true.
Izmir University of Economics 17
If F is called with a value of 4, then 2*F(3)+4*4 will be int F( int X ) { required to be computed. Thus if( X == 0 ) /* base case */ a call is made to find F(3). return 0; F(4)=2*F(3)+4*4 else return 2 * F( X - 1 ) + X * X; F(3)=2*F(2)+3*3 } F(2)=2*F(1)+2*2 F(1)=2*F(0)+1*1 main( ) { F(0)=0 base case. Recursive printf( "F(4) = %d\n", F( 4 ) ); calls until a base case. F(-1)= return 0; Automatic Bookkeeping }
Izmir University of Economics 18
Recursion - Bad
Bad(0)=0, Bad(N)=Bad(N/3 + 1) + N 1 To compute Bad(1), the computer will repeatedly make calls to Bad(1). Eventually, it will run out of space
#include <stdio.h> int Bad( unsigned int N ) { if( N == 0 ) return 0; else return Bad( N / 3 + 1 ) + N - 1; } main( ) { printf( "Bad is infinite recursion\n" ); return 0; }
Izmir University of Economics 19
Recursion - Induction
PrintOut(N) prints out positive integers. PrintDigit(Ch) take a single digit number as character and output it to the terminal
#include <stdio.h> #define PrintDigit( Ch ) ( putchar( ( Ch ) + '0' ) ) /* Print nonnegative N */ void PrintOut( unsigned int N ) { if( N >= 10 ) Theorem: Recursive number-printing PrintOut( N / 10 ); algorithm is correct for N 0. PrintDigit( N % 10 ); Proof (by induction): If N has one digit, then it } main( ) { PrintOut( 1369 ); putchar( '\n' ); return 0; }
is correct. Assume it works for all numbers of k or fewer digits. A number of k+1 digits is expressed by its first k digits followed by its least significant digit. N ! N / 10 N (mod 10) By the inductive hypothesis, first part is printed correctly and then the last digit is appended.
21