Prime Number Logic

Solve one Java logic problem at a time, then flip for the complete explanation and program.

0/150 Known filtered set
Difficulty
Read Full Guide Question: 1 of 150

Java Logic Development · Chapter 04 Companion Article

Prime Number Logic

Fourteen prime-number techniques — checking, ranges, factorization, twin and co-prime pairs, the 6k ± 1 optimization, and the Sieve of Eratosthenes — plus seven interview-style extension problems, all built on the same square-root divisibility idea.

Overview

Prime number problems are common in Java interviews because they test loops, divisibility, mathematical optimization, arrays, preprocessing, and problem-solving ability.

A prime number has exactly two positive divisors: 1 and the number itself. Examples: 2, 3, 5, 7, 11, 13 are prime; 4, 6, 8, 9, 10 are composite; 0 and 1 are neither prime nor composite.

A basic prime check tests divisors, while advanced problems reduce unnecessary checks or preprocess many values using the Sieve of Eratosthenes.

1. Prime Number Check

What Is a Prime Number?

A number greater than 1 is prime when it is divisible only by 1 and itself. Examples: 2 → Prime, 7 → Prime, 9 → Not prime because 9 = 3 × 3, 1 → Not prime.

Basic Logic

For a number n: reject values less than 2, check divisibility from 2 to n - 1, and if any number divides n exactly it is not prime, otherwise it is prime. The condition n % i == 0 means i is a divisor of n.

Java Program

Java
public class PrimeNumberCheck {
    public static void main(String[] args) {
        int number = 29;
        boolean isPrime = true;
        if (number < 2) {
            isPrime = false;
        } else {
            for (int i = 2; i < number; i++) {
                if (number % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }
        if (isPrime) {
            System.out.println(number + " is a prime number");
        } else {
            System.out.println(number + " is not a prime number");
        }
    }
}

Output

Output
29 is a prime number

Dry Run

For number = 29:

  • 29 % 2 != 0
  • 29 % 3 != 0
  • Continue checking divisors.
  • No divisor from 2 to 28 divides 29.
  • Therefore, 29 is prime.

Complexity

  • Time: O(n)
  • Space: O(1)

Important Edge Cases

  • Negative numbers are not prime.
  • 0 is not prime.
  • 1 is not prime.
  • 2 is the smallest prime number.
  • 2 is also the only even prime number.

Common Mistake

Starting with boolean isPrime = true; and forgetting to handle 0, 1, and negative numbers causes incorrect results.

2. Composite Number Check

What Is a Composite Number?

A composite number is a positive integer greater than 1 that has more than two positive divisors. Examples: 4 → divisors are 1, 2, 4; 6 → divisors are 1, 2, 3, 6; 15 → divisors are 1, 3, 5, 15. Prime and composite are opposite classifications only for numbers greater than 1. 1 is neither prime nor composite.

Logic

A number is composite when it is greater than 1 and at least one divisor exists between 2 and n - 1.

Java Program

Java
public class CompositeNumberCheck {
    public static void main(String[] args) {
        int number = 21;
        boolean isComposite = false;
        if (number > 1) {
            for (int i = 2; i < number; i++) {
                if (number % i == 0) {
                    isComposite = true;
                    break;
                }
            }
        }
        if (isComposite) {
            System.out.println(number + " is a composite number");
        } else if (number > 1) {
            System.out.println(number + " is not a composite number");
        } else {
            System.out.println(number + " is neither prime nor composite");
        }
    }
}

Output

Output
21 is a composite number

Why It Works

21 % 3 == 0, so 3 is a divisor other than 1 and 21. That is enough to prove that 21 is composite. There is no need to continue checking remaining divisors.

Complexity

  • Time: O(n)
  • Space: O(1)

Interview Point

Use break immediately after finding a divisor. Continuing the loop does unnecessary work when the requirement is only to determine whether the number is composite.

3. Prime Numbers in Range

This problem finds every prime number between two boundaries. Example: input range 10 to 30 → prime numbers 11, 13, 17, 19, 23, 29.

Logic

Use two levels of logic: traverse every number in the range, then check whether the current number is prime. Keeping prime-checking logic in a separate method makes the program easier to reuse.

Java Program

Java
public class PrimeNumbersInRange {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int start = 10;
        int end = 30;
        for (int number = start; number <= end; number++) {
            if (isPrime(number)) {
                System.out.print(number + " ");
            }
        }
    }
}

Output

Output
11 13 17 19 23 29

Why Check Only Up to Square Root?

If a number has two factors, a × b = n, at least one factor must be less than or equal to √n. For example, 36 = 6 × 6. If no divisor exists up to √n, a larger divisor cannot exist without having a corresponding smaller divisor.

Complexity

For a range containing roughly R numbers:

  • Time: approximately O(R√n)
  • Space: O(1)

where n represents the largest number being tested.

Useful Improvement

If the program needs all primes over a large continuous range, checking each number independently becomes expensive. A sieve is usually a better choice.

4. Count Prime Numbers in Range

Instead of printing primes, this variation counts how many primes exist between two values. Example: range 1 to 20, prime numbers 2, 3, 5, 7, 11, 13, 17, 19, count 8.

Java Program

Java
public class CountPrimeNumbers {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int start = 1;
        int end = 20;
        int count = 0;
        for (int number = start; number <= end; number++) {
            if (isPrime(number)) {
                count++;
            }
        }
        System.out.println("Prime count = " + count);
    }
}

Output

Output
Prime count = 8

Key Variable

count increases only when the current value passes the prime test.

Complexity

  • Time: O(R√n)
  • Space: O(1)

Interview Variation

Instead of counting within one range, multiple range queries may be given. Running prime checks repeatedly is inefficient for many queries. A sieve combined with a prefix-count array can answer each query much faster.

5. Sum of Prime Numbers

This problem adds all prime numbers within a given range. Example: prime numbers from 1 to 10, 2 + 3 + 5 + 7 = 17.

Java Program

Java
public class SumOfPrimeNumbers {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int start = 1;
        int end = 20;
        int sum = 0;
        for (int number = start; number <= end; number++) {
            if (isPrime(number)) {
                sum += number;
            }
        }
        System.out.println("Sum of primes = " + sum);
    }
}

Output

Output
Sum of primes = 77

Dry Run

Primes from 1 to 20: 2, 3, 5, 7, 11, 13, 17, 19. Sum: 2 + 3 + 5 + 7 + 11 + 13 + 17 + 19 = 77.

Complexity

  • Time: O(R√n)
  • Space: O(1)

Practical Consideration

For large ranges, use long for the sum because adding many prime numbers may exceed the int limit. Example: long sum = 0;

6. First N Prime Numbers

Here the input represents how many prime numbers are required rather than an upper boundary. Example: for n = 10, result 2 3 5 7 11 13 17 19 23 29.

Logic

Maintain two values: number as the current candidate, and count as the number of primes already found. Continue until count == n.

Java Program

Java
public class FirstNPrimeNumbers {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int n = 10;
        int count = 0;
        int number = 2;
        while (count < n) {
            if (isPrime(number)) {
                System.out.print(number + " ");
                count++;
            }
            number++;
        }
    }
}

Output

Output
2 3 5 7 11 13 17 19 23 29

Why a while Loop Fits Well

The final number that must be checked is unknown. The loop stops according to the number of primes found rather than according to a fixed numeric boundary.

Edge Case

If n <= 0, there are no prime numbers to generate.

Interview Improvement

After processing 2, even candidates can be skipped because no other even number is prime.

7. Next Prime Number

The next prime of a number is the first prime strictly greater than that number. Example: input 14, next prime 17. Even if the input itself is prime, the search begins from the next integer — input 13, next prime 17.

Java Program

Java
public class NextPrimeNumber {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int number = 14;
        int candidate = number + 1;
        while (!isPrime(candidate)) {
            candidate++;
        }
        System.out.println("Next prime = " + candidate);
    }
}

Output

Output
Next prime = 17

Dry Run

Start: candidate = 15

  • 15 → composite
  • 16 → composite
  • 17 → prime

Search stops at 17.

Complexity

The exact number of candidates checked depends on the distance to the next prime. Each candidate requires approximately O(√n) prime-checking work.

Common Mistake

Starting from candidate = number would return the same number when the input itself is already prime. Use number + 1 when the requirement says strictly next prime.

8. Previous Prime Number

The previous prime is the largest prime strictly smaller than the supplied number. Example: input 20, result 19.

Java Program

Java
public class PreviousPrimeNumber {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int number = 20;
        int candidate = number - 1;
        while (candidate >= 2 && !isPrime(candidate)) {
            candidate--;
        }
        if (candidate >= 2) {
            System.out.println("Previous prime = " + candidate);
        } else {
            System.out.println("No previous prime exists");
        }
    }
}

Output

Output
Previous prime = 19

Important Boundary

For input values 2 or smaller, there may be no valid previous positive prime. The condition candidate >= 2 prevents the search from continuing indefinitely toward negative numbers.

Interview Point

Boundary conditions matter more in previous-prime logic than next-prime logic because prime numbers have a lower boundary at 2.

9. Prime Factors

Prime factorization expresses a number as a product of prime numbers. Example: 84 can be written as 2 × 2 × 3 × 7. Therefore, its prime factors including repetition are 2 2 3 7.

Logic

Start with the smallest possible divisor. While the current divisor divides the number: print the divisor, divide the number by that divisor, and test the same divisor again. Move to the next divisor only when the current divisor no longer divides the remaining number.

Java Program

Java
public class PrimeFactors {
    public static void main(String[] args) {
        int number = 84;
        int remaining = number;
        for (int factor = 2; factor * factor <= remaining; factor++) {
            while (remaining % factor == 0) {
                System.out.print(factor + " ");
                remaining /= factor;
            }
        }
        if (remaining > 1) {
            System.out.print(remaining);
        }
    }
}

Output

Output
2 2 3 7

Dry Run

Output
remaining = 84
84 / 2 = 42   (divide by 2)
42 / 2 = 21   (divide by 2 again)
21 not divisible by 2, try 3
21 / 3 = 7    (7 is prime, print it)

Final factors: 2 2 3 7

Why the Remaining Value Is Printed

After removing all smaller factors, a remaining value greater than 1 must itself be prime. This prevents unnecessary looping up to the original number.

Complexity

  • Time: approximately O(√n)
  • Space: O(1)

10. Distinct Prime Factors

Distinct prime factors contain each prime divisor only once. Example: 360, prime factorization 360 = 2 × 2 × 2 × 3 × 3 × 5, distinct prime factors 2, 3, 5.

Logic

When a factor is found: print it once, remove every occurrence of that factor, and continue searching.

Java Program

Java
public class DistinctPrimeFactors {
    public static void main(String[] args) {
        int number = 360;
        int remaining = number;
        for (int factor = 2; factor * factor <= remaining; factor++) {
            if (remaining % factor == 0) {
                System.out.print(factor + " ");
                while (remaining % factor == 0) {
                    remaining /= factor;
                }
            }
        }
        if (remaining > 1) {
            System.out.print(remaining);
        }
    }
}

Output

Output
2 3 5

Difference from Normal Prime Factors

Normal prime factors of 360: 2 2 2 3 3 5. Distinct prime factors: 2 3 5. The inner while loop removes repeated occurrences before moving forward.

Complexity

  • Time: approximately O(√n)
  • Space: O(1)

Interview Variation

A common follow-up is: "Count the number of distinct prime factors." Instead of printing the factor, increment a counter once when each new factor is found.

11. Twin Prime Numbers

Twin primes are two prime numbers whose difference is exactly 2. Examples: (3, 5), (5, 7), (11, 13), (17, 19), (29, 31).

Logic

For each prime p, check whether p + 2 is also prime.

Java Program

Java
public class TwinPrimeNumbers {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int start = 1;
        int end = 50;
        for (int number = start; number + 2 <= end; number++) {
            if (isPrime(number) && isPrime(number + 2)) {
                System.out.println("(" + number + ", " + (number + 2) + ")");
            }
        }
    }
}

Output

Output
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)

Why number + 2 <= end Is Used

Both members of the twin-prime pair must remain inside the requested range. Without this condition, the second number could fall outside the upper boundary.

Complexity

  • Time: approximately O(R√n)
  • Space: O(1)

Interview Tip

Do not confuse twin primes with consecutive prime numbers. 7 and 11 are consecutive primes, but they are not twin primes because their difference is 4.

