Easy
Check Whether a Number Is Prime
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Primes and Sieves
Description
Given a non-negative integer n, determine whether it is a prime number.
A prime number is a positive integer greater than 1 that has exactly two distinct positive divisors: 1 and itself.
For example, 2 is prime because its only positive divisors are 1 and 2. The number 9 is not prime because it is divisible by 1, 3, and 9.
Numbers less than or equal to 1 are not prime. Return true if n is prime and false otherwise.
Implement the function isPrime(n) that checks whether the given number is prime.
Constraints
0 <= n <= 1000000000
Sample cases
3Sample 1
Input
n = 7
Expected output
true
Sample 2
Input
n = 9
Expected output
false
Sample 3
Skills and languages
Concepts
- Primality Testing
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer