Modular Inverses
Suppose you want to solve . Is there an integer that plays the role of ""? This is true in the world of remainders modulo 31. This is the question of modular inverses. It turns out to have a remarkably clean answer. Division is the one arithmetic operation that fails to survive intact. This happens when we pass from ordinary integers to residues modulo . Understanding exactly when it survives — and how to recover it — is one of the foundational pillars on which the rest of elementary number theory is built. The idea matters because so much of number theory, cryptography, and computer algebra is secretly about solving equations inside . Every time RSA encryption decrypts a message, the same question lurks underneath. Every time a computer algebra system solves a linear congruence, it lurks too. Every time a hash table probes indices modulo its size, the question reappears. For which does there exist a multiplicative partner with ? And how do we find it fast? The answer — exactly when — is short to state. But it is rich in consequence. It connects Bezout's Identity, The Extended Euclidean Algorithm, and the structure of as a group. The big picture is that modular arithmetic modulo forms a ring. The units of that ring have multiplicative inverses. These units form a group under multiplication. Classifying the units of is the first step toward Euler's theorem. It also leads toward Fermat's little theorem, and the notion of the Order of an Element. Eventually it connects to the structure theory of finite abelian groups applied to number theory. Without inverses we cannot even meaningfully "divide" in modular arithmetic. So solving Linear Congruences of the form reduces almost entirely to first finding an inverse of . This article gives the precise definition of a modular inverse. It proves the fundamental existence theorem: invertibility holds exactly when . It gives a full algorithmic account of how to compute inverses via the extended Euclidean algorithm. It works two nontrivial numerical examples. Finally, it examines exactly what goes wrong when the coprimality hypothesis fails.
- An inverse of modulo is a residue with ; when it exists it is unique modulo .
- The inverse of mod exists if and only if .
- Existence is proved via Bezout's Identity: gives integers with , so is the inverse.
- The Extended Euclidean Algorithm computes this efficiently, in arithmetic steps.
- The invertible residues mod form a group under multiplication, denoted , of order .
- Key idea: modular division by is well-defined precisely on the units of , and Bezout's identity turns the abstract existence statement into an explicit, computable inverse.
Where this lives today
Modular inverses are the computational backbone of RSA and elliptic-curve cryptography: key generation and decryption both require computing an inverse modulo a large composite or prime, almost always via the extended Euclidean algorithm rather than brute force, because brute force is infeasible for numbers with hundreds of digits. In error-correcting codes and finite-field arithmetic (e.g. Reed–Solomon codes), inverses in or are needed at every decoding step. In computer algebra systems, solving linear congruences and systems of congruences via the Chinese Remainder Theorem reduces to computing inverses in each modulus. In hashing and pseudorandom number generation, invertibility modulo the table size or modulus ensures certain linear maps remain bijective, which is essential for full-period linear congruential generators. The same existence criterion also foreshadows The Order of an Element in , Wilson's Theorem's use of inverse-pairing among residues modulo a prime, and general Linear Congruences theory, where solving reduces immediately to multiplying both sides by whenever it exists.
Ask the tutor: How does the extended Euclidean algorithm's back-substitution actually produce the coefficients in Bezout's identity, step by step, for a modulus of my choosing?
Let be a positive integer and let be an integer. A modular inverse of modulo is an integer such that
that is, . If such a exists, we say is invertible (or a unit) modulo , and we write . Because depends only on the residue classes of and modulo , invertibility and the inverse (when it exists) are properties of the residue class , not of the particular representative integer . The set of invertible residue classes modulo , with multiplication induced from , is denoted
(the equality of this set with the units will be exactly the content of the theorem below).
Intuition
Ordinary division by a nonzero integer is a global operation on : every nonzero rational number has a reciprocal. Modular arithmetic is different because is a finite ring with zero divisors when is composite — for instance , yet neither nor is . In such a ring, not every nonzero element can have a reciprocal, since if had an inverse and with , multiplying by would force , a contradiction. So zero divisors are never invertible, and the question becomes: which residues escape being zero divisors entirely? The answer is visible already in the structure of the complete residue system . Picture multiplication by as a function on this set. If , then every multiple of modulo is automatically a multiple of (since is), so the map can never hit when . Coprimality removes this obstruction entirely, and Bezout's identity shows the obstruction is the only one: the linear combinations range exactly over the multiples of , so is achievable precisely when .
Intuition (continued): the Euclidean algorithm as a search for the inverse
The extended Euclidean algorithm gives more than an abstract existence proof: it is an efficient constructive method. Running the Euclidean algorithm on backward through its sequence of remainders expresses as an integer combination , and reducing modulo produces the inverse directly — no search over required.
Let be a positive integer and an integer. Then: 1. (Existence) has a modular inverse modulo if and only if . 2. (Uniqueness) If has an inverse modulo , it is unique modulo : any two inverses satisfy .
Existence, (). Suppose . By Bezout's Identity, there exist integers such that
Reducing this equation modulo eliminates the term , since , giving
Hence is a modular inverse of modulo ; existence is proved, and the proof is constructive once we recall that Bezout's identity itself is proved constructively via the Extended Euclidean Algorithm.
Existence, (). Suppose has a modular inverse modulo , so . By definition of congruence this means , i.e. there is an integer with
Let . Since and , divides every integer linear combination of and ; in particular . As is a positive integer dividing , we conclude , i.e. . This proves the biconditional in (1): the direction shows coprimality is necessary, and shows it is sufficient, and neither direction invokes the theorem itself, so the argument is non-circular.
Uniqueness. Suppose and are both modular inverses of modulo , so
Subtracting the two congruences (permissible since congruence modulo is compatible with addition and subtraction) gives
that is, . By part (1) (already established), since we know . A standard consequence of together with is : indeed, by Bezout's identity there are integers with , so
and both terms on the right are divisible by (the first because , the second visibly), so . Hence , proving uniqueness.
∎The set of residue classes modulo that possess a multiplicative inverse is exactly
and this set, with multiplication induced from , forms an abelian group of order , where is Euler's totient function.
By the theorem, has an inverse exactly when , so the two descriptions of the set coincide; its cardinality is by the definition of Euler's totient function. We check the group axioms. Closure: if then (a prime dividing would have to divide or , contradicting one of the coprimality assumptions), so is again a unit. Associativity and commutativity are inherited from multiplication in , which descends to well-defined, associative, commutative multiplication on residue classes. Identity: is a unit and acts as identity. Inverses: by the theorem, every element of the set has an inverse, and that inverse is itself coprime to (if then any common factor of and would divide hence force issues; more directly, is a unit by the same theorem applied to , since exhibits as an inverse of ), so inverses stay inside the set. All group axioms hold.
∎Worked examples
Example 1: Inverse of 17 modulo 43. First confirm . Since is prime and , this is immediate, but let us verify with the Euclidean algorithm, since we need its steps anyway:
The last nonzero remainder is , confirming , so an inverse exists. Now back-substitute to write as an integer combination of and :
So
which reads . Reducing modulo gives . So the inverse is
Check: , and since . Indeed , confirming .
Example 2: Inverse of 24 modulo 91 — a case requiring the full algorithm on composite modulus. Here . Check via Euclid:
Last nonzero remainder is , so and the inverse exists (this also reconfirms independently that shares no factor with either or ). Back-substitute:
So , giving
Check: . Divide: , since . So , confirming .
Consider and , so . The theorem's necessity direction predicts no inverse exists; let us verify this directly by brute force, confirming there is no gap. Compute for every residue :
The complete list of values attained is — exactly the multiples of inside — and never appears. So no satisfies : has no inverse modulo . This is exactly what the () direction of the theorem's proof predicts: if an inverse existed, then would have to divide 's complement, forcing , which is false. Dropping the hypothesis genuinely breaks the conclusion — it is not a vacuous or a technical assumption, but the precise dividing line between residues that are units and residues that are zero divisors. Note also that is a zero divisor modulo : with , consistent with the general fact that zero divisors are never invertible.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| Unique inverse mod exists | Bezout's identity, theorem above | |
| No inverse exists | forces , contradiction | |
| prime, | Inverse always exists | automatic since prime |
| , | No inverse (special case of ) | |
| Working with negative | Reduce first; theorem is stated for the residue class | Congruence only depends on residue class |
| Every integer is "invertible" trivially, since all integers are | Degenerate ring with one element |
The mathematician behind it
Origins · 1 minÉtienne Bézout and how this was discovered
Bézout is best remembered for Bézout's identity and Bézout's theorem in algebraic geometry, both expressing the same intuition: that the "size" of an intersection or combination is governed by a greatest common divisor or a product of degrees. His systematic study, in the 1770s, of the integer solvability of gave the theoretical machinery — later streamlined via the Euclidean algorithm — that makes the existence of modular inverses a one-line consequence rather than a case-by-case verification.
Does every nonzero residue modulo have an inverse?
Only when is prime. If is prime, every with automatically satisfies , since 's only divisors are and itself, and rules out . So modulo a prime, every nonzero residue is invertible, and has all nonzero classes as units. For composite , this fails: any sharing a common prime factor with is a zero divisor and has no inverse, as shown in the counterexample with modulo .
How do I actually compute a modular inverse by hand efficiently?
Run the Euclidean algorithm on to obtain a sequence of quotients and remainders down to , then back-substitute from the last nontrivial step upward to express for some integers . The residue of modulo is the inverse. This is the extended Euclidean algorithm, and it runs in time proportional to the number of steps in the ordinary Euclidean algorithm, which is divisions — dramatically faster than testing every residue.
Is the modular inverse the same as as a real number reduced mod ?
No — there is no meaningful reduction of the real number modulo since typically is not an integer. The modular inverse is a genuinely different integer , found by solving , and it has nothing to do with rounding or truncating the real reciprocal. For instance , which bears no numerical resemblance to .
What happens if I try to invert 0 modulo n, for n > 1?
It never has an inverse. Since for every , and whenever , no can satisfy . This matches the theorem: , which equals only when , a degenerate modulus where every congruence is trivially true.
Can an element have more than one inverse modulo n?
No, and this is the content of the uniqueness half of the theorem. If and both invert modulo , then , and because this forces , i.e. . So while there are infinitely many integers representing the inverse (e.g. all represent the same class modulo in Example 1), there is exactly one residue class.
How does this relate to solving general linear congruences ?
Once you know exists (i.e., ), you solve by multiplying both sides by , giving the unique solution . When , the congruence needs a separate, more general treatment: it has solutions only if , and then there are exactly solutions modulo . That fuller theory is the subject of Linear Congruences, and it builds directly on the existence criterion proved here.
Why is coprimality with n, rather than with any particular prime factor, the right condition?
Because is exactly equivalent to sharing no prime factor with at all — if shared even one prime factor of , that would divide , blocking invertibility, as the necessity proof shows via divisibility of . Conversely, having no shared prime factor is precisely what Bezout's identity requires and delivers: is the necessary and sufficient condition, not merely coprimality with some factor or most factors.