Java ProgramsNumbersFind Nth Prime Number

Find Nth Prime Number in Java

intermediate·  Numbers  ·  Number Theory

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.

Input
10
Output
10th prime number: 29

Java Program

Java
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

10th prime number: 29

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.

How It Works
  1. 1isPrime(n) is a helper method testing divisors only up to √n.
  2. 2candidate starts at 1 and count starts at 0, tracking how many primes have been found so far.
  3. 3Each loop iteration advances candidate by one and checks whether it's prime.
  4. 4The loop keeps going until count reaches n, at which point candidate holds the Nth prime.
Counting upward from 2, the 10th prime found is 29 — the loop stops there because 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.

Complexity
Time Complexity: O(nth prime × √(nth prime))Space Complexity: O(1)

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

helper methodwhile looprunning count

Approach 2: Java 8

Java
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

10th prime number: 29

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.

How It Works
  1. 1IntStream.iterate(2, i -> i + 1) generates an infinite stream of numbers starting from 2.
  2. 2.filter(...) keeps only the prime numbers, the same check the loop version uses.
  3. 3.skip(n - 1) drops the first n - 1 primes, since the one right after them is the Nth.
  4. 4.findFirst() pulls the next surviving element — the Nth prime — and stops the otherwise-infinite stream there.
For 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.

Complexity
Time Complexity: O(nth prime × √(nth prime))Space Complexity: O(1)

Why: The stream still runs the same O(√n) primality check per candidate, and skip()/findFirst() don't collect anything beyond the single answer.

Key Concepts

StreamIntStream.iterate()skip()

Related Programs