Python
modular arithmetic
multiplicative inverse
programming
coding tutorial

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 11 exists because gcd(3, 11) = 1
  • the inverse of 6 mod 15 does not exist because gcd(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.

python
1def extended_gcd(a, b):
2    if a == 0:
3        return b, 0, 1
4    gcd, x1, y1 = extended_gcd(b % a, a)
5    x = y1 - (b // a) * x1
6    y = x1
7    return gcd, x, y
8
9
10def mod_inverse(a, m):
11    gcd, x, _ = extended_gcd(a, m)
12    if gcd != 1:
13        raise ValueError("inverse does not exist")
14    return x % m
15
16
17print(mod_inverse(3, 11))

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

python
print(pow(3, -1, 11))

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:

python
print(pow(3, 11 - 2, 11))

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:

python
b_inv = pow(7, -1, 13)
result = (5 * b_inv) % 13
print(result)

This computes the modular version of dividing 5 by 7 modulo 13.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms