public static int nextPrime()

in commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java [68:99]


    public static int nextPrime(int n) {
        if (n < 0) {
            throw new IllegalArgumentException(String.format(NUMBER_TOO_SMALL, n, 0));
        }
        if (n <= 2) {
            return 2;
        }
        n |= 1; // make sure n is odd

        if (isPrime(n)) {
            return n;
        }

        // prepare entry in the +2, +4 loop:
        // n should not be a multiple of 3
        final int rem = n % 3;
        if (0 == rem) { // if n % 3 == 0
            n += 2; // n % 3 == 2
        } else if (1 == rem) { // if n % 3 == 1
            n += 4; // n % 3 == 2
        }
        while (true) { // this loop skips all multiple of 3
            if (isPrime(n)) {
                return n;
            }
            n += 2; // n % 3 == 1
            if (isPrime(n)) {
                return n;
            }
            n += 4; // n % 3 == 2
        }
    }