Since gcd(a,b) = ax + by, x, y Z, finding x and y will be useful for computing modular multiplicative inverses
Extended-Euclid(a,b) if b = 0 then return(a, 1, 0) (d', x', y') = Extended-Euclid(b, a mod b) (d, x, y) = (d', y', x' - y') return(d, x, y)
Running time same as Euclid algorithm.
Example
(d, x, y) = Extended-Euclid(6, 3) (d', x', y') = Extended-Euclid(3, 0) = (3, 1, 0) d = 3, x = 0, y = 1 - 6/3 * 0 = 1 = (3, 0, 1) d = 3, x = 0, y = 1 6x + 3y = d 6*0 + 3*1 = 3