Permalink
Browse files

Make various functions work with bigints

A bug report reported by Dana Jacobsen reported that bigint was incompatible
with Math::Primality, so we stringify our input so we can accept any object
that has a stringification method.

We also declare a min Perl version of 5.8, which is when bigint appeared
in core.
  • Loading branch information...
1 parent ed1fd63 commit a83767173be9e56fd8a18cd538695b14dab10ab8 @leto committed Oct 13, 2012
Showing with 23 additions and 5 deletions.
  1. +2 −1 Build.PL
  2. +4 −4 lib/Math/Primality.pm
  3. +17 −0 t/bigint.t
View
@@ -19,7 +19,8 @@ my $builder = Module::Build->new(
},
configure_requires => { 'Module::Build' => 0.38 },
requires => {
- 'Math::GMPz' => 0.26,
+ 'Math::GMPz' => '0.26',
+ 'perl' => '5.008',
},
add_to_cleanup => [ 'Math-Primality-*' ],
meta_merge => {
View
@@ -137,8 +137,8 @@ sub is_pseudoprime($;$)
return 0 unless $n;
$base ||= 2;
# we should check if we are passed a GMPz object
- $base = GMP->new($base);
- $n = GMP->new($n);
+ $base = GMP->new("$base");
+ $n = GMP->new("$n");
my $m = GMP->new();
Rmpz_sub_ui($m, $n, 1); # $m = $n - 1
@@ -297,7 +297,7 @@ Then a strong Lucas-Selfridge pseudoprime is an odd, non-perfect square number $
sub is_strong_lucas_pseudoprime($)
{
my ($n) = @_;
- $n = GMP->new($n);
+ $n = GMP->new("$n");
# we also need to handle all N < 3 and all even N
my $cmp = _check_two_and_even($n);
return $cmp if $cmp != 2;
@@ -480,7 +480,7 @@ L<http://primes.utm.edu/prove/prove2_3.html> if $n < 9,080,191 is a both a base-
sub is_prime($) {
my $n = shift;
- $n = GMP->new($n);
+ $n = GMP->new("$n");
if (Rmpz_cmp_ui($n, 2) == -1) {
return 0;
View
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use Math::Primality qw/:all/;
+use bigint;
+
+plan;
+
+ok(is_prime(18446744073709551629), 'is_prime() works with bigint');
+ok(is_pseudoprime(18446744073709551629), 'is_pseudoprime() works with bigint');
+ok(is_pseudoprime(18446744073709551629, 3), 'is_pseudoprime(x,3) works with bigint');
+
+ok(is_strong_lucas_pseudoprime(18446744073709551629), 'is_strong_lucas_pseudoprime() works with bigint');
+
+done_testing;

0 comments on commit a837671

Please sign in to comment.