Skip to content

Instantly share code, notes, and snippets.

@mydreambei-ai
Last active February 28, 2025 03:45
Show Gist options
  • Save mydreambei-ai/6d1509b367a1b709a409326862e5341c to your computer and use it in GitHub Desktop.
Save mydreambei-ai/6d1509b367a1b709a409326862e5341c to your computer and use it in GitHub Desktop.
inverse mod
def invmod(n, p):
if n >=p :
q, r = divmod(n, p)
if r == 0:
raise ValueError("xxx")
else:
n = r
if n == 1: return 1
q, r = divmod(p, n)
if r == 1:
return p - q
if r == n - 1:
return q + 1
return ((p-q) * invmod(r, p)) % p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment