Javatpoint Logo
Javatpoint Logo

Sieve of Eratosthenes Algorithm in Java

The Sieve of Eratosthenes is an ancient and efficient algorithm used to find all prime numbers up to a given limit. Named after the Greek mathematician Eratosthenes of Cyrene, this algorithm has stood the test of time and remains a fundamental concept in number theory and computer science. In this section, we will delve into the mechanics of the Sieve of Eratosthenes and implement it in Java to understand how it efficiently identifies prime numbers.

Understanding the Sieve of Eratosthenes Algorithm

The Sieve of Eratosthenes works by iteratively marking off multiples of each prime number starting from 2, revealing the prime numbers and eliminating the non-prime ones. The algorithm's main steps are as follows:

Create a boolean array to represent the range of numbers from 2 to the given limit.

Initialize all elements of the array to true, as initially, all numbers are considered prime.

Starting with the first prime number (2), mark off all of its multiples as non-prime by setting their corresponding array elements to false.

Find the next number in the array that is still marked as prime and repeat step 3.

Continue the process until you reach the square root of the given limit, as no non-prime numbers beyond this point need to be considered.

Java Implementation of the Sieve of Eratosthenes

Below is a Java implementation of the Sieve of Eratosthenes algorithm:

File Name: SieveOfEratosthenes.java

Output:

Prime numbers up to 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Explanation:

The findPrimes() method takes an integer limit as input and returns a list of all prime numbers up to the given limit.

The isPrime() boolean array is initialized to store the primality status of each number from 2 to the limit.

The outer loop starts with the first prime number, 2, and iterates up to the square root of the limit. For each prime number p, the inner loop marks off all its multiples as non-prime by setting the corresponding isPrime() array elements to false.

After completing the marking process, the algorithm collects all the prime numbers (marked as true in the isPrime() array) into the primes list.

Finally, the main method demonstrates how to use the findPrimes() method by finding prime numbers up to a limit of 50 and printing the result.

The program successfully finds and prints all prime numbers up to the limit of 50 using the Sieve of Eratosthenes algorithm. The list of prime numbers is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]. These are the numbers that have no divisors other than 1 and themselves.

Conclusion

The Sieve of Eratosthenes algorithm is a powerful and efficient method for finding prime numbers up to a given limit. With a time complexity of O(n*log(log(n))), it outperforms many other prime-finding algorithms, making it an essential tool in various mathematical and computational applications. The Java implementation provided in this article showcases how this ancient algorithm can be implemented in modern programming languages to solve practical problems efficiently. Whether it's for number theory or performance optimization, understanding the Sieve of Eratosthenes is a valuable asset for any programmer.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA