# Euler's totient function

In number theory, Euler's totient function (or Euler's phi function), denoted as φ(n) or ϕ(n), is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n. (These integers are sometimes referred to as totatives of n.) Thus, if n is a positive integer, then φ(n) is the number of integers k in the range 1 ≤ kn for which the greatest common divisor gcd(n, k) = 1.

Euler's totient function is a multiplicative function, meaning that if two numbers m and n are coprime, then φ(mn) = φ(m) φ(n).

For example, let n = 9. Then gcd(9, 3) = gcd(9, 6) = 3 and gcd(9, 9) = 9. The other six numbers in the range 1 ≤ k ≤ 9, that is 1, 2, 4, 5, 7 and 8 are relatively prime to 9. Therefore, φ(9) = 6. As another example, φ(1) = 1 since gcd(1, 1) = 1.

Euler's phi function is important mainly because it gives the order of the multiplicative group of integers modulo n (the group of units of the ring ℤ/nℤ). It also plays a key role in the definition of the RSA encryption system.

## History, terminology, and notation

Leonhard Euler introduced the function in 1763. However, he did not at that time choose any specific symbol to denote it. In a 1784 publication, Euler studied the function further, choosing the Greek letter π to denote it: he wrote πD for "the multitude of numbers less than D, and which have no common divisor with it". The standard notation φ(A) comes from Gauss's 1801 treatise Disquisitiones Arithmeticae. However, Gauss didn't use parentheses around the argument and wrote φA. Thus, it is often called Euler's phi function or simply the phi function.

In 1879, J. J. Sylvester coined the term totient for this function, so it is also referred to as Euler's totient function, the Euler totient, or Euler's totient. Jordan's totient is a generalization of Euler's.

The cototient of n is defined as n – φ(n), i.e. the number of positive integers less than or equal to n that are divisible by at least one prime that also divides n.

## Computing Euler's totient function

There are several formulas for computing φ(n).

### Euler's product formula

It states $\varphi(n) =n \prod_{p\mid n} \left(1-\frac{1}{p}\right),$

where the product is over the distinct prime numbers dividing n. (The notation is described in the article Arithmetical function.)

The proof of Euler's product formula depends on two important facts.

#### 1) The function φ(n) is multiplicative

This means that if gcd(m, n) = 1, then φ(mn) = φ(m) φ(n). (Sketch of proof: let A, B, C be the sets of residue classes modulo-and-coprime to m, n, mn respectively; then there is a bijection between A × B and C, by the Chinese remainder theorem.)

#### 2) φ(pk) = pk − pk−1 = pk−1(p − 1)

That is, if p is prime and k ≥ 1 then $\varphi(p^k) = p^k -p^{k-1} =p^{k-1}(p-1) = p^k \left( 1 - \frac{1}{p} \right).$

Proof: since p is a prime number the only possible values of gcd(pk, m) are 1, p, p2, ..., pk, and the only way for gcd(pk, m) to not equal 1 is for m to be a multiple of p. The multiples of p that are less than or equal to pk are p, 2p, 3p, ..., pk − 1p = pk, and there are pk − 1 of them. Therefore, the other pkpk − 1 numbers are all relatively prime to pk.

#### Proof of Euler's product formula

The fundamental theorem of arithmetic states that if n > 1 there is a unique expression for n, $n = p_1^{k_1} \cdots p_r^{k_r},$

where p1 < p2 < ... < pr are prime numbers and each ki ≥ 1. (The case n = 1 corresponds to the empty product.)

Repeatedly using the multiplicative property of φ and the formula for φ(pk) gives \begin{align} \varphi(n) &= \varphi(p_1^{k_1}) \varphi(p_2^{k_2}) \cdots\varphi(p_r^{k_r})\\ &= p_1^{k_1} \left(1- \frac{1}{p_1} \right) p_2^{k_2} \left(1- \frac{1}{p_2} \right) \cdots p_r^{k_r} \left(1- \frac{1}{p_r} \right)\\ &= p_1^{k_1} p_2^{k_2} \cdots p_r^{k_r} \left(1- \frac{1}{p_1} \right) \left(1- \frac{1}{p_2} \right) \cdots \left(1- \frac{1}{p_r} \right)\\ &=n \left(1- \frac{1}{p_1} \right)\left(1- \frac{1}{p_2} \right) \cdots\left(1- \frac{1}{p_r} \right). \end{align}

