Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range

This is java program to implement Wheel Seive method to generate the prime numbers from 2 to the given limit. This algorithm reduces the time by checking only till n^2.

Here is the source code of the Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a sample program to print all the prime numbers between 2 and n
import java.util.LinkedList;
import java.util.Scanner;
 
public class Sieve_Method
{
    public static LinkedList<Integer> sieve(int n)
    {
        if(n < 2) 
            return new LinkedList<Integer>();
 
        LinkedList<Integer> primes = new LinkedList<Integer>();
        LinkedList<Integer> nums = new LinkedList<Integer>();
 
        for(int i = 2;i <= n;i++)
        { //unoptimized
            nums.add(i);
        }
 
        while(nums.size() > 0)
        {
            int nextPrime = nums.remove();
            for(int i = nextPrime * nextPrime;i <= n;i += nextPrime)
            {
                nums.removeFirstOccurrence(i);
            }
            primes.add(nextPrime);
        }
        return primes;
    }
    public static void main(String args[])
    {
        System.out.println("Enter the upper bound : ");
        Scanner sc = new Scanner(System.in);
        int end = sc.nextInt();
 
        System.out.println(sieve(end));
        sc.close();
 
    }
}

Output:

$ javac Sieve_Method.java
$ java Sieve_Method
Enter the upper bound : 
70
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]

Related posts:

Java Program to Implement TreeSet API
A Quick Guide to Spring Cloud Consul
Spring Boot - Cloud Configuration Client
Exploring the Spring Boot TestRestTemplate
Java Program to Implement Merge Sort Algorithm on Linked List
Jackson Annotation Examples
Notify User of Login From New Device or Location
Map Serialization and Deserialization with Jackson
Converting Between a List and a Set in Java
Spring Boot - Google Cloud Platform
Java Program to Implement Floyd Cycle Algorithm
Java Program to Find the Mode in a Data Set
Guide to the Volatile Keyword in Java
Java Program to Implement Direct Addressing Tables
Concrete Class in Java
Java Program to Implement Sorted Circular Doubly Linked List
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Implement Best-First Search
Apache Camel with Spring Boot
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java – Write to File
Java Program to Implement LinkedBlockingQueue API
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Array to String Conversions
Java Program to Implement Suffix Array
How to Define a Spring Boot Filter?
Count Occurrences of a Char in a String