The greatest common divisor of two positive integers a and b, GCD(a,b), can be found by the Euclid's algorithm (I am sure you can figure out the convoluted then-clause, here just for fun (:-)x=a; y=b; while (x!=y) do {if x<y then {y=x+y; x=y-x; y=y-x}; x=x-y}
- Define a loop invariant to prove that the value of x=y obtained from the execution of the loop is the GCD(a,b).
- What is the complexity of the above algorithm?
- (extra) Can you speed up the algorithm (make your own assumptions)?