This is Euler's product formula.

#### Example $\varphi(36)=\varphi\left(2^2 3^2\right)=36\left(1-\frac{1}{2}\right)\left(1-\frac{1}{3}\right)=36\cdot\frac{1}{2}\cdot\frac{2}{3}=12.$

In words, this says that the distinct prime factors of 36 are 2 and 3; half of the thirty-six integers from 1 to 36 are divisible by 2, leaving eighteen; a third of those are divisible by 3, leaving twelve numbers that are coprime to 36. And indeed there are twelve positive integers that are coprime with 36 and lower than 36: 1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, and 35.

### Fourier transform

The totient is the discrete Fourier transform of the gcd, evaluated at 1: Let $\mathcal{F} \{ \mathbf{x} \}[m] = \sum\limits_{k=1}^n x_k \cdot e^{{-2\pi i}\frac{mk}{n}}$

where ${x_k} =\gcd(k,n)$ for $k \in \{1, \ldots, n\} .$ Then $\varphi (n) = \mathcal{F} \{ \mathbf{x} \} = \sum\limits_{k=1}^n \gcd(k,n) e^{{-2\pi i}\frac{k}{n}}.$

The real part of this formula is $\varphi (n)=\sum\limits_{k=1}^n \gcd(k,n) \cos {2\pi\frac{k}{n}}.$

Note that unlike the other two formulae (the Euler product and the divisor sum) this one does not require knowing the factors of n. However, it does involve the calculation of the greatest common divisor of n and every positive integer less than n, which suffices to provide the factorization anyway.

### Divisor sum

The property established by Gauss, that $\sum_{d\mid n}\varphi(d)=n,$

where the sum is over all positive divisors d of n, can be proven in several ways. (see Arithmetical function for notational conventions.)

One way is to note that φ(d) is also equal to the number of possible generators of the cyclic group Cd; specifically, if Cd = <g>, then gk is a generator for every k coprime to d. Since every element of Cn generates a cyclic subgroup, and all subgroups of CdCn are generated by some element of Cn, the formula follows. In the article Root of unity Euler's formula is derived by using this argument in the special case of the multiplicative group of the nth roots of unity.

This formula can also be derived in a more concrete manner. Let n = 20 and consider the fractions between 0 and 1 with denominator 20: $\tfrac{ 1}{20},\,\tfrac{ 2}{20},\,\tfrac{ 3}{20},\,\tfrac{ 4}{20},\, \tfrac{ 5}{20},\,\tfrac{ 6}{20},\,\tfrac{ 7}{20},\,\tfrac{ 8}{20},\, \tfrac{ 9}{20},\,\tfrac{10}{20},\,\tfrac{11}{20},\,\tfrac{12}{20},\, \tfrac{13}{20},\,\tfrac{14}{20},\,\tfrac{15}{20},\,\tfrac{16}{20},\, \tfrac{17}{20},\,\tfrac{18}{20},\,\tfrac{19}{20},\,\tfrac{20}{20}$

Put them into lowest terms: $\tfrac{ 1}{20},\,\tfrac{ 1}{10},\,\tfrac{ 3}{20},\,\tfrac{ 1}{ 5},\, \tfrac{ 1}{ 4},\,\tfrac{ 3}{10},\,\tfrac{ 7}{20},\,\tfrac{ 2}{ 5},\, \tfrac{ 9}{20},\,\tfrac{ 1}{ 2},\,\tfrac{11}{20},\,\tfrac{ 3}{ 5},\, \tfrac{13}{20},\,\tfrac{ 7}{10},\,\tfrac{ 3}{ 4},\,\tfrac{ 4}{ 5},\, \tfrac{17}{20},\,\tfrac{ 9}{10},\,\tfrac{19}{20},\,\tfrac{ 1}{ 1}$

