-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdickson_linear_forms_prime_sieve_in_range_2.pl
More file actions
332 lines (246 loc) · 10.1 KB
/
dickson_linear_forms_prime_sieve_in_range_2.pl
File metadata and controls
332 lines (246 loc) · 10.1 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/perl
# Sieve for linear forms primes of the form `a_1*m + b_1`, `a_2*m + b_2`, ..., `a_k*m + b_k`.
# See also:
# https://oeis.org/A088250
# https://oeis.org/A318646
# https://oeis.org/A372238/a372238.gp.txt
# https://en.wikipedia.org/wiki/Dickson%27s_conjecture
use utf8;
use 5.036;
use ntheory qw(:all);
use Time::HiRes qw(time);
use Test::More tests => 36;
sub remainders_mod_p($p, $terms) {
my @bad; # bad[m] = 1 means m is forbidden modulo p
foreach my $pair (@$terms) {
my ($n, $k) = @$pair;
$n %= $p;
$k %= $p;
if ($n == 0) {
# Term is constant mod p
if ($k == 0) {
# Always 0 mod p -> no admissible residue exists
return ();
}
next; # This term forbids no residue for this p
}
# Forbid the unique residue m ≡ -k * n^{-1} (mod p)
my $n_inv = invmod($n, $p);
my $m_forbid = (-$k * $n_inv) % $p;
$bad[$m_forbid] = 1;
}
return grep { !$bad[$_] } 0 .. $p - 1;
}
sub combine_crt($arr, $M, $p, $S_p) {
my @res;
my $Minv = invmod($M % $p, $p);
foreach my $r (@$arr) {
my $r_mod_p = $r % $p;
foreach my $s (@$S_p) {
push @res, (((($s - $r_mod_p) % $p) * $Minv) % $p) * $M + $r;
}
}
return \@res;
}
sub remainders_for_primes($primes) {
my $M = 1;
my $residues = [0];
foreach my $pair (@$primes) {
my ($p, $S_p) = @$pair;
# Early return if no valid residues
return ($M, []) unless @$S_p;
$residues = combine_crt($residues, $M, $p, $S_p);
$M *= $p;
}
return ($M, [sort { $a <=> $b } @$residues]);
}
sub deltas ($integers) {
my @deltas;
my $prev = 0;
foreach my $n (@$integers) {
push @deltas, $n - $prev;
$prev = $n;
}
shift(@deltas);
return \@deltas;
}
sub select_optimal_primes ($A, $B, $terms) {
my $range = $B - $A + 1;
return () if $range <= 0;
my $target_modulus = (1 + rootint($range, 5))**4;
my $M = 1;
my @primes;
for (my $p = 2 ; $M <= $target_modulus ; $p = next_prime($p)) {
my @S_p = remainders_mod_p($p, $terms);
if (scalar(@S_p) == $p) {
next; # skip trivial primes
}
push(@primes, [$p, \@S_p]);
$M *= $p;
}
return @primes;
}
sub linear_form_primes_in_range($A, $B, $terms) {
return [] if ($A > $B);
return [] if !@$terms;
# Select an optimal subset of small primes for the modular sieve
my @primes = select_optimal_primes($A, $B, $terms);
return [] unless @primes;
my ($M, $r) = remainders_for_primes(\@primes);
# If there are no admissible residues modulo M, there can be no solutions
return [] if !@$r;
# Compute differences
my @d = @{deltas($r)};
# Remove leading zeros
while (@d and $d[0] == 0) {
shift @d;
}
# Add wraparound delta
push @d, $r->[0] + $M - $r->[-1];
my $compute_small_values = 0;
my $small_values_limit = vecmin(500, $B);
my $original_A = undef;
if ($A < $small_values_limit) {
$original_A = $A;
$A = $small_values_limit + 1;
$compute_small_values = 1;
}
my $m = $r->[0];
my $d_len = scalar(@d);
my $t0 = time;
my $prev_m = $m;
# Jump near to the start of the range
$m += $M * divint($A, $M);
my $j = 0;
while ($m < $A) {
$m += $d[$j++ % $d_len];
}
my @multiples = map { $_->[0] } @$terms;
my @alphas = map { $_->[1] } @$terms;
my @range = (0 .. $#multiples);
my ($ok, @arr);
# Compute small values if needed
if ($compute_small_values) {
foreach my $k ($original_A .. $small_values_limit) {
$ok = 1;
foreach my $i (@range) {
if (!is_prime($multiples[$i] * $k + $alphas[$i])) {
$ok = 0;
last;
}
}
$ok && push @arr, $k;
}
}
while ($m <= $B) {
$ok = 1;
foreach my $k (@range) {
if (!is_prime($multiples[$k] * $m + $alphas[$k])) {
$ok = 0;
last;
}
}
if ($ok) {
push @arr, $m;
}
if ($j % 1e7 == 0 and $j > 0) {
my $tdelta = time - $t0;
say "Searching with m = $m";
say "Performance: ", (($m - $prev_m) / 1e9) / $tdelta, " * 10^9 terms per second";
$t0 = time;
$prev_m = $m;
}
$m += $d[$j++ % $d_len];
}
return \@arr;
}
is_deeply(linear_form_primes_in_range(1, 41, [[1, 41]]), [2, 6, 12, 18, 20, 26, 30, 32, 38]);
is_deeply(linear_form_primes_in_range(1, 50, [[1, 1]]), [1, 2, 4, 6, 10, 12, 16, 18, 22, 28, 30, 36, 40, 42, 46]);
is_deeply(linear_form_primes_in_range(1, 100, [[1, 1], [2, 1]]), [1, 2, 6, 18, 30, 36, 78, 96]);
is_deeply(linear_form_primes_in_range(1, 1000, [[1, 1], [2, 1], [3, 1]]), [2, 6, 36, 210, 270, 306, 330, 336, 600, 726]);
is_deeply(linear_form_primes_in_range(1, 10000, [[1, 1], [2, 1], [3, 1], [4, 1]]), [330, 1530, 3060, 4260, 4950, 6840]);
is_deeply(linear_form_primes_in_range(1, 12000, [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1]]), [10830]);
is_deeply(linear_form_primes_in_range(9538620, 9993270, [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1]]), [9538620, 9780870, 9783060, 9993270]);
is_deeply(linear_form_primes_in_range(9538620 + 1, 9993270, [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1]]), [9780870, 9783060, 9993270]);
is_deeply(linear_form_primes_in_range(1, 1000, [[1, -1], [2, -1], [3, -1]]), [4, 6, 24, 30, 84, 90, 174, 234, 240, 294, 420, 660, 954]);
is_deeply(linear_form_primes_in_range(1, 10000, [[1, -1], [2, -1], [3, -1], [4, -1]]), [6, 90, 1410, 1890]);
is_deeply(linear_form_primes_in_range(1, 500, [[2, -1], [4, -1], [6, -1]]), [2, 3, 12, 15, 42, 45, 87, 117, 120, 147, 210, 330, 477]);
is_deeply(linear_form_primes_in_range(1, 500, [[2, 1], [4, 3], [8, 7]]), [2, 5, 20, 44, 89, 179, 254, 359]);
is_deeply(linear_form_primes_in_range(1, 500, [[2, -1], [4, -1], [8, -1]]), [3, 6, 21, 45, 90, 180, 255, 360]);
is_deeply(linear_form_primes_in_range(1, 500, [[2, -1], [4, -1], [8, -1], [16, -1]]), [3, 45, 90, 180, 255]);
is_deeply(linear_form_primes_in_range(1, 500, [[17, 1], [23, 5]]), [18, 24, 66, 126, 186, 216, 378, 384, 426]);
#<<<
is_deeply(linear_form_primes_in_range(1, 500, [[17, 4], [15, -8], [19, 2]]), [5, 9, 11, 65, 75, 105, 125, 159, 191, 221, 231, 291, 341, 369, 419, 461, 471, 479]);
is_deeply(linear_form_primes_in_range(1, 500, [[17, 4], [15, +8], [19, 2]]), [5, 11, 45, 65, 105, 159, 161, 189, 221, 275, 291, 299, 431, 479]);
#>>>
sub f($n, $multiple = 1, $alpha = 1) {
my @terms = map { [$multiple * $_, $alpha] } 1 .. $n;
my $A = 1;
my $B = 2 * $A;
while (1) {
my @arr = @{linear_form_primes_in_range($A, $B, \@terms)};
if (@arr) {
return $arr[0];
}
$A = $B + 1;
$B = 2 * $A;
}
}
is_deeply([map { f($_, 1, +1) } 1 .. 8], [1, 1, 2, 330, 10830, 25410, 512820, 512820]); # A088250
is_deeply([map { f($_, 1, -1) } 1 .. 8], [3, 3, 4, 6, 6, 154770, 2894220, 2894220]); # A088651
is_deeply([map { f($_, 9, +1) } 1 .. 8], [2, 2, 4, 170, 9860, 23450, 56980, 56980]); # A372238
is_deeply([map { f($_, 2, -1) } 1 .. 8], [2, 2, 2, 3, 3, 77385, 1447110, 1447110]); # A124492
is_deeply([map { f($_, 2, +1) } 1 .. 8], [1, 1, 1, 165, 5415, 12705, 256410, 256410]); # A071576
is_deeply([map { f($_, $_, +1) } 1 .. 8], [1, 1, 2, 765, 2166, 4235, 73260, 2780085]);
is_deeply([map { f($_, $_, -1) } 1 .. 8], [3, 2, 2, 3225, 18, 25795, 413460, 7505190]);
is_deeply([map { f($_, $_, -13) } 1 .. 6], [15, 8, 6, 15, 24, 2800]);
is_deeply([map { f($_, $_, +13) } 1 .. 6], [4, 12, 10, 90, 18, 40705]);
is_deeply([map { f($_, $_, -23) } 1 .. 6], [25, 13, 10, 255, 6, 5]);
is_deeply([map { f($_, $_, +23) } 1 .. 6], [6, 9, 10, 60, 48, 13300]);
is_deeply([map { f($_, 1, +23) } 1 .. 6], [6, 18, 30, 210, 240, 79800]);
is_deeply([map { f($_, 1, -23) } 1 .. 8], [25, 26, 30, 30, 30, 30, 142380, 1319010]);
is_deeply([map { f($_, 1, +101) } 1 .. 6], [2, 6, 96, 180, 3990, 1683990]);
is_deeply([map { f($_, 1, -101) } 1 .. 6], [103, 104, 104, 240, 3630, 78540]);
is_deeply(linear_form_primes_in_range(1, 1e3, [[2, 1], [4, 1], [6, 1]]), [1, 3, 18, 105, 135, 153, 165, 168, 300, 363, 585, 618, 648, 765, 828]); # A124408
is_deeply(linear_form_primes_in_range(1, 1e4, [[2, 1], [4, 1], [6, 1], [8, 1]]), [165, 765, 1530, 2130, 2475, 3420, 5415, 7695, 9060]); # A124409
is_deeply(linear_form_primes_in_range(1, 1e5, [[2, 1], [4, 1], [6, 1], [8, 1], [10, 1]]), [5415, 12705, 13020, 44370, 82950, 98280]); # A124410
is_deeply(linear_form_primes_in_range(1, 1e6, [[2, 1], [4, 1], [6, 1], [8, 1], [10, 1], [12, 1]]), [12705, 13020, 105525, 256410, 966840]); # A124411
say "\n=> The least Chernick's \"universal form\" Carmichael number with n prime factors";
foreach my $n (3 .. 9) {
my $terms = [map { [$_, 1] } (6, 12, (map { 9 * (1 << $_) } 1 .. $n - 2))];
my $A = 1;
my $B = 2 * $A;
while (1) {
my @arr = @{linear_form_primes_in_range($A, $B, $terms)};
@arr = grep { valuation($_, 2) >= $n - 4 } @arr;
if (@arr) {
say "a($n) = $arr[0]";
last;
}
$A = $B + 1;
$B = 2 * $A;
}
}
say "\n=> Smallest number k such that r*k + 1 is prime for all r = 1 to n";
foreach my $n (1 .. 10) {
say "a($n) = ", f($n, 1, 1);
}
__END__
=> The least Chernick's "universal form" Carmichael number with n prime factors
a(3) = 1
a(4) = 1
a(5) = 380
a(6) = 380
a(7) = 780320
a(8) = 950560
=> Smallest number k such that r*k + 1 is prime for all r = 1 to n
a(1) = 1
a(2) = 1
a(3) = 2
a(4) = 330
a(5) = 10830
a(6) = 25410
a(7) = 512820
a(8) = 512820
a(9) = 12960606120
a(10) = 434491727670