SCIENCE OF PROGRAMMING
VIGGNESH KANDASAMY
Assignment # 5
CWID:10458478
Q1.
Prove partial correctness for the GCD program given in class (nested while statements).
Sol:
Part A
Proof:
{a>0 ^ b>0}
(GCD(a, b)=GCD( x, y ))
y
x
b
a
,
,
{a>0 ^ b>0}
GCD(a, b)=GCD(a, b)
{a>0 ^ b>0} =>
true
Part B
Proof:
In the case of Part B we have three while loops. The functions of two while loops are proved to
be correct in the following statements.
There are two cases: first case is B11 and the second case is B12.Both these cases are
evaluated.
Case 1: B11
It has two parts in it
{x≠y ^ GCD(a,b)=GCD(x,y) ^ x>y} x:=x-y {x≠y ^ GCD(a,b)=GCD(x,y)}
^
{x≠y ^ GCD(a,b)=GCD(x,y) ^x≤y} =>{GCD(a,b) = GCD(x,y)}
Sub-Case 1:
(x≠y ^ GCD(a,b)=GCD(x,y) ^ x>y) => (x≠y ^ GCD(a,b)=GCD(x,y))
x
x-y
(x≠y ^ GCD(a,b)=GCD(x,y) ^ x>y) =>(x-y≠y ^ GCD(a,b)=GCD(x-y,y) )
The LHS and RHS of the part 1 are true.
Hence it is
true
Sub-Case 2:
(x≠y ^ GCD(a,b)=GCD(x,y)^ x≤y) => (GCD(a,b) = GCD(x,y))