# [libmath-prime-util-perl] 27/54: Only test for squares and cubes in factor. Lowers cost.

```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.```
```
Author: Dana Jacobsen <d...@acm.org>
Date:   Wed Feb 12 17:28:38 2014 -0800

Only test for squares and cubes in factor.  Lowers cost.
---
factor.c                    | 10 ++++------
t/92-release-pod-coverage.t |  1 +
2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/factor.c b/factor.c
index f23c227..cf028e6 100644
--- a/factor.c
+++ b/factor.c
@@ -10,6 +10,7 @@
#include "cache.h"
#include "primality.h"
#define FUNC_isqrt  1
+#define FUNC_icbrt  1
#define FUNC_gcd_ui 1
#define FUNC_is_perfect_square 1
#define FUNC_clz 1
@@ -81,14 +82,11 @@ int factor(UV n, UV *factors)
factors[nfactors++] = n;
return nfactors;
}
-  /* Perfect powers.  Factor root only once. */
+  /* Perfect squares and cubes.  Factor root only once. */
{
-    int i, j, k = powerof(n);
+    int i, j, k = is_power(n,2) ? 2 : is_power(n,3) ? 3 : 1;
if (k > 1) {
-      UV p = (k == 2) ? isqrt(n) : (UV) (pow(n, 1.0/(double)k) + 0.01);
-      UV pk = p*p;
-      for (i = 2; i < k; i++)  pk *= p;
-      MPUassert( pk == n, "incorrect root in factor" );
+      UV p = (k == 2) ? isqrt(n) : icbrt(n);
if (is_prob_prime(p)) {
for (j = 0; j < k; j++)
factors[nfactors++] = p;
diff --git a/t/92-release-pod-coverage.t b/t/92-release-pod-coverage.t
index ba0a023..3d235d8 100644
--- a/t/92-release-pod-coverage.t
+++ b/t/92-release-pod-coverage.t
@@ -46,6 +46,7 @@ sub mpu_public_regex {
is_almost_extra_strong_lucas_pseudoprime
is_frobenius_underwood_pseudoprime
is_aks_prime is_bpsw_prime
+      is_power
miller_rabin miller_rabin_random
lucas_sequence
primes

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

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits
```