X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fprime.c;h=eec6f5d3db0dd34881ce772a1560635c8f7619cf;hb=88714d18176f047eb4d298bb3f22520217671513;hp=fe47ecd3472290ab87203f496e9adcd049e65698;hpb=4ecd6b5eabaf81c764a8ecf4ba8bacb7452a26d1;p=libucw.git diff --git a/lib/prime.c b/lib/prime.c index fe47ecd3..eec6f5d3 100644 --- a/lib/prime.c +++ b/lib/prime.c @@ -1,13 +1,13 @@ /* - * Sherlock Library -- Prime Number Tests + * UCW Library -- Prime Number Tests * - * (c) 1997 Martin Mares, + * (c) 1997 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ -#include -#include - -#include "lib.h" +#include "lib/lib.h" static int /* Sequential search */ __isprime(uns x) /* We know x != 2 && x != 3 */ @@ -47,30 +47,32 @@ isprime(uns x) } uns -nextprime(uns x) /* Returns some prime greater than X, usually the next one or the second next one */ +nextprime(uns x) /* Returns some prime greater than x */ { x += 5 - (x % 6); /* x is 6k-1 */ for(;;) { - if (__isprime(x)) - return x; x += 2; /* 6k+1 */ if (__isprime(x)) return x; x += 4; /* 6k-1 */ + if (__isprime(x)) + return x; } } -#ifdef PRIME_DEBUG +#ifdef TEST + +#include +#include int main(int argc, char **argv) { uns k = atol(argv[1]); - if (isprime(k)) - printf("%d is prime\n"); - else - printf("Next prime is %d\n", nextprime(k)); + printf("%d is%s prime\n", k, isprime(k) ? "" : "n't"); + printf("Next prime is %d\n", nextprime(k)); + return 0; } #endif