Professional Documents
Culture Documents
Computing GCD's The Euclidean Algorithm: A QB + R
Computing GCD's The Euclidean Algorithm: A QB + R
Computing GCD's The Euclidean Algorithm: A QB + R
MIT 6.042J/18.062J
Lemma:
1
Euclidean Algorithm GCD partial correctness
as a State Machine: By Lemma, gcd(x,y) is constant.
States ::= N × N so preserved invariant is
start ::= (a,b) P((x,y)) ::= [gcd(a,b) = gcd(x,y)]
state transitions defined by P(start) is trivially true:
(x,y) → (y, rem(x,y)) [gcd(a,b) = gcd(a,b)]
for y ≠ 0
Albert R Meyer March 6, 2015 gcdeuclid.5 Albert R Meyer March 6, 2015 gcdeuclid.6
Proof: at termination, y = 0, so
x = gcd(x,0) = gcd(x,y) = gcd(a,b)
preserved invariant
Albert R Meyer March 6, 2015 gcdeuclid.7 Albert R Meyer March 6, 2015 gcdeuclid.8
2
GCD Termination GCD Termination
GCD Termination
y halves or smaller at
every other step, so
reaches minimum in ≤
2 log2 b
steps.
3
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.