Number Theory

Euler's Theorem and the Totient Function

Compute the totient function, prove Euler's theorem and see how it tames huge powers and makes a small RSA example work.

Key takeaways

  • φ(n) counts the integers from 1 to n whose greatest common divisor with n is 1.
  • For a prime power, φ(pᵏ) = pᵏ − pᵏ⁻¹, and φ is multiplicative across coprime factors.
  • Euler's theorem states that a^φ(n) ≡ 1 (mod n) whenever gcd(a, n) = 1.
  • The theorem lets you reduce exponents modulo φ(n), which makes enormous powers manageable.
  • RSA encryption relies on this behavior, with primes far larger than any classroom example.

Euler's theorem says that if a and n share no common factor, then a raised to the power φ(n) leaves remainder 1 on division by n. Here φ(n), Euler's totient function, counts the whole numbers from 1 to n that share no factor with n.

It extends Fermat's little theorem from prime moduli to every modulus. Along the way, the totient function turns out to be a lovely object in its own right.

Counting coprime numbers

Two integers are coprime when their greatest common divisor is 1. For n = 12, the numbers from 1 to 12 that are coprime to 12 are 1, 5, 7 and 11. So φ(12) = 4.

n       1  2  3  4  5  6  7  8  9  10  11  12
φ(n)    1  1  2  2  4  2  6  4  6   4  10   4

The values bounce around. For a prime p every smaller positive number qualifies, so φ(p) = p − 1. Composite numbers lose more candidates, especially those with several small prime factors. Checking coprimality is quick with the Euclidean algorithm.

Computing φ from the prime factors

For a prime power pᵏ, the only numbers up to pᵏ that fail are the multiples of p, and there are pᵏ⁻¹ of them.

φ(9)  = 9 − 3  = 6
φ(32) = 32 − 16 = 16

If m and n are coprime, then φ(mn) = φ(m) × φ(n). The reason is the Chinese remainder theorem: a remainder modulo mn corresponds to exactly one pair of remainders modulo m and modulo n, and it is coprime to mn exactly when both members of the pair are coprime to their moduli.

Putting these together gives a formula with one factor for each distinct prime dividing n.

φ(n) = n × (1 − 1/p) × (1 − 1/q) × ...

360 = 2³ × 3² × 5
φ(360) = 360 × 1/2 × 2/3 × 4/5 = 96

Beware that multiplicativity needs coprime factors: φ(4) = 2, but φ(2) × φ(2) = 1. Finding the factorization is the hard part for large numbers, a point taken up in why factoring is hard.

The theorem and its proof

If n is a positive integer and gcd(a, n) = 1, then a^φ(n) ≡ 1 (mod n).

The proof shuffles remainders. List the coprime remainders modulo n, multiply each by a, and reduce. Take n = 10 and a = 3.

coprime remainders:   1   3   7   9
times 3:              3   9  21  27
reduced mod 10:       3   9   1   7

The same four numbers return in a new order. This happens in general. Each product is still coprime to n, because neither factor shares a prime with n. And no two products coincide, because a has an inverse modulo n, so ax ≡ ay forces x ≡ y.

Multiply the whole list in both forms. If P is the product of the coprime remainders, then a^φ(n) × P ≡ P. Since P is coprime to n, cancel it. What remains is the theorem.

When n is a prime p, φ(p) = p − 1 and you recover Fermat's little theorem.

Watching the theorem on a small modulus

It helps to see every case at once. Modulo 9 the coprime remainders are 1, 2, 4, 5, 7 and 8, so φ(9) = 6. Here are the powers of three of them.

exponent      1  2  3  4  5  6
base 2        2  4  8  7  5  1
base 4        4  7  1  4  7  1
base 8        8  1  8  1  8  1

Each row ends in 1 at exponent 6, as the theorem promises. Base 2 runs through all six coprime remainders before returning. Base 4 cycles every three steps and base 8 every two. Both 3 and 2 divide 6, which is why all the rows line up again at the end.

Now try base 3, which is not coprime to 9. Its powers are 3, 0, 0, 0 and so on. Once the remainder reaches 0 it can never come back to 1.

Why the order divides φ(n)

Let k be the order of a, and divide φ(n) by k to get φ(n) = qk + r, with r smaller than k. Then a^φ(n) = (a^k)^q × a^r. The left side is 1 by the theorem and a^k is 1 by definition, so a^r ≡ 1. Because k is the smallest positive exponent with that property, r must be 0.

This gives a quick way to find orders. For modulus 13, φ is 12, so an order can only be 1, 2, 3, 4, 6 or 12. To confirm that 2 has order 12, you only need to check the exponents 4 and 6.

2⁴ = 16 ≡ 3        not 1
2⁶ = 64 ≡ 12       not 1
so the order of 2 modulo 13 is 12

Taming huge powers

Find the last digit of 7²²². That means working modulo 10, where φ(10) = 4 and gcd(7, 10) = 1.

222 = 55 × 4 + 2
7²²² ≡ 7² = 49 ≡ 9 (mod 10)

Now find the last two digits of 3¹⁰⁰⁰. Here φ(100) = 40.

3¹⁰⁰⁰ = (3⁴⁰)²⁵ ≡ 1 (mod 100)

So the number ends in 01. More techniques, including what to do when the base and modulus share a factor, appear in finding last digits.

When the modulus is composite and large

Suppose you want 11¹²⁵ modulo 72. First check that gcd(11, 72) = 1. Then compute the totient from the factorization 72 = 8 × 9.

φ(72) = φ(8) × φ(9) = 4 × 6 = 24
125 = 5 × 24 + 5
11¹²⁵ ≡ 11⁵ (mod 72)
11² = 121 ≡ 49
11⁴ ≡ 49² = 2401 ≡ 25
11⁵ ≡ 25 × 11 = 275 ≡ 59

You could also solve the problem separately modulo 8 and modulo 9 and glue the answers together. That route uses smaller exponents, since 11 ≡ 3 modulo 8 and 11 ≡ 2 modulo 9, and it is how many programs speed up this kind of calculation.

Finding inverses

Since a × a^(φ(n) − 1) ≡ 1, the number a^(φ(n) − 1) is an inverse of a modulo n. For a = 7 and n = 10, that is 7³ = 343 ≡ 3, and indeed 7 × 3 = 21.

In practice the extended Euclidean method is better, because it does not require knowing φ(n), and therefore does not require factoring n.

The smallest exponent that works

φ(n) is an exponent that always works, but not necessarily the smallest. Modulo 8, φ(8) = 4, yet every odd number squared is already 1.

1² = 1,   3² = 9 ≡ 1,   5² = 25 ≡ 1,   7² = 49 ≡ 1

The order of a particular a, the least positive exponent giving 1, always divides φ(n). The smallest exponent that works for every coprime a at once is called the Carmichael function, written λ(n). Here λ(8) = 2.

When some a has order exactly φ(n), it generates all the coprime remainders and is called a primitive root. Modulo 8 there is none.

A tiny RSA example

Choose primes p = 5 and q = 11. Then n = 55 and φ(n) = 4 × 10 = 40. Pick e = 3, which is coprime to 40, and find d with e × d ≡ 1 (mod 40). Since 3 × 27 = 81, we have d = 27.

encrypt m = 2:     c = 2³ = 8
decrypt:           8²⁷ mod 55
  8² ≡ 9,  8⁴ ≡ 26,  8⁸ ≡ 16,  8¹⁶ ≡ 36
  27 = 16 + 8 + 2 + 1
  36 × 16 ≡ 26,   26 × 9 ≡ 14,   14 × 8 ≡ 2
recovered m = 2

It works because m^(ed) = m^(1 + 40k), which Euler's theorem reduces to m when m is coprime to 55. A separate argument using each prime shows it also works for the other messages.

This is a demonstration only. Numbers this small are broken at a glance, and real systems need large random primes, padding and reviewed software. See RSA with small primes for more.

Why RSA decryption returns the message
1 Choose primes p and q give n = pq 2 Count coprimes φ(n) = (p − 1)(q − 1), kept secret 3 Pair exponents Pick e and d with ed ≡ 1 mod φ(n) 4 Lock and unlock Raising to e then d brings back the message Choose primes p and q give n = pq 1 Count coprimes φ(n) = (p − 1)(q − 1), kept secret 2 Pair exponents Pick e and d with ed ≡ 1 mod φ(n) 3 Lock and unlock Raising to e then d brings back the message 4

Gauss's divisor sum

Add φ(d) over all divisors d of n and you get n itself.

n = 12, divisors 1, 2, 3, 4, 6, 12
φ values:        1, 1, 2, 2, 2, 4      sum = 12

To see why, write the fractions 1/12, 2/12, ..., 12/12 in lowest terms. Each ends up with some divisor d of 12 as its denominator, and exactly φ(d) of them have denominator d. This identity is a first example of the tools in arithmetic functions and Mobius inversion.

Facts worth knowing

For n of 3 or more, φ(n) is even, because coprime remainders pair off as k and n − k.

The totient can be small compared with n when n has many small prime factors. For n = 2 × 3 × 5 × 7 = 210, φ(n) = 1 × 2 × 4 × 6 = 48, less than a quarter of n.

Some values are never taken: no n has φ(n) = 14, for instance. And a famous open question, Carmichael's conjecture, asks whether every value that does occur is taken at least twice. It is unresolved as of the mid-2020s.

Try it yourself

  1. Compute φ(100) from the factorization 4 × 25. You should get 2 × 20 = 40.
  2. Find the remainder of 3²⁰² on division by 20. Since φ(20) = 8 and 202 leaves remainder 2 on division by 8, the answer is 9.
  3. Find every n with φ(n) = 4. The answers are 5, 8, 10 and 12.
  4. Show that φ(2n) = φ(n) when n is odd. Use multiplicativity and the fact that φ(2) = 1.
  5. Check the divisor sum for n = 18: the totients of 1, 2, 3, 6, 9 and 18 are 1, 1, 2, 2, 6 and 6, which total 18.

The third question shows that the totient cannot be run backward uniquely. Knowing φ(n) alone does not tell you n. Knowing both n and φ(n), however, is very revealing: if n is a product of two primes, then p + q equals n − φ(n) + 1, and a quadratic equation yields p and q. This is why the value of φ(n) must be kept as secret as the factors in RSA.

n = 55, φ(n) = 40
p + q = 55 − 40 + 1 = 16,   pq = 55
t² − 16t + 55 = 0   gives t = 5 and t = 11

Euler and the theorem

Leonhard Euler published proofs of Fermat's statement and then, around 1760, the generalization to any modulus. The symbol φ came later, through Gauss's notation, and the English word "totient" was introduced by J. J. Sylvester in the nineteenth century. You will also see the function called Euler's phi function. More on the man and his output is in Leonhard Euler and his range.

In modern terms, the coprime remainders modulo n form a group under multiplication with φ(n) elements. The theorem is then an instance of a general rule: raising any element of a finite group to the size of the group gives the identity.

Common mistakes

Dropping the coprime condition. With a = 2 and n = 4, 2² = 4 ≡ 0, not 1.

Using n − 1 for composite n. The right exponent is φ(n).

Counting distinct primes twice. In the product formula each prime appears once, however high its power.

Reducing exponents modulo n. Exponents reduce modulo φ(n), while bases reduce modulo n.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic