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 Represent Graph Using Linked List
REST Web service: Upload và Download file với Jersey 2.x
Guide to java.util.concurrent.Locks
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Vòng lặp for, while, do-while trong Java
How to Kill a Java Thread
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Solve Knapsack Problem Using Dynamic Programming
Jackson – Unmarshall to Collection/Array
Optional trong Java 8
Từ khóa static và final trong java
Enum trong java
Java Program to Implement Circular Doubly Linked List
Java Program to Compute the Area of a Triangle Using Determinants
Chuyển đổi từ HashMap sang ArrayList
Java Program to Perform Arithmetic Operations on Numbers of Size
Collect a Java Stream to an Immutable Collection
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
wait() and notify() Methods in Java
Working With Maps Using Streams
Java Program to Implement Self Balancing Binary Search Tree
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Word Wrap Problem
So sánh HashMap và Hashtable trong Java
Guide to @JsonFormat in Jackson
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Find All Pairs Shortest Path
New in Spring Security OAuth2 – Verify Claims
Java Program to Implement Adjacency List
Java Program to Implement Borwein Algorithm
A Guide to TreeSet in Java