Java ProgramsNumbersCheck Twin Prime

Check Twin Prime in Java

beginner·  Numbers  ·  Number Theory

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.

Input
11, 13
Output
Twin prime: true

Java Program

Java
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

Twin prime: true

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.

How It Works
  1. 1isPrime(n) is a helper method testing divisors only up to √n, the standard efficient primality check.
  2. 2isPrime(a) and isPrime(b) both have to be true — a twin prime pair requires both numbers to be prime, not just one.
  3. 3Math.abs(b - a) == 2 checks that the two numbers are exactly 2 apart, regardless of which one is passed first.
  4. 4All three conditions are combined with &&, so the pair only counts as twin primes if every one holds.
For 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.

Complexity
Time Complexity: O(√n)Space Complexity: O(1)

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

helper methodsquare-root boundboolean logic

Approach 2: Java 8

Java
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

Twin prime: true

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.

How It Works
  1. 1isPrime(n) now returns IntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0) instead of looping with an explicit return false.
  2. 2noneMatch() short-circuits the moment it finds a divisor, just like the manual loop's early return did.
  3. 3isPrime(a) && isPrime(b) && Math.abs(b - a) == 2 combines the two stream-based checks exactly as before.
For 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.

Complexity
Time Complexity: O(√n)Space Complexity: O(1)

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.

Key Concepts

IntStreamnoneMatch()method reference

Related Programs