Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,32 @@
|
|||
use bigint try => 'GMP';
|
||||
|
||||
sub is_prime {
|
||||
my ($n, $k) = @_;
|
||||
return 1 if $n == 2;
|
||||
return 0 if $n < 2 or $n % 2 == 0;
|
||||
|
||||
my $d = $n - 1;
|
||||
my $s = 0;
|
||||
|
||||
while (!($d % 2)) {
|
||||
$d /= 2;
|
||||
$s++;
|
||||
}
|
||||
|
||||
LOOP: for (1 .. $k) {
|
||||
my $a = 2 + int(rand($n - 2));
|
||||
|
||||
my $x = $a->bmodpow($d, $n);
|
||||
next if $x == 1 or $x == $n - 1;
|
||||
|
||||
for (1 .. $s - 1) {
|
||||
$x = ($x * $x) % $n;
|
||||
return 0 if $x == 1;
|
||||
next LOOP if $x == $n - 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
print join ", ", grep { is_prime $_, 10 } (1 .. 1000);
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
use ntheory qw/is_strong_pseudoprime miller_rabin_random/;
|
||||
sub is_prime_mr {
|
||||
my $n = shift;
|
||||
# If 32-bit, we can do this with 3 bases.
|
||||
return is_strong_pseudoprime($n, 2, 7, 61) if ($n >> 32) == 0;
|
||||
# If 64-bit, 7 is all we need.
|
||||
return is_strong_pseudoprime($n, 2, 325, 9375, 28178, 450775, 9780504, 1795265022) if ($n >> 64) == 0;
|
||||
# Otherwise, perform a number of random base tests, and the result is a probable prime test.
|
||||
return miller_rabin_random($n, 20);
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
use Math::Primality qw/is_strong_pseudoprime/;
|
||||
sub is_prime_mr {
|
||||
my $n = shift;
|
||||
return 0 if $n < 2;
|
||||
for (2,3,5,7,11,13,17,19,23,29,31,37) {
|
||||
return 0 unless $n <= $_ || is_strong_pseudoprime($n,$_);
|
||||
}
|
||||
1;
|
||||
}
|
||||
for (1..100) { say if is_prime_mr($_) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue