PythonEasyMath10 min
Check whether a number is prime
A prime is an integer greater than 1 with exactly two positive divisors. Negative numbers, zero and one are not prime. Interviewers use this problem to assess test potential factors only up to the square root.
Input
One integer n.
Output
Print true or false in lowercase.
Constraints
-10^9 <= n <= 10^9.
Example
Input
29
Output
true
Example 2 input
999999937
Output
true
The smallest prime needs explicit handling.
Public test cases (2)
Input
29
Expected output
true
Input
49
Expected output
false
Hints (2)
- Identify the state needed to avoid repeated work.
- Test potential factors only up to the square root.
Target complexity: time O(sqrt(n)) for positive n · space O(1)
