]> mj.ucw.cz Git - libucw.git/blobdiff - lib/prime.c
Polished the introductory comments in sorter.h.
[libucw.git] / lib / prime.c
index 56bcc87ef050573467861b416523a150718fdc3c..eec6f5d3db0dd34881ce772a1560635c8f7619cf 100644 (file)
@@ -1,7 +1,10 @@
 /*
- *     Sherlock Library -- Prime Number Tests
+ *     UCW Library -- Prime Number Tests
  *
  *     (c) 1997 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
 #include "lib/lib.h"
@@ -44,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 <stdio.h>
+#include <stdlib.h>
 
 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