Learn
Modular Multiplicative Inverse
The modular inverse of modulo is a number such that . It's also known as the multiplicative inverse in modular arithmetic.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
The modular inverse of modulo is a number such that . It's also known as the multiplicative inverse in modular arithmetic.
Apply what you learned by solving the practice problem for Modular Inverse.
Go to Practice ProblemYou can only find the modular inverse when . When is prime, you use Fermat's Little Theorem: .
function modInverse(a, m):
return power(a, m - 2, m)
function power(x, n, mod):
result = 1
x = x mod mod
while n > 0:
if n is odd:
result = (result * x) mod mod
x = (x * x) mod mod
n = n / 2
return result
When to use it: When you divide in modular arithmetic, you multiply by the inverse instead. For , you compute .
Extended GCD method: For non-prime moduli, you use the extended Euclidean algorithm which finds and such that .
Applications: You use modular inverse for division in modular arithmetic, computing nCr mod p, RSA encryption, and Chinese Remainder Theorem.
Time complexity: for the binary exponentiation.
Space complexity: for iterative version.