Modular Inverse Calculator - Find a^-1 mod n Online

n = 2 × 13, φ(n) = 12

Fill this in to divide b by a modulo n

7⁻¹ mod 26

15

7 × 15 = 105 ≡ 1 (mod 26)

That is 4 × 26 + 1, which is the only check worth running — if the product is not 1, the inverse is wrong.

gcd(a, n)

1

coprime, so the inverse exists

Signed representative

-11

same class, what raw Euclid returns

Division steps used

4

to settle a 5-bit modulus

Invertible residues

12

φ(n) out of 26 — 46%

Extended Euclid on (26, 7) — the t column carries the coefficient of a, and its value on the gcd row is the inverse

iDivisionRemainder rs (× n)t (× a)
0start: n2610
1start: a701
226 = 3 × 7 + 551-3
37 = 1 × 5 + 22-14
45 = 2 × 2 + 113-11
52 = 2 × 1 + 00-726

(3) × 26 + (-11) × 7 = 1

t = -11 → add 26 → 15

The Bézout identity above is the whole algorithm. Drop the n term modulo n and you are left with t × a ≡ gcd, which is exactly the definition of an inverse when the gcd is 1.

What each method costs on this exact input

MethodOperationsNeeds to know
Extended Euclid4 divisionsnothing but a and n
Euler exponentiation5 modular multiplicationsφ(n) = 12, so n's factorisation
Brute-force scan15 trialsnothing, and it shows

Every residue mod 26 — filled squares are invertible, and the two rings mark a and its inverse

012345678910111213141516171819202122232425

12 of the 26 residues have an inverse. Invertible residues pair off — a maps to its inverse and that inverse maps straight back to a, so the whole set folds into pairs plus the handful of self-inverse values.

How to Use This Calculator

  1. Type the number you want to invert into the a field. Negatives are fine — they get reduced into the range 0 to n − 1 first, and the note under the field shows that reduction.
  2. Enter the modulus (n). The helper text tells you straight away whether n is prime and how many residues are invertible, which is the fastest existence check there is.
  3. Read the result card. If it says no inverse exists, the gcd tile shows the shared factor that blocks it.
  4. Switch Method to trace between extended Euclid, Fermat/Euler exponentiation, and the brute-force scan to see the same answer arrived at three ways — and what each one costs.
  5. To divide instead of invert, put a value in the third field: it solves a·x ≡ b (mod n), including the awkward case where gcd(a, n) ≠ 1 but solutions still exist.
average • 0 ratings
Your rating
Tap a star to rate

Your rating helps improve Modular Inverse Calculator - Find a^-1 mod n Online. We store only an anonymized vote (no personal data).

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Modular Inverse Calculator: Three Routes to a⁻¹ mod n

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:
Modular Inverse Calculator showing a number and its inverse looping back to 1 on a modular dial beside a shrinking Euclidean staircase

A modular inverse calculator answers a question that looks like division but isn’t: which whole number x satisfies 7x ≡ 1 (mod 26)? The answer is 15, because 7 × 15 = 105 = 4 × 26 + 1. No fractions appear anywhere. That single number is what lets you divide by 7 in modular arithmetic, and it’s the reason RSA key generation works at all.

Three methods find it. One is what every library uses, one is what cryptographic code uses when timing attacks matter, and one is what you should never use but everyone tries first. Below: the condition that decides whether an inverse exists at all, the three routes measured against each other, two hand methods worked through in full, and the check that catches every arithmetic slip.

gcd(a, n) = 1, or There Is No Inverse

Most numbers have no modular inverse, and the test takes one line. An inverse of a modulo n exists exactly when a and n share no factor beyond 1 — when gcd(a, n) = 1. That’s not a convention; it falls out of the arithmetic. Every multiple of 6 is also a multiple of 3, so modulo 9 the products 6 × 1, 6 × 2, 6 × 3, … only ever land on 6, 3, or 0. The value 1 is never in that list, so 6 has no inverse mod 9. Try it in the calculator: gcd(6, 9) = 3 and the verdict card names the blocking factor.

