40 likes | 138 Views
Example. Function SQ(A) C=0 D=0 While (D A) a. C=C+A b. D=D+1 Return (A). A must be a positive integer if the looping is to end. A few trials with particulars values of A will provide evidence that the function does carry out this task.
E N D
Function SQ(A) C=0 D=0 While (D A) a. C=C+A b. D=D+1 Return (A) A must be a positive integer if the looping is to end. A few trials with particulars values of A will provide evidence that the function does carry out this task.
Let P(n) be the predicate Cn=A X Dn. Our task is to prove that n 0 P(n) is true. Here n0 is 0. • Step 1: P(0) is the statement C0=A X D0, which is true since the value of both C and D is zero after zero passes through the WHILE loop. • Step 2: Assume P(k) is true : Ck =A X Dk ………(i)
Step 3: To prove Ck+1=A X Dk+1 is true. After a pass through the loop, C is increased by A and D is increased by 1, so Ck+1=Ck + A and Dk+1= Dk +1. LHS P(k+1): Ck+1= Ck + A = A X Dk + A = A (Dk +1) = A Dk+1 By MI it follows that as long as looping occurs Cn=A X Dn. The loop must terminates.