Modular Inverse Calculator

Stephanie Ben-Joseph headshot Stephanie Ben-Joseph

Introduction to finding modular inverses

A modular inverse calculator answers a precise congruence question: for a chosen integer a and positive modulus m, is there an x for which a × x ≡ 1 (mod m), and what least nonnegative residue can represent that x? This operation is fundamental in residue arithmetic, including number-theory exercises, cryptographic calculations, checksums, coding theory, and algorithms that require division modulo a number.

This page focuses on that one test. Enter the value to invert and its modulus; the extended Euclidean algorithm determines their greatest common divisor and, when possible, supplies a coefficient that is the inverse. The result is either a usable residue or an explanation that the required inverse does not exist.

The sections below explain what the two entries mean, why coprimality decides the question, how the formula and algorithm produce an inverse, and how to verify a result by multiplication. No physical unit is involved: a modular inverse is an integer residue interpreted relative to a particular modulus.

What modular inverse problem does this calculator solve?

This modular inverse calculator determines whether the entered value a has a multiplicative inverse in arithmetic modulo m. It seeks an x whose product with a leaves remainder 1 on division by m; it does not perform ordinary division or return a decimal reciprocal.

The deciding condition is coprimality. When gcd(a, m) = 1, the residue class of a has exactly one inverse modulo m. When the gcd is not 1, a and m share a factor, so no product a × x can be congruent to 1 modulo m. That quick distinction is useful before using a value as a divisor in a modular equation.

Equivalent representatives lead to the same question: replacing a with a value congruent to it modulo m does not change whether an inverse exists or which inverse residue is returned. For example, 33 and 7 represent the same residue modulo 26. Negative values of a are also meaningful because they can be reduced to a nonnegative residue before the inverse is found.

How to use this modular inverse calculator

  1. Enter Integer a as the whole-number residue or integer you want to invert.
  2. Enter Modulus m as an integer greater than 1 that defines the congruence.
  3. Select Calculate inverse to obtain either an inverse residue or a message stating the gcd that prevents one.
  4. When an inverse is returned, multiply it by a and reduce the product modulo m to confirm that the remainder is 1.

For repeated modular inverse checks with one modulus, keep m unchanged and test each integer a separately. The inverse is tied to the chosen modulus: a value that works modulo one number need not work modulo another. If you copy a result into your notes, record the modulus beside it so the residue keeps its mathematical context.

Inputs for a modular inverse calculation

For this modular inverse calculator, the two inputs are integers with different roles: a is the value being inverted, while m sets the arithmetic system in which the inverse must work.

Integer a is the number, or residue-class representative, whose multiplicative inverse is needed. Modulus m is the positive integer used to reduce products and residues. Although a may be negative or larger than m, both entries should be whole numbers within JavaScript’s safe integer range so that the arithmetic remains exact.

If a is larger than m, it is still meaningful because modular arithmetic reduces it to its residue class; reducing it yourself first can make a hand check easier. A result of no inverse is not a calculator failure—it is the expected outcome when the two inputs have a common factor.

The modular inverse formula and extended Euclidean method

The defining formula for a modular inverse requires the product of a and x to differ from 1 by an integer multiple of m. In congruence notation, the condition is:

a x 1 ( mod m )

This statement is equivalent to saying that there is some integer y for which a × x + m × y = 1. The extended Euclidean algorithm finds the required coefficients without testing every possible residue. Specifically, it finds integers x and y satisfying:

a x + m y = gcd ( a , m )

When the greatest common divisor is 1, reducing that identity modulo m removes the m × y term and leaves the modular inverse condition. The coefficient x attached to a is therefore an inverse, although it may initially be negative.

The script converts that coefficient to its least nonnegative representative by reducing it modulo m. This normalization is why a coefficient such as −11 can appear as 15 when the modulus is 26: the two integers differ by 26 and represent the same residue class. If the algorithm finds a gcd other than 1, no integer coefficients can make the linear combination equal 1, so no modular inverse exists.

Worked example: inverse of 7 modulo 26