First note that all the divisors of 20 are denominators. And second, note that there are 20 fractions. Which fractions have 20 as denominator? The ones whose numerators are relatively prime to 20 $\left(\tfrac{ 1}{20},\,\tfrac{ 3}{20},\,\tfrac{ 7}{20},\,\tfrac{ 9}{20},\,\tfrac{ 11}{20},\,\tfrac{13}{20},\,\tfrac{17}{20},\,\tfrac{19}{20}\right).$ By definition this is φ(20) fractions. Similarly, there are φ(10) = 4 fractions with denominator 10 $\left(\tfrac{ 1}{10},\,\tfrac{ 3}{10},\,\tfrac{ 7}{10},\,\tfrac{ 9}{10}\right),$ φ(5) = 4 fractions with denominator 5 $\left(\tfrac{ 1}{5},\,\tfrac{ 2}{5},\,\tfrac{ 3}{5},\,\tfrac{ 4}{5}\right),$ and so on.

In detail, we're considering the fractions of the form k/n where k is an integer from 1 to n inclusive. Upon reducing these to lowest terms, each fraction will have as its denominator some divisor of n. We can group the fractions together by denominator, and we must show that for a given divisor d of n, the number of such fractions with denominator d is φ(d).

Note that to reduce k/n to lowest terms, we divide the numerator and denominator by gcd(k, n). The reduced fractions with denominator d are therefore precisely the ones originally of the form k/n in which gcd(k, n)=n/d. The question therefore becomes: how many k are there less than or equal to n which verify gcd(k, n)=n/d? Any such k must clearly be a multiple of n/d, but it must also be coprime to d (if it had any common divisor s with d, then sn/d would be a larger common divisor of n and k). Conversely, any multiple k of n/d which is coprime to d will satisfy gcd(n, k)=n/d. We can generate φ(d) such numbers by taking the numbers less than d coprime to d and multiplying each one by n/d (these products will of course each by smaller than n, as required). This in fact generates all such numbers, as if k is a multiple of n/d coprime to d (and less than n), then k/(n/d) will still be coprime to d, and must also be smaller than d, else k would be larger than n. Thus there are precisely φ(d) values of k less than or equal to n such that gcd(k, n)=n/d, which was to be demonstrated.

Möbius inversion gives $\varphi(n) = \sum_{d\mid n} d \cdot \mu\left(\frac{n}{d} \right) = n\sum_{d\mid n} \frac{\mu (d)}{d},$

where μ is the Möbius function.

This formula may also be derived from the product formula by multiplying out $\prod_{p\mid n} \left(1 - \frac{1}{p}\right)$   to get $\sum_{d\mid n} \frac{\mu (d)}{d}.$

### Riemann zeta function limit

For $n>1$ the Euler totient function can be calculated as a limit involving the Riemann zeta function:[citation needed] $\varphi(n)=n\lim\limits_{s \rightarrow 1} \zeta(s)\sum\limits_{d|n} \mu(d)(e^{1/d})^{(s-1)}$

where $\zeta(s)$ is the Riemann zeta function, $\mu$ is the Möbius function, $e$ is e (mathematical constant), and $d$ is a divisor.

## Some values of the function

The first 99 values (sequence A000010 in OEIS) are shown in the table and graph below: $\varphi(n)$ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9
0+   1 1 2 2 4 2 6 4 6
10+ 4 10 4 12 6 8 8 16 6 18
20+ 8 12 10 22 8 20 12 18 12 28
30+ 8 30 16 20 16 24 12 36 18 24
40+ 16 40 12 42 20 24 22 46 16 42
50+ 20 32 24 52 18 40 24 36 28 58
60+ 16 60 30 36 32 48 20 66 32 44
70+ 24 70 24 72 36 40 36 60 24 78
80+ 32 54 40 82 24 64 42 56 40 88
90+ 24 72 44 60 46 72 32 96 42 60

The top line in the graph, y = n − 1, is a true upper bound. It is attained whenever n is prime. There is no lower bound that is a straight line of positive slope; no matter how gentle the slope of a line is, there will eventually be points of the plot below the line. More precisely, the lower limit of the graph is proportional to n/log log n rather than being linear.

## Euler's theorem

This states that if a and n are relatively prime then $a^{\varphi(n)} \equiv 1\mod n.\,$

