mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-28 07:21:07 +00:00
9d745b6156
* updating DIRECTORY.md * updating DIRECTORY.md * Fixed accidental commit of file I have't touched * fixup! Format Python code with psf/black push * updating DIRECTORY.md * updating DIRECTORY.md * Fixed some suggested coding style issues * Update rsa_key_generator.py * Update rsa_key_generator.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <johnlaw.po@gmail.com>
16 lines
372 B
Python
16 lines
372 B
Python
def gcd(a: int, b: int) -> int:
|
|
while a != 0:
|
|
a, b = b % a, a
|
|
return b
|
|
|
|
|
|
def findModInverse(a: int, m: int) -> int:
|
|
if gcd(a, m) != 1:
|
|
return None
|
|
u1, u2, u3 = 1, 0, a
|
|
v1, v2, v3 = 0, 1, m
|
|
while v3 != 0:
|
|
q = u3 // v3
|
|
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
|
|
return u1 % m
|