Check Co-Prime Numbers in Java
Problem
Two numbers are co-prime (or relatively prime) when the only positive integer that divides both of them evenly is 1 — they don't need to be prime themselves, just share no other common factor.
Given two integers, determine whether they are co-prime.
Java Program
public class CoPrimeCheck {
public static void main(String[] args) {
int a = 8, b = 15;
boolean isCoPrime = true;
for (int i = 2; i <= Math.min(a, b); i++) {
if (a % i == 0 && b % i == 0) {
isCoPrime = false;
break; // found a shared factor besides 1, no need to check further
}
}
System.out.println("Co-prime: " + isCoPrime);
}
}Output
Core Logic
Checking every possible common divisor from 2 up to the smaller number, and stopping the instant one is found, confirms whether the numbers share any factor besides 1.
- 1
isCoPrimestarts astrue, assuming no common factor exists until proven otherwise. - 2The loop tries every candidate
ifrom2up toMath.min(a, b)— 1 is skipped, since every pair of numbers trivially shares it. - 3
a % i == 0 && b % i == 0checks whetheridivides both numbers evenly. - 4The first shared factor found sets
isCoPrimetofalseand exits the loop immediately withbreak.
8 and 15, no number from 2 to 8 divides both evenly, so isCoPrime stays true.Key Point: Starting the search at 2 instead of 1 is what makes this meaningful — every pair of integers shares 1 as a divisor, so co-primality is really about whether anything larger is also shared.
Why: In the worst case the loop checks every candidate from 2 up to the smaller number, using only a single boolean flag regardless of the numbers' size.
Key Concepts
Approach 2: Using GCD
public class CoPrimeCheckGCD {
static int gcd(int a, int b) {
if (b == 0) return a; // base case: a is the GCD once b runs out
return gcd(b, a % b);
}
public static void main(String[] args) {
int a = 8, b = 15;
// Two numbers are co-prime exactly when their GCD is 1
boolean isCoPrime = gcd(a, b) == 1;
System.out.println("Co-prime: " + isCoPrime);
}
}
Output
Core Logic
Two numbers are co-prime exactly when their greatest common divisor is 1 — computing the GCD directly skips checking individual candidate factors altogether.
- 1
gcd(a, b)computes the greatest common divisor using the Euclidean algorithm. - 2
gcd(a, b) == 1checks whether that GCD is exactly 1, which is the definition of co-primality. - 3No candidate factors are tried one by one — the Euclidean algorithm reaches the GCD directly.
8 and 15, gcd(8, 15) reduces to 1, so isCoPrime is true.Key Point: This reaches the answer in a handful of steps regardless of how large the numbers are, unlike the brute-force version whose cost grows with the smaller number.
Why: Computing gcd() via the Euclidean algorithm takes logarithmic time and recursion depth in the smaller number, much faster than trying every candidate factor.
Key Concepts
Approach 3: Java 8
import java.util.stream.IntStream;
public class CoPrimeCheckStream {
public static void main(String[] args) {
int a = 8, b = 15;
// No candidate from 2 up to the smaller number divides both evenly
boolean isCoPrime = IntStream.rangeClosed(2, Math.min(a, b))
.noneMatch(i -> a % i == 0 && b % i == 0);
System.out.println("Co-prime: " + isCoPrime);
}
}
Output
Core Logic
Streaming every candidate from 2 up to the smaller number and confirming none divides both evenly expresses the same brute-force search as the primary approach, without an explicit loop or break.
- 1
IntStream.rangeClosed(2, Math.min(a, b))generates the same candidate range the manual loop iterated over. - 2
.noneMatch(i -> a % i == 0 && b % i == 0)checks that no candidate in that range divides bothaandbevenly. - 3
noneMatch()short-circuits the instant it finds a shared factor, the same early exit the manualbreakprovided.
8 and 15, streaming candidates 2 through 8 finds none that divide both numbers, so noneMatch() returns true.Key Point: This is the stream equivalent of the brute-force primary approach, not the GCD alternate — it still checks every candidate factor, just declaratively instead of with a manual loop and flag.
Why: The stream still tests every candidate from 2 up to the smaller number in the worst case and short-circuits on the first shared factor, the same bound as the manual loop.