Flip it around and the same logic tells you how many invertible numbers a modulus has: exactly φ(n) of them, Euler’s totient. Modulo 26 that’s 12 — barely half the residues, which is why the classical affine cipher only permits twelve multiplier keys. Modulo a prime, φ(p) = p − 1, so every nonzero residue is invertible and the existence question stops mattering. That difference is worth checking before you start: the greatest common factor calculator settles coprimality in a second, and the Euler phi calculator counts the invertible class for any modulus.

Modulus nInvertible φ(n)ShareWhat blocks the rest
11 (prime)1091%only 0
26 = 2 × 131246%every even number, plus 13
3120 = 2⁴ × 3 × 5 × 1376825%anything even, or divisible by 3, 5, or 13
10⁹ + 7 (prime)10⁹ + 6~100%only 0

Read the 3120 row twice, because it’s the RSA case. Three quarters of all candidate exponents are unusable against that modulus — not a rare edge case, the common one. Key generation picks e and then checks gcd(e, φ(n)) = 1 for exactly this reason.

Extended Euclid, Fermat, or a Search: What a Modular Inverse Calculator Runs

All three routes below return the identical number. They differ in what they need to know beforehand and in how much work they do — and the gap is not subtle. Take 17⁻¹ mod 3120, the textbook RSA key with p = 61 and q = 53, where the answer is d = 2753.

RouteCost for 17⁻¹ mod 3120PrerequisiteUse it when
Extended Euclid4 divisionsnonealways, unless timing leaks matter
Fermat / Euler power17 modular multiplicationsφ(3120) = 768, so n factoredprime modulus, or constant-time code
Brute-force scan2,753 trialsnonenever, past n ≈ 10,000

Four divisions against 2,753 multiplications is a 700-fold gap on a four-digit modulus, and it widens without limit: Euclid’s step count grows with the number of digits in n while the scan grows with n itself. Push the modulus to 10⁹ + 7 and Euclid averages about 17 divisions — the theoretical ceiling is 45 — while the average scan needs half a billion multiplications.

The Fermat row hides the real trap. Raising a to the power φ(n) − 1 is genuinely elegant — 17 modular multiplications is competitive — but you cannot write down φ(3120) = 768 without first factoring 3120 into 2⁴ × 3 × 5 × 13. For a 2048-bit RSA modulus that factorisation is the thing the entire security model assumes nobody can do. Set the modulus to something large in the calculator, switch to the Euler method, and the panel refuses the job rather than pretend. Extended Euclid never asks the question. That asymmetry, not raw speed, is why every crypto library computes inverses with Euclid.

The Table That Produces d = 2753

Extended Euclid runs the ordinary gcd algorithm while carrying two extra columns. Each row tracks how the current remainder can be written as s × n + t × a. When the remainder hits 1, the t in that row is the inverse. Here it is on a = 17, n = 3120:

DivisionRemainder rs (× 3120)t (× 17)
start312010
start1701
3120 = 183 × 17 + 991−183
17 = 1 × 9 + 88−1184
9 = 1 × 8 + 112−367

The final row says 2 × 3120 + (−367) × 17 = 1, and you can check that by hand: 6240 − 6239 = 1. Reduce it modulo 3120 and the 3120 term vanishes, leaving −367 × 17 ≡ 1. So −367 is an inverse of 17. Add the modulus once to bring it into range: −367 + 3120 = 2753. That’s the RSA private exponent, and 17 × 2753 = 46,801 = 15 × 3120 + 1 confirms it.

Two details trip people up in that table. The s column is never needed — it exists only to make the row arithmetic uniform, and you can drop it entirely if you only want the inverse. And the t values grow before they resolve (0, 1, −183, 184, −367), which looks like a mistake the first time. It isn’t; the coefficients of a Bézout identity are genuinely larger than the remainders they describe. If you want the plain remainders without the coefficient bookkeeping, the modulo calculator handles those directly.

