From cd5b7ea487ab7692635ba7eca98fcb55f6d996a7 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 18 Nov 2002 17:56:16 +0000 Subject: [PATCH] In some cases, nextprime(x) could have been equal to x (reported by Milan). --- lib/prime.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/prime.c b/lib/prime.c index 0ed0eda1..9be4c411 100644 --- a/lib/prime.c +++ b/lib/prime.c @@ -47,32 +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 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 -- 2.39.2