* fns.c (next_almost_prime): Don't return a multiple of 3 or 5.
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 14 Jun 2011 20:57:33 +0000 (13:57 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 14 Jun 2011 20:57:33 +0000 (13:57 -0700)
The previous code was bogus.  For example, next_almost_prime (32)
returned 39, which is undesirable as it is a multiple of 3; and
next_almost_prime (24) returned 25, which is a multiple of 5 so
why was the code bothering to check for multiples of 7?

src/ChangeLog
src/fns.c

index 54fe58d..7bd1d47 100644 (file)
@@ -1,5 +1,11 @@
 2011-06-14  Paul Eggert  <eggert@cs.ucla.edu>
 
+       * fns.c (next_almost_prime): Don't return a multiple of 3 or 5.
+       The previous code was bogus.  For example, next_almost_prime (32)
+       returned 39, which is undesirable as it is a multiple of 3; and
+       next_almost_prime (24) returned 25, which is a multiple of 5 so
+       why was the code bothering to check for multiples of 7?
+
        * bytecode.c (exec_byte_code): Use ptrdiff_t, not int, for vector length.
 
        * eval.c, doprnt.c (SIZE_MAX): Remove; inttypes.h defines this now.
index 0e98a8d..333a75a 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -3379,13 +3379,9 @@ check_hash_table (Lisp_Object obj)
 EMACS_INT
 next_almost_prime (EMACS_INT n)
 {
-  if (n % 2 == 0)
-    n += 1;
-  if (n % 3 == 0)
-    n += 2;
-  if (n % 7 == 0)
-    n += 4;
-  return n;
+  for (n |= 1; ; n += 2)
+    if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
+      return n;
 }