2 * UCW Library -- Prime Number Table
4 * (c) 2005 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/prime.h>
12 #include <ucw/binsearch.h>
14 /* A table of odd primes, each is about 1.2 times the previous one */
15 static uns prime_table[] = {
123 #define NPRIMES ARRAY_SIZE(prime_table)
126 next_table_prime(uns x)
128 if (x >= prime_table[NPRIMES-1])
131 return prime_table[BIN_SEARCH_FIRST_GE(prime_table, NPRIMES, x+1)];
135 prev_table_prime(uns x)
137 int i = BIN_SEARCH_FIRST_GE(prime_table, NPRIMES, x);
138 return i ? prime_table[i-1] : 0;
147 #if 0 /* Generate the table */
153 x = nextprime(1.2*x);
157 for (int i=1; i<=100; i++)
158 printf("%d\t%d\t%d\n", i, next_table_prime(i), prev_table_prime(i));
159 for (uns i=0xfffffff0; i; i++)
160 printf("%u\t%u\t%u\n", i, next_table_prime(i), prev_table_prime(i));