Modular multiplicative inverse function in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A modular multiplicative inverse of a modulo m is a number x such that a * x mod m = 1. It exists only when a and m are coprime. In Python, you can compute it with the extended Euclidean algorithm, and in modern Python you can also use the built-in pow function directly.
The Mathematical Condition
The inverse exists if and only if gcd(a, m) = 1.
Example:
- inverse of
3 mod 11exists becausegcd(3, 11) = 1 - inverse of
6 mod 15does not exist becausegcd(6, 15) = 3
That condition matters because modular inverse is not defined for every pair of integers.
Extended Euclidean Algorithm
The most general algorithm is the extended Euclidean algorithm. It finds integers x and y such that:
- '
a * x + m * y = gcd(a, m)'
If the gcd is 1, then x is the modular inverse of a modulo m.
This prints 4, because 3 * 4 = 12 and 12 mod 11 = 1.
Built-In Python Shortcut
Modern Python has a very convenient option:
This also prints 4.
It is concise and usually the best choice when you simply need the inverse. If the inverse does not exist, Python raises an error rather than returning a meaningless value.
Fermat's Little Theorem
If the modulus is prime and a is not divisible by that prime, Fermat's little theorem gives another approach:
- '
a^(m-2) mod m'
This works for m = 11 because 11 is prime.
It is a useful technique in competitive programming and number theory code, but the extended Euclidean algorithm or pow(a, -1, m) is usually more general and easier to explain.
Practical Uses
Modular inverses appear in:
- RSA and other cryptographic algorithms
- modular division in number theory
- combinatorics under a prime modulus
- solving linear congruences
For example, if you need to compute a / b mod m, you often rewrite it as a * inverse(b) mod m, assuming the inverse exists.
This computes the modular version of dividing 5 by 7 modulo 13.
Negative Values and Normalization
The inverse returned by the extended Euclidean algorithm may be negative before normalization. That is normal.
For instance, if x = -7 is a valid algebraic solution, x % m converts it into the standard nonnegative representative in the range 0 through m - 1.
That is why return x % m appears in most implementations.
Common Pitfalls
The biggest mistake is trying to compute a modular inverse when a and m are not coprime.
Another mistake is using the Fermat shortcut with a modulus that is not prime.
A third issue is forgetting to normalize a negative inverse into the usual modular range.
Summary
- A modular inverse exists only when
gcd(a, m) = 1 - The extended Euclidean algorithm is the general-purpose way to compute it
- In modern Python,
pow(a, -1, m)is the simplest built-in option - Fermat's little theorem works when the modulus is prime
- Normalize the result with
% mso the inverse is returned in the expected range

