MathsNumber Theory

Euler's Totient
calculator.

Count the whole numbers up to n that share no factor with n, with the working.

How it's worked out

Euler's totient, written φ(n) and read "phi of n", counts the whole numbers from 1 to n that are coprime to n. Coprime means the two share no factor except 1. For 12 those numbers are 1, 5, 7, 11, so φ(12) = 4.

Listing works for small n, but a formula is faster. Factor n into primes. Then, for each distinct prime p, multiply n by 1 minus 1 over p. Each factor removes the share of numbers that p divides. How many times a prime repeats does not change the formula.

φ(n)\displaystyle \varphi(n)=n∏p|n(1−1p)\displaystyle {}= n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)

For 36, the primes are 2 and 3, so φ(36) = 36 × 12\frac{1}{2} × 23\frac{2}{3} = 12. You can also work prime power by prime power. A prime power has a short rule of its own, and the pieces multiply.

φ(pe)\displaystyle \varphi(p^e)=(p−1)pe−1\displaystyle {}= (p - 1)\,p^{e-1}

The pieces multiply because φ is multiplicative: φ(mn) = φ(m) × φ(n) whenever m and n share no factor. Factoring is the hard step. The tool tries divisors up to the square root of n, which is quick for any n up to 10¹².

The working ends with a check from OEIS A000010: add φ(d) over every divisor d of n, and you get n back. When n has too many divisors for that, the check uses Euler's theorem instead.

∑d|nφ(d)\displaystyle \sum_{d \mid n} \varphi(d)=n\displaystyle {}= n

Worked examples

Hana works out φ(360) for her homework

Hana factors 360 as 2³ × 3² × 5. Its distinct primes are 2, 3 and 5, so she multiplies 360 by 12\frac{1}{2}, 23\frac{2}{3} and 45\frac{4}{5} and gets 96.

Prime power by prime power, the pieces are φ(2³) = 4 × 1 = 4, φ(3²) = 3 × 2 = 6, φ(5) = 1 × 4 = 4. Their product is again 96. As a check, 360 has 24 divisors, and φ of each adds up to 360.

Open this example in the tool

Omar builds a toy RSA key from 61 and 53

In RSA you multiply two primes to make the public number n. Omar uses 61 and 53, so n = 3,233, which factors as 53 × 61. The pieces are φ(53) = 1 × 52 = 52 and φ(61) = 1 × 60 = 60, so φ(3,233) = 3,120.

His two key numbers must multiply to leave 1 when divided by 3,120. Euler's theorem is what makes decryption work: 5 shares no factor with 3,233, and 5 to the power 3,120 leaves 1 when divided by 3,233. Real keys use primes hundreds of digits long, so nobody can factor n to find φ.

Open this example in the tool

Kai lists every number whose totient is 12

Kai's puzzle asks for every n with φ(n) = 12. Only primes p where p − 1 divides 12 can appear in such an n, which leaves 2, 3, 5, 7 and 13. Checking the ways to combine them gives 6 answers: 13, 21, 26, 28, 36, 42.

That matches the list in OEIS A032447. Some values have no answer at all. No n has φ(n) = 14, so the search returns nothing for it, while 8 has 5 answers: 15, 16, 20, 24, 30.

Open this example in the tool

Reading your result

The share depends only on the primes. φ(n) divided by n is the share of numbers coprime to n. For 12 and for 36 it is the same, because both are built from 2 and 3.

φ(12)12\displaystyle \frac{\varphi(12)}{12}=φ(36)36\displaystyle {}= \frac{\varphi(36)}{36}=13\displaystyle {}= \frac{1}{3}

Primes score highest. Every number below a prime is coprime to it, so φ(97) = 96. A prime always gives one less than itself.

It is even for every n above 2. OEIS A000010 notes this. If k is coprime to n, so is n − k, and the two are different numbers, so the coprimes come in pairs.

More small primes, smaller share. The table multiplies in one more prime each row. The share coprime to n keeps falling, though slowly.

Products of the first primes
nφ(n)Share coprime
6233.3%
30826.7%
2104822.9%
2,31048020.8%
30,0305,76019.2%
510,51092,16018.1%

Common mistakes

  • Using the exponents in the product. For 36 = 2² × 3² you use each prime once, not twice. The answer is 12, not 36 × 14\frac{1}{4} × 49\frac{4}{9}.
  • Multiplying totients of numbers that share a factor. φ(4) = 2, but φ(2) × φ(2) = 1. The rule needs no shared factor, as with 9 and 4: φ(36) = 12 = 2 × 6.
  • Leaving out 1. One shares no factor with any number, so it always counts, and φ(1) = 1.
  • Using Euler's theorem when a and n share a factor. With a = 4 and n = 12 they share 4, so no power of 4 leaves 1 when divided by 12. Check that the two are coprime first.

Questions people ask

Factor n into primes, then multiply n by 1 minus 1 over each distinct prime. For 36 that is 36 times one half times two thirds, which is 12. For small n you can also list the numbers from 1 to n and cross out those that share a factor.

Sources