Returns the next prime-number after number - Java java.lang

Java examples for java.lang:int prime

Description

Returns the next prime-number after number

Demo Code


//package com.java2s;

public class Main {
    /**//  w ww  . j a v a2  s .c  o m
     * Returns the next prime-number after <b>number</b>
     * @param number
     * @return the next prime after <b>number</b>
     */
    public static long getPrimeAfter(long number) {
        long ret = number + 1;
        for (; !isPrime(ret); ret++)
            ;
        return ret;
    }

    /**
     * Finds out if <b>number</b> is a prime-number
     * @param number
     * @return TRUE if prime
     */
    public static boolean isPrime(long number) {
        boolean result = true;

        for (long current = number / 2; current > 1; current--) {
            if ((number / current) * current == number) {
                result = false;
                break;
            }
        }

        return result;
    }
}

Related Tutorials