-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pseudoprimes.pl
57 lines (43 loc) · 1015 Bytes
/
generate_pseudoprimes.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::AnyNum qw(prod);
use ntheory qw(:all);
sub fermat_pseudoprimes ($limit, $callback) {
my %common_divisors;
forprimes {
my $p = $_;
foreach my $d (divisors($p - 1)) {
if (powmod(2, $d, $p) == 1) {
push @{$common_divisors{$d}}, $p;
}
}
} $limit;
my %seen;
foreach my $arr (values %common_divisors) {
my $l = $#{$arr} + 1;
foreach my $k (2 .. $l) {
forcomb {
my $n = prod(@{$arr}[@_]);
$callback->($n) if !$seen{$n}++;
} $l, $k;
}
}
}
my @pseudoprimes;
fermat_pseudoprimes(
1e5,
sub ($n) {
my $t = "$n";
if ($t > ~0) {
say $t;
}
if ($t eq reverse($t)) {
if ($t > 1037998220228997301) {
die "New term: $t";
}
say "Palindrome: $t";
}
}
);