Check If Two Numbers Are Coprime Using Python



In this article, we are going to learn how we can check whether two given numbers are co-prime in Python using different approaches.

What are Co-prime Numbers?

Two numbers are said to be co-prime numbers if they have no common factors other than 1.

Checking if two numbers are co-prime

We can check if two given numbers are co-prime numbers by finding their GCD (Greatest Common Factor). GCD is also known as HCF (Highest Common Factor). If the GCD of two numbers is 1, then those numbers are co-prime numbers; otherwise, they are not co-prime numbers.

Below are the examples to understand the problem statement clearly:

Example 1

The factors of 2 are {1, 2}, the factors of 3 are {1, 3}. Since they have only 1 as a common factor, they are co-prime numbers.

Input Number1 = 2, Number2 = 3 Output Co-prime 

Example 2

The factors of 6 are {1, 2, 3, 6}, The factors of 9 are {1, 3, 9}. Since they have two common factors, 1 and 3, they are not co-prime numbers.

Input Number1 = 6, Number2 = 9 Output Not Co-prime 
Explanation

Below are different approaches for checking if two numbers are co-prime in Python.

Using the math.gcd() Function

Python has an in-built function math.gcd() that directly calculates the greatest common divisor of two numbers. If the GCD is 1, then the numbers are said to be co-prime numbers.

Example

import math number1 = 2 number2 = 3 gcd = math.gcd(number1, number2) if gcd == 1: print(f"The numbers {number1} and {number2} are Co-prime numbers") else: print(f"The numbers {number1} and {number2} are Not Co-prime numbers") 

Output

The numbers 2 and 3 are Co-prime numbers 

Time Complexity :O(log N).

Using a Loop

In this approach, we use a loop to iterate through the given numbers manually and find their Greatest Common Divisor (GCD). If the GCD of two numbers is 1, then they are co-prime numbers; otherwise, they are not.

Example

def gcd(a, b): while b != 0: temp = b b = a % b a = temp return a number1 = 6 number2 = 9 gcd_value = gcd(number1, number2) if gcd_value == 1: print(f"The numbers {number1} and {number2} are Co-prime numbers") else: print(f"The numbers {number1} and {number2} are Not Co-prime numbers") 

Output

The numbers 6 and 9 are Not Co-prime numbers 

Time Complexity: O(log N).

Using Recursion

In this approach, we use a recursive method to calculate the GCD of two numbers. We use the Euclidean algorithm recursively to find the greatest common divisor (GCD) of the two numbers.

Example

def gcd_recursive(a, b): if b == 0: return a return gcd_recursive(b, a % b) number1 = 23 number2 = 29 gcd_value = gcd_recursive(number1, number2) if gcd_value == 1: print(f"The numbers {number1} and {number2} are Co-prime numbers") else: print(f"The numbers {number1} and {number2} are Not Co-prime numbers") 

Output

The numbers 23 and 29 are Co-prime numbers 

Time Complexity: O(log N).

Updated on: 2025-03-27T14:42:07+05:30

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements
close