Back-Substitution: The Same Identity, Built Backwards

Exams often teach the other hand method, and students who learned one panic when they see the other. They produce identical output. Back-substitution runs the plain Euclidean division chain first, then unwinds it. Take 7⁻¹ mod 26:

26 = 3 × 7 + 5

7 = 1 × 5 + 2

5 = 2 × 2 + 1 ← remainder 1, stop

Now walk back up, replacing each remainder with the expression that produced it. Start from the last line, 1 = 5 − 2 × 2. Substitute 2 = 7 − 1 × 5 to get 1 = 5 − 2(7 − 5) = 3 × 5 − 2 × 7. Substitute 5 = 26 − 3 × 7 to get 1 = 3(26 − 3 × 7) − 2 × 7 = 3 × 26 − 11 × 7.

There’s the identity: 3 × 26 − 11 × 7 = 78 − 77 = 1, so −11 is an inverse of 7, and −11 + 26 = 15. The tabular method on the same input produces s = 3 and t = −11 in its final row — the same two coefficients, because the Bézout identity for a coprime pair is unique up to adding multiples of the modulus. Pick whichever method your grader expects; the calculator traces the tabular one because it needs no second pass and never grows a substitution stack.

What 3 ÷ 7 Means Modulo 26

Modular arithmetic has no fractions, so division is defined as multiplication by an inverse: b ÷ a becomes b × a⁻¹. Solving 7x ≡ 3 (mod 26) means multiplying both sides by 15, giving x ≡ 45 ≡ 19. Check: 7 × 19 = 133 = 5 × 26 + 3. Enter a = 7, n = 26, b = 3 in the calculator and the congruence panel walks that through.

Competitive programmers lean on this constantly. Answers “modulo 10⁹ + 7” often involve dividing by a factorial, and since 10⁹ + 7 is prime every nonzero value has an inverse, so a fraction like 1/6 becomes 6⁻¹ mod (10⁹ + 7) = 166,666,668. Combine that with the modular exponentiation calculator and binomial coefficients stay exact with no big-integer arithmetic anywhere.

The case nearly every tool botches is a non-coprime one that’s still solvable. Consider 6x ≡ 3 (mod 9). No inverse of 6 exists, yet x = 2, 5, and 8 all work. The rule: a·x ≡ b (mod n) has solutions exactly when gcd(a, n) divides b, and then it has gcd(a, n) of them. Here gcd = 3 divides 3, so divide the congruence through by 3 to get 2x ≡ 1 (mod 3), solve that (x ≡ 2), and lift it back in steps of 9 ÷ 3 = 3. Most inverse calculators stop at “no inverse” and leave you stuck; this one solves the reduced congruence instead. If you need to check which divisions are clean in the first place, the divisibility calculator is quicker than factoring by hand.

One Multiplication Catches Every Mistake

Every modular inverse is self-checking. Multiply a by your answer, reduce mod n, and you must get 1 — no exceptions, no rounding, no judgement call. That one line catches all four common slips:

  • Inverting against the wrong modulus. The classic RSA error: d is e⁻¹ mod φ(n), not e⁻¹ mod n. With p = 61 and q = 53, encrypting the message 65 gives 65¹⁷ mod 3233 = 2790. The correct key, 17⁻¹ mod 3120 = 2753, turns that back into 65. Invert 17 against 3233 instead and you get 2092 — a perfectly valid inverse of 17 that decrypts 2790 to 2699. Nothing errors out; the plaintext is simply wrong. The published PKCS #1 specification is explicit that d inverts e modulo λ(n).
  • Leaving the coefficient negative. Euclid hands back −367 or −11. Both are correct residues, but a language whose % truncates toward zero — C, Java, JavaScript, Go — will keep the sign and every downstream comparison silently breaks. Add n once.
  • Reaching for a decimal. 7⁻¹ mod 26 is 15, not 0.142857. The modular inverse is always a whole number in 1 … n − 1, and any answer with a decimal point is a sign you computed 1/a instead.
  • Skipping the gcd check. Feeding a non-coprime pair to a naive implementation doesn’t always error out — some return the Bézout coefficient anyway, which is a real number that is not an inverse. Verify with the multiplication and it dies immediately: 6 × 5 mod 9 = 3, not 1.

