javascript prime number check

Solutions on MaxInterview for javascript prime number check by the best coders in the world

showing results for - "javascript prime number check"
Jonathon
04 Feb 2019
1// 6k+-1 optimisation
2function is_prime (n) {
3  if (n < 3) return n > 1;
4  else if (n % 2 === 0 || n % 3 === 0) return false;
5  else if (n < 25) return true;
6  let i = 5;
7  while (i * i <= n ) {
8    if (n % i === 0 || n % (i + 2) === 0) return false;
9    i += 6;
10  }
11  return true;
12}