# nth Prime Number Java

A number is prime if it is divisible by 1 and itself. In other words, a prime number is a natural number with exactly two distinct natural number divisors 1 and itself. For example, 2, 3, 5, 7, 11, etc. are the prime numbers. Note that 0 and 1 are not prime numbers. The number 2 is the only even prime number because all the other even numbers are divisible by 2. In this section, we will learn how to find the nth prime number in Java.

There are two ways to find the nth prime number in Java:

• Using Basic/ traditional Approach
• Using Sieve of Eratosthenes Approach

## Using Basic/ traditional Approach

In the basic approach, we follow the same approach that we have used to find the prime number. Follow the steps given below.

• Read an integer (n) from the user.
• In the while loop, execute the condition (c!=n). Initially, the variable c is 0 and counts the discovered prime numbers.
• Increment the variable i (initially 1) by 1 for the next number check.
• Check if the variable i is prime or not.
• If yes, increment the variable c by 1.

Let's find the nth prime number through a Java program using a while loop.

Output 1:

```Enter the value of n to compute the nth prime number: 3
The 5th prime number is: 5
```

Output 2:

```Enter the value of n to compute the nth prime number: 25
The 25th prime number is: 97
```

We have taken an integer from the user and store it in the variable n. The while loop continues until the value of the count variable is less than n. If the while loop returns true, the value of the variable num is incremented by 1.

After that, a for loop comes into existence that begins with the initialization of i by 2. The for loop executes until the condition i<=num returns true. Every time when the condition returns true, it divides the variable num by i also compare the resultant with 0. If the resultant is equal to 0, the loop breaks and jumps to the next statement that compares i is equal to num or not. If the variable i is equal to num, the value of the variable count is incremented by 1.

After that, the control again moves to the while loop. On terminating the while loop, we get the nth prime number.

Let's see another approach to find the nth prime number.

## Using Sieve of Eratosthenes Approach

The Sieve of Eratosthenes is an ancient algorithm through which we can find the prime numbers up to a specified number (limit). It does so by identifying and marking the multiples of each prime number, starting from the first prime number 2. When all the multiples of each prime are marked as composite (not prime), the remaining unmarked numbers are prime numbers. It is the most efficient approach to find all the prime numbers smaller than n (better for n<10,000,000). The approach consumes the O(n) of the memory and its time complexity is O(nloglogn). Let's see what should be the approach.

### Approach

• First, find all the prime numbers up to the given limit using the Sieve of Eratosthenes algorithm.
• Store all the prime numbers in a Vector.
• For a given number N, return the element at (N-1)th index in a vector.

### Algorithm

• Generate a list of consecutive integers starting from 2 to n.
• Initially, let p=2. We have considered that the first prime number 2 is p.
• Starting from p2, count up in increments of p and mark each of these numbers greater than or equal to p2 itself in the list. These numbers will be p(p+1), p(p+2), p(p+3), etc.
• Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

After terminating the algorithm, all the numbers that are not marked are prime.

### Example

Let's understand the above approach through an example.

Suppose, we want to find all the prime numbers less than or equal to 20 (n). So, we need to print all the prime numbers less than or equal to n. Let's create a list of the numbers starting from 2 to 20. In the above table, mark all the numbers that are divisible by 2 (we have marked by red color), also greater than or equal to the square of it. After the first prime (2) and unmarked number, the next unmarked number is 3. Now we will mark the numbers that are divisible by 3 (we have marked by blue color), also greater than or equal to the square of it. The next unmarked prime number in the table is 5, so we will mark the numbers that are divisible by 5, also greater than or equal to the square of it. The numbers that are divisible by 5 are 10, 15, and 20 that are already marked. Hence, there is no number to mark. In the above table, unmarked numbers are: Hence, we get the prime numbers (2, 3, 5, 7, 11, 13, 17, and 19).

Output:

```7th prime number is 17
22nd prime number is 79
10000th prime number is 104729
```

## Help Others, Please Share   