| 13 | ** - All primes except 2 are odd. |
| 14 | */ |
| 15 | function 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 |