JavaScriptEasyNumbers10 min
Check whether a number is prime
Return whether n is prime; integers below 2 are not prime. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess test divisors only up to the square root.
Input
A JSON integer n.
Output
Print one compact JSON value: a boolean.
Constraints
0 <= n <= 1000000000.
Example
Input
29
Output
true
Example 2 input
99991
Output
true
Boundary case: Two is prime
Public test cases (2)
Input
29
Expected output
true
Input
21
Expected output
false
Hints (2)
- A composite number has a factor at most its square root
- Two is prime
Target complexity: time O(sqrt(n)) · space O(1)
