Find Nth Prime Number in Java
Problem
The Nth prime number is whichever prime you'd land on if you counted primes in order starting from the smallest — the 1st prime is 2, the 2nd is 3, and so on.
Given a count N, find the Nth prime number.
Java Program
public class NthPrimeNumber {
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 n = 10;
int count = 0;
int candidate = 1;
while (count < n) {
candidate++;
if (isPrime(candidate)) count++; // one more prime found
}
System.out.println(n + "th prime number: " + candidate);
}
}Output
Core Logic
Scanning upward one number at a time, and counting every prime found along the way, reaches the Nth prime exactly when the running count hits N.
- 1
isPrime(n)is a helper method testing divisors only up to√n. - 2
candidatestarts at 1 andcountstarts at 0, tracking how many primes have been found so far. - 3Each loop iteration advances
candidateby one and checks whether it's prime. - 4The loop keeps going until
countreachesn, at which pointcandidateholds the Nth prime.
count has just reached 10.Key Point: This doesn't need to know in advance how large the Nth prime will be — it just keeps scanning and counting until enough primes have been found.
Why: The scan visits every number up to the Nth prime, running an O(√n) primality check at each one, while keeping only a running count and candidate value.
Key Concepts
Approach 2: Java 8
import java.util.stream.IntStream;
public class NthPrimeNumberStream {
static boolean isPrime(int n) {
if (n < 2) return false;
return IntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0);
}
public static void main(String[] args) {
int n = 10;
// Filters an infinite stream of numbers down to primes, then skips to the nth one
int result = IntStream.iterate(2, i -> i + 1)
.filter(NthPrimeNumberStream::isPrime)
.skip(n - 1)
.findFirst()
.getAsInt();
System.out.println(n + "th prime number: " + result);
}
}
Output
Core Logic
An infinite stream of numbers, filtered down to just the primes, can be asked directly for the Nth one by skipping past the first N - 1.
- 1
IntStream.iterate(2, i -> i + 1)generates an infinite stream of numbers starting from 2. - 2
.filter(...)keeps only the prime numbers, the same check the loop version uses. - 3
.skip(n - 1)drops the firstn - 1primes, since the one right after them is the Nth. - 4
.findFirst()pulls the next surviving element — the Nth prime — and stops the otherwise-infinite stream there.
n = 10, skipping the first 9 primes and taking the next one lands on the same answer, 29.Key Point: IntStream.iterate() is lazy — it only ever generates as many numbers as skip() and findFirst() actually need, so the infinite stream never becomes a problem in practice.
Why: The stream still runs the same O(√n) primality check per candidate, and skip()/findFirst() don't collect anything beyond the single answer.