Euler’s Totient φ(n), Extended Euclidean GCD & Modular Multiplicative Inverse Calculator

Compute Euler’s totient φ(m), gcd(a, m), Bézout coefficients, and modular inverse a⁻¹ mod m.

Euler’s Totient φ(43) 42 coprime integers
Modular Multiplicative Inverse (17^−1 mod 43) 38 mod m
Greatest Common Divisor gcd(17, 43) 1 gcd
Coprime Density φ(m) / m 97.674 %
Inputs
Try:
a
Base integer a to invert modulo m.
mod m
Positive integer modulus m (≥ 2).
Results & Graph
Euler’s Totient φ(43)
42 coprime integers
Modular Multiplicative Inverse (17^−1 mod 43)
38 mod m
Greatest Common Divisor gcd(17, 43)
1 gcd
Coprime Density φ(m) / m
97.674 %

Euler’s Totient φ(43)

Graph 1 • How the inputs feed Euler’s Totient φ(n) & Mod Inverse
Euler’s Totient φ(n), Extended Euclidean GCD & Modular Multiplicative Inverse Calculator • Custom Domain Schematic (Number Theory & Discrete Math) integerAmodulusMEuler’s Totient φ(n) & Integer A: 17 a • Modulus M: 43 mod m EULER’S TOTIENT Φ(43) 42 coprime integers • Modular Multiplicative: 38 mod m • Greatest Common Diviso: 1 gcd Eq: φ(m) = m × Π_{p|m} (1 − 1/p);
Graph 2 • Euler’s Totient φ(43) as Integer A changes (40% to 160% of current)
0 coprim 36.96 coprim 73.92 coprim 16.80 6.80a 25.20 10.20a 33.60 13.60a 42 17a 50.40 20.40a 58.80 23.80a 67.20 27.20a
Graph 3 • Integer A and Modulus M together Compares Euler’s Totient φ(43) when Modulus M is shifted +25% (solid) vs. −25% (dashed).
0 coprim 30.03 coprim 60.06 coprim --- Modulus M @ 75% 29.40 -30% 35.70 -15% 42 +0% 48.30 +15% 54.60 +30%
Summary: φ(43) = 42, gcd(17, 43) = 1, and the modular inverse of 17 mod 43 is 38 (17×38 ≡ 1 mod 43).
Summary: φ(43) = 42, gcd(17, 43) = 1, and the modular inverse of 17 mod 43 is 38 (17×38 ≡ 1 mod 43).
Interactive chart & math engine powered by CalculatorGraph

Formula: Euler’s Totient φ(n) & Mod Inverse Formula

φ(m) = m × Πp|m (1 − 1/p)
a × a⁻¹ ≡ 1 (mod m) when gcd(a, m) = 1
φ(m) = m × Π_{p|m} (1 − 1/p); a × a⁻¹ ≡ 1 (mod m) when gcd(a, m) = 1
  • integerA: Integer A (a)
  • modulusM: Modulus M (mod m)

Frequently Asked Questions

Why does the headline show φ(m) rather than the inverse?

The page treats the totient as its main result because it describes the modulus itself, and it depends only on m. The inverse and gcd appear underneath because they depend on your choice of a. If you only need the inverse, read the first supporting line, and check the gcd line reads 1 before trusting it.

Is 38 the only inverse of 17 modulo 43?

It is the only one between 0 and 42. Any number that differs from 38 by a multiple of 43 also works, such as −5 or 81. The page reports the smallest non negative one, which is the usual convention. The −5 is the Bézout coefficient the extended Euclidean algorithm produces before reduction.

What does the coprime density line tell me?

It is φ(m) divided by m, as a percentage. It is the share of numbers below m that have an inverse. For the prime 43 it is 97.674%, almost everything. For 40 it is 40%, and for 42 it falls below 30%, because 42 has three small prime factors that rule out many candidates.

Can I use this to find the private key d in an RSA homework problem?

Yes, as long as you put the right numbers in. Enter the public exponent e as a and φ(n) as m, not n itself. For primes 5 and 11 with e = 3, that means a = 3 and m = 40, which gives d = 27. Entering m = 55 gives 37, which will not decrypt correctly.

Why is the inverse line missing from my result?

Because a and m share a factor, so no inverse exists. The page hides the inverse line and says so in the interpretation sentence. Check the gcd line: whenever it is above 1, there is no inverse to find, and the fix is to change a or m so they share no prime factor.

Does φ(m) count m itself?

Only when m is 1, which this page does not accept. For m of 2 or more, m always shares a factor with itself, so it is never counted. That is why φ of a prime p is p − 1 and not p. For 43 the count runs from 1 to 42.

Is there a fast way to check the page's inverse?

