简体   繁体   中英

Implementing Extended Euclid Algorithm

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} 
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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM