// 计算2~2000000之间的所有素数 long start = System.currentTimeMillis(); int n = 200_0000; int count = 0; int i; BitSet bit = new BitSet(n + 1); for (i = 2; i <= n; i++) { bit.set(i); } i = 2; int k; while (i * i <= n) { if (bit.get(i)) { count++; k = i * 2; while (k <= n) { bit.clear(k); k += i; } } i++; } while (i <= n) { if (bit.get(i)) { count++; } i++; } long end = System.currentTimeMillis(); System.out.println("count: " + count); // count: 148933 System.out.println("time: " + (end - start));