Euler's Totient Function

Definition

Euler's totient function, denoted by φ(n)\varphi(n), counts the integers less than or equal to nn that are coprime to nn.

For example, φ(1)=1\varphi(1) = 1.

When nn is prime, clearly φ(n)=n−1\varphi(n) = n - 1.

Properties

  • The Euler totient function is a multiplicative function.

    That is, for any integers satisfying gcd⁡(a,b)=1\gcd(a, b) = 1, written as a,ba,b, we have φ(ab)=φ(a)φ(b)\varphi(ab) = \varphi(a)\varphi(b).

    In particular, when nn is odd, φ(2n)=φ(n)\varphi(2n) = \varphi(n).

    See composition of residue systems for a proof.

  • n=∑d∣nφ(d)n = \sum_{d \mid n}{\varphi(d)}.

    ???+ note "Proof" This follows from Möbius inversion and related facts.

    It can also be seen as follows: if $\gcd(k, n) = d$, then $\gcd(\dfrac{k}{d},\dfrac{n}{d}) = 1, ( k < n )$.
    
    Let $f(x)$ denote the number of integers satisfying $\gcd(k, n) = x$. Then $n = \sum_{i = 1}^n{f(i)}$.
    
    From the argument above, we find that $f(x) = \varphi(\dfrac{n}{x})$, and hence $n = \sum_{d \mid n}\varphi(\dfrac{n}{d})$. Since the divisors $d$ and $\dfrac{n}{d}$ are symmetric, this becomes $n = \sum_{d \mid n}\varphi(d)$.
    
  • If n=pkn = p^k, where pp is prime, then φ(n)=pk−pk−1\varphi(n) = p^k - p^{k - 1}. (This follows directly from the definition.)

  • By the fundamental theorem of arithmetic, let n=∏i=1spikin = \prod_{i=1}^{s}p_i^{k_i}, where each pip_i is prime. Then φ(n)=n×∏i=1spi−1pi\varphi(n) = n \times \prod_{i = 1}^s{\dfrac{p_i - 1}{p_i}}.

    ???+ note "Proof" - Lemma: for any prime pp, φ(pk)=pk−1×(p−1)\varphi(p^k)=p^{k-1}\times(p-1).

        Proof: among the integers from 1 to $p^k$, exactly $p^{k-1}$ are multiples of $p$; all the others are coprime to $p^k$. Thus $\varphi(p^k)=p^k-p^{k-1}=p^{k-1}\times(p-1)$.
    
    We next prove $\varphi(n) = n \times \prod_{i = 1}^s{\dfrac{p_i - 1}{p_i}}$. By the fundamental theorem of arithmetic and the multiplicativity of $\varphi(x)$,
    
    $$
    \begin{aligned}
        \varphi(n) &= \prod_{i=1}^{s} \varphi(p_i^{k_i}) \\
        &= \prod_{i=1}^{s} (p_i-1)\times {p_i}^{k_i-1}\\
        &=\prod_{i=1}^{s} {p_i}^{k_i} \times(1 - \frac{1}{p_i})\\
        &=n~ \prod_{i=1}^{s} (1- \frac{1}{p_i})
        &\square
    \end{aligned}
    $$
    
  • For any integers that are not both 00, written as m,nm,n, φ(mn)φ(gcd⁡(m,n))=φ(m)φ(n)gcd⁡(m,n)\varphi(mn)\varphi(\gcd(m,n))=\varphi(m)\varphi(n)\gcd(m,n).

    This follows by direct calculation from the preceding identity.

Implementation

If only the totient of one number is needed, factor the number according to the definition while computing the answer. This process can be optimized with the Pollard Rho algorithm.

Reference implementation

C++

#include <cmath>

int euler_phi(int n) {
  int ans = n;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}

Python

import math


def euler_phi(n):
    ans = n
    for i in range(2, math.isqrt(n) + 1):
        if n % i == 0:
            ans = ans // i * (i - 1)
            while n % i == 0:
                n = n // i
    if n > 1:
        ans = ans // n * (n - 1)
    return ans

For the totients of multiple numbers, use the linear sieve introduced later.

For details, see sieving for the Euler totient function.

Applications

The Euler totient function is often used to simplify sums of greatest common divisors. Some Chinese articles call this Euler inversion1.

In the conclusion

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

Substituting n=gcd⁡(a,b)n=\gcd(a,b) into it gives

gcd⁡(a,b)=∑d∣gcd⁡(a,b)φ(d)=∑d[d∣a][d∣b]φ(d),\gcd(a,b) = \sum_{d|\gcd(a,b)}\varphi(d) = \sum_d [d|a][d|b]\varphi(d),

where [⋅][\cdot] is an Iverson bracket. Summing the identity yields

∑i=1ngcd⁡(i,n)=∑d∑i=1n[d∣i][d∣n]φ(d)=∑d⌊nd⌋[d∣n]φ(d)=∑d∣n⌊nd⌋φ(d).\sum_{i=1}^n\gcd(i,n)=\sum_{d}\sum_{i=1}^n[d|i][d|n]\varphi(d)=\sum_d\left\lfloor\frac{n}{d}\right\rfloor[d|n]\varphi(d)=\sum_{d|n}\left\lfloor\frac{n}{d}\right\rfloor\varphi(d).

The key observation is ∑i=1n[d∣i]=⌊nd⌋\sum_{i=1}^n[d|i]=\lfloor\frac{n}{d}\rfloor: between 11 and nn, the number of integers divisible by dd, indexed by ii, is ⌊nd⌋\lfloor\frac{n}{d}\rfloor.

Using this identity, we can enumerate the divisors and sum over them. For multiple queries, precompute prefix sums of the Euler totient function and use number-theoretic block decomposition.

GCD SUM

Given n≤100000n\le 100000, find

∑i=1n∑j=1ngcd⁡(i,j).\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j).
Idea

Following the derivation above, we obtain

∑i=1n∑j=1ngcd⁡(i,j)=∑d=1n⌊nd⌋2φ(d).\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j) = \sum_{d=1}^n\left\lfloor\frac{n}{d}\right\rfloor^2\varphi(d).

We need to compute the Euler totient function from 11 through nn; a linear sieve does this in O(n)O(n) and gives the answer.

Euler's Theorem

A theorem closely related to the Euler totient function is Euler's theorem. It states:

If gcd⁡(a,m)=1\gcd(a, m) = 1, then aφ(m)≡1(modm)a^{\varphi(m)} \equiv 1 \pmod{m}.

Extended Euler Theorem

The extended Euler theorem handles the general case of aa and mm.

ab≡{ab mod φ(m), gcd⁡(a, m)=1ab,gcd⁡(a, m)≠1, b<φ(m)ab mod φ(m)+φ(m),gcd⁡(a, m)≠1, b≥φ(m)(modm)a^b\equiv \begin{cases} a^{b\bmod\varphi(m)},\,&\gcd(a,\,m)=1\\ a^b,&\gcd(a,\,m)\ne1,\,b<\varphi(m)\\ a^{b\bmod\varphi(m)+\varphi(m)},&\gcd(a,\,m)\ne1,\,b\ge\varphi(m) \end{cases} \pmod m

For the proof and exercises, see Euler's theorem.

Exercises

References and Notes

Footnotes

  1. This terminology does not appear in academic journals or foreign forums; use it with caution. ↩

This content comes from OI Wiki: Euler's Totient Function. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.