JavaEasyNumbers10 min
Check whether an integer is prime
Print true if n is prime, otherwise false. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.
Input
One integer n.
Output
Print true if n is prime, otherwise false.
Constraints
-1000000000 <= n <= 1000000000.
Example
Input
29
Output
true
Example 2 input
999999937
Output
true
This example exercises a large prime.
Public test cases (2)
Input
29
Expected output
true
Input
21
Expected output
false
Hints (2)
- Identify the state needed to avoid recomputing work for each element.
- A composite number has a divisor no larger than its square root.
Target complexity: time O(sqrt(n)) · space O(1)
