next Prime - Java java.lang

Java examples for java.lang:int prime

Description

next Prime

Demo Code



public class Main{
    public static long nextPrime(long input) {
        long lastFound = input;
        if (input == 2)
            lastFound++;/*from   w  w  w .j  a v a 2  s.c  om*/
        else
            lastFound += 2;

        while (!PrimeUtil.isPrime(lastFound)) {
            lastFound += 2;
        }
        return lastFound;
    }
    public static boolean isPrime(long num) {
        if (num < 2) {
            return false;
        } else if (num == 2) {
            return true;
        } else if (num % 2 == 0) {
            return false;
        } else {
            for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
                if (num % divisor == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

Related Tutorials