This modular inverse worked example uses a = 7 and m = 26, a pair for which the extended Euclidean algorithm gives an exact, easily checked result. First, Euclidean division shows that gcd(7, 26) = 1, so an inverse must exist.

Working backward through the Euclidean divisions gives 1 = 3 × 26 − 11 × 7. The coefficient on 7 is −11, and reducing −11 modulo 26 gives 15. Therefore the calculator returns 15 as the inverse of 7 modulo 26.

The multiplication check is 7 × 15 = 105, and 105 leaves remainder 1 when divided by 26 because 105 = 4 × 26 + 1. This is the meaningful way to validate an inverse: multiply the entered a by the returned residue and confirm congruence to 1.

For a contrasting example, choose a = 8 and m = 26. Their greatest common divisor is 2, so every product 8 × x is even while every number congruent to 1 modulo 26 is odd. The mismatch makes an inverse impossible, and the result correctly reports the shared factor.

Comparison table: modular inverse outcomes with modulus 12

This modular inverse comparison keeps m = 12 fixed and shows that changing a can preserve an inverse, change its residue, or eliminate invertibility when a shares a factor with the modulus.

Scenario Integer a Modulus m Modular inverse result Interpretation
First coprime residue 5 12 5 Because 5 × 5 = 25 ≡ 1 (mod 12), 5 is its own inverse.
Second coprime residue 7 12 7 Because 7 × 7 = 49 ≡ 1 (mod 12), 7 is also its own inverse.
Shared-factor residue 8 12 No inverse gcd(8, 12) = 4, so no product 8 × x can be congruent to 1 modulo 12.

These rows illustrate why percentage-style changes are not meaningful for modular inputs: a and m are integers, and divisibility—not the size of an adjustment—controls solvability. Test the exact residues required by your problem.

How to interpret a modular inverse result

The modular inverse calculator returns either a residue x or a no-inverse message, and each result has a direct mathematical interpretation. When an inverse appears, it is the nonnegative representative of the unique inverse class modulo m, so verify that a × x ≡ 1 (mod m).

Other integers can represent that same answer. If the result is x, then x + m, x − m, and x plus any integer multiple of m are equivalent inverses. The calculator displays the least nonnegative representative because it is compact and conventional, not because the other representatives are mathematically wrong.

When the result says no inverse exists, read the displayed gcd as the reason. A gcd greater than 1 means the entered a and modulus have a common divisor; every product a × x retains that divisibility relationship and cannot become congruent to 1 modulo m.

The Copy Inverse button copies the result text shown by the calculator. It can be useful for preserving a checked residue in notes, but retain the original a and m alongside it because an inverse without its modulus is incomplete information.

Limitations and assumptions for modular inverses

Even a straightforward modular inverse calculation depends on strict integer and divisibility assumptions. Keep the following boundaries in mind when applying a result:

If you use a modular inverse in cryptography, a proof, or number-theory coursework, verify the product in the same modulus used in the source problem. The most reliable use of this calculator is as a check on the exact congruence a × x ≡ 1 (mod m), together with the gcd condition that explains why the check can or cannot succeed.

Calculate an integer’s inverse modulo m

Enter a and m to compute the modular inverse.

Modular Lock: an inverse-finding mini-game

Put the same modular inverse idea into motion. Each lock displays a congruence of the form a × ? ≡ 1 (mod m). Select the correct residue on the ring, or choose the central No inverse core when a and m share a factor. Correct locks build a streak and add time; mistakes cost time, so checking coprimality before guessing pays off.

Score0
Time75
Streak0
Locks0
Best0
Your browser does not support the canvas used by the modular inverse game.

Unlock the residue rings

Solve a × ? ≡ 1 (mod m). Tap a numbered residue, or tap the center when no inverse exists. You have 75 seconds, and the moduli grow as your run advances.

Pointer or tap: choose a node. Keyboard: ←/→ and Enter; press N for no inverse.

The game is optional and does not change the calculator inputs or result. Its central lesson is the same: an inverse exists exactly when gcd(a, m) = 1.