(a: int, m: int)
| 2 | |
| 3 | |
| 4 | def find_mod_inverse(a: int, m: int) -> int: |
| 5 | if gcd_by_iterative(a, m) != 1: |
| 6 | msg = f"mod inverse of {a!r} and {m!r} does not exist" |
| 7 | raise ValueError(msg) |
| 8 | u1, u2, u3 = 1, 0, a |
| 9 | v1, v2, v3 = 0, 1, m |
| 10 | while v3 != 0: |
| 11 | q = u3 // v3 |
| 12 | v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 |
| 13 | return u1 % m |
nothing calls this directly
no test coverage detected