1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include "primes.h"
#include "defs.h"
#include "utils.h"
sboolean
s_prime_is (suint n) {
if (n <= 1 || (n % 2 == 0 && n > 2) ) {
return FALSE;
}
/*
* Binary search in list.
*/
if (n <= 4999) {
return s_binary_search (SPrimeListLong, 0, S_PRIME_LIST_LONG_LEN, n);
}
/*
* Since all non-primes are a product of two primes, we only need to check
* a subset of all values.
*/
for (sint i = 0; i < S_PRIME_LIST_LONG_LEN; i++){
if (n % SPrimeListLong[i] == 0) {
return FALSE;
}
}
/*
* if we exit the loop now, and n is less than the highest value in the list
* squared, we have found a prime.
*/
if (n <= 4999 * 4999 ) {
return TRUE;
}
/*
* Last chance to see if it is not a prime. This will be expensive!
*/
for (sint i = 4999 + 1; i <= floor(sqrt(n)); i++) {
if (n % i == 0) {
return FALSE;
}
}
return TRUE;
}
|