Skip to main content
Abdi Moalim

Euler's totient function

Euler's totient function, denoted ϕ(n), counts the positive integers up to a given integer n that are relatively prime to n, meaning their greatest common divisor with n is 1.

The precise definition states that for any positive integer n, the value of ϕ(n) equals the number of integers k in the range 1kn such that gcd(k,n)=1.

Take a prime number p. All positive integers less than p are relatively prime to p.

 ϕ(p)=p1

For a prime power pa, where p is prime and a is a positive integer, the totient function is:

ϕ(pa)=papa1=pa(11p)

This only arises because among the numbers from 1 to pa, exactly those that are divisible by p are not relatively prime to pa. There are pa1 such numbers.

One of the most valuable properties of Euler's totient function is its multiplicativity.

If m and n are coprime (i.e. gcd(m,n)=1), then:

ϕ(mn)=ϕ(m)×ϕ(n)

This let's us compute ϕ(n) for any integer n by first finding its prime factorization. Given n=p1a1×p2a2××pkak, where each pi is a distinct prime, we have:

ϕ(n)=ϕ(p1a1)×ϕ(p2a2)××ϕ(pkak)

Substituting the formula for prime powers, we get:

ϕ(n)=p1a1(11p1)×p2a2(11p2)××pkak(11pk)

Which simplifies to:

ϕ(n)=n×(11p1)×(11p2)××(11pk)

The time complexity of computing ϕ(n) depends on the algorithm. Using the formula above requires finding the prime factorization of n, which has a time complexity of O(n1/4) using the general number field sieve (GNFS) for large integers.

Although, for practical purposes with reasonably sized numbers, the complexity is expressed as O(n) using trial division methods.

The space complexity for computing ϕ(n) is O(logn) as we need to store the prime factorization of n.

Euler's totient function appears in several important theorems in number theory. One of the most significant is Euler's theorem, which states that if a and n are coprime, then:

aϕ(n)1(modn)

This is a generalization of Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then:

ap11(modp)

Note that for a prime p, we have ϕ(p)=p1, which aligns the two theorems.

In RSA, the security relies on the difficulty of factoring large composite numbers. The encryption and decryption operations use modular exponentiation, with the decryption key d satisfying:

ed1(modϕ(n))

Where e is the encryption key and n is the product of two large primes.

The computational complexity of modular exponentiation used in RSA is O(log3n) using the square-and-multiply algorithm which is way faster than attempting to factor n to find ϕ(n) directly.

Another important result of ϕ(n) is the formula for the sum of ϕ(d) over all divisors d of n:

d|nϕ(d)=n

It's an elegant result that relates the totient function and the structure of integer divisors.

E.g. let's compute this sum for n=12:

As predicted, the sum equals n=12.

The Möbius inversion formula provides a way to express ϕ(n) in terms of the Möbius function μ(n).

ϕ(n)=d|nμ(d)nd

The Möbius function μ(n) is defined as:

Computing ϕ(n) using this approach has a time complexity of O(nloglogn) if we precompute the Möbius function using the sieve of Eratosthenes.

Euler's totient function also appears in the context of cyclotomic polynomials. The n-th cyclotomic polynomial, denoted Φn(x), is the minimal polynomial over the rational numbers of the primitive n-th roots of unity. The degree of Φn(x) is precisely ϕ(n).

For example, Φ1(x)=x1 and ϕ(1)=1, confirming that the degree is ϕ(1).

The totient function satisfies several interesting identities and inequalities.

ϕ(n)n/2 for n>6

The average order of the totient function is:

1nk=1nϕ(k)3nπ23n10

This means that as n grows, the average value of ϕ(k) for k from 1 to n approaches 3nπ2.

Computing ϕ(n) for a large range of values can be done using the sieve method. If we want to compute ϕ(k) for all k from 1 to n, the time complexity is O(nloglogn) and the space complexity is O(n).

The algorithm proceeds as follows:

  1. Initialize an array ϕ[1...n] with ϕ[i]=i for all i
  2. For each prime p from 2 to n:
    • If ϕ[p]=p (meaning p is still marked as a prime):
      • Subtract ϕ[j] by ϕ[j]/p for all multiples j of p up to n

This applies ϕ(n)=np|n(11p) to all numbers simultaneously.

Let's compute some values of ϕ(n) for small n.

The Chinese Remainder Theorem (CRT) is closely related to Euler's totient function. CRT states that if m1,m2,,mk are pairwise coprime positive integers, then the system of congruences:

xa1(modm1)xa2(modm2)xak(modmk)

has a unique solution modulo M=m1×m2××mk.

The number of solutions to the congruence x21(modn) is related to the prime factorization of n and can be expressed in terms of ϕ(n) for certain types of n.

In group theory, if G is a finite abelian group, then the number of elements of order n in G is either 0 or ϕ(n). This is useful in the study of the multiplicative group of integers modulo n, denoted (Z/nZ), which has order ϕ(n).

For prime p, the multiplicative group (Z/pZ) is cyclic of order ϕ(p)=p1. A generator of this group is called a primitive root modulo p.

The complexity of finding a primitive root modulo p is O((logp)4) using deterministic algorithms or O((logp)2) using probabilistic algorithms.

Euler's totient function is also related to the Carmichael function λ(n), which gives the smallest positive integer m such that am1(modn) for all integers a coprime to n. The Carmichael function always divides ϕ(n) and equals ϕ(n) when n is 1, 2, 4, a power of an odd prime or twice a power of an odd prime.

In the context of computational number theory, algorithms for computing ϕ(n) can be optimized using various techniques. E.g. if we have access to the prime factorization of n, we can compute ϕ(n) in O(k) time where k is the number of distinct prime factors of n.

Research on the distribution of values of ϕ(n) has revealed interesting patterns. E.g. ϕ(a)=ϕ(b) can have multiple solutions, meaning there exist distinct integers that share the same totient value. The smallest such pair is (a,b)=(4,6) since ϕ(4)=ϕ(6)=2.

The totient function has connections to perfect numbers as well. A perfect number is equal to the sum of its proper divisors. If 2p1 is prime (making it a Mersenne prime), then n=2p1(2p1) is a perfect number and ϕ(n)=2p1(2p11)=2p1(2p2)=2p(2p11).