Check if a number if prime or not - Java java.lang

Java examples for java.lang:int prime

Description

Check if a number if prime or not

Demo Code


import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main{
    /**//from w  ww .j  a va 2s.  c  om
     * Check if a number if prime or not
     *
     * @param number Number to check
     * @return True if number is prime
     */
    public static boolean isPrime(long number) {
        return isPrime(number, new Context());
    }
    /**
     * Check if number is prime with given context object
     *
     * @param number Number to check
     * @param context A reusable context for performance gain
     * @return True if number is prime
     */
    public static boolean isPrime(long number, Context context) {
        if (number == 1 || context.knownPrimes.contains(number)) {
            return true;
        }
        long sqrt = (long) Math.sqrt(number);
        while (context.range < sqrt) {
            context.range += 2;
            if (isPrime(context.range)) {
                context.knownPrimes.add(context.range);
            }
        }
        for (long knownPrime : context.knownPrimes) {
            if (knownPrime > sqrt) {
                return true;
            }
            if (number % knownPrime == 0) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials