Last active
October 11, 2018 12:50
-
-
Save Francis-Lee-Earth/f5d16c22a2aac3457541df3844c8abd7 to your computer and use it in GitHub Desktop.
Modular Multiplicative Inverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def greatest_common_divisor_extended(a, b): | |
''' | |
擴展歐幾里得算法 (Extended Euclidean algorithm) | |
''' | |
if b == 0: | |
return a, 1, 0 | |
gcd, y, x = greatest_common_divisor_extended(b, a % b) | |
return gcd, x, y - (a // b) * x | |
def inverse_modular(a, b): | |
''' | |
Mod 反元素 (Modular Multiplicative Inverse) | |
''' | |
gcd, x, y = greatest_common_divisor_extended(a, b) | |
return x % b | |
a, b = 43, 26 | |
c = inverse_modular(a, b) | |
print(c) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment