[libmath-prime-util-perl] 12/16: More bignum and 5.6 changes

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


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

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

commit 86ac1d8566f40008ab2f6c8431ea438d86d2254d
Author: Dana Jacobsen <dana at acm.org>
Date:   Sat Jan 19 05:28:05 2013 -0800

    More bignum and 5.6 changes
---
 Changes                   |  3 ++-
 MANIFEST                  |  2 +-
 lib/Math/Prime/Util.pm    | 12 +++++++-----
 lib/Math/Prime/Util/PP.pm | 36 +++++++++++++++++++++++++++---------
 4 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/Changes b/Changes
index 02a80c6..108703b 100644
--- a/Changes
+++ b/Changes
@@ -4,7 +4,8 @@ Revision history for Perl extension Math::Prime::Util.
 
     - Update MR bases.
 
-    - Fixed some issues when using bignum and Calc BigInt backend.
+    - Fixed some issues when using bignum and Calc BigInt backend, and bignum
+      and Perl 5.6.
 
     - Added tests for bigint is_provable_prime.
 
diff --git a/MANIFEST b/MANIFEST
index cc0cb5e..d0f6e0b 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -77,7 +77,7 @@ t/30-relations.t
 t/31-threading.t
 t/50-factoring.t
 t/51-primearray.t
-5/70-rt-bignum.t
+t/70-rt-bignum.t
 t/80-pp.t
 t/81-bignum.t
 t/90-release-perlcritic.t
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index a97623b..cc2a033 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -212,7 +212,8 @@ sub prime_set_config {
 sub _validate_positive_integer {
   my($n, $min, $max) = @_;
   croak "Parameter must be defined" if !defined $n;
-  croak "Parameter '$n' must be a positive integer" if $n =~ tr/0123456789//c;
+  croak "Parameter '$n' must be a positive integer"
+        if ref($n) ne 'Math::BigInt' && $n =~ tr/0123456789//c;
   croak "Parameter '$n' must be >= $min" if defined $min && $n < $min;
   croak "Parameter '$n' must be <= $max" if defined $max && $n > $max;
   # The second term is used instead of '<=' to fix strings like ~0+delta.
@@ -223,6 +224,7 @@ sub _validate_positive_integer {
   } elsif (ref($n) ne 'Math::BigInt') {
     croak "Parameter '$n' outside of integer range" if !defined $bigint::VERSION;
     $_[0] = Math::BigInt->new("$n"); # Make $n a proper bigint object
+    $_[0]->upgrade(undef) if $_[0]->upgrade();  # Stop BigFloat upgrade
   } else {
     $_[0]->upgrade(undef) if $_[0]->upgrade();  # Stop BigFloat upgrade
   }
@@ -450,10 +452,10 @@ sub primes {
     my $p1 = primorial(Math::BigInt->new(2052));
     my $p2 = primorial(Math::BigInt->new(6028));
     my $p3 = primorial(Math::BigInt->new($_big_gcd_top));
-    $_big_gcd[0] = int( $p0 / 223092870 );
-    $_big_gcd[1] = int( $p1 / $p0 );
-    $_big_gcd[2] = int( $p2 / $p1 );
-    $_big_gcd[3] = int( $p3 / $p2 );
+    $_big_gcd[0] = $p0->bdiv(223092870)->bfloor->as_int;
+    $_big_gcd[1] = $p1->bdiv($p0)->bfloor->as_int;
+    $_big_gcd[2] = $p2->bdiv($p1)->bfloor->as_int;
+    $_big_gcd[3] = $p3->bdiv($p2)->bfloor->as_int;
   }
 
   # Returns a function that will get a uniform random number between 0 and
diff --git a/lib/Math/Prime/Util/PP.pm b/lib/Math/Prime/Util/PP.pm
index f2f87fb..b17689c 100644
--- a/lib/Math/Prime/Util/PP.pm
+++ b/lib/Math/Prime/Util/PP.pm
@@ -65,7 +65,8 @@ sub _is_positive_int {
 sub _validate_positive_integer {
   my($n, $min, $max) = @_;
   croak "Parameter must be defined" if !defined $n;
-  croak "Parameter '$n' must be a positive integer" if $n =~ tr/0123456789//c;
+  croak "Parameter '$n' must be a positive integer"
+        if ref($n) ne 'Math::BigInt' && $n =~ tr/0123456789//c;
   croak "Parameter '$n' must be >= $min" if defined $min && $n < $min;
   croak "Parameter '$n' must be <= $max" if defined $max && $n > $max;
   if ($n <= ~0) {
@@ -74,6 +75,7 @@ sub _validate_positive_integer {
   } elsif (ref($n) ne 'Math::BigInt') {
     croak "Parameter '$n' outside of integer range" if !defined $bigint::VERSION;
     $_[0] = Math::BigInt->new("$n"); # Make $n a proper bigint object
+    $_[0]->upgrade(undef) if $_[0]->upgrade();  # Stop BigFloat upgrade
   } else {
     $_[0]->upgrade(undef) if $_[0]->upgrade();  # Stop BigFloat upgrade
   }
@@ -667,9 +669,13 @@ sub _gcd_ui {
 
 sub _is_perfect_power {
   my $n = shift;
-  my $log2n = _log2($n);
+  return 0 if $n <= 3 || $n != int($n);
+  return 1 if ($n & ($n-1)) == 0;                       # Power of 2
   $n = Math::BigInt->new("$n") unless ref($n) eq 'Math::BigInt';
-  for my $e (@{primes($log2n)}) {
+  # Perl 5.6.2 chokes on this, so do it via as_bin
+  # my $log2n = 0; { my $num = $n; $log2n++ while $num >>= 1; }
+  my $log2n = length($n->as_bin) - 2;
+  for (my $e = 2; $e <= $log2n; $e = next_prime($e)) {
     return 1 if $n->copy()->broot($e)->bpow($e) == $n;
   }
   0;
@@ -1074,12 +1080,24 @@ sub _basic_factor {
   return ($_[0]) if $_[0] < 4;
 
   my @factors;
-  while ( !($_[0] % 2) ) { push @factors, 2;  $_[0] = int($_[0] / 2); }
-  while ( !($_[0] % 3) ) { push @factors, 3;  $_[0] = int($_[0] / 3); }
-  while ( !($_[0] % 5) ) { push @factors, 5;  $_[0] = int($_[0] / 5); }
-
-  # Stop using bignum if we can
-  $_[0] = int($_[0]->bstr) if ref($_[0]) eq 'Math::BigInt' && $_[0] <= ~0;
+  if (ref($_[0]) ne 'Math::BigInt') {
+    while ( !($_[0] % 2) ) { push @factors, 2;  $_[0] = int($_[0] / 2); }
+    while ( !($_[0] % 3) ) { push @factors, 3;  $_[0] = int($_[0] / 3); }
+    while ( !($_[0] % 5) ) { push @factors, 5;  $_[0] = int($_[0] / 5); }
+  } else {
+    if (Math::BigInt::bgcd($_[0], 2*3*5) != 1) {
+      while ( $_[0]->is_even)   { push @factors, 2;  $_[0]->brsft(1); }
+      foreach my $div (3, 5) {
+        my ($q, $r) = $_[0]->copy->bdiv($div);
+        while ($r->is_zero) {
+          push @factors, $div;
+          $_[0] = $q;
+          ($q, $r) = $_[0]->copy->bdiv($div);
+        }
+      }
+    }
+    $_[0] = int($_[0]->bstr) if $_[0] <= ~0;
+  }
 
   if ( ($_[0] > 1) && _is_prime7($_[0]) ) {
     push @factors, $_[0];

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