FindPrime

Define a function that takes an integer argument and returns a logical value true or false depending on if the integer is a prime. Per Wikipedia, a prime number ( or a prime ) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

DO NOT TRY to acquire fancy optimizations first

public static boolean isPrime(int num) {  
    // single exception  
    if (num == 2 || num == 5) {  
        return true;  
    } else if (num < 2) {  
        return false;  
    }    // last digit exception  
    switch (num % 10) {  
        case 0:  
        case 2:  
        case 4:  
        case 5:  
        case 6:  
        case 8:  
            return false;  
    }    // else  
    for (int i = 3; i <= Math.sqrt(num); i += 2) {  
        if (num % i == 0) {  
            return false;  
        }    }    return true;  
}
Last modified April 7, 2025: update codewar (7e82e67)