Whether prime - Java java.lang

Java examples for java.lang:Math Prime Number

Description

Whether prime

Demo Code


//package com.java2s;

public class Main {
    /**/*from  ww w  . j a v  a  2 s.c  om*/
     * Whether prime
     * @param n
     * @return
     */
    public static boolean primalityTest(int n) {
        if (n < 2)
            return false;
        else if (n % 2 == 0)
            return true;
        for (int i = 3; i <= n / i; i += 2) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
}

Related Tutorials