[libmath-prime-util-perl] 14/59: Turn on bignum support for PP code
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:44:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.10
in repository libmath-prime-util-perl.
commit fc219708072ea2aa226c1672e7a6c6c14265a6fc
Author: Dana Jacobsen <dana at acm.org>
Date: Fri Jun 29 04:29:50 2012 -0600
Turn on bignum support for PP code
---
TODO | 2 ++
lib/Math/Prime/Util.pm | 10 ++++++++--
lib/Math/Prime/Util/PP.pm | 33 ++++++++++++++++++++++-----------
3 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/TODO b/TODO
index a879bfd..828b213 100644
--- a/TODO
+++ b/TODO
@@ -24,3 +24,5 @@
- Move .c / .h files into separate directory.
version does it in a painful way. Something simpler to be had?
+
+- In PP nth_prime* and prime_count*, turn on bignum if bigint is on.
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 0d1ff4f..d81d3b5 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -103,7 +103,9 @@ sub primes {
}
# Verify the parameters are in range.
- croak "Parameters [ $low $high ] not in range 0-$_maxparam" unless $low <= $_maxparam && $high <= $_maxparam;
+ if (!$_pure_perl || !defined $bigint::VERSION) {
+ croak "Parameters [ $low $high ] not in range 0-$_maxparam" unless $low <= $_maxparam && $high <= $_maxparam;
+ }
return $sref if ($low > $high) || ($high < 2);
@@ -167,7 +169,9 @@ sub random_prime {
$high = 'undef' unless defined $high;
croak "Parameters [ $low $high ] must be positive integers";
}
- croak "Parameters [ $low $high ] not in range 0-$_maxparam" unless $low <= $_maxparam && $high <= $_maxparam;
+ if (!$_pure_perl || !defined $bigint::VERSION) {
+ croak "Parameters [ $low $high ] not in range 0-$_maxparam" unless $low <= $_maxparam && $high <= $_maxparam;
+ }
$low = 2 if $low < 2;
# Make sure we have a valid range.
@@ -200,6 +204,7 @@ sub random_prime {
# Generate random numbers in the interval until one is prime.
my $loop_limit = 2000 * 1000; # To protect against broken rand
do {
+ # TODO: bigint with huge range
my $rand = ($range <= 4294967295) ? $irandf->($range) :
( ($irandf->(4294967295) << 32) + $irandf->(4294967295) ) % $range;
$prime = $low + $rand;
@@ -216,6 +221,7 @@ my @_random_ndigit_ranges;
sub random_ndigit_prime {
my $digits = shift;
+ # TODO: bigint with many digits
if ((!defined $digits) || ($digits > $_maxdigits) || ($digits < 1)) {
croak "Digits must be between 1 and $_maxdigits";
}
diff --git a/lib/Math/Prime/Util/PP.pm b/lib/Math/Prime/Util/PP.pm
index e95934c..84613d6 100644
--- a/lib/Math/Prime/Util/PP.pm
+++ b/lib/Math/Prime/Util/PP.pm
@@ -76,6 +76,7 @@ my @_prevwheel30 = (29,29,1,1,1,1,1,1,7,7,7,7,11,11,13,13,13,13,17,17,19,19,19,1
sub _is_prime7 { # n must not be divisible by 2, 3, or 5
my($n) = @_;
+ # TODO: bignum on 32-bit
return is_prob_prime($n) if (~0 == 18446744073709551615) && ($n > 10_000_000);
foreach my $i (qw/7 11 13 17 19 23 29/) {
@@ -275,7 +276,8 @@ sub primes {
sub next_prime {
my($n) = @_;
croak "Input must be a positive integer" unless _is_positive_int($n);
- return 0 if $n >= ((_maxbits == 32) ? 4294967291 : 18446744073709551557);
+ return 0 if ($n >= ((_maxbits == 32) ? 4294967291 : 18446744073709551557))
+ && (!defined $bigint::VERSION);
return $_prime_next_small[$n] if $n <= $#_prime_next_small;
my $d = int($n/30);
@@ -443,7 +445,7 @@ sub nth_prime_lower {
# Dusart 1999 page 14, for all n >= 2
my $lower = $n * ($flogn + $flog2n - 1.0 + (($flog2n-2.25)/$flogn));
- if ($lower >= ~0) {
+ if ( ($lower >= ~0) && (!defined $bignum::VERSION) ) {
if (_maxbits == 32) {
return 4294967291 if $n <= 203280221;
} else {
@@ -474,7 +476,7 @@ sub nth_prime_upper {
$upper = $n * ( $flogn + $flog2n );
}
- if ($upper >= ~0) {
+ if ( ($upper >= ~0) && (!defined $bignum::VERSION) ) {
if (_maxbits == 32) {
return 4294967291 if $n <= 203280221;
} else {
@@ -512,7 +514,7 @@ sub nth_prime_approx {
elsif ($n < 200000000) { $approx += 0.0 * $order; }
else { $approx += -0.010 * $order; }
- if ($approx >= ~0) {
+ if ( ($approx >= ~0) && (!defined $bignum::VERSION) ) {
if (_maxbits == 32) {
return 4294967291 if $n <= 203280221;
} else {
@@ -530,10 +532,12 @@ sub nth_prime {
return $_primes_small[$n] if $n <= $#_primes_small;
- if (_maxbits == 32) {
- croak "nth_prime($n) overflow" if $n > 203280221;
- } else {
- croak "nth_prime($n) overflow" if $n > 425656284035217743;
+ if (!defined $bigint::VERSION) {
+ if (_maxbits == 32) {
+ croak "nth_prime($n) overflow" if $n > 203280221;
+ } else {
+ croak "nth_prime($n) overflow" if $n > 425656284035217743;
+ }
}
my $prime = 0;
@@ -1155,9 +1159,16 @@ methods, is_prime, prime_count, nth_prime, approximations and bounds for
the prime_count and nth prime, next_prime and prev_prime, factoring utilities,
and more.
-All routines currently work in native integers (32-bit or 64-bit). Bignum
-support may be added later. If you need bignum support for these types of
-functions inside Perl now, I recommend L<Math::Pari>.
+All routines should work with native integers or multi-precision numbers. To
+enable big numbers, use bignum:
+
+ use bignum;
+ say prime_count_approx(1000000000000000000000000)'
+ # says 18435599767347543283712
+
+This is still experimental, and some functions will be very slow. I recommend
+looking into L<Math::Pari> if you need serious bignum support for this type
+of functionality right now.
=head1 FUNCTIONS
--
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