private static boolean isPrime(int n) {
        if (n < 10) {
            return n == 2 || n == 3 || n == 5 || n == 7;
        }
        
        if (n % 2 == 0 || n % 3 == 0)
            return false;
        
        int end = (int) Math.ceil(Math.sqrt(n));
        for (int i = 5; i <= end; i += 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
        
        return true;
    }