[libmath-prime-util-perl] 49/54: Speedup for znprimroot

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


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

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

commit 06930b0297e804b2f1298f223e216c15096bb820
Author: Dana Jacobsen <dana at acm.org>
Date:   Thu Feb 27 19:38:37 2014 -0800

    Speedup for znprimroot
---
 Changes |  2 ++
 util.c  | 21 +++++++++++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/Changes b/Changes
index 1f0eb34..25ea682 100644
--- a/Changes
+++ b/Changes
@@ -10,6 +10,8 @@ Revision history for Perl module Math::Prime::Util
 
     - Factoring powers (and k*n^m for small k) is much faster.
 
+    - Speed up znprimroot.
+
     - Add Bernstein+Voloch improvements to AKS.  Much faster than the v6
       implementation, though still terribly slow vs. BPSW or other proofs.
 
diff --git a/util.c b/util.c
index 9484ab9..b0ace49 100644
--- a/util.c
+++ b/util.c
@@ -1238,12 +1238,25 @@ UV znprimroot(UV n) {
   UV fac[MPU_MAX_FACTORS+1];
   UV exp[MPU_MAX_FACTORS+1];
   UV a, phi;
-  int i, nfactors;
+  int i, j, nfactors;
   if (n <= 4) return (n == 0) ? 0 : n-1;
   if (n % 4 == 0)  return 0;
-  phi = totient(n);
-  /* Check if a primitive root exists. */
-  if (!is_prob_prime(n) && phi != carmichael_lambda(n))  return 0;
+  if (is_prob_prime(n)) {
+    phi = n-1;
+  } else {  /* Calculate Totient and Carmichael Lambda at same time */
+    UV lambda = 1;
+    nfactors = factor_exp(n, fac, exp);
+    phi = 1;
+    for (i = 0; i < nfactors; i++) {
+      UV pk = fac[i]-1;
+      for (j = 1; j < exp[i]; j++)
+        pk *= fac[i];
+      phi *= pk;
+      if (i == 0 && fac[0] == 2 && exp[0] > 2)  pk >>= 1;
+      lambda = lcm_ui(lambda, pk);
+    }
+    if (phi != lambda) return 0; /* prim root exists only if phi(n) = CL(n) */
+  }
   nfactors = factor_exp(phi, fac, exp);
   for (i = 0; i < nfactors; i++)
     exp[i] = phi / fac[i];  /* exp[i] = phi(n) / i-th-factor-of-phi(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