2007-11-29 14:18:54 +00:00
|
|
|
/* -*- mode: C; c-basic-offset: 4 -*- */
|
|
|
|
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
|
|
|
|
|
2007-08-13 21:53:19 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2007-11-20 00:34:18 +00:00
|
|
|
static int is_prime (int n) {
|
2007-08-13 21:53:19 +00:00
|
|
|
int i;
|
|
|
|
if (n==2) return 1;
|
|
|
|
if (n%2==0) return 0;
|
|
|
|
for (i=3; i*i<=n; i+=2) {
|
|
|
|
if (n%i==0) return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define N_PRIMES 30
|
|
|
|
static unsigned int primes[N_PRIMES]={0};
|
|
|
|
|
2007-11-20 00:34:18 +00:00
|
|
|
int toku_get_prime (unsigned int idx) {
|
2007-08-13 21:53:19 +00:00
|
|
|
if (primes[0]==0) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<N_PRIMES; i++) {
|
|
|
|
int j;
|
|
|
|
for (j=2<<i; !is_prime(j); j++) {
|
|
|
|
}
|
|
|
|
primes[i]=j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(idx<N_PRIMES);
|
|
|
|
return primes[idx];
|
|
|
|
}
|
|
|
|
|
2007-11-20 00:34:18 +00:00
|
|
|
void toku_test_primes (void) {
|
|
|
|
assert(toku_get_prime(0)==2);
|
|
|
|
assert(toku_get_prime(1)==5);
|
|
|
|
assert(toku_get_prime(2)==11);
|
|
|
|
assert(toku_get_prime(3)==17);
|
|
|
|
assert(toku_get_prime(4)==37);
|
|
|
|
assert(toku_get_prime(5)==67);
|
|
|
|
assert(toku_get_prime(6)==131);
|
2007-08-13 21:53:19 +00:00
|
|
|
}
|