# 欧几里得算法与扩展欧几里得算法

gcd算法简单高效，是对数级别的算法。

def gcd(a, b):
if b == 0:
return a
return gcd(b, a%b)

def gcd1(a, b):
while b != 0:
r = a % b
a = b
b = r
return a

print gcd(132, 324)
print gcd1(132, 324)
Python

gcd(a, b) = a*xi + b*yi
gcd(b, a % b) = b*xi+1 + (a - [a/b]*b)*yi+1    （q = [a/b]）
gcd(a, b) = gcd(b, a % b) => a*xi + b*yi = a*yi+1 + b*(xi+1 - [a/b]*yi+1)
xi = yi+1
yi = xi+1 - [a/b]*yi+1

def extendedGCD1(a, b):
# a*xi + b*yi = ri
if b == 0:
return (1, 0, a)
(x, y, r) = extendedGCD1(b, a%b)
"""
gcd(a, b) = a*xi + b*yi
gcd(b, a %  b) = b*xi+1 + (a - [a/b]*b)*yi+1
gcd(a, b) = gcd(b, a %  b)   =>   a*xi + b*yi = a*yi+1 + b*(xi+1 - [a/b]*yi+1)
xi = yi+1
yi = xi+1 - [a/b]*yi+1
"""
tmp = x
x = y
y = tmp - (a/b) * y
return (x, y, r)
Python

r1 = a mod b

r2 = b mod r1

r3 = r1 mod r2

...

ri = ri-2 mod ri-1   即（1） ri = ri-2 - q*ri-1   （q = [ri-2 / ri-1]）

（令r-1 = a, r0 = b）

def extendedGCD(a, b):
#a*xi + b*yi = ri
if b == 0:
return (1, 0, a)
#a*x1 + b*y1 = a
x1 = 1
y1 = 0
#a*x2 + b*y2 = b
x2 = 0
y2 = 1
while b != 0:
q = a / b
#ri = r(i-2) % r(i-1)
r = a % b
a = b
b = r
#xi = x(i-2) - q*x(i-1)
x = x1 - q*x2
x1 = x2
x2 = x
#yi = y(i-2) - q*y(i-1)
y = y1 - q*y2
y1 = y2
y2 = y
return(x1, y1, a)
Python

posted @ 2013-11-04 14:50  7hat  阅读(4271)  评论(0编辑  收藏  举报