[python]扩展欧几里得

 正向

def ext(a,b):
    if b==0:
        print('found',queue[0],a)
        return queue[0],a
    else:
        q=a//b
        r=a%b
        x=queue[0][0]-q*queue[1][0]
        y=queue[0][1]-q*queue[1][1]
        queue.pop(0)
        queue.append([x,y])
        print('({},{}){}=75*{}+48*{}'.format(x,y,r,x,y))
        return ext(b,a%b)
queue=[[1,0],[0,1]]

ret1=ext(5*7,7*7)
print(ret1)

 逆向

def exgcd(a,b):
    if b==0:
        return 1,0,a
    else:
        x,y,gcd=exgcd(b,a%b)
        x,y=y,x-(a//b)*y
        return x,y,gcd

你可能感兴趣的:(python,开发语言)