# Euler’s Totient Function using Python

Euler’s Totient function Φ(n) for an input n is count of numbers in the format of {1, 2, 3, 4, 5, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.

Examples:

Φ(1) = 1

gcd(1, 1) is 1

Φ(2) = 1

gcd(1, 2) is 1, but gcd(2, 2) is 2.

Φ(3) = 2

gcd(1, 3) is 1 and gcd(2, 3) is 1

Φ(4) = 2

gcd(1, 4) is 1 and gcd(3, 4) is 1

Φ(5) = 4

gcd(1, 5) is 1, gcd(2, 5) is 1,

gcd(3, 5) is 1 and gcd(4, 5) is 1

Φ(6) = 2

gcd(1, 6) is 1 and gcd(5, 6) is 1,

Steps to be performed to get the result:

1) Initialize : result = n

2) Run a loop from ‘p’ = 2 to sqrt(n), do following for every ‘p’.

a) If p divides n, then

Set: result = result * (1.0 – (1.0 / (float) p));

Divide all occurrences of p in n.

3) Return result

**Find the below code in Python 3.0 to get the required Output:**

# Python 3 program to calculate

# Euler's Totient Function

# using Euler's product formula

def run(n) :

result = n # Initialize result as n

# Consider all prime factors

# of n and for every prime

# factor p, multiply result with (1 – 1/p)

p=2

while(p*p<=n) :

# Check if p is a prime factor.

if (n % p == 0) :

# If yes, then update n and result

while (n % p == 0) :

n = n // p

result = result * (1.0 – (1.0 / (float) (p)))

p = p + 1

# If n has a prime factor

# greater than sqrt(n)

# (There can be at-most one

# such prime factor)

if (n > 1) :

result = result * (1.0 – (1.0 / (float)(n)))

return (int)(result)

# Driver program to test above function

for n in range(1,11) :

print(“run(“,n,”) = “,run(n))

## Sachin

Thanks a lot for this information. I loves to read this blog.

## Nitikesh

Thank you Sachin

## frolep rotrem

Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch because I found it for him smile Therefore let me rephrase that: Thanks for lunch! “Procrastination is the thief of time.” by Edward Young.

## Nitikesh

Thank you so much Frolep for your valuable comment on the topic. We are really glad that the blog helped you a lot.

## Shanelle Pappa

Magnificent beat ! I wish to apprentice even as you amend your website, how could i subscribe for a weblog website? The account helped me a applicable deal. I had been a little bit familiar of this your broadcast offered shiny transparent idea

## Rory Moschetti

El ejemplo y servicio que aportaron al mundo los rusos y otras nacionalidades durante estos acontecimientos terribles, sirvieron para que el mundo avanzara en el camino de la prosperidad y el bien de los desposeídos en el planeta. La troika (CE, BCE y FMI) ha presentado al Gobierno de Chipre una nueva propuesta con recortes de 1.200 millones de euros a cambio de asistencia financiera, en vez de los 975 millones de ajustes solicitados a Nicosia el pasado agosto, informó hoy la prensa local. El paro coincide con la huelga indefinida que anunció la asamblea de la Asociación de Facultativos Especialistas de Madrid (Afem) a partir del próximo 2 de noviembre.

## Google

GoogleEvery when in a even though we decide on blogs that we read. Listed beneath are the latest sites that we pick out.

## vreyro linomit

Valuable information. Lucky me I found your website by accident, and I’m shocked why this accident didn’t happened earlier! I bookmarked it.