[libmath-prime-util-perl] 20/35: Fix PP is_pseudoprime; Fix test issues

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


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

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

commit b443c370676d7ce46802bedc7c9ba5aed22bf729
Author: Dana Jacobsen <dana at acm.org>
Date:   Tue Oct 29 21:08:37 2013 -0700

    Fix PP is_pseudoprime; Fix test issues
---
 Changes                   | 2 ++
 lib/Math/Prime/Util.pm    | 3 ++-
 lib/Math/Prime/Util/PP.pm | 4 ++--
 t/16-randomprime.t        | 9 ++++-----
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/Changes b/Changes
index 7e84105..8c2726c 100644
--- a/Changes
+++ b/Changes
@@ -21,6 +21,8 @@ Revision history for Perl module Math::Prime::Util
 
     - exp_mangoldt defaults to XS for speed.
 
+    - Fixed Pure Perl 33- to 64-bit is_pseudoprime.
+
     - Perl RiemannZeta changes:
 
       - Borwein Zeta calculations done in BigInt instead of BigFloat (speed).
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 3c8bca2..fc1603d 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -101,6 +101,7 @@ BEGIN {
     *is_prime      = \&Math::Prime::Util::_generic_is_prime;
     *next_prime    = \&Math::Prime::Util::_generic_next_prime;
     *prev_prime    = \&Math::Prime::Util::_generic_prev_prime;
+    *exp_mangoldt  = \&Math::Prime::Util::_generic_exp_mangoldt;
     *forprimes     = sub (&$;$) { _generic_forprimes(@_); }; ## no critic qw(ProhibitSubroutinePrototypes)
 
     *_prime_memfreeall = \&Math::Prime::Util::PP::_prime_memfreeall;
@@ -1642,9 +1643,9 @@ sub prime_iterator_object {
 # Return p if n = p^m [p prime, m >= 1], 1 otherwise.
 sub _generic_exp_mangoldt {
   my($n) = @_;
+  return 1 if defined $n && $n <= 1;  # n <= 1
   _validate_num($n) || _validate_positive_integer($n);
 
-  return 1 if $n <= 1;                # n <= 1
   return 2 if ($n & ($n-1)) == 0;     # n power of 2
   return 1 unless $n & 1;             # even n can't be p^m
 
diff --git a/lib/Math/Prime/Util/PP.pm b/lib/Math/Prime/Util/PP.pm
index 65ac6d6..d17d383 100644
--- a/lib/Math/Prime/Util/PP.pm
+++ b/lib/Math/Prime/Util/PP.pm
@@ -704,8 +704,8 @@ sub _powmod {
   my($n, $power, $m) = @_;
   my $t = 1;
 
+  $n %= $m if $n >= $m;
   if  ($m < $_half_word) {
-    $n %= $m;
     while ($power) {
       $t = ($t * $n) % $m if ($power & 1);
       $power >>= 1;
@@ -774,7 +774,7 @@ sub is_pseudoprime {
   }
   my $x = (ref($n) eq 'Math::BigInt')
         ? $n->copy->bzero->badd($base)->bmodpow($n-1,$n)
-        : _native_powmod($base, $n-1, $n);
+        : _powmod($base, $n-1, $n);
   return ($x == 1) ? 1 : 0;
 }
 
diff --git a/t/16-randomprime.t b/t/16-randomprime.t
index 7624c8a..4ca0986 100644
--- a/t/16-randomprime.t
+++ b/t/16-randomprime.t
@@ -65,7 +65,7 @@ plan tests => 13+3+3+3
               + (2 * scalar @random_to)
               + (1 * scalar @random_ndigit_tests)
               + (3 * scalar @random_nbit_tests)
-              + 3 + 4
+              + 2 + 4
               + 0;
 
 my $infinity = 20**20**20;
@@ -179,15 +179,14 @@ prime_set_config(nobigint=>0);
 
 # Now check with custom irand
 {
-  my $seed = 1;
+  my $seed = 2389743;
   sub mysrand { $seed = $_[0]; }
   #sub irand { $seed = (1103515245*$seed + 12345) % 4294967296; }
   sub irand { $seed = ( 16807 * $seed ) % 2147483647; }
   prime_set_config( irand => \&irand );
 }
-is( random_nbit_prime(10), 571, "random 10-bit prime with custom irand" );
-is( random_nbit_prime(20), 558769, "random 20-bit prime with custom irand" );
-is( random_ndigit_prime(9), 756473437, "random 9-digit with custom irand" );
+is( random_nbit_prime(20), 771283, "random 20-bit prime with custom irand" );
+is( random_ndigit_prime(9), 980824987, "random 9-digit with custom irand" );
 
 {
   my $n = random_nbit_prime(80);

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