Check Twin Prime in Java
Problem
A twin prime pair is two prime numbers that differ by exactly 2, like 11 and 13 or 17 and 19.
Given two integers, determine whether they form a twin prime pair.
Java Program
public class TwinPrimeCheck {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false; // found a divisor, not prime
}
return true;
}
public static void main(String[] args) {
int a = 11, b = 13;
// Both numbers must be prime, and exactly 2 apart
boolean isTwinPrime = isPrime(a) && isPrime(b) && Math.abs(b - a) == 2;
System.out.println("Twin prime: " + isTwinPrime);
}
}Output
Core Logic
Checking that both numbers are prime, and that they differ by exactly 2, covers both requirements a twin prime pair has to satisfy.
- 1
isPrime(n)is a helper method testing divisors only up to√n, the standard efficient primality check. - 2
isPrime(a)andisPrime(b)both have to betrue— a twin prime pair requires both numbers to be prime, not just one. - 3
Math.abs(b - a) == 2checks that the two numbers are exactly 2 apart, regardless of which one is passed first. - 4All three conditions are combined with
&&, so the pair only counts as twin primes if every one holds.
11 and 13, both are prime and they differ by exactly 2, so isTwinPrime is true.Key Point: Math.abs() matters here — without it, passing the larger number first would compute a negative difference and never match 2.
Why: Each isPrime() call only tests divisors up to the square root of its input, and the two calls plus the difference check use only a constant amount of extra memory.
Key Concepts
Approach 2: Java 8
import java.util.stream.IntStream;
public class TwinPrimeCheckStream {
static boolean isPrime(int n) {
return n >= 2 && IntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0);
}
public static void main(String[] args) {
int a = 11, b = 13;
// Both numbers must be prime, and exactly 2 apart
boolean isTwinPrime = isPrime(a) && isPrime(b) && Math.abs(b - a) == 2;
System.out.println("Twin prime: " + isTwinPrime);
}
}
Output
Core Logic
Expressing the primality test as a stream that looks for any divisor — and confirming none exist — replaces the manual loop-and-break with a single declarative check.
- 1
isPrime(n)now returnsIntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0)instead of looping with an explicitreturn false. - 2
noneMatch()short-circuits the moment it finds a divisor, just like the manual loop's earlyreturndid. - 3
isPrime(a) && isPrime(b) && Math.abs(b - a) == 2combines the two stream-based checks exactly as before.
11 and 13, isPrime streams divisors 2 and 3 for each number, finds none that divide evenly, and the pair is confirmed twin prime.Key Point: noneMatch() reads as 'no divisor exists', which states the definition of primality more directly than a loop that returns false on the first counterexample.
Why: The stream still only tests divisors up to √n for each number and short-circuits on the first match, the same bound as the manual loop.