This is a Java Program to Implement Sieve Of Sundaram 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 Sundaram 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 Sundaram Prime generation
 **/
 
import java.util.Scanner;
 
/** Class SieveOfSundaram **/
public class  SieveOfSundaram
{
    /** Function to calculate all primes less than n **/
    private boolean[] calcPrimes(int limit)
    {
        int n = limit/2;
        /** initialize the sieve **/
        boolean[] prime = new boolean[limit];        
 
        for (int i = 1; i < n; i++)
            for (int j = i; j <= (n - i) / (2 * i + 1); j++)
                prime[i + j + 2 * i * j] = true;
 
        return prime;
    }
    /** Function to get all primes **/
    public void getPrimes(int N)
    {
        boolean[] primes = calcPrimes(N);
        display(primes);
    }
    /** Function to display all primes **/
    public void display(boolean[] primes)
    {
        System.out.print("\nPrimes = 2 3 ");
        for (int i = 2; i < primes.length/2; i++)
            if (!primes[i])
                System.out.print((2 * i + 1) +" ");
        System.out.println();
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Sieve Of Sundaram Prime Algorithm Test\n");
        /** Make an object of SieveOfSundaram class **/
         SieveOfSundaram sos = new  SieveOfSundaram();
        /** Accept n **/
        System.out.println("Enter number to find all primes less than the number\n");
        int n = scan.nextInt();
        sos.getPrimes(n);        
    }
}
Output:
Sieve Of Sundaram 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:
Spring Boot With H2 Database
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Binary Tree
Map Interface trong java
Spring Boot - Exception Handling
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Inheritance with Jackson
Chuyển đổi từ HashMap sang ArrayList
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Filtering and Transforming Collections in Guava
Vòng lặp for, while, do-while trong Java
Retrieve User Information in Spring Security
An Example of Load Balancing with Zuul and Eureka
Java Program to Implement LinkedList API
Base64 encoding và decoding trong Java 8
Java Program to Check Multiplicability of Two Matrices
Spring Cloud Connectors and Heroku
Từ khóa throw và throws trong Java
Java Program to Encode a Message Using Playfair Cipher
Logging in Spring Boot
Spring RestTemplate Request/Response Logging
Java Program to Implement WeakHashMap API
Lập trình đa luồng với CompletableFuture trong Java 8
Converting Between Byte Arrays and Hexadecimal Strings in Java
Registration – Password Strength and Rules
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Difference Between Wait and Sleep in Java
Java Program to Implement Word Wrap Problem
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Implement Regular Falsi Algorithm