12. Co-Prime Numbers

Two numbers are co-prime when their greatest common divisor is 1. The numbers themselves do not need to be prime. Examples: 8 and 15 are co-prime because GCD(8, 15) = 1, even though both numbers are composite. Another example: 14 and 25 have no common prime factor, so they are co-prime.

Best Logic

Instead of listing factors of both numbers, calculate their GCD using Euclid's algorithm. If GCD(a, b) == 1, the numbers are co-prime.

Java Program

Java
public class CoPrimeNumbers {
    public static int gcd(int a, int b) {
        while (b != 0) {
            int remainder = a % b;
            a = b;
            b = remainder;
        }
        return Math.abs(a);
    }
    public static void main(String[] args) {
        int first = 14;
        int second = 25;
        if (gcd(first, second) == 1) {
            System.out.println(first + " and " + second + " are co-prime");
        } else {
            System.out.println(first + " and " + second + " are not co-prime");
        }
    }
}

Output

Output
14 and 25 are co-prime

Dry Run

For 14 and 25:

  • 14 % 25 = 14
  • 25 % 14 = 11
  • 14 % 11 = 3
  • 11 % 3 = 2
  • 3 % 2 = 1
  • 2 % 1 = 0

GCD is 1. Therefore, the numbers are co-prime.

Complexity

Euclid's algorithm:

  • Time: O(log(min(a, b)))
  • Space: O(1)

Important Distinction

Co-prime does not mean both numbers are prime. For example, 8 and 9 are both composite, but GCD(8, 9) = 1, so they are co-prime.

13. Optimized Prime Check

The basic prime check may test too many divisors. Three useful optimizations are: stop at the square root, handle even numbers separately, and test divisors of the form 6k ± 1.

Why 6k ± 1?

Every integer can be represented as one of 6k, 6k + 1, 6k + 2, 6k + 3, 6k + 4, 6k + 5. Numbers represented by 6k, 6k + 2, 6k + 4 are even. 6k + 3 is divisible by 3. Therefore, primes greater than 3 must occur in the forms 6k - 1 or 6k + 1.

This does not mean every 6k ± 1 number is prime. It only reduces the number of candidates that need testing. For example, 25 = 6 × 4 + 1 but 25 is composite.

Java Program

Java
public class OptimizedPrimeCheck {
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        if (number <= 3) {
            return true;
        }
        if (number % 2 == 0 || number % 3 == 0) {
            return false;
        }
        for (int i = 5; i <= number / i; i += 6) {
            if (number % i == 0 || number % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int number = 97;
        System.out.println(number + (isPrime(number) ? " is prime" : " is not prime"));
    }
}

Output

Output
97 is prime

Why Use i <= number / i?

A common condition is i * i <= number. For very large int values, i * i can theoretically overflow. Using i <= number / i avoids multiplication overflow while representing the same square-root boundary for positive values.

Complexity

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

When to Use It

This approach is suitable when one number must be checked, only a few prime checks are required, and building a sieve would be unnecessary overhead.

14. Sieve of Eratosthenes

The Sieve of Eratosthenes efficiently finds all prime numbers up to a given limit. It is especially useful when many prime-related queries must be answered within the same numeric range.

Core Idea

Assume all numbers from 2 onward are prime initially. Then repeatedly mark multiples of each discovered prime as composite. For example, for numbers up to 20: start with 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20. Using 2, remove 4, 6, 8, 10, 12, 14, 16, 18, 20. Using 3, remove 6, 9, 12, 15, 18. Remaining prime values are 2, 3, 5, 7, 11, 13, 17, 19.

Java Program

Java
import java.util.Arrays;

public class SieveOfEratosthenes {
    public static void main(String[] args) {
        int limit = 50;
        boolean[] isPrime = new boolean[limit + 1];
        Arrays.fill(isPrime, true);
        isPrime[0] = false;
        isPrime[1] = false;
        for (int prime = 2; prime <= limit / prime; prime++) {
            if (isPrime[prime]) {
                for (int multiple = prime * prime; multiple <= limit; multiple += prime) {
                    isPrime[multiple] = false;
                }
            }
        }
        for (int number = 2; number <= limit; number++) {
            if (isPrime[number]) {
                System.out.print(number + " ");
            }
        }
    }
}

Output

Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Why Start Marking from prime * prime?

For prime 5, numbers such as 10, 15, and 20 were already marked while processing 2 or 3. The first new composite that needs attention is 5 × 5 = 25. Starting from prime * prime avoids repeated work.

Why Stop the Outer Loop at Square Root?

If a composite number has not already been marked by the time all primes up to √limit are processed, it cannot have a smaller undiscovered factor.

Complexity

