Advertisement
Guest User

aniki loves u

a guest
May 28th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. public class PrimeSieve {
  2.  
  3.   public static void print(int a) {
  4.     System.out.println(a);
  5.   }
  6.  
  7.   static int size = 1000000;
  8.   static boolean[] arrayList = new boolean[size+1];
  9.  
  10.   public static void main(String args[]) {
  11.    
  12.     // set all values to true
  13.     for (int i = 2; i <= size; i++) { arrayList[i] = true; }
  14.  
  15.     // remove non-primes
  16.     for (int i = 2; i*i <= size; i++) {
  17.       if (arrayList[i]) {
  18.         for (int j = i; i*j <= size; j++) { arrayList[i*j] = false; }
  19.       }
  20.     }
  21.  
  22.     // print off the primes
  23.     for (int i = 0; i < arrayList.length; i++) {
  24.       if (arrayList[i] == true) { print(i); }
  25.     }
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement