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.

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
print(is_coprime(17, 13))
print(is_coprime(17, 21))
print(is_coprime(15, 21))
print(is_coprime(25, 45))

Sample Output:



Flowchart: Python - Check if two given numbers are Co Prime or not.

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.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value:


def tips_map_dictionary(itr, fn):
  ret = {}
  for a in itr:
    ret[a] = fn(a)
  return ret
print(tips_map_dictionary([2,4,6], lambda a: a * a))


{2: 4, 4: 16, 6: 36}


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook