Sieve of Erastothenes

Sieve of Erastothenes

Run-time of approximately O(N log log N) static boolean[] b ; static int[] p ; static int n ; public static void sieve {    Arrays.fill(b,true) ; b[0] = b[1] = false ; n = 0 ; for (int i = 2 ; i < b.length ; i ++) {        if (!b[i]) continue ; p[n++] = i ; for (int j = 2 ; i * j < b.length ; j ++) b[i*j] = false ; } } b = new int[1<<20] ;

b is a boolean array for an O(1) check: Is N prime?

p is an integer array holding all the primes from 2-length(b), and has length 'n'.