Euler phi function



p = eulerPhi(n) evaluates the Euler phi function or (also known as the totient function) for a positive integer n.


collapse all

Compute the Euler phi function ϕ(n) for the integer n=35.

p = eulerPhi(35)
p = 24

The Euler phi function satisfies the multiplicative property ϕ(xy)=ϕ(x)ϕ(y) if the two integers x and y are relatively prime (also known as coprime). The integer factorization of 35 is 7 and 5, which are relatively prime. Show that ϕ(35) satisfies the multiplicative property.

Calculate ϕ(x) and ϕ(y) for the two factors.

px = eulerPhi(7)
px = 6
py = eulerPhi(5)
py = 4

Verify that px and py satisfy the multiplicative property.

p = px*py
p = 24

If a positive integer n has prime factorization n=p1k1p2k2prkr with distinct prime factors p1,p2,,pr, then the Euler phi function satisfies the product formula


The integer n=36 has distinct prime factors 2 and 3. Show that ϕ(36) satisfies the Euler's product formula.

Declare 36 as a symbolic number and evaluate ϕ(36).

n = sym(36)
n = 36sym(36)

List the prime factors of 36.

f_n = factor(n)
f_n = (2233)[sym(2), sym(2), sym(3), sym(3)]

Substitute the prime factors 2 and 3 into the product formula.

p_product = n*(1-1/2)*(1-1/3)
p_product = 12sym(12)

Euler's theorem states that aϕ(n)1(modn) if and only if the two positive integers a and n are relatively prime. Show that the Euler phi function ϕ(n) satisfies Euler's theorem for the integers a=15 and n=4.

a = 15;
n = 4;
isCongruent = powermod(a,eulerPhi(n),n) == mod(1,n)
isCongruent = logical

Confirm that a and n are relatively prime.

g = gcd(a,n)
g = 1

Calculate the Euler phi function ϕ(n) for the integers n from 1 to 1000.

P = eulerPhi(1:1000);

Find the mean value of ϕ(n)/n.

Pave = mean(P./(1:1000))
Pave = 0.6082

Input Arguments

collapse all

Input, specified as a number, vector, matrix, array, symbolic number, or symbolic array. The elements of n must be positive integers.

Data Types: single | double | sym

More About

collapse all

Euler Phi Function

The Euler phi function ϕ(n) computes the number of integers between 1 and n that are relatively prime (also known as coprime) to n. Two integers are relatively prime if there is no integer greater than one that divides them both. In other words, their greatest common divisor is one.


[1] Redmond, D. Number Theory: An Introduction to Pure and Applied Mathematics. New York: Marcel Dekker, 1996.

See Also


Introduced in R2020a