I programmed the extended Euclidean algorithm together with the inverse modulo because I am making an RSA system from scratch. Any feedback regarding efficiency etc. is welcome :)
def ext_gcd(a, b):
a0, a1 = a, b
x0, x1 = 1, 0
y0, y1 = 0, 1
while a1 != 0:
q = a0//a1
r, s, t = a1, x1, y1
a1 = a0 % a1
x1 = x0 - q*x1
y1 = y0 - q*y1
a0, x0, y0 = r, s, t
return x0, y0, a0
def inverse_mod(a, mod):
va, y0, a0 = Math.ext_gcd(a, mod)
return va % mod