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.
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.
Let's find the nth prime number through a Java program using a while loop.
Enter the value of n to compute the nth prime number: 3 The 5th prime number is: 5
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.
After terminating the algorithm, all the numbers that are not marked are prime.
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).
7th prime number is 17 22nd prime number is 79 10000th prime number is 104729