  • Time: O(n log log n)
  • Space: O(n)

When Sieve Is Better

Use a sieve when all primes up to n are required, many numbers need prime checks, multiple queries use the same upper limit, or prime counts/sums need repeated queries. Do not automatically use it for checking one small number because it requires an array proportional to the upper limit.

15. Prime Number Interview Problems

Prime-number interview questions often extend the basic prime check into counting, searching, factorization, or preprocessing problems. The key is to identify whether the problem involves one number, several independent numbers, a continuous range, many repeated queries, prime factors, or relationships between primes.

Problem 1: Find the Nth Prime Number

Example: n = 6, prime sequence 2, 3, 5, 7, 11, 13, answer 13.

Java Program

Java
public class NthPrimeNumber {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int n = 6;
        int count = 0;
        int number = 1;
        while (count < n) {
            number++;
            if (isPrime(number)) {
                count++;
            }
        }
        System.out.println(n + "th prime = " + number);
    }
}

Output

Output
6th prime = 13

Interview Point

The stopping condition depends on how many primes have been found, not on a fixed upper limit.

Problem 2: Check Whether the Sum of Two Numbers Is Prime

Example: 12 + 17 = 29. Since 29 is prime, the answer is true.

Java Program

Java
public class PrimeSumCheck {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int first = 12;
        int second = 17;
        int sum = first + second;
        System.out.println("Sum = " + sum);
        System.out.println("Prime = " + isPrime(sum));
    }
}

Output

Output
Sum = 29
Prime = true

Learning Point

Do not duplicate prime-checking logic inside the addition logic. Calculate the derived value first and pass it to a reusable method.

Problem 3: Find the Largest Prime Below N

Example: input 30, answer 29.

Java Program

Java
public class LargestPrimeBelowN {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int n = 30;
        int number = n - 1;
        while (number >= 2 && !isPrime(number)) {
            number--;
        }
        if (number >= 2) {
            System.out.println("Largest prime below " + n + " = " + number);
        } else {
            System.out.println("No prime exists below " + n);
        }
    }
}

Output

Output
Largest prime below 30 = 29

Problem 4: Count Prime Factors Including Repetition

For 72, prime factorization is 2 × 2 × 2 × 3 × 3, number of prime factors 5.

Java Program

Java
public class CountPrimeFactors {
    public static void main(String[] args) {
        int number = 72;
        int remaining = number;
        int count = 0;
        for (int factor = 2; factor <= remaining / factor; factor++) {
            while (remaining % factor == 0) {
                count++;
                remaining /= factor;
            }
        }
        if (remaining > 1) {
            count++;
        }
        System.out.println("Prime factor count = " + count);
    }
}

Output

Output
Prime factor count = 5

Interview Trap

Make sure the requirement is clear: count all prime factors including repetition, or count only distinct prime factors. These are different problems. For 72: repeated factor count = 5, distinct factor count = 2.

Problem 5: Check Whether a Number Has Exactly Two Distinct Prime Factors

Example: 12 = 2² × 3, distinct prime factors 2 and 3. Therefore, 12 has exactly two distinct prime factors.

Java Program

Java
public class TwoDistinctPrimeFactors {
    public static void main(String[] args) {
        int number = 12;
        int remaining = number;
        int count = 0;
        for (int factor = 2; factor <= remaining / factor; factor++) {
            if (remaining % factor == 0) {
                count++;
                while (remaining % factor == 0) {
                    remaining /= factor;
                }
            }
        }
        if (remaining > 1) {
            count++;
        }
        System.out.println("Exactly two distinct prime factors = " + (count == 2));
    }
}

Output

Output
Exactly two distinct prime factors = true

Problem 6: Find Prime Numbers from an Array

The input does not always come as a continuous range. Example array {4, 7, 10, 13, 18, 23}, prime elements 7, 13, 23.

