@Test public void testPrefixConsistency() { final int N = 10000; { boolean[] prev = {}; for (int i = 0; i < N; i++) { boolean[] cur = EratosthenesSieves.sievePrimeness(i); Assert.assertEquals(prev.length + 1, cur.length); for (int j = 0; j < prev.length; j++) Assert.assertTrue(cur[j] == prev[j]); prev = cur; } } { IntSieve[] FUNCS = { EratosthenesSieves::sieveSmallestPrimeFactor, EratosthenesSieves::sieveTotient, EratosthenesSieves::sieveOmega, EratosthenesSieves::sieveRadical, }; for (IntSieve func : FUNCS) { int[] prev = {}; for (int i = 0; i < N; i++) { int[] cur = func.sieve(i); Assert.assertEquals(prev.length + 1, cur.length); for (int j = 0; j < prev.length; j++) Assert.assertEquals(prev[j], cur[j]); prev = cur; } } } }
public static boolean[] sievePrimeness(int limit) { if (limit < 0 || limit == Integer.MAX_VALUE) throw new IllegalArgumentException("Limit out of range"); boolean[] result = new boolean[limit + 1]; if (limit > 0) Arrays.fill(result, 2, result.length, true); for (int i = 2, end = sqrt(limit); i <= end; i++) { if (result[i]) { for (int j = i * i; j < result.length; j += i) result[j] = false; } } return result; }
EratosthenesSieves { public static boolean[] sievePrimeness(int limit) { if (limit < 0 || limit == Integer.MAX_VALUE) throw new IllegalArgumentException("Limit out of range"); boolean[] result = new boolean[limit + 1]; if (limit > 0) Arrays.fill(result, 2, result.length, true); for (int i = 2, end = sqrt(limit); i <= end; i++) { if (result[i]) { for (int j = i * i; j < result.length; j += i) result[j] = false; } } return result; } }
EratosthenesSieves { public static boolean[] sievePrimeness(int limit) { if (limit < 0 || limit == Integer.MAX_VALUE) throw new IllegalArgumentException("Limit out of range"); boolean[] result = new boolean[limit + 1]; if (limit > 0) Arrays.fill(result, 2, result.length, true); for (int i = 2, end = sqrt(limit); i <= end; i++) { if (result[i]) { for (int j = i * i; j < result.length; j += i) result[j] = false; } } return result; }  }
EratosthenesSieves { public static boolean[] sievePrimeness(int limit) { if (limit < 0 || limit == Integer.MAX_VALUE) throw new IllegalArgumentException("Limit out of range"); boolean[] result = new boolean[limit + 1]; if (limit > 0) Arrays.fill(result, 2, result.length, true); for (int i = 2, end = sqrt(limit); i <= end; i++) { if (result[i]) { for (int j = i * i; j < result.length; j += i) result[j] = false; } } return result; }  static boolean[] sievePrimeness(int limit); static int[] sieveSmallestPrimeFactor(int limit); static int[] sieveTotient(int limit); static int[] sieveOmega(int limit); static int[] sieveRadical(int limit); }
EratosthenesSieves { public static boolean[] sievePrimeness(int limit) { if (limit < 0 || limit == Integer.MAX_VALUE) throw new IllegalArgumentException("Limit out of range"); boolean[] result = new boolean[limit + 1]; if (limit > 0) Arrays.fill(result, 2, result.length, true); for (int i = 2, end = sqrt(limit); i <= end; i++) { if (result[i]) { for (int j = i * i; j < result.length; j += i) result[j] = false; } } return result; }  static boolean[] sievePrimeness(int limit); static int[] sieveSmallestPrimeFactor(int limit); static int[] sieveTotient(int limit); static int[] sieveOmega(int limit); static int[] sieveRadical(int limit);  }