Worth internalising the shape of the whole thing: the extended Euclidean algorithm computes a gcd and an inverse in the same pass, so the existence test is free — the standard formulation returns both. You never need to test coprimality first and then invert. Run one algorithm; if the final remainder is 1 you have your answer, and if it isn’t, that remainder is precisely the factor standing in the way. For the plain remainder arithmetic underneath all of this, the remainder calculator is the simpler starting point.

Frequently Asked Questions

How do you find a modular inverse by hand?

Run the extended Euclidean algorithm on the modulus and the number, tracking how each remainder can be written as a combination of the two. For 7 mod 26 the divisions are 26 = 3 x 7 + 5, then 7 = 1 x 5 + 2, then 5 = 2 x 2 + 1, and unwinding them gives 3 x 26 - 11 x 7 = 1. So -11 is an inverse, and adding 26 puts it in range as 15.

What is a modular multiplicative inverse?

It is the whole number x that satisfies a times x congruent to 1 modulo n. Modulo 26 the inverse of 7 is 15, because 7 x 15 = 105 = 4 x 26 + 1. There are no fractions involved: the inverse is always an integer between 1 and n - 1, and it is what replaces division in modular arithmetic.

Why does 6 have no inverse mod 9?

Because gcd(6, 9) = 3 rather than 1. Every multiple of 6 is also a multiple of 3, so modulo 9 the products 6x only ever land on 0, 3 or 6, and never on 1. An inverse exists exactly when the number and the modulus share no common factor, which is why prime moduli are so convenient: every nonzero residue is then invertible.

Extended Euclid or Fermat's little theorem: which is faster for inverses?

Extended Euclid wins in practice because it needs nothing but the two numbers. Finding 17 inverse mod 3120 takes 4 divisions with Euclid, versus 17 modular multiplications by raising 17 to the power phi(3120) - 1 = 767 — and that route first requires factoring 3120 into 2^4 x 3 x 5 x 13 to know phi at all. For a real RSA modulus that factorisation is impossible, so libraries use Euclid. The exponentiation route is still preferred in constant-time cryptographic code, where Euclid's data-dependent branching can leak timing information.

How do you calculate the private exponent d in RSA?

d is the inverse of e modulo phi(n), not modulo n — that distinction is the single most common RSA implementation bug. With p = 61 and q = 53, phi(n) = 60 x 52 = 3120, so for e = 17 the private exponent is 17 inverse mod 3120 = 2753. Inverting 17 against the modulus 3233 instead yields 2092, which is a genuine inverse of 17 but decrypts every message into garbage.

How do you divide by a number in modular arithmetic?

Multiply by the inverse instead. To solve 7x congruent to 3 modulo 26, multiply both sides by 7 inverse = 15, giving x congruent to 45 congruent to 19, and 7 x 19 = 133 = 5 x 26 + 3 confirms it. This is how competitive programmers handle fractions modulo 10^9 + 7, where dividing by 6 means multiplying by 166,666,668.

Can a modular inverse be negative?

A negative inverse is a valid representative of the same residue class. For 7 modulo 26, -11 and 15 both work. Normalize an arbitrary integer result with ((x % n) + n) % n when the language’s remainder can be negative. Adding n just once is enough only if the original value is already within one modulus of the desired range.

How many numbers have an inverse modulo n?

Exactly phi(n) of them, where phi is Euler's totient function. Modulo 26 that is 12 of the 26 residues, which is why the classical affine cipher allows only twelve multiplier keys. Modulo 3120 only 768 values are invertible, about a quarter, and modulo a prime p all p - 1 nonzero residues qualify.