is Prime - Java java.lang

Java examples for java.lang:int prime

Description

is Prime

Demo Code


//package com.java2s;

public class Main {
    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 {/*from   w  ww. ja  v a2  s .  c  o  m*/
            for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
                if (num % divisor == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

Related Tutorials