Check whether given number is prime or not

Java
voted: 1
vote
prime-number maths

Efficient way of checking whether given number is prime or not without additional libraries or pre-generating array of prime numbers. Remember to search prime numbers in positive integers.

public static boolean isPrime(int n) {
    if (n == 2)
        return true;
    if (n == 1 || n % 2 == 0)
        return false;
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return false;
    }
    return true;
}
working url: not entered
version: not entered
Leave a Comment:
Characters left:
No comments yet.