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.
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))
4 8 20
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to check if two given numbers are Co Prime or not. Return True if two numbers are Co Prime otherwise return false.
Next: Write a Python program to create a coded string from a given string, using specified formula.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join