Extended Euclidean Algorithm
Extended Euclidean Algorithm(EEA) as suggested by its name, is an extension of Euclidean Algorithm(EA) that alongside with finding GCD(x,y), also finds a way to represent GCD(x,y) in terms of x and y.
Function’s Input: x,y
Function’s Output: GCD(x,y),a,b
In EEA, we will use EA as sub-routine while simultaneously calculating a and b.
Euclidean Algorithm to calculate GCD(x,y)
, recursively calls GCD(y%x,x)
.
Let g = GCD(x,y), x' = y%x, y' = x
.
g, A, B = EEA(x',y')
<—– recursive call
The above statement implies:
A.x' + B.y' = g
Substituting x' = y - floor(y/x).x and y' = x
, we get
Therefore
Implementation: