Implement the method isPrime that checks whether an integer is prime.

Decide whether a number is prime. A prime number is greater than 1 and has no positive divisors except 1 and itself.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Return false for numbers less than 2.
  • Only divisors up to the square root need to be checked.
  • Return true only when no divisor is found.
Example 1
Input:
n (int) = 2
Return:
(boolean) true
Example 2
Input:
n (int) = 4
Return:
(boolean) false
Example 3
Input:
n (int) = 17
Return:
(boolean) true
Example 4
Input:
n (int) = 1
Return:
(boolean) false
Example 5
Input:
n (int) = 29
Return:
(boolean) true

If a number has a large factor, it also has a matching smaller factor. Checking while i * i <= n is enough.

Run your code to see the result.