]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
next-prime: Tweaks for GNU gettext.
authorBruno Haible <bruno@clisp.org>
Sat, 19 Jul 2025 19:38:57 +0000 (21:38 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 19 Jul 2025 19:39:27 +0000 (21:39 +0200)
* lib/next-prime.c (is_prime): Change so that is_prime (3) returns true.
* tests/test-next-prime.c (main): Allow next_prime (1) to be 1.

ChangeLog
lib/next-prime.c
tests/test-next-prime.c

index 87fdf0d51a478d43edd022f510afae51775c5ff5..d87c009d015dd27888ab0eb0524e50278733bf26 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-07-19  Bruno Haible  <bruno@clisp.org>
+
+       next-prime: Tweaks for GNU gettext.
+       * lib/next-prime.c (is_prime): Change so that is_prime (3) returns true.
+       * tests/test-next-prime.c (main): Allow next_prime (1) to be 1.
+
 2025-07-18  Collin Funk  <collin.funk1@gmail.com>
 
        doc: Mention GNU/Hurd is missing sync_file_range.
index 58630db9b09f294232be783966e9d67c737ade76..9aa3264f31c34e5a4a141075aacf32526d8fd9b2 100644 (file)
 
 #include <stdint.h> /* for SIZE_MAX */
 
-/* Return true if CANDIDATE is a prime number.  CANDIDATE should be an odd
-   number at least equal to 11.  */
+/* Return true if CANDIDATE is a prime number or 1.
+   CANDIDATE should be an odd number >= 1.  */
 static bool _GL_ATTRIBUTE_CONST
 is_prime (size_t candidate)
 {
   size_t divisor = 3;
   size_t square = divisor * divisor;
 
-  while (square < candidate && (candidate % divisor))
+  for (;;)
     {
+      if (square > candidate)
+        return true;
+      if ((candidate % divisor) == 0)
+        return false;
+      /* Increment divisor by 2.  */
       divisor++;
       square += 4 * divisor;
       divisor++;
     }
-
-  return (candidate % divisor ? true : false);
 }
 
 size_t _GL_ATTRIBUTE_CONST
index b74257ae83feb50b9e8f5b0676f554653bb97538..a5a2e9133c56f841ff66dd232ee05f9faa53c156 100644 (file)
@@ -26,8 +26,9 @@
 int
 main ()
 {
-  ASSERT (next_prime (1) >= 3 && next_prime (1) <= 11);
+  ASSERT (next_prime (1) >= 1 && next_prime (1) <= 11);
   ASSERT (next_prime (3) >= 3 && next_prime (3) <= 11);
+  ASSERT (next_prime (5) >= 5 && next_prime (5) <= 11);
   ASSERT (next_prime (7) >= 7 && next_prime (7) <= 11);
   ASSERT (next_prime (10) == 11);
   ASSERT (next_prime (11) == 11);