Modular multiplicative inverse in Python: pow(a, -1, m) and extended Euclid
Codemia Knowledge Hub ·
In modern Python, pow(a, -1, m) returns the modular multiplicative inverse of a modulo m, the number x with a * x mod m = 1. The inverse exists only when a and m are coprime. Without pow, use the extended Euclidean algorithm.
When the inverse exists
The inverse exists if and only if gcd(a, m) = 1. For example:
- the inverse of
3 mod 11exists becausegcd(3, 11) = 1 - the inverse of
6 mod 15does not exist becausegcd(6, 15) = 3
Extended Euclidean algorithm
The extended Euclidean algorithm is the general method. It finds integers x and y such that a * x + m * y = gcd(a, m). If the gcd is 1, then x is the inverse of a modulo m.
This prints 4, because 3 * 4 = 12 and 12 mod 11 = 1.
The x the algorithm returns can be negative, which is normal. For instance, if x = -7 is a valid solution, x % m turns it into the standard representative in the range 0 through m - 1. That is why return x % m appears in most implementations.
The built-in pow shortcut
This also prints 4. It is concise and usually the best choice when you just need the inverse. If the inverse does not exist, Python raises an error instead of returning a meaningless value.
Fermat's little theorem for a prime modulus
If m is prime and a is not divisible by it, the inverse is a^(m-2) mod m:
This works for m = 11 because 11 is prime. It is common in competitive programming and number theory code, but pow(a, -1, m) and the extended Euclidean algorithm are more general, and Fermat's shortcut gives wrong results with a modulus that is not prime.
Modular division and other uses
Modular inverses appear in:
- RSA and other cryptographic algorithms
- modular division in number theory
- combinatorics under a prime modulus
- solving linear congruences
To compute a / b mod m, rewrite it as a * inverse(b) mod m, assuming the inverse exists:
This computes the modular version of dividing 5 by 7 modulo 13.
Related reading
- module 'keras.engine' has no attribute 'Layer
- module 'keras.utils.generic_utils' has no attribute 'get_custom_objects' when importing segmentation_models
- module 'numpy.distutils.__config__' has no attribute 'blas_opt_info
- ''module'' object has no attribute ''feature_column''
- ''module'' object has no attribute ''SummaryWriter''
- module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__' google colab error
- module 'tensorflow._api.v2.train' has no attribute 'GradientDescentOptimizer
- Module 'tensorflow' has no attribute 'contrib
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.