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

Function isPrime

project_euler_problems/10001st prime/solution.c:21–42  ·  view source on GitHub ↗

** 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. */

Source from the content-addressed store, hash-verified

19 ** - All primes except 2 are odd.
20 */
21int 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

Callers 1

mainFunction · 0.70

Calls 1

isEvenFunction · 0.70

Tested by

no test coverage detected