Java Program

Java
public class PrimeNumbersFromArray {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int[] numbers = {4, 7, 10, 13, 18, 23};
        for (int number : numbers) {
            if (isPrime(number)) {
                System.out.print(number + " ");
            }
        }
    }
}

Output

Output
7 13 23

Learning Point

A sieve works naturally for continuous numeric limits, while individual prime checks may be simpler when only a small unrelated set of values is provided.

Problem 7: Find the Difference Between Consecutive Primes

Suppose primes are 2, 3, 5, 7, 11. Differences are 3 - 2 = 1, 5 - 3 = 2, 7 - 5 = 2, 11 - 7 = 4. This concept appears in problems involving prime gaps.

Java Program

Java
public class PrimeGaps {
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int limit = 20;
        int previousPrime = -1;
        for (int number = 2; number <= limit; number++) {
            if (isPrime(number)) {
                if (previousPrime != -1) {
                    System.out.println(previousPrime + " -> " + number + " : gap = " + (number - previousPrime));
                }
                previousPrime = number;
            }
        }
    }
}

Output

Output
2 -> 3 : gap = 1
3 -> 5 : gap = 2
5 -> 7 : gap = 2
7 -> 11 : gap = 4
11 -> 13 : gap = 2
13 -> 17 : gap = 4
17 -> 19 : gap = 2

Prime Checking Approaches Compared

ApproachTime ComplexitySpaceSuitable For
Check 2 to n - 1O(n)O(1)Learning basic logic
Check 2 to n / 2O(n)O(1)Slight improvement, still unnecessary
Check up to √nO(√n)O(1)Standard individual prime check
Skip even divisorsO(√n)O(1)Faster individual checks
6k ± 1 checkO(√n)O(1)Optimized individual checks
Sieve of EratosthenesO(n log log n)O(n)Finding many primes up to n

Why Checking Up to n / 2 Is Not Optimal

A common beginner implementation uses for (int i = 2; i <= number / 2; i++). This works, but it performs unnecessary checks. For number = 1000000, number / 2 = 500000, while √1000000 = 1000. Checking only up to the square root can reduce hundreds of thousands of unnecessary iterations.

Why Square Root Prime Checking Works

Suppose n = a × b. If both a and b were greater than √n, then a × b > n, which is impossible. Therefore, every composite number must have at least one factor less than or equal to its square root. This mathematical property is the reason the optimized prime check can stop early.

Special Cases You Should Know

0

0 is not prime. It has infinitely many integer divisors and does not satisfy the definition of a prime number.

1

1 is not prime. It has only one positive divisor.

2

2 is prime. It is the smallest prime and the only even prime number.

Negative Numbers

Negative integers are not considered prime under the standard definition used in Java programming problems.

Large Numbers

For large values: avoid checking every divisor up to n, use square-root-based checking, avoid arithmetic overflow in loop conditions, and consider long when values can exceed the int range.

Overflow-Safe Prime Loop

This condition is common: i * i <= number. For normal small inputs, it works correctly. For large integer values, the multiplication may overflow before comparison. An overflow-safe alternative is i <= number / i. Example:

Java
for (int i = 2; i <= number / i; i++) {
    if (number % i == 0) {
        return false;
    }
}

This is useful when writing defensive integer logic.

Reusable Prime Utility Method

A clean solution usually keeps prime logic separate from the main problem.

