Prime Checker

  • + 1 comment

    Can you explain me why you check all odd numbers until square root of n? I don't understand why square root and not n/2..

    Anyway this is my working solution:

        private boolean isPrime(int n) {
            if (n < 2) {
                return false;
            }
    
            for (int i = 2; i <= n / 2; i++) {
                if (n % i == 0) {
                    return false;
                }
            }
    
            return true;
        }