** A prime number is a number that can only be divisible by 1 or itself ** Facts about prime numbers: ** - 1 is not a prime. ** - All primes except 2 are odd. */
| 19 | ** - All primes except 2 are odd. |
| 20 | */ |
| 21 | int isPrime(int nb) { |
| 22 | int root; |
| 23 | |
| 24 | if (nb == 1) { |
| 25 | return FALSE; |
| 26 | } else if (nb == 2 || nb == 3) { |
| 27 | return TRUE; |
| 28 | } else if (isEven(nb)) { |
| 29 | return FALSE; |
| 30 | } |
| 31 | |
| 32 | // square root of `nb` rounded to the greatest integer `root` so that: |
| 33 | // root * root <= nb |
| 34 | root = ceil(sqrt(nb)); |
| 35 | |
| 36 | for (int f = 3; f <= root; f += 2) { |
| 37 | if (nb % f == 0) { |
| 38 | return FALSE; |
| 39 | } |
| 40 | } |
| 41 | return TRUE; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | ** `count` starts to 1 because we already know that 2 is prime |