Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

    d1,x1,y1 = extended_euclid(b, a % b)
    d = d1x=y1y= 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.

Solution 2:

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):
    ifa== 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)

Post a Comment for "Implementing Extended Euclid Algorithm"