// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class Prime {
    public static void main(String[] args) {
        for(int i = 10; i <= 10000000; i *= 10) {
           prime1(i);
           prime2(i);
        }
    }
    public static void prime1(int n) {
        long t1 = System.currentTimeMillis();
        int count = 0;
        for(int i = 2; i <= n; i++) {
            int maxj = (int) Math.sqrt(i);
            boolean fnd = false;
            for(int j = 2; j <= maxj; j++) {
                if((i % j) == 0) {
                    fnd = true;
                    break;
                }
            }
            if(!fnd) count++;
        }
        long t2 = System.currentTimeMillis();
        System.out.println(count + " of " + n + " by division: " + (t2 - t1));
        return;
    }
    public static void prime2(int n) {
        long t1 = System.currentTimeMillis();
        boolean[] pri = new boolean[n+1];
        for(int j = 0; j <= n; j++) pri[j] = true;
        pri[2] = true;
        for(int i = 2; i <= n; i++) {
            if(pri[i]) {
                for(int j = 2 * i; j <= n; j += i) {
                    pri[j] = false;
                }
            }
        }
        int count = 0;
        for(int j = 2; j <= n; j++) if(pri[j]) count++;
        long t2 = System.currentTimeMillis();
        System.out.println(count + " of " + n + " by flagging: " + (t2 - t1));
        return;
    }
}