The special case where n is prime is known as Fermat's little theorem.

This follows from Lagrange's theorem and the fact that φ(n) is the order of the multiplicative group of integers modulo n.

The RSA cryptosystem is based on this theorem: it implies that the inverse of the function $a\mapsto a^e \mod n$, where $e$ is the (public) encryption exponent, is the function $b\mapsto b^d \mod n$, where $d$, the (private) decryption exponent, is the multiplicative inverse of $e$ modulo $\varphi(n)$. The difficulty of computing $\varphi(n)$ without knowing the factorization of $n$ is thus the difficulty of computing $d$: this is known as the RSA problem which can be solved by factoring $n$. The owner of the private key knows the factorization, since an RSA private key is constructed by choosing $n$ as the product of two (randomly chosen) large primes $p$ and $q$. Only $n$ is publicly disclosed, and given the difficulty to factor large numbers we have the guarantee that no-one else knows the factorization.

## Other formulae involving φ

• $a\mid b$ implies $\varphi(a)\mid\varphi(b).$
• $n \mid \varphi(a^n-1)$       (a, n > 1)
• $\varphi(mn) = \varphi(m)\varphi(n)\cdot\frac{d}{\varphi(d)}$       where d = gcd(m, n). Note the special cases
• $\varphi(2m) = \begin{cases} 2\varphi(m) &\text{ if } m \text{ is even} \\ \varphi(m) &\text{ if } m \text{ is odd} \end{cases}$

and

• $\;\varphi\left(n^m\right) = n^{m-1}\varphi(n).$
• $\varphi(\mathrm{lcm}(m,n))\cdot\varphi(\mathrm{gcd}(m,n)) = \varphi(m)\cdot\varphi(n).$
Compare this to the formula $\mathrm{lcm}(m,n)\cdot \mathrm{gcd}(m,n) = m \cdot n.$       (See lcm).
• $\varphi(n)\;$ is even for $n \geq 3.$ Moreover, if n has r distinct odd prime factors, $2^r \mid \varphi(n).$
• For any a > 1 and n > 6 such that $4 \nmid n$ there exists an $l \geq 2n$ such that $l \mid \varphi(a^n-1)$.
• $\sum_{d \mid n} \frac{\mu^2(d)}{\varphi(d)} = \frac{n}{\varphi(n)}$      
• $\sum_{1\le k\le n \atop (k,n)=1}\!\!k = \frac{1}{2}n\varphi(n)\text{ for }n>1$
• $\sum_{k=1}^n\varphi(k) = \frac{1}{2}\left(1+ \sum_{k=1}^n \mu(k)\left\lfloor\frac{n}{k}\right\rfloor^2\right) =\frac3{\pi^2}n^2+O\left(n(\log n)^{2/3}(\log\log n)^{4/3}\right)$ ( cited in )
• $\sum_{k=1}^n\frac{\varphi(k)}{k} = \sum_{k=1}^n\frac{\mu(k)}{k}\left\lfloor\frac{n}{k}\right\rfloor=\frac6{\pi^2}n+O\left((\log n)^{2/3}(\log\log n)^{4/3}\right)$ 
• $\sum_{k=1}^n\frac{k}{\varphi(k)} = \frac{315\zeta(3)}{2\pi^4}n-\frac{\log n}2+O\left((\log n)^{2/3}\right)$ 
• $\sum_{k=1}^n\frac{1}{\varphi(k)} = \frac{315\zeta(3)}{2\pi^4}\left(\log n+\gamma-\sum_{p\text{ prime}}\frac{\log p}{p^2-p+1}\right)+O\left(\frac{(\log n)^{2/3}}n\right)$ 

(here γ is the Euler constant).

• $\sum_{1\le k\le n \atop (k,m)=1} 1 = n \frac {\varphi(m)}{m} + O \left ( 2^{\omega(m)} \right ),$

where m > 1 is a positive integer and ω(m) is the number of distinct prime factors of m. (a, b) is a standard abbreviation for gcd(a, b).

### Menon's identity

In 1965 P. Kesava Menon proved $\sum_{\stackrel{1\le k\le n}{ \gcd(k,n)=1}} \gcd(k-1,n) =\varphi(n)d(n),$

where d(n) = σ0(n) is the number of divisors of n.

### Formulae involving the golden ratio

Schneider found a pair of identities connecting the totient function, the golden ratio and the Möbius function $\mu(n)$. In this section $\varphi(n)$ is the totient function, and $\phi = \frac{1+\sqrt{5}}{2}= 1.618\dots$ is the golden ratio.

They are: $\phi=-\sum_{k=1}^\infty\frac{\varphi(k)}{k}\log\left(1-\frac{1}{\phi^k}\right)$

and $\frac{1}{\phi}=-\sum_{k=1}^\infty\frac{\mu(k)}{k}\log\left(1-\frac{1}{\phi^k}\right).$

Subtracting them gives $\sum_{k=1}^\infty\frac{\mu(k)-\varphi(k)}{k}\log\left(1-\frac{1}{\phi^k}\right)=1.$

Applying the exponential function to both sides of the preceding identity yields an infinite product formula for Euler's number e $e= \prod_{k=1}^{\infty} \left(1-\frac{1}{\phi^k}\right)^\frac{\mu(k)-\varphi(k)}{k}.$

The proof is based on the formulae $\sum_{k=1}^\infty\frac{\varphi(k)}{k}(-\log(1-x^k))=\frac{x}{1-x}$     and $\sum_{k=1}^\infty\frac{\mu(k)}{k}(-\log(1-x^k))=x,$     valid for 0 < x < 1.

## Generating functions

The Dirichlet series for φ(n) may be written in terms of the Riemann zeta function as: $\sum_{n=1}^\infty \frac{\varphi(n)}{n^s}=\frac{\zeta(s-1)}{\zeta(s)}.$

The Lambert series generating function is $\sum_{n=1}^{\infty} \frac{\varphi(n) q^n}{1-q^n}= \frac{q}{(1-q)^2}$

which converges for |q| < 1.

Both of these are proved by elementary series manipulations and the formulae for φ(n).

## Growth of the function

In the words of Hardy & Wright, the order of φ(n) is "always ‘nearly n’."

First $\lim\sup \frac{\varphi(n)}{n}= 1,$

but as n goes to infinity, for all δ > 0 $\frac{\varphi(n)}{n^{1-\delta}}\rightarrow\infty.$

These two formulae can be proved by using little more than the formulae for φ(n) and the divisor sum function σ(n).

In fact, during the proof of the second formula, the inequality $\frac {6}{\pi^2} < \frac{\varphi(n) \sigma(n)}{n^2} < 1,$

true for n > 1, is proven.

We also have $\lim\inf\frac{\varphi(n)}{n}\log\log n = e^{-\gamma}.$

Here γ is Euler's constant,   γ = 0.577215665...,   eγ = 1.7810724...,   e−γ = 0.56145948... .

Proving this doesn't quite require the prime number theorem. Since log log (n) goes to infinity, this formula shows that $\lim\inf\frac{\varphi(n)}{n}= 0.$

In fact, more is true. $\varphi(n) > \frac {n} {e^\gamma\; \log \log n + \frac {3} {\log \log n}}$       for n > 2, and $\varphi(n) < \frac {n} {e^{ \gamma}\log \log n}$                       for infinitely many n.

The second inequality was shown by Jean-Louis Nicolas. Ribenboim says "The method of proof is interesting, in that the inequality is shown first under the assumption that the Riemann hypothesis is true, secondly under the contrary assumption."

For the average order, we have $\varphi(1)+\varphi(2)+\cdots+\varphi(n) = \frac{3n^2}{\pi^2}+O\left(n(\log n)^{2/3}(\log\log n)^{4/3}\right)\ \ (n\rightarrow\infty),$

due to Arnold Walfisz, its proof exploiting estimates on exponential sums due to I. M. Vinogradov and N.M. Korobov (this is currently the best known estimate of this type). The "Big O" stands for a quantity that is bounded by a constant times the function of "n" inside the parentheses (which is small compared to n2).

This result can be used to prove that the probability of two randomly chosen numbers being relatively prime is $\tfrac{6}{\pi^2}.$

## Ratio of consecutive values

