[libmath-prime-util-perl] 02/33: Simplify next_prime / prev_prime

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:51:40 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.37
in repository libmath-prime-util-perl.

commit ed3542127ecad847316e8e8cbb944221004f44f0
Author: Dana Jacobsen <dana at acm.org>
Date:   Thu Jan 16 11:09:35 2014 -0800

    Simplify next_prime / prev_prime
---
 factor.c |  2 +-
 sieve.h  |  8 ++++++--
 util.c   | 21 +++++++--------------
 3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/factor.c b/factor.c
index d1259b1..690e3f6 100644
--- a/factor.c
+++ b/factor.c
@@ -131,7 +131,7 @@ int factor(UV n, UV *factors)
           croak("bad factor\n");
         n = tofac_stack[ntofac];  /* Set n to the other one */
       } else {
-        /* Factor via trial division.  Nothing should make it here. */
+        /* Factor via trial division.  Nothing should ever get here. */
         UV m = f % 30;
         UV limit = isqrt(n);
         if (verbose) printf("doing trial on %"UVuf"\n", n);
diff --git a/sieve.h b/sieve.h
index c14cc2d..63a7cda 100644
--- a/sieve.h
+++ b/sieve.h
@@ -32,9 +32,13 @@ static const unsigned char imask30[129] = {
 /* Add this to a number and you'll ensure you're on a wheel location */
 static const unsigned char distancewheel30[30] =
     {1,0,5,4,3,2,1,0,3,2,1,0,1,0,3,2,1,0,1,0,3,2,1,0,5,4,3,2,1,0};
-/* Once on the wheel, add this to get to next spot.  In p space, not m. */
+/* add this to n to get to the next wheel location */
 static const unsigned char wheeladvance30[30] =
-    {0,6,0,0,0,0,0,4,0,0,0,2,0,4,0,0,0,2,0,4,0,0,0,6,0,0,0,0,0,2};
+    {1,6,5,4,3,2,1,4,3,2,1,2,1,4,3,2,1,2,1,4,3,2,1,6,5,4,3,2,1,2};
+/* subtract this from n to get to the previous wheel location */
+static const unsigned char wheelretreat[30] =
+    {1,2,1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,1,2,1,2,3,4,1,2,3,4,5,6};
+
 
 #ifdef FUNC_is_prime_in_sieve
 static int is_prime_in_sieve(const unsigned char* sieve, UV p) {
diff --git a/util.c b/util.c
index 39e3e22..d1a5117 100644
--- a/util.c
+++ b/util.c
@@ -235,17 +235,12 @@ UV next_prime(UV n)
   release_prime_cache(sieve);
   if (next != 0) return next;
 
-  d = n/30;
-  m = n - d*30;
-  /* Move forward one, knowing we may not be on the wheel */
-  if (m == 29) { d++; m = 1; } else  { m = nextwheel30[m]; }
-  n = d*30+m;
-  while (!is_prob_prime(n)) {
-    /* Move forward one, knowing we are on the wheel */
+  m = n % 30;
+  do { /* Move forward one. */
     n += wheeladvance30[m];
     m = nextwheel30[m];
-  }
-  return(n);
+  } while (!is_prob_prime(n));
+  return n;
 }
 
 
@@ -264,12 +259,10 @@ UV prev_prime(UV n)
   }
   release_prime_cache(sieve);
 
-  d = n/30;
-  m = n - d*30;
-  do {
+  m = n % 30;
+  do { /* Move back one. */
+    n -= wheelretreat[m];
     m = prevwheel30[m];
-    if (m==29) d--;
-    n = d*30+m;
   } while (!is_prob_prime(n));
   return n;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list