Java Program to Implement Sieve Of Eratosthenes

This is a Java Program to Implement Sieve Of Eratosthenes Algorithm. This is a program to find all primes less than a number.

Here is the source code of the Java program to Implement Sieve Of Eratosthenes Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to implement Sieve Of Eratosthenes
 **/
 
import java.util.Scanner;
 
/** Class SieveOfEratosthenes **/
public class SieveOfEratosthenes
{
    /** Function to calculate all primes less than n **/
    private int[] calcPrimes(int N)
    {
        int[] arr = new int[N + 1];
        for (int i = 2; i <= Math.sqrt(N); i++)
        {
            if (arr[i] == 0)
            {
                for (int j = i * i; j <= N; j += i)
                {
                    arr[j] = 1;
                }
            }
        }
        return arr;
    }
    /** Function to get all primes **/
    public void getPrimes(int N)
    {
        int[] primes = calcPrimes(N);
        display(primes);
    }
    /** Function to display all primes **/
    public void display(int[] primes)
    {
        System.out.print("\nPrimes = ");
        for (int i = 2; i < primes.length; i++)
            if (primes[i] == 0)
                System.out.print(i +" ");
        System.out.println();
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Sieve Of Eratosthenes Prime Algorithm Test\n");
        /** Make an object of SieveOfEratosthenes class **/
        SieveOfEratosthenes soe = new SieveOfEratosthenes();
        /** Accept n **/
        System.out.println("Enter number to find all primes less than the number\n");
        int n = scan.nextInt();
        soe.getPrimes(n);        
    }
}
Sieve Of Eratosthenes Prime Algorithm Test
 
Enter number to find all primes less than the number
 
500
 
Primes = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
443 449 457 461 463 467 479 487 491 499

Related posts:

StringBuilder vs StringBuffer in Java
Guide to the Java ArrayList
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
A Guide to Spring Boot Admin
Java Program to Perform Stooge Sort
Java Program to Find Nearest Neighbor for Static Data Set
An Intro to Spring Cloud Zookeeper
Java toString() Method
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Disable Spring Data Auto Configuration
Java Program to Implement the String Search Algorithm for Short Text Sizes
Bootstrap a Web Application with Spring 5
Changing Annotation Parameters At Runtime
@Order in Spring
Java Program to Perform Insertion in a BST
Java Program to Implement Kosaraju Algorithm
Introduction to Spring Boot CLI
Java Program to Implement Sorted Singly Linked List
Spring Boot - Application Properties
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
The StackOverflowError in Java
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
A Guide to @RepeatedTest in Junit 5
Spring Security Login Page with React
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Spring WebClient and OAuth2 Support
Java Program to Implement D-ary-Heap
Java Program to Implement Pollard Rho Algorithm
Hướng dẫn Java Design Pattern – Builder
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Weight Balanced Tree