Multiply a by the inverse and divide by m. The remainder should be 1. For the presets, 10 × 13 and 26 × 5 are both one more than three times 43. If the remainder is anything other than 1, recheck that the gcd line reads 1 and that you copied the inverse correctly.

What happens if I enter a negative number for a?

The page reduces it modulo m before doing anything else. For −5 with m = 43 it adds 43 to get 38 and returns 17, the correct inverse of −5. The labels then show 38 rather than −5, which can look odd at first, but the two are the same number modulo 43.

Why do textbooks use the extended Euclidean algorithm instead of trying every number?

Trying every candidate takes up to m − 1 multiplications, which is hopeless for large moduli. Euclid's algorithm needs a number of steps that grows only with the number of digits. For 17 and 43 it takes three divisions. For numbers hundreds of digits long it still finishes quickly, because the step count grows roughly in proportion to the number of digits.

Can the page handle moduli used in real cryptography?

No. It accepts moduli only up to 10 to the 12th, and it factors m by trial division. Cryptographic moduli are hundreds of digits long. Use the page to understand and check small examples, and use a big integer library for anything real.

How do I solve 17x ≡ 5 (mod 43) with this page?

Enter a = 17 and m = 43 to get the inverse, 38. Multiply your right hand side, 5, by 38 and reduce modulo 43. The answer is x = 18, and every number that differs from 18 by a multiple of 43 also works. If the gcd line is not 1, the congruence may have several solutions or none.

Why is φ(26) equal to 12?

26 is 2 × 13, two distinct primes, so φ(26) = (2 − 1)(13 − 1) = 12. Those 12 numbers are the odd numbers from 1 to 25, leaving out 13. They are exactly the multipliers that work as keys in an affine cipher on a 26 letter alphabet, because each of them has an inverse modulo 26.

Three answers from two numbers: 17 and 43

The page takes an integer a and a modulus m and returns three separate things. With the defaults, a = 17 and m = 43, it reports φ(43) = 42 as the headline, then gcd(17, 43) = 1, then the inverse 17⁻¹ mod 43 = 38.

The totient depends only on m. It counts the whole numbers from 1 to m that share no factor with m. Since 43 is prime, every number from 1 to 42 qualifies, so φ(43) is 42. The coprime density line, 97.674%, is just 42 divided by 43.

The gcd and the inverse depend on both numbers. Because 17 and 43 share no factor, the gcd is 1 and an inverse exists. The inverse is the number that multiplies 17 to give a remainder of 1 when divided by 43. You can check it in your head: 17 × 38 is one more than 15 × 43.

Keep the two halves apart when reading the result. Changing a never changes the headline, as the presets show: a = 10 and a = 26 both still report φ(43) = 42, while their inverses are 13 and 5.

Working the extended Euclidean algorithm for 17 and 43

The inverse comes from the extended Euclidean algorithm, and it is short enough to do on paper. First run ordinary Euclid, dividing and keeping remainders. 43 = 2 × 17 + 9. Then 17 = 1 × 9 + 8. Then 9 = 1 × 8 + 1. The last non zero remainder is 1, so the gcd is 1.

Now run it backwards to write 1 as a combination of 17 and 43. From the last line, 1 = 9 − 8. The line before says 8 = 17 − 9, so 1 = 9 − (17 − 9) = 2 × 9 − 17. The first line says 9 = 43 − 2 × 17, so 1 = 2 × (43 − 2 × 17) − 17 = 2 × 43 − 5 × 17.

That gives the Bézout identity 17 × (−5) + 43 × 2 = 1. Check it: −85 plus 86 is 1. The coefficient on 17 is −5, and reducing −5 into the range 0 to 42 gives 38, which is the page's answer.

The engine computes these coefficients internally, carrying them along as it divides. The page description mentions Bézout coefficients, but the result panel only shows the reduced inverse, the gcd and the totient. If you need x and y themselves, the hand method above is the quickest route.

Computing φ(m) when m is not prime

For a prime p, φ(p) is p − 1. For anything else, factor m into primes and apply the formula on the page: multiply m by (1 − 1/p) once for each distinct prime p that divides it. Repeated primes count only once.

Take m = 40, which is 2 × 2 × 2 × 5. The distinct primes are 2 and 5, so φ(40) = 40 × 1/2 × 4/5 = 16. The page agrees, and shows a coprime density of 40%. For m = 36, which is 2 × 2 × 3 × 3, φ(36) = 36 × 1/2 × 2/3 = 12. For m = 64, a power of 2, half the numbers are odd and so φ(64) = 32.

For two distinct primes the formula becomes very simple: φ(p × q) = (p − 1)(q − 1). With 5 and 11, φ(55) = 4 × 10 = 40. That shortcut is the one RSA relies on.

