RandomUtils { public static int nextTwinPrime(int n) { if (n > MAX_INT_SMALLER_TWIN_PRIME) { throw new IllegalArgumentException(); } if (n <= 3) { return 5; } int next = nextPrime(n); while (isNotPrime(next + 2)) { next = nextPrime(next + 4); } return next + 2; } private  RandomUtils(); static void useTestSeed(); static Random getRandom(); static Random getRandom(long seed); static byte[] longSeedtoBytes(long seed); static long seedBytesToLong(byte[] seed); static int hashDouble(double value); static int hashFloat(float value); static int nextTwinPrime(int n); static int nextPrime(int n); static boolean isNotPrime(int n); static final int MAX_INT_SMALLER_TWIN_PRIME; }
@Test public void testNextTwinPrime() { assertEquals(5, RandomUtils.nextTwinPrime(-1)); assertEquals(5, RandomUtils.nextTwinPrime(1)); assertEquals(5, RandomUtils.nextTwinPrime(2)); assertEquals(5, RandomUtils.nextTwinPrime(3)); assertEquals(7, RandomUtils.nextTwinPrime(4)); assertEquals(7, RandomUtils.nextTwinPrime(5)); assertEquals(13, RandomUtils.nextTwinPrime(6)); assertEquals(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME + 2, RandomUtils.nextTwinPrime(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME)); try { RandomUtils.nextTwinPrime(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME + 1); fail(); } catch (IllegalArgumentException iae) { } }