Implementing Extended Euclid Algorithm

情到浓时终转凉″ 提交于 2019-12-12 05:12:30

问题


Why is the following implementation of the Extended Euclid Algorithm failing?

def extended_euclid(a,b):
    if b == 0:
        return {a, 1, 0}

    d1,x1,y1 = extended_euclid(b, a % b)
    d = d1
    x = y1
    y = x1 - math.floor(a/b) * y1
    return {d, x, y} 

回答1:


This is the implementation found in https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm , looks similar to your's one.

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



回答2:


def extended_euclid(a,b):
    if b == 0:
        return a, 1, 0

    d1,x1,y1 = extended_euclid(b, a % b)
    d = d1
    x = y1
    y = x1 - math.floor(a/b) * y1
    return d, x, y

remove {} from your return. take a look at d1,x1,y1 = extended_euclid(b, a % b), you don't have enough values to unpack if you keep {} in return.



来源:https://stackoverflow.com/questions/42978574/implementing-extended-euclid-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!