-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_fermat_superpseudoprimes.pl
74 lines (53 loc) · 1.65 KB
/
generate_fermat_superpseudoprimes.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 28 January 2019
# https://github.com/trizen
# A new algorithm for generating Fermat overpseudoprimes to any given base.
# See also:
# https://oeis.org/A141232 -- Overpseudoprimes to base 2: composite k such that k = A137576((k-1)/2).
# See also:
# https://en.wikipedia.org/wiki/Fermat_pseudoprime
# https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::AnyNum qw(prod);
use ntheory qw(:all);
sub fermat_overpseudoprimes ($base, $prime_limit, $callback) {
my %common_divisors;
say ":: Sieving...";
forprimes {
my $p = $_;
my $z = znorder($base, $p);
foreach my $d(divisors($p-1)) {
$d % $z == 0 or next;
if ($p < 3e7 or exists($common_divisors{$d})) {
push @{$common_divisors{$d}}, $p;
}
}
} 3, $prime_limit;
say ":: Done sieving...";
foreach my $arr (values %common_divisors) {
my $l = scalar(@$arr);
foreach my $k (5 .. $l) {
forcomb {
my $n = prod(@{$arr}[@_]);
$callback->($n);
} $l, $k;
}
}
}
sub is_fibonacci_pseudoprime ($n) {
(lucas_sequence($n, 1, -1, $n - kronecker($n, 5)))[0] == 0;
}
my $base = 2; # generate overpseudoprime to this base
my $prime_limit = 1e9; # sieve primes up to this limit
open my $fh, '>', 'file9.txt';
fermat_overpseudoprimes(
$base, # base
$prime_limit, # prime limit
sub ($n) {
say $n;
say $fh $n;
}
);