-
Notifications
You must be signed in to change notification settings - Fork 0
/
620_from_big_prime_list_3.pl
142 lines (109 loc) · 3.72 KB
/
620_from_big_prime_list_3.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 07 October 2018
# https://github.com/trizen
# A simple algorithm for generating a subset of strong-Lucas pseudoprimes.
# See also:
# https://oeis.org/A217120 -- Lucas pseudoprimes
# https://oeis.org/A217255 -- Strong Lucas pseudoprimes
# https://oeis.org/A177745 -- Semiprimes n such that n divides Fibonacci(n+1).
# https://oeis.org/A212423 -- Frobenius pseudoprimes == 2,3 (mod 5) with respect to Fibonacci polynomial x^2 - x - 1.
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::GMPz;
use ntheory qw(forcomb forprimes is_strong_lucas_pseudoprime random_prime);
use Math::Prime::Util::GMP qw(vecprod powmod divisors kronecker lucas_sequence);
use List::Util qw(uniq);
sub fibonacci_pseudoprimes ($limit, $callback) {
my %common_divisors;
#~ my $r = random_prime(1e8);
#~ my $r2 = random_prime(1e9);
#~ die 'error' if $r <= 1e7;
#~ die 'error' if $r2+1e7 <= $r;
while (<>) {
my $p_str = (split(' ', $_))[-1] || next;
my $p = Math::GMPz->new($p_str);
#Math::GMPz::Rmpz_kronecker_ui($p, 5) == -1 or next;
say "Processing $p_str...";
Math::GMPz::Rmpz_add_ui($p, $p, 1);
foreach my $d (divisors(Math::GMPz::Rmpz_get_str($p, 10))) {
if ((lucas_sequence($p_str, 1, -1, $d))[0] == 0) {
push @{$common_divisors{$d}}, $p_str;
last;
}
}
}
#~ forprimes {
#~ my $p = $_;
#~ foreach my $d (divisors($p - kronecker($p, 5))) {
#~ if ((lucas_sequence($p, 1, -1, $d))[0] == 0) {
#~ push @{$common_divisors{$d}}, $p;
#~ }
#~ }
#~ } 1e7;
#~ forprimes {
#~ my $p = $_;
#~ foreach my $d (divisors($p - kronecker($p, 5))) {
#~ if ((lucas_sequence($p, 1, -1, $d))[0] == 0) {
#~ push @{$common_divisors{$d}}, $p;
#~ }
#~ }
#~ } $r, $r+1e7;
#~ forprimes {
#~ my $p = $_;
#~ foreach my $d (divisors($p - kronecker($p, 5))) {
#~ if ((lucas_sequence($p, 1, -1, $d))[0] == 0) {
#~ push @{$common_divisors{$d}}, $p;
#~ }
#~ }
#~ } $r2, $r2+1e7;
my %seen;
foreach my $arr (values %common_divisors) {
#@$arr = uniq(@$arr);
my $l = scalar(@$arr); #$#{$arr} + 1;
foreach my $k (2 .. $l) {
forcomb {
my $nstr = vecprod(@{$arr}[@_]);
$callback->(Math::GMPz->new($nstr), @{$arr}[@_]) if !$seen{$nstr}++;
} $l, $k;
}
}
}
sub PSW_primality_test ($n) {
# Find P such that kronecker(n, P^2 + 4) = -1.
my $P;
for (my $k = 1 ; ; ++$k) {
if (kronecker($n, $k * $k + 4) == -1) {
$P = $k;
last;
}
}
# If LucasU(P, -1, n+1) = 0 (mod n), then n is probably prime.
(lucas_sequence($n, $P, -1, $n + 1))[0] == 0;
}
fibonacci_pseudoprimes(
10_000,
sub ($n, @f) {
if (is_strong_lucas_pseudoprime($n)) {
say "Lucas pseudoprime: $n";
if (powmod(2, $n-1, $n) == 1) {
die "Found a BPSW counter-example: $n = prod(@f)";
}
}
if (powmod(2, $n-1, $n) == 1) {
say "Fermat pseudoprime: $n";
if (PSW_primality_test($n)) {
die "PSW counter-example: $n = prod(@f)";
}
if (kronecker($n, 5) == -1) {
die "Found a Fibonacci special number: $n = prod(@f)";
}
}
#~ if (kronecker($n, 5) == -1) {
#~ if (powmod(2, $n-1, $n) == 1) {
#~ die "Found a Fibonacci special number: $n = prod(@f)";
#~ }
#~ }
}
);