Skip to content

Instantly share code, notes, and snippets.

@b3z
Last active November 26, 2021 06:52
Show Gist options
  • Save b3z/d55d8345ccd02785f792384fa3b637a7 to your computer and use it in GitHub Desktop.
Save b3z/d55d8345ccd02785f792384fa3b637a7 to your computer and use it in GitHub Desktop.
Multiplicative inverse element modulo n
def euka(a, b):
u, v, s, t = 1, 0, 0, 1
while b!=0:
q=a//b
a, b = b, a-q*b
u, s = s, u-q*s
v, t = t, v-q*t
return a, u, v
def modinverse(a, n):
g, u, v = euka(a, n)
return u%n
# print(modinverse(a, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment