📜  素数检查java代码示例

📅  最后修改于: 2022-03-11 14:52:21.544000             🧑  作者: Mango

代码示例1
static boolean isPrime(int n){
        boolean result=true;
        if(n <= 1) return false;
        for(int i = 2; i <= n/2; i++){ //n/2 because the largest factor of any number is either it's half or less than it
            if(n % i == 0){
                result = false; //if some number divides it, then prime=false;
                break;
            }
        }
        return result;
    }