Last active
February 28, 2025 03:45
-
-
Save mydreambei-ai/6d1509b367a1b709a409326862e5341c to your computer and use it in GitHub Desktop.
inverse mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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