Check for Prime Number
Easy
Given a positive integer n, determine whether it is a prime number.
A prime number is a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself.
Return true if the given number is prime; otherwise, return false.
Example 1
Input
7
Output
true
Explanation
7 has exactly two positive divisors: 1 and 7.
Therefore, it is a prime number.
Example 2
Input
12
Output
false
Explanation
12 has more than two positive divisors: 1, 2, 3, 4, 6 and 12.
Therefore, it is not a prime number.
Constraints
1 ≤ n ≤ 10^9
Hints:
Hint 1
A number greater than 1 is prime if it has no divisors other than 1 and itself.
Hint 2
Instead of checking all numbers from 2 to n-1, it is sufficient to check divisors only up to √n.
Auto
Loading editor...
Input
Expected Output