The engine finds the factors by trial division, testing divisors up to the square root of m. That is instant for the sizes people check by hand. It is not how real cryptographic software would do it, and it would be far too slow for numbers of cryptographic size.

A toy RSA key, step by step

The modular inverse is the step in RSA that turns a public exponent into a private one. A tiny example shows where this page fits and where people go wrong.

Choose primes 5 and 11. Their product n is 55, and φ(55) is 40. Pick a public exponent e that shares no factor with 40, such as 3. The private exponent d is the inverse of e modulo φ(n), not modulo n. Enter a = 3 and m = 40, and the page returns 27. Check: 3 × 27 = 81, which is 2 × 40 + 1.

Now test the pair. Encrypt the message 2 by computing 2³ mod 55, which is 8. Decrypt by computing 8 raised to the 27th power mod 55, which brings back 2. The key works.

The common mistake is to enter the modulus n instead of φ(n). With a = 3 and m = 55 the page returns 37, a perfectly valid inverse of 3 modulo 55 that is useless as an RSA private key. For textbook examples, run the page twice: once with m = n to see φ(n) as the headline, then again with m set to that totient and a set to e to get d.

Real keys use primes hundreds of digits long. This page is for learning and checking homework, not for generating keys.

When no inverse exists

An inverse of a modulo m exists only when gcd(a, m) is 1. If a and m share a factor, every multiple of a also shares it, so none of them can leave a remainder of 1.

Try a = 12 and m = 42. The gcd line reads 6, and the interpretation says no modular inverse exists. The inverse line disappears from the result, which is the page's way of saying there is nothing to report. Twelve times any number is a multiple of 6, so it can never leave a remainder of 1 modulo 42.

The same happens with a = 10 and m = 64. Both are even, the gcd is 2, and again no inverse line appears. For an even modulus, no even number has an inverse. For a prime modulus such as the default 43, every number from 1 to 42 has one, which is another way of reading φ(43) = 42.

In fact φ(m) is exactly the count of numbers between 1 and m that do have an inverse modulo m. The totient and the inverse question are two views of the same set of numbers.

Solving congruences and decoding an affine cipher

Inverses are not only for RSA. Any linear congruence of the form a × x ≡ b (mod m) is solved by multiplying both sides by a⁻¹. Suppose you need x with 17x ≡ 5 (mod 43). The page gives 17⁻¹ = 38, so x is 5 × 38 reduced modulo 43, which is 18. Check: 17 × 18 leaves a remainder of 5 when divided by 43.

The affine cipher from classroom cryptography uses the same step with m = 26, one slot per letter. Encryption multiplies each letter's position by a key a and adds a shift. To decrypt you need a⁻¹ mod 26. With a = 7, the page returns 15, and φ(26) = 12 tells you there are exactly 12 usable multiplier keys.

Try a = 13 with m = 26 and the gcd line reads 13. No inverse exists, so a cipher built on that key could never be decrypted. Two different letters would encrypt to the same output. Checking the gcd before choosing a key catches this at once.

Inverses from Euler's theorem, and a shortcut with negatives

Euler's theorem says that when gcd(a, m) = 1, a raised to the power φ(m) leaves a remainder of 1 modulo m. Dividing one factor of a out of that gives another way to find the inverse: a⁻¹ is a raised to φ(m) − 1, reduced modulo m.

For the default that is 17 to the 41st power mod 43, which works out to 38, the same answer the Euclidean method gave. The exponent route is neat in theory but needs repeated squaring to be practical. Extended Euclid is usually faster, and it works even when you do not know φ(m).

A neat check on the presets: 26 is 43 − 17, so 26 behaves like −17 modulo 43. Its inverse should then be the negative of 17's inverse, −38, which reduces to 5. That is exactly what the page reports for a = 26.

The same logic tells you what to expect for a value of a larger than m. The page first reduces a modulo m. Enter a = 60 with m = 43 and it treats it as 17, returning the same inverse of 38.

Whole numbers, negative a and the size limit

Both boxes must hold whole numbers. Type 17.5 for a and the page asks you to correct it rather than rounding, since number theory only makes sense for integers.

The modulus must be 2 or more. Modulo 1 every number is 0, so there is nothing useful to compute, and the page says so.

The integer a can be any whole number, including zero and negatives, because the page reduces it modulo m first. Enter −5 with m = 43 and it works with 38, returning an inverse of 17: check that −5 × 17 is −85, which is one more than −86, or −2 × 43. Enter 0 and the gcd line reads 43 with no inverse, which is correct, since zero has no inverse.

The modulus is capped at 10 to the 12th. That keeps the trial division factoring quick and the arithmetic inside the range where ordinary floating point numbers stay exact. For anything bigger, use software with exact big integer arithmetic.