forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodular_inverse.py
More file actions
61 lines (44 loc) · 1.33 KB
/
modular_inverse.py
File metadata and controls
61 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Modular Multiplicative Inverse
Find x such that a * x = 1 (mod m) using the Extended Euclidean Algorithm.
Requires a and m to be coprime.
Reference: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
Complexity:
Time: O(log(min(a, m)))
Space: O(1)
"""
from __future__ import annotations
def _extended_gcd(a: int, b: int) -> tuple[int, int, int]:
"""Compute the extended GCD of two integers.
Args:
a: First integer.
b: Second integer.
Returns:
A tuple (s, t, g) where a * s + b * t = g = gcd(a, b).
"""
old_s, s = 1, 0
old_t, t = 0, 1
old_r, r = a, b
while r != 0:
quotient = old_r // r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
return old_s, old_t, old_r
def modular_inverse(a: int, m: int) -> int:
"""Find x such that a * x = 1 (mod m).
Args:
a: The integer to find the inverse of.
m: The modulus (must be coprime with a).
Returns:
The modular multiplicative inverse of a modulo m.
Raises:
ValueError: If a and m are not coprime.
Examples:
>>> modular_inverse(2, 19)
10
"""
s, _, g = _extended_gcd(a, m)
if g != 1:
raise ValueError("a and m must be coprime")
return s % m