001_Singh_Abhay Kumar_Assignment_5

001_Singh_Abhay Kumar_Assignment_5 - CS 536 SCIENCE OF...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 536 – SCIENCE OF PROGRAMMING, IIT, Spring 2010 SCIENCE OF PROGRAMMING ASSIGNMENT # 5 47/50 Submitted by Abhay Kumar Singh CWID: A20242810 Spring - 2010 Question 1.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 536 – SCIENCE OF PROGRAMMING, IIT, Spring 2010 Prove total correctness using the lexicographic order for proving termination for the following GCD program. {a>0 ^ b>0} (x,y) : (a,b) LOOPSELECT x> y => x:=x-y or y>x => y:=y-x end LOOPSELECT; GCD:=x {GCD = gcd(a,b) } Answer: INV = gcd(x,y) = gcd(a,b) t_inv = (x > 0 ^ y > 0} t_func = (x, y) WFS = (N x N) Under = “<” (-1) need to define it Step 1: Proof of T_INV and INV {a>0 and b>0} (x,y) : (a,b); { gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 } {a>0 and b>0} => [ gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 ] y b x a {a>0 and b>0} => gcd(a,b)=gcd(a,b) ^ a > 0 ^ b > 0 True Proof B: Case 1: x > y {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y } x := x – y { gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 } {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y } => [ gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 ] x x-y {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y } => gcd(x-y, y)=gcd(a,b) ^ x -y > 0 ^ y 0 True As we know that, If x>y then gcd(x,y)=gcd(x-y,y) Case 2: x < y {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x < y } y := y – x { gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 } {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x < y } => [ gcd(x,y)=gcd(a,b) ^ x > 0 ^ y > 0 ] y y-x
Background image of page 2
CS 536 – SCIENCE OF PROGRAMMING, IIT, Spring 2010 {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x < y } => gcd(x, y-x)=gcd(a,b) ^ x > 0 ^ y - x > 0 True As we know that, If x<y then gcd(x,y)=gcd(x,y-x) Case 3: No guards true {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x = y } LOOPSELECT { x=y ^ gcd(a,b) = x ^ gcd(x,y)=gcd(a,b) } {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x = y } => [ x=y ^ gcd(a,b) = x ^ gcd(x,y)=gcd(a,b) ] True Proof C: { x=y ^ gcd(a,b) = x ^ gcd(x,y)=gcd(a,b)} GCD:=x; {GCD=gcd (a,b)} { x=y ^ gcd(a,b) = x ^ gcd(x,y)=gcd(a,b)} => [GCD=gcd (a,b)] GCD x { x=y ^ gcd(a,b) = x ^ gcd(x,y)=gcd(a,b)} => x = gcd(a,b) True Here, we prove that all part of program is correct. Hence, we can say program is correct. Step 2: We have to prove T_inv ^ INV => t_func WFS {x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) } => (x,y) (N x N) True Because x and y are natural numbers Step 3: {T_inv ^ INV ^ t_old = (x,y)} LOOPSELECT x> y => x:=x-y or y>x => y:=y-x end LOOPSELECT {T_inv ^ INV ^ t_func < t_old} Case 1: x > y { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y ^ t_old = (x,y)} x := x-y { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ t_func < t_old} { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y ^ t_old = (x,y)} => [x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ (x, y) < t_old] x x-y
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 536 – SCIENCE OF PROGRAMMING, IIT, Spring 2010 { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x > y ^ t_old = (x,y)} => x-y>0 ^ y > 0 ^ gcd(x-y,y)=gcd(a,b) ^ (x- y, y) < t_old True Case 2: x < y { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x < y ^ t_old = (x,y)} y := y-x { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ t_func < t_old} { x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b) ^ x < y ^ t_old = (x,y)} => [x > 0 ^ y > 0 ^ gcd(x,y)=gcd(a,b)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

001_Singh_Abhay Kumar_Assignment_5 - CS 536 SCIENCE OF...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online