Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 coprime in Python using different approaches.
What are Coprime Numbers?
Two numbers are said to be coprime numbers (also called relatively prime) if they have no common factors other than 1. In other words, their Greatest Common Divisor (GCD) is 1.
Examples
Example 1: Numbers 2 and 3
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 coprime numbers.
Input: Number1 = 2, Number2 = 3 Output: Coprime
Example 2: Numbers 6 and 9
The factors of 6 are {1, 2, 3, 6}, the factors of 9 are {1, 3, 9}. Since they have common factors 1 and 3, they are not coprime numbers.
Input: Number1 = 6, Number2 = 9 Output: Not Coprime
Method 1: Using math.gcd() Function
Python has a built-in function math.gcd() that directly calculates the greatest common divisor of two numbers. If the GCD is 1, then the numbers are coprime ?
import math
def are_coprime(num1, num2):
return math.gcd(num1, num2) == 1
# Test with coprime numbers
number1 = 2
number2 = 3
if are_coprime(number1, number2):
print(f"The numbers {number1} and {number2} are Coprime numbers")
else:
print(f"The numbers {number1} and {number2} are Not Coprime numbers")
# Test with non-coprime numbers
number1 = 6
number2 = 9
if are_coprime(number1, number2):
print(f"The numbers {number1} and {number2} are Coprime numbers")
else:
print(f"The numbers {number1} and {number2} are Not Coprime numbers")
The numbers 2 and 3 are Coprime numbers The numbers 6 and 9 are Not Coprime numbers
Time Complexity: O(log min(a,b))
Method 2: Using Euclidean Algorithm with Loop
In this approach, we implement the Euclidean algorithm manually using a loop to find the GCD of two numbers ?
def gcd_iterative(a, b):
while b != 0:
a, b = b, a % b
return a
def are_coprime_iterative(num1, num2):
return gcd_iterative(num1, num2) == 1
# Test the function
number1 = 15
number2 = 28
gcd_value = gcd_iterative(number1, number2)
print(f"GCD of {number1} and {number2} is: {gcd_value}")
if are_coprime_iterative(number1, number2):
print(f"The numbers {number1} and {number2} are Coprime numbers")
else:
print(f"The numbers {number1} and {number2} are Not Coprime numbers")
GCD of 15 and 28 is: 1 The numbers 15 and 28 are Coprime numbers
Time Complexity: O(log min(a,b))
Method 3: Using Recursion
This approach uses the Euclidean algorithm recursively to calculate the GCD of two numbers ?
def gcd_recursive(a, b):
if b == 0:
return a
return gcd_recursive(b, a % b)
def are_coprime_recursive(num1, num2):
return gcd_recursive(num1, num2) == 1
# Test with different pairs
test_pairs = [(23, 29), (12, 18), (17, 19)]
for num1, num2 in test_pairs:
gcd_val = gcd_recursive(num1, num2)
result = "Coprime" if gcd_val == 1 else "Not Coprime"
print(f"Numbers {num1} and {num2}: GCD = {gcd_val}, Result = {result}")
Numbers 23 and 29: GCD = 1, Result = Coprime Numbers 12 and 18: GCD = 6, Result = Not Coprime Numbers 17 and 19: GCD = 1, Result = Coprime
Time Complexity: O(log min(a,b))
Comparison
| Method | Pros | Cons | Best For |
|---|---|---|---|
math.gcd() |
Built-in, optimized, simple | Requires import | Production code |
| Iterative | No recursion overhead | More code to write | Understanding algorithms |
| Recursive | Clean, mathematical | Stack overflow risk | Small numbers |
Conclusion
Use math.gcd() for production code as it's optimized and reliable. All three methods have the same time complexity, but the built-in function is the most efficient and readable approach for checking coprime numbers.
