Monday, March 17, 2014

Sieve of Eratosthenes- Prime number Generator

1:  package com.mridul.java.dump;  
2:  import java.io.BufferedReader;  
3:  import java.io.IOException;  
4:  import java.io.InputStreamReader;  
5:  import java.util.Arrays;  
6:  /**Program to compute the prime numbers  
7:   * by using the Sieve of Eratosthenes  
8:   * @author Mridul Jayan  
9:   * @date 03/17/2014  
10:   */  
11:  public class SieveOfEratosthenes{  
12:       /*  
13:        * main method-program starting point  
14:        */  
15:       public static void main(String[] args) {  
16:            try{  
17:                 SieveOfEratosthenes soe=new SieveOfEratosthenes();            
18:                 int limit=Integer.parseInt(soe.readConsoleInput(  
19:                           "Enter the limit till prime is to be generated:"));  
20:                 System.out.println(String.format(  
21:                           "The prime numbers from 0 till %d are : ", limit));  
22:                 int prime=0; //index for the primes  
23:                 //increment limit by one to account for zero index  
24:                 for(boolean candidate:soe.computePrime(++limit)){  
25:                      if(candidate){  
26:                           System.out.println(prime);  
27:                      }  
28:                      ++prime;  
29:                 }  
30:            }catch(Throwable t){  
31:                 System.out.println("An error occured!");  
32:                 t.printStackTrace();  
33:            }  
34:       }  
35:       /*  
36:        * method to generate prime  
37:        * based on Sieve of Eratosthenes  
38:        */  
39:       private boolean[] computePrime(int limit){  
40:            boolean[] prime=new boolean[limit];  
41:            //assume that all numbers are prime by default  
42:            Arrays.fill(prime,true);  
43:            //setting 0 and 1 as non-primes  
44:            prime[0]=false;  
45:            prime[1]=false;  
46:            //starting from 2 use the sieve to weed out non-primes  
47:            int index=2;  
48:            //speed up technique  
49:            int sqrRoot=(int)Math.sqrt(limit);  
50:            while(index<sqrRoot){  
51:                 if(prime[index]){  
52:                      //eliminate all multiples of that index  
53:                      for(int ctr=index*index;ctr<limit;ctr=ctr+index){  
54:                           prime[ctr]=false;  
55:                      }  
56:                 }  
57:                 //increment to the next prime       
58:                 ++index;  
59:            }  
60:            return prime;  
61:       }  
62:       /*  
63:        * method to read input from console   
64:        */  
65:       private String readConsoleInput(String message) throws IOException{  
66:            System.out.println(message);  
67:            //read input from console  
68:            BufferedReader br=new BufferedReader(  
69:                      new InputStreamReader(System.in));       
70:            return br.readLine();       
71:       }  
72:  //End of Sieve of Eratosthenes       
73:  }  

No comments:

Post a Comment