﻿ Python: Calculate Euclid's totient function of a given integer - w3resource

# Python: Calculate Euclid's totient function of a given integer

## Python Basic - 1: Exercise-120 with Solution

In number theory, Euler's totient function counts the positive integers up to a given integer n that are relatively prime to n. It is written using the Greek letter phi as φ(n) or ϕ(n), and may also be called Euler's phi function.
Write a Python program to calculate Euclid's totient function for a given integer. Use a primitive method to calculate Euclid's totient function.

Sample Solution:

Python Code:

``````def gcd(p,q):
# Create the gcd of two positive integers.
while q != 0:
p, q = q, p%q
return p

def is_coprime(x, y):
return gcd(x, y) == 1

def phi_func(x):
if x == 1:
return 1
else:
n = [y for y in range(1,x) if is_coprime(x,y)]
return len(n)
print(phi_func(10))
print(phi_func(15))
print(phi_func(33))
``````

Sample Output:

```4
8
20
```

Flowchart: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