-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherdos_special_carmichael_with_multiple.pl
238 lines (182 loc) · 6.4 KB
/
erdos_special_carmichael_with_multiple.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/perl
# Erdos construction method for Carmichael numbers:
# 1. Choose an even integer L with many prime factors.
# 2. Let P be the set of primes d+1, where d|L and d+1 does not divide L.
# 3. Find a subset S of P such that prod(S) == 1 (mod L). Then prod(S) is a Carmichael number.
# Alternatively:
# 3. Find a subset S of P such that prod(S) == prod(P) (mod L). Then prod(P) / prod(S) is a Carmichael number.
use 5.020;
use warnings;
use ntheory qw(:all);
use List::Util qw(shuffle);
use experimental qw(signatures);
# Modular product of a list of integers
sub vecprodmod ($arr, $mod) {
#~ if ($mod > ~0) {
#~ my $prod = Math::GMPz->new(1);
#~ foreach my $k(@$arr) {
#~ $prod = ($prod * $k) % $mod;
#~ }
#~ return $prod;
#~ }
if ($mod < ~0) {
my $prod = 1;
foreach my $k(@$arr) {
$prod = mulmod($prod, $k, $mod);
}
return $prod;
}
my $prod = 1;
foreach my $k (@$arr) {
$prod = Math::Prime::Util::GMP::mulmod($prod, $k, $mod);
}
#Math::GMPz->new($prod);
Math::GMPz::Rmpz_init_set_str($prod, 10);
}
# Primes p such that p-1 divides L and p does not divide L
sub lambda_primes ($L) {
#grep { ($L % $_) != 0 } grep { $_ > 2 and is_prime($_) } map { $_ + 1 } divisors($L);
grep { ($_ > 2) and (($L % $_) != 0) and is_prime($_) } map { ($_ >= ~0) ? (Math::GMPz->new($_)+1) : ($_ + 1) } divisors($L);
}
my @prefix = (3, 5, 17, 23, 29, 53, 83, 89, 113, 197, 257, 353, 449, 617, 1409, 2003, 2297, 2549, 3137, 3329, 4019);
my $prefix_prod = Math::GMPz->new(vecprod(@prefix));
sub method_1 ($L) { # smallest numbers first
(vecall { ($L % ($_-1)) == 0 } @prefix) or return;
my @P = lambda_primes($L);
@P = grep {
(($prefix_prod % $_) == 0)
? 1
: Math::Prime::Util::GMP::gcd(Math::Prime::Util::GMP::totient(Math::Prime::Util::GMP::mulint($prefix_prod, $_)), Math::Prime::Util::GMP::mulint($prefix_prod, $_)) eq '1'
} @P;
# Collect only primes p such that gcd(k*p, DedekindPsi(k*p)) = 1
@P = grep {
gcd(mulint($prefix_prod, $_), addint($_,1)) == 1
} @P;
#vecprodmod(@P, 3*5*17*23) == 0 or return;
#vecprodmod(\@P, 3*5*17*23*29) == 0 or return;
if (@prefix) {
$prefix_prod = gcd($prefix_prod, vecprod(@P));
if ($prefix_prod > ~0) {
$prefix_prod = Math::GMPz->new($prefix_prod);
}
}
#return if (vecprod(@P) < ~0);
my $lucas_L = lcm(map{ addint($_,1) } factor($prefix_prod));
# Prime p must not divide the Lucas lambda value
@P = grep{ ($lucas_L % $_) != 0} @P;
#@P = grep{ ($lucas_L % addint($_,1)) == 0} @P;
# Keep only primes p such that p+1 is B-smooth
my $max_p = vecmax(map{ factor($_-1) } @P);
@P = grep{ is_smooth(addint($_, 1), $max_p) } @P;
if (@prefix) {
$prefix_prod = gcd($prefix_prod, vecprod(@P));
if ($prefix_prod > ~0) {
$prefix_prod = Math::GMPz->new($prefix_prod);
}
}
#@P = grep { $_ > $prefix[-1] } @P;
@P = grep { gcd($prefix_prod, $_) == 1 } @P;
if (@P) {
say "# Testing: $L -- ", scalar(@P);
}
else {
return;
}
my $n = scalar(@P);
my @orig = @P;
my $max = 1e4;
my $max_k = scalar(@P);
my $L_rem = invmod($prefix_prod, $L);
foreach my $k (1 .. @P) {
#next if (binomial($n, $k) > 1e6);
next if ($k > $max_k);
@P = @orig;
my $count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $L_rem) {
say vecprod(@P[@_], $prefix_prod);
}
lastfor if (++$count > $max);
} $n, $k;
next if (binomial($n, $k) < $max);
@P = reverse(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $L_rem) {
say vecprod(@P[@_], $prefix_prod);
}
lastfor if (++$count > $max);
} $n, $k;
@P = shuffle(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $L_rem) {
say vecprod(@P[@_], $prefix_prod);
}
lastfor if (++$count > $max);
} $n, $k;
}
my $B = vecprodmod([@P, $prefix_prod], $L);
my $T = Math::GMPz->new(Math::Prime::Util::GMP::vecprod(@P));
foreach my $k (1 .. @P) {
#next if (binomial($n, $k) > 1e6);
last if ($k > $max_k);
@P = @orig;
my $count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $B) {
my $S = Math::GMPz->new(Math::Prime::Util::GMP::vecprod(@P[@_]));
say vecprod($prefix_prod, $T / $S) if ($T != $S);
}
lastfor if (++$count > $max);
} $n, $k;
next if (binomial($n, $k) < $max);
@P = reverse(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $B) {
my $S = Math::GMPz->new(Math::Prime::Util::GMP::vecprod(@P[@_]));
say vecprod($prefix_prod, $T / $S) if ($T != $S);
}
lastfor if (++$count > $max);
} $n, $k;
@P = shuffle(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $B) {
my $S = Math::GMPz->new(Math::Prime::Util::GMP::vecprod(@P[@_]));
say vecprod($prefix_prod, $T / $S) if ($T != $S);
}
lastfor if (++$count > $max);
} $n, $k;
}
}
use Math::GMPz;
my %seen;
# https://oeis.org/A350340
my @arr = qw(
35 595 13685 506345 26836285 1702480351 80016576497 5681176931287 414725915983951 40228413850443247 4304440281997427429 546663915813673283483 75986284298100586404137 10144780646398552482233711 1511572316313384319852822939 252432576824335181415421430813 49729217634394030738838021870161
);
while (<>) {
next if /^\h*#/;
/\S/ or next;
my $n = (split(' ', $_))[-1];
$n || next;
$n =~ /^[0-9]+\z/ || next;
#$n < ~0 or next;
length($n) <= 45 or next;
@prefix = factor($n);
@prefix = grep { gcd($n, subint($_,1)) == 1 } @prefix;
@prefix = grep { gcd($n, addint($_,1)) == 1 } @prefix;
#$#prefix = 20;
$prefix_prod = vecprod(@prefix);
if ($prefix_prod > ~0) {
$prefix_prod = Math::GMPz->new($prefix_prod);
}
my $L = carmichael_lambda($prefix_prod);
if ($L > ~0) {
$L = Math::GMPz->new("$L");
}
next if $seen{$L}++;
method_1($L);
}