Efficiently check if two numbers are co-primes relatively primes?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Two integers are co-prime if their greatest common divisor is 1. The efficient way to check that is not to list all factors, but to compute the GCD directly with the Euclidean algorithm and compare the result to 1.
Use the Euclidean Algorithm
The Euclidean algorithm is the standard solution because it reduces the problem quickly using repeated remainder operations. Instead of checking every possible divisor, it repeatedly replaces the pair (a, b) with (b, a % b) until the second value becomes zero.
If the final non-zero value is 1, the numbers are relatively prime.
For example, check 35 and 64:
- '
64 % 35 = 29' - '
35 % 29 = 6' - '
29 % 6 = 5' - '
6 % 5 = 1' - '
5 % 1 = 0'
The last non-zero value is 1, so 35 and 64 are co-prime.
This method is efficient because the numbers shrink fast. Its running time is logarithmic in practice and in theory, which is far better than trial division over a large range.
A Simple Python Implementation
You can implement the algorithm in a few lines:
This works for positive or negative integers because the sign does not affect common divisors. Taking the absolute value up front keeps the logic clear.
In real Python code, the built-in math.gcd is even simpler:
If your goal is just correctness and clarity, math.gcd is the best default.
Why Factorization Is Usually the Wrong Tool
A common beginner approach is to factor both numbers and compare the factor lists. That can work for small integers, but it is more work than necessary and gets expensive quickly.
For example, factorization forces you to answer questions you do not actually need. To decide whether two numbers are co-prime, you do not need every prime factor. You only need to know whether the final GCD is 1.
That is why the Euclidean algorithm appears in cryptography, modular arithmetic, and competitive programming. It gives the exact answer with minimal extra work.
Extended Cases and Shortcuts
A few observations are useful in practice:
- consecutive integers are always co-prime
- if both numbers are even, they are not co-prime
- if
math.gcd(a, b) > 1, they share a factor and the check is finished
You can also use the GCD result for more than a boolean answer. If the numbers are not relatively prime, the GCD tells you the largest divisor they share.
That is often useful for debugging numeric algorithms or simplifying fractions.
Common Pitfalls
The most common mistake is trying every divisor from 2 up to the smaller number. That works, but it is unnecessarily slow for large inputs.
Another issue is forgetting edge cases. For example, 0 is not co-prime with most numbers because gcd(0, n) is |n|, not 1. Also, 1 is co-prime with every integer except 0 if you follow the standard GCD-based definition carefully.
Negative inputs also confuse some implementations. Common divisors are usually discussed for positive integers, but code should normalize signs so the result is stable.
Finally, do not confuse co-prime with prime. Two numbers can be composite and still be relatively prime. For example, 8 and 15 are not prime numbers, but they are co-prime because their GCD is 1.
Summary
- Two integers are co-prime when their GCD is
1. - The Euclidean algorithm is the efficient standard way to check that.
- In Python,
math.gcd(a, b) == 1is the clearest solution. - Factorization is usually unnecessary and slower.
- Handle edge cases such as
0, negative values, and the difference between prime and co-prime.
Related reading
- Efficiently computing a - K / a K with improved accuracy
- Efficiently determine the parity of a permutation
- Efficiently find all connected induced subgraphs
- efficiently find amount of integers in a sorted array
- Efficiently finding the largest surrounding square in 2D grid
- Efficiently getting all divisors of a given number
- Efficiently find an integer not in a set of size 40, 400, or 4000
- Efficiently find binary strings with low Hamming distance in large set

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.