MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / invert_modulo

Function invert_modulo

maths/chinese_remainder_theorem.py:59–71  ·  view source on GitHub ↗

>>> invert_modulo(2, 5) 3 >>> invert_modulo(8,7) 1

(a: int, n: int)

Source from the content-addressed store, hash-verified

57
58# This function find the inverses of a i.e., a^(-1)
59def invert_modulo(a: int, n: int) -> int:
60 """
61 >>> invert_modulo(2, 5)
62 3
63
64 >>> invert_modulo(8,7)
65 1
66
67 """
68 (b, _x) = extended_euclid(a, n)
69 if b < 0:
70 b = (b % n + n) % n
71 return b
72
73
74# Same a above using InvertingModulo

Callers 1

Calls 1

extended_euclidFunction · 0.70

Tested by

no test coverage detected