In 1950 Somayajulu proved $\lim\inf \frac{\varphi(n+1)}{\varphi(n)}= 0$         and $\lim\sup \frac{\varphi(n+1)}{\varphi(n)}= \infty.$

In 1954 Schinzel and Sierpiński strengthened this, proving that the set $\left\{\frac{\varphi(n+1)}{\varphi(n)},\;\;n = 1,2,\cdots\right\}$

is dense in the positive real numbers. They also proved that the set $\left\{\frac{\varphi(n)}{n},\;\;n = 1,2,\cdots\right\}$

is dense in the interval (0, 1).

## Totient numbers

A totient number is a value of Euler's totient function: that is, an m for which there is at least one n for which φ(n) = m. The valency or multiplicity of a totient number m is the number of solutions to this equation. A nontotient is a natural number which is not a totient number: there are infinitely many nontotients, and indeed every odd number has an even multiple which is a nontotient.[clarification needed]

The number of totient numbers up to a given limit x is $\frac{x}{\log x}\exp\left({ (C+o(1))(\log\log\log x)^2 }\right) \$

for a constant C = 0.8178146... .

If counted accordingly to multiplicity, the number of totient numbers up to a given limit x is $\vert\{ n : \phi(n) \le x \}\vert = \frac{\zeta(2)\zeta(3)}{\zeta(6)} \cdot x + R(x) \$

where the error term R is of order at most $x / (\log x)^k$ for any positive k.

It is known that the multiplicity of m exceeds mδ infinitely often for any δ < 0.55655.

### Ford's theorem

Ford (1999) proved that for every integer k ≥ 2 there is a totient number m of multiplicity k: that is, for which the equation φ(n) = m has exactly k solutions; this result had previously been conjectured by Wacław Sierpiński, and it had been obtained as a consequence of Schinzel's hypothesis H. Indeed, each multiplicity that occurs, does so infinitely often.

However, no number m is known with multiplicity k = 1. Carmichael's totient function conjecture is the statement that there is no such m.

## Applications

### Cyclotomy

In the last section of the Disquisitiones Gauss proves that a regular n-gon can be constructed with straightedge and compass if φ(n) is a power of 2. If n is a power of an odd prime number the formula for the totient says its totient can be a power of two only if a) n is a first power and b) n − 1 is a power of 2. The primes that are one more than a power of 2 are called Fermat primes, and only five are known: 3, 5, 17, 257, and 65537. Fermat and Gauss knew of these. Nobody has been able to prove whether there are any more.

Thus, a regular n-gon has a straightedge-and-compass construction if n is a product of distinct Fermat primes and any power of 2. The first few such n are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 17, 20, 24, 30, 32, 34, 40, ... .     (sequence A003401 in OEIS)

### The RSA cryptosystem

Setting up an RSA system involves choosing large prime numbers p and q, computing n = pq and k = φ(n), and finding two numbers e and d such that ed ≡ 1 (mod k). The numbers n and e (the "encryption key") are released to the public, and d (the "decryption key") is kept private.

A message, represented by an integer m, where 0 < m < n, is encrypted by computing S = me (mod n).

It is decrypted by computing t = Sd (mod n). Euler's Theorem can be used to show that if 0 < t < n, then t = m.

The security of an RSA system would be compromised if the number n could be factored or if φ(n) could be computed without factoring n.

## Unsolved problems

### Lehmer's conjecture

If p is prime, then φ(p) = p − 1. In 1932 D. H. Lehmer asked if there are any composite numbers n such that φ(n) | n − 1. None are known.

In 1933 he proved that if any such n exists, it must be odd, square-free, and divisible by at least seven primes (i.e. ω(n) ≥ 7). In 1980 Cohen and Hagis proved that n > 1020 and that ω(n) ≥ 14. Further, Hagis showed that if 3 divides n then n > 101937042 and ω(n) ≥ 298848.

### Carmichael's conjecture

This states that there is no number n with the property that for all other numbers m, mn, φ(m) ≠ φ(n). See Ford's theorem above.

As stated in the main article, if there is a single counterexample to this conjecture, there must be infinitely many counterexamples, and the smallest one has at least ten billion digits in base 10.