Euclid's algorithms for GCD

GCD is the greatest common divisor. pseudo-code:

function Euclid(x,y)

if y==0 return x;

return Euclid(y, x MOD y).

correctness:

Euclid's rule: If x and y are positive integers with x >= y, then gcd(x, y) = gcd(x mod y, y).

Prove: just to prove gcd(x, y) = gcd(x-y, y). Let GCD is k, then if k can divide x and y, then k can also divide x-y, so gcd(x, y) <= gcd(x-y, y). On the other hand, if k can divide x-y and y, then k can also divide x, so gcd(x, y) >= gcd(x-y, y). Thus the rule is proved.

running time:O( n3)

posted @ 2012-06-07 10:47  zdai  阅读(135)  评论(0编辑  收藏  举报