Python: Check if two given numbers are Co Prime or not
Python Basic - 1: Exercise-119 with Solution
Two numbers are coprime if their highest common factor (or greatest common divisor if you must) is 1.
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.
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 print(is_coprime(17, 13)) print(is_coprime(17, 21)) print(is_coprime(15, 21)) print(is_coprime(25, 45))
True True False False
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to show the individual process IDs (parent process, process id etc.) involved.
Next: Write a Python program to calculate Euclid's totient function of a given integer. Use a primitive method to calculate Euclid's totient function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
What is the difference between Python's list methods append and extend?
append: Appends object at the end.
x = [1, 2, 3] x.append([4, 5]) print (x)
[1, 2, 3, [4, 5]]
extend: Extends list by appending elements from the iterable.
x = [1, 2, 3] x.extend([4, 5]) print (x)
[1, 2, 3, 4, 5]
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework