[libmath-prime-util-perl] 25/54: Small speedup for is_power(x,2)

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:52:08 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 6661699b6a7e06a99b5338d799362b3e60697a2e
Author: Dana Jacobsen <dana at acm.org>
Date:   Wed Feb 12 10:18:56 2014 -0800

    Small speedup for is_power(x,2)
---
 util.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/util.c b/util.c
index 9c2df00..2485e87 100644
--- a/util.c
+++ b/util.c
@@ -1041,12 +1041,13 @@ int is_power(UV n, UV a)
   if (a > 0) {
     if (a == 1 || n <= 1) return 1;
     if ((a % 2) == 0)
-      return is_perfect_square(n) ? is_power(isqrt(n),a>>1) : 0;
+      return !is_perfect_square(n) ? 0 : (a == 2) ? 1 : is_power(isqrt(n),a>>1);
     if ((a % 3) == 0)
-      { UV cb = icbrt(n); return (cb*cb*cb == n) ? is_power(cb, a/3) : 0; }
+      { UV cb = icbrt(n);
+        return (cb*cb*cb != n)       ? 0 : (a == 3) ? 1 : is_power(cb, a/3); }
     if ((a % 5) == 0)
-      { UV r5 = (UV)(pow(n,0.2) + 0.1);
-        return (r5*r5*r5*r5*r5 == n) ? is_power(r5, a/5) : 0; }
+      { UV r5 = (UV)(pow(n,0.2) + 0.0001);
+        return (r5*r5*r5*r5*r5 != n) ? 0 : (a == 5) ? 1 : is_power(r5, a/5); }
   }
   ret = powerof(n);
   if (a != 0) return !(ret % a);  /* Is the max power divisible by a? */

-- 
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