Euclid's algorithms for GCD

来源:互联网 发布:sas编程技术教程 编辑:程序博客网 时间:2024/05/17 08:19

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)