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
34 lines (26 loc) · 821 Bytes
/
modular_inverse.py
File metadata and controls
34 lines (26 loc) · 821 Bytes
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
# extended_gcd(a, b) modified from
# https://github.com/keon/algorithms/blob/master/algorithms/maths/extended_gcd.py
def extended_gcd(a: int, b: int) -> [int, int, int]:
"""Extended GCD algorithm.
Return s, t, g
such that a * s + b * t = GCD(a, b)
and s and t are co-prime.
"""
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:
"""
Returns x such that a * x = 1 (mod m)
a and m must be coprime
"""
s, _, g = extended_gcd(a, m)
if g != 1:
raise ValueError("a and m must be coprime")
return s % m