Skip to content

Instantly share code, notes, and snippets.

@mydreambei-ai
Last active February 28, 2025 03:45
Show Gist options
  • Select an option

  • Save mydreambei-ai/6d1509b367a1b709a409326862e5341c to your computer and use it in GitHub Desktop.

Select an option

Save mydreambei-ai/6d1509b367a1b709a409326862e5341c to your computer and use it in GitHub Desktop.

Revisions

  1. mydreambei-ai revised this gist Feb 28, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion invmod.py
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,4 @@ def invmod(n, p):
    return p - q
    if r == n - 1:
    return q + 1
    return ((p-q) * inv(r, p)) % p
    return ((p-q) * invmod(r, p)) % p
  2. mydreambei-ai created this gist Feb 25, 2025.
    15 changes: 15 additions & 0 deletions invmod.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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) * inv(r, p)) % p