Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

EXPERIMENT 1

Aim: To implement Extended Euclidean algorithm

CODE:

def gcdExtended(a, b):


# Base Case
if a == 0:
return b, 0, 1
gcd, x1, y1 = gcdExtended(b % a, a)
#Update x and y using results of recursive call
x = y1 - (b//a) * x1
y = x1
return gcd, x, y

a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))
g, x, y = gcdExtended(a, b)
print("GCD of",a,"and",b, "is",g)

OUTPUT:

Enter the first number: 50


Enter the second number: 35
GCD of 50 and 35 is 5

You might also like