How to Find the Multiplicative Inverse Modulo n
The multiplicative inverse of an integer a modulo n is a number b such that
[ a \cdot b \equiv 1 \pmod{n}. ]
When such a b exists, it is unique modulo n and makes a real difference in cryptography, coding theory, and solving linear congruences. Below is a step‑by‑step guide that explains the theory, lists reliable algorithms, and walks through concrete examples so you can compute inverses confidently.
What Is a Multiplicative Inverse Modulo n?
In ordinary arithmetic, the inverse of a is (1/a). In modular arithmetic we replace division with multiplication by a number that “undoes” a under the modulus n. Formally, we seek an integer b satisfying
[ a b = 1 + kn \quad\text{for some integer }k. ]
If such a b exists, we write (b \equiv a^{-1} \pmod{n}). Not every a has an inverse; existence depends on the relationship between a and n.
When Does an Inverse Exist? (The Coprime Condition)
A multiplicative inverse of a modulo n exists iff a and n are coprime, i.e.,
[ \gcd(a,n)=1. ]
Proof sketch: Bézout’s identity guarantees integers x,y with (ax+ny=\gcd(a,n)). If the gcd is 1, we have (ax+ny=1), which reduces modulo n to (ax\equiv1\pmod{n}). Hence x is the inverse. Conversely, if a common divisor d>1 divided both a and n, then (ab\equiv1\pmod{n}) would imply (d\mid1), impossible.
Key point: Always check (\gcd(a,n)=1) before attempting to compute an inverse.
Methods to Find the Inverse
Several algorithms can produce the inverse efficiently. Choose the one that best fits the size of n and whether n is prime Most people skip this — try not to. And it works..
1. Extended Euclidean Algorithm (EEA)
The EEA not only computes (\gcd(a,n)) but also returns the Bézout coefficients x,y such that
[ ax + ny = \gcd(a,n). ]
When the gcd is 1, the coefficient x (taken modulo n) is the desired inverse.
Steps
- Apply the Euclidean algorithm to a and n, recording each remainder.
- Back‑substitute (or use the iterative version) to express the gcd as a linear combination of a and n.
- If the gcd = 1, set (b = x \bmod n). If the gcd > 1, no inverse exists.
Why it works: The algorithm directly yields the Bézout identity, which is the definition of a modular inverse.
2. Fermat’s Little Theorem (Prime Modulus)
If n = p is prime and a is not divisible by p, then
[ a^{p-1} \equiv 1 \pmod{p}. ]
Multiplying both sides by (a^{-1}) gives
[ a^{p-2} \equiv a^{-1} \pmod{p}. ]
Thus, the inverse is (a^{p-2}\bmod p), computable with fast modular exponentiation (binary exponentiation) in (O(\log p)) time.
3. Euler’s Theorem (General Modulus)
When n is not prime but (\gcd(a,n)=1), Euler’s theorem states
[ a^{\phi(n)} \equiv 1 \pmod{n}, ]
where (\phi(n)) is Euler’s totient function. As a result,
[ a^{\phi(n)-1} \equiv a^{-1} \pmod{n}. ]
This method requires computing (\phi(n)), which is easy if the prime factorization of n is known.
4. Brute Force Search (Small Moduli)
For very small n (say n < 1000), simply test each candidate b from 1 to n‑1 until (ab\bmod n = 1). While simple, this (O(n)) approach becomes impractical for large moduli Worth knowing..
5. Using Built‑In Functions (Programming Languages)
Most languages provide a modular inverse routine:
- Python:
pow(a, -1, n)(since Python 3.8) orpow(a, phi(n)-1, n)when (\phi(n)) known. - C++: Implement EEA or use libraries like Boost.Multiprecision.
- Matlab/Octave:
modinv(a,n)from the Symbolic Math Toolbox.
These implementations internally rely on the EEA, guaranteeing logarithmic time complexity Worth keeping that in mind..
Step‑by‑Step Example: Inverse of 3 Modulo 11
Let’s find (3^{-1}\pmod{11}) using the Extended Euclidean Algorithm.
-
Euclidean division [ 11 = 3\cdot3 + 2 \ 3 = 2\cdot1 + 1 \ 2 = 1\cdot2 + 0 ] The last non‑zero remainder is 1 → (\gcd(3,11)=1); an inverse exists.
-
Back‑substitution [ 1 = 3 - 2\cdot1 \ 2 = 11 - 3\cdot3 \ \Rightarrow 1 = 3 - (11 - 3\cdot3)\cdot1 = 3 - 11 + 3\cdot3 = 4\cdot3 - 1\cdot11 ] Hence, (4\cdot3 + (-1)\cdot11 = 1).
-
Extract the coefficient of 3: (x = 4). Reduce modulo 11: (4 \bmod 11 = 4).
-
Verification: (3 \times 4 = 12 \equiv 1 \pmod{11}). ✅
Thus, the multiplicative inverse of 3 modulo 11 is 4.
Step‑by‑Step Example: Inverse of 7 Modulo 26 (Non‑Prime Modulus)
Here (n=26) (not prime). First check coprimality: (\gcd(7,26)=1). We’ll use the EEA Simple, but easy to overlook..
-
Euclidean steps [ 26 = 7\cdot3 + 5 \ 7 = 5\cdot1 + 2 \ 5 = 2\cdot2 + 1 \ 2 = 1\cdot2 + 0 ]
-
Back‑substitution [ 1 =
-
Back‑substitution
Substituting backwards through the Euclidean chain:
[ \begin{aligned} 1 &= 5 - 2\cdot 2 \ &= 5 - (7 - 5\cdot 1)\cdot 2 \ &= 5 - 2\cdot 7 + 2\cdot 5 \ &= 3\cdot 5 - 2\cdot 7 \ &= 3,(26 - 3\cdot 7) - 2\cdot 7 \ &= 3\cdot 26 - 9\cdot 7 - 2\cdot 7 \ &= 3\cdot 26 - 11\cdot 7 . \end{aligned} ]
Thus (1 \equiv -11\cdot 7 \pmod{26}), or equivalently (-11 \equiv 15 \pmod{26}). Which means, the multiplicative inverse of (7) modulo (26) is (15). Verification confirms that (7 \times 15 = 105 \equiv 1 \pmod{26}).
Summary of Methods
Each technique offers distinct advantages depending on the context:
| Method | Best For | Complexity |
|---|---|---|
| Fermat’s Little Theorem | Prime modulus (p) | (O(\log p)) via fast exponentiation |
| Euler’s Theorem | General coprime modulus (n) | Requires (\phi(n)); still (O(\log n)) after factorisation |
| Brute Force | Very small (n < 1000) | Simple but linear, quickly becomes infeasible |
| Built‑in Routines | Production code | Optimised implementation (usually extended Euclidean algorithm) |
This is the bit that actually matters in practice.
In practice, the extended Euclidean algorithm (EEA) remains the workhorse because it computes both (\gcd(a,n)) and the coefficients giving the inverse directly, running in (O(\log n)) time regardless of whether (n) is prime or composite. Most modern programming languages expose this capability either explicitly (as in Python’s pow(a, -1, n)) or indirectly through specialized libraries.
Beyond these core techniques, advanced variants exist. In practice, the Quadratic Sieve and Pollard’s rho algorithm can find discrete logarithms needed for cryptographic protocols such as RSA, while Gaussian elimination over finite fields provides another perspective on solving linear congruences. Even so, for everyday modular inversion tasks, the EEA stands out as the most reliable and efficient choice.
This changes depending on context. Keep that in mind.
Conclusion
Modular inversion—calculating the integer (b) satisfying (ab
Conclusion
Modular inversion—calculating the integer (b) satisfying (ab \equiv 1 \pmod{n})—remains a cornerstone operation in number theory and its applied fields. Day to day, as hardware continues to accelerate modular arithmetic, research into faster variants (e. Even so, in modern practice, the extended Euclidean algorithm is the default workhorse, offering logarithmic‑time performance and a clear path to both the inverse and a proof of coprimality. , binary GCD, Montgomery‑based inversion) and parallel implementations ensures that modular inversion will keep pace with the ever‑growing demands of post‑quantum cryptography and large‑scale scientific computing. Whether one is solving linear congruences, implementing cryptographic schemes, or optimizing error‑correcting codes, the ability to compute inverses efficiently underpins the correctness and performance of the whole system. g.Mastery of these techniques equips mathematicians, computer scientists, and engineers with a versatile tool that bridges abstract theory and real‑world problem solving.
People argue about this. Here's where I land on it And that's really what it comes down to..