MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / isPrime

Function isPrime

project_euler_problems/10001st prime/solution.js:15–34  ·  view source on GitHub ↗
(nb)

Source from the content-addressed store, hash-verified

13 ** - All primes except 2 are odd.
14 */
15function isPrime(nb) {
16 if (nb === 1) {
17 return false;
18 } else if (nb === 2 || nb === 3) {
19 return true;
20 } else if (isEven(nb)) {
21 return false;
22 }
23
24 // square root of `nb` rounded to the greatest integer `root` so that:
25 // root * root <= nb
26 const root = Math.ceil(Math.sqrt(nb));
27
28 for (let f = 3; f <= root; f += 2) {
29 if (nb % f === 0) {
30 return false;
31 }
32 }
33 return true;
34}
35
36/*
37 ** `count` starts to 1 because we already know that 2 is prime

Callers 1

solution.jsFile · 0.70

Calls 1

isEvenFunction · 0.70

Tested by

no test coverage detected