Prime Number Check

Easy~10 min

Given an integer n, return true if n is a prime number, and false otherwise.

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

For example, isPrime(7) returns true because 7 is only divisible by 1 and 7, while isPrime(4) returns false because 4 is divisible by 2.

Examples

Example 1
Input: n = 7
Output: true
Explanation: 7 has no divisors other than 1 and 7, so it is prime.
Example 2
Input: n = 4
Output: false
Explanation: 4 is divisible by 2, so it is not prime.
Example 3
Input: n = 1
Output: false
Explanation: 1 is not considered a prime number by definition.

Constraints

  • -10^9 <= n <= 10^9
  • Numbers less than 2 are not prime.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run