Learner Level
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
falsefor numbers less than2. - Only divisors up to the square root need to be checked.
- Return
trueonly when no divisor is found.
If a number has a large factor, it also has a matching smaller factor. Checking while i * i <= n is enough.