Check whether a number is prime
Coding problemEasyAsked in: Accenture, Capgemini, TCS
Given a non-negative integer n, return true if it is a prime number, and false otherwise.
A prime has exactly two divisors: 1 and itself. So 0 and 1 are not prime, and 2 is the only even prime. The largest hidden tests are close to 1,000,000,000,000, so trying every possible divisor is too slow.
Examples
Example 1
Input: n = 97
Output: true
Example 2
Input: n = 1
Output: false
Example 3
Input: n = 91
Output: false
91 = 7 x 13.
Constraints
- n is between 0 and 1,000,000,000,000
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.