Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge changes from upstream

  • Loading branch information...
commit ad5bd9dfc8c63e6acec5cd6172be391aea78e47c 2 parents 6c977fd + 3bba000
@danaj danaj authored
Showing with 15 additions and 2 deletions.
  1. +15 −2 lib/Math/Primality.pm
View
17 lib/Math/Primality.pm
@@ -506,12 +506,25 @@ sub is_prime($) {
my $n = shift;
$n = GMP->new("$n") unless ref($n) eq 'Math::GMPz';
+ # is it less than 2?
return 0 if Rmpz_cmp_ui($n, 2) == -1;
- return _is_small_prime($n) if Rmpz_cmp_ui($n, 257) == -1;
+
+ # is it a small prime? (<=257)
+ return _is_small_prime($n) if Rmpz_cmp_ui($n, 257) <= 0;
+
# Divisible by 2?
return 0 if Rmpz_even_p($n);
- # 4127218095 = 3*5*7*11*13*17*19*23*37, 3948078067 = 29*31*41*43*47*53
+
+ # The following two gci_ui calls detect if the first few
+ # small primes are factors, which is a very quick test
+ # to filter out numbers with small factors
+
+ # On 64bit machines, we could use a single GCD with 16294579238595022365
+ # but to work on 32bit machines, we split that into 4127218095 * 3948078067
+ # 4127218095 = 3*5*7*11*13*17*19*23*37
return 0 unless 1 == Rmpz_gcd_ui($Math::GMPz::NULL, $n, 4127218095);
+
+ # 3948078067 = 29*31*41*43*47*53
return 0 unless 1 == Rmpz_gcd_ui($Math::GMPz::NULL, $n, 3948078067);
if ( Rmpz_cmp_ui($n, 9_080_191) == -1 ) {
Please sign in to comment.
Something went wrong with that request. Please try again.