Java
public static boolean isPrime(int number) {
    if (number <= 1) {
        return false;
    }
    if (number <= 3) {
        return true;
    }
    if (number % 2 == 0 || number % 3 == 0) {
        return false;
    }
    for (int i = 5; i <= number / i; i += 6) {
        if (number % i == 0 || number % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}

This method can be reused for:

  • Prime number checking
  • Prime numbers in a range
  • Prime elements in arrays
  • First N primes
  • Nth prime
  • Next prime
  • Previous prime
  • Twin primes
  • Prime gaps

Separating the method avoids rewriting the same divisibility logic throughout a program.

Common Prime Number Mistakes

Treating 1 as Prime

Incorrect: if (number >= 1). Prime numbers begin from 2. Correct boundary: if (number < 2) { return false; }

Checking Until the Number Itself

Using for (int i = 2; i < number; i++) is logically correct but inefficient. Prefer a square-root boundary.

Forgetting break or Early return

Once a divisor is found, the number is known to be composite. Continuing the loop wastes work.

Starting Sieve Marking from 2 * prime

This still produces correct prime results, but many multiples were already processed by smaller primes. Use prime * prime as the starting point.

Forgetting to Mark 0 and 1 as Non-Prime

When a boolean sieve array is initialized to true, explicitly set isPrime[0] = false; and isPrime[1] = false; when the array size permits those indexes.

Confusing Prime Factors with All Factors

Factors of 12: 1, 2, 3, 4, 6, 12. Prime factors of 12: 2, 2, 3. Distinct prime factors: 2, 3. These represent different requirements.

Confusing Co-Prime with Prime

8 and 15 are co-prime even though neither number is prime. Co-prime status depends on their common divisor: GCD(8, 15) = 1.

Choosing the Correct Prime Algorithm

Use an optimized isPrime() method when checking one number or a relatively small number of independent values. Use the Sieve of Eratosthenes when all primes up to a known upper limit are needed. Use prime factorization when the problem asks about prime divisors, distinct prime divisors, number of prime factors, or factor-based properties. Use Euclid's GCD algorithm for co-prime checks rather than manually generating prime factors.

This distinction is important because the best solution depends on what information the problem actually asks for.

Interview Questions to Practice

  1. Check whether a number is prime.
  2. Check whether a number is composite.
  3. Print primes between two numbers.
  4. Count primes in a range.
  5. Calculate the sum of primes in a range.
  6. Generate the first N prime numbers.
  7. Find the Nth prime number.
  8. Find the next prime after a number.
  9. Find the previous prime before a number.
  10. Find the nearest prime to a number.
  11. Print prime factors of a number.
  12. Print distinct prime factors.
  13. Count prime factors including repetition.
  14. Count distinct prime factors.
  15. Check whether two numbers are co-prime.
  16. Find twin-prime pairs in a range.
  17. Find prime numbers from an integer array.
  18. Find the largest prime in an array.
  19. Find the smallest prime in an array.
  20. Find the largest prime smaller than N.
  21. Find the smallest prime greater than N.
  22. Find the difference between consecutive primes.
  23. Find the largest prime gap within a range.
  24. Check whether the sum of two numbers is prime.
  25. Check whether the difference of two numbers is prime.
  26. Check whether a number can be represented as the sum of two primes.
  27. Count prime numbers using a sieve.
  28. Generate all primes up to N using the Sieve of Eratosthenes.
  29. Answer multiple prime-check queries efficiently.
  30. Compare trial division with the Sieve of Eratosthenes.

Quick Revision

ConceptKey Rule
Prime numberExactly two positive divisors
Composite numberMore than two positive divisors
0Neither prime nor composite
1Neither prime nor composite
Smallest prime2
Only even prime2
Basic prime checkTest possible divisors
Optimized boundaryCheck only up to √n
6k ± 1Reduces candidate divisors
Prime factorsPrime divisors including repetition
Distinct prime factorsEach prime divisor once
Twin primesPrime pair differing by 2
Co-prime numbersGCD equals 1
SieveGenerates many primes efficiently
Trial division spaceO(1)
Sieve spaceO(n)
Sieve timeO(n log log n)

Key Learning Outcomes

After completing these prime-number problems, a Java learner should be able to:

  • Apply divisibility rules correctly.
  • Handle 0, 1, 2, and negative values properly.
  • Build reusable boolean helper methods.
  • Stop loops as soon as the required result is known.
  • Use square-root optimization instead of unnecessary full-range scanning.
  • Understand why factor pairs allow the square-root boundary.
  • Generate primes over a numeric range.
  • Count and sum prime values.
  • Generate the first N primes.
  • Search forward or backward for prime values.
  • Perform prime factorization.
  • Separate repeated and distinct prime factors.
  • Identify twin-prime pairs.
  • Determine whether two integers are co-prime.
  • Use Euclid's algorithm where GCD is the real requirement.
  • Apply the 6k ± 1 optimization appropriately.
  • Use the Sieve of Eratosthenes for bulk prime generation.

Question Hint