isPrime

Medium · Mathematical Calculations · 2 test cases

Given a number, return true if the number is prime, otherwise return false.

Examples

isPrime(7) → true
isPrime(10) → false

Starter code

function isPrime(n) {

}

Complexity of the optimal solution

Time O(sqrt(n)), space O(1). The algorithm checks for factors only up to the square root of n. This is a significant optimization over checking up to n. The time complexity is therefore O(sqrt(n)). Space usage is constant.

Solve isPrime in the browser