Skip to content

Instantly share code, notes, and snippets.

@Bitatlas
Created February 3, 2021 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bitatlas/4a6fa3742713f50c07ed6aa6ae78bd94 to your computer and use it in GitHub Desktop.
Save Bitatlas/4a6fa3742713f50c07ed6aa6ae78bd94 to your computer and use it in GitHub Desktop.
Common divider and mod inverse
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exemption('modular inverse does not exist')
else:
return x % m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment