-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_carmichael_of_second_order.pl
226 lines (169 loc) · 5.05 KB
/
generate_carmichael_of_second_order.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
#!/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.
# The sequence of Carmichael numbers of order 2:
# 443372888629441, 39671149333495681, 842526563598720001, 2380296518909971201, 3188618003602886401, ...
# OEIS sequence:
# https://oeis.org/A175531
use 5.020;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures);
use Math::GMPz;
#use Math::AnyNum qw(:overload);
# Modular product of a list of integers
sub vecprodmod ($arr, $mod) {
my $prod = 1;
foreach my $k (@$arr) {
$prod = mulmod($prod, $k, $mod);
}
$prod;
}
# Primes p such that p-1 divides L and p does not divide L
sub lambda_primes ($L) {
grep { $L % $_ != 0 } grep { $_ > 2 } map { sqrtint($_) } grep { is_square($_) && is_prime(sqrtint($_)) } map { $_ + 1 } divisors($L);
#grep { $L % $_ != 0 } grep { $_ > 2 and is_prime($_) } map { $_ + 1 } divisors($L);
}
sub method_1 ($L) { # smallest numbers first
my @P = lambda_primes($L);
foreach my $k (3 .. @P) {
forcomb {
if (vecprodmod([@P[@_]], $L) == 1) {
say vecprod(@P[@_]);
}
} scalar(@P), $k;
}
}
#~ sub method_2 ($L) { # largest numbers first
#~ my @P = lambda_primes($L);
#~ my $B = vecprodmod(\@P, $L);
#~ my $T = vecprod(@P);
#~ #say "@P";
#~ foreach my $k (1 .. (@P-3)) {
#~ #say "Testing: $k -- ", binomial(scalar(@P), $k);
#~ my $count = 0;
#~ forcomb {
#~ if (vecprodmod([@P[@_]], $L) == $B) {
#~ my $S = vecprod(@P[@_]);
#~ say ($T / $S) if ($T != $S);
#~ }
#~ lastfor if (++$count > 1e6);
#~ } scalar(@P), $k;
#~ }
#~ }
sub method_2($L) {
my @P = lambda_primes($L);
return if (vecprod(@P) < ~0);
my $n = scalar(@P);
my @orig = @P;
my $max = 1e5;
my $max_k = 10;
foreach my $k (3 .. @P>>1) {
#next if (binomial($n, $k) > 1e6);
next if ($k > $max_k);
@P = @orig;
my $count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == 1) {
say vecprod(@P[@_]);
}
lastfor if (++$count > $max);
} $n, $k;
next if (binomial($n, $k) < $max);
@P = reverse(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == 1) {
say vecprod(@P[@_]);
}
lastfor if (++$count > $max);
} $n, $k;
@P = shuffle(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == 1) {
say vecprod(@P[@_]);
}
lastfor if (++$count > $max);
} $n, $k;
}
my $B = Math::GMPz->new(vecprodmod(\@P, $L));
my $T = Math::GMPz->new(vecprod(@P));
foreach my $k (1 .. @P>>1) {
#next if (binomial($n, $k) > 1e6);
last if ($k > $max_k);
@P = @orig;
my $count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $B) {
my $S = vecprod(@P[@_]);
say ($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 = vecprod(@P[@_]);
say ($T / $S) if ($T != $S);
}
lastfor if (++$count > $max);
} $n, $k;
@P = shuffle(@P);
$count = 0;
forcomb {
if (vecprodmod([@P[@_]], $L) == $B) {
my $S = vecprod(@P[@_]);
say ($T / $S) if ($T != $S);
}
lastfor if (++$count > $max);
} $n, $k;
}
}
sub check_valuation ($n, $p) {
if ($p == 2) {
return valuation($n, $p) < 11;
}
if ($p == 3) {
return valuation($n, $p) < 5;
}
if ($p == 5) {
return valuation($n, $p) < 3;
}
if ($p == 7) {
return valuation($n, $p) < 3;
}
if ($p == 11) {
return valuation($n, $p) < 2;
}
($n % $p) != 0;
}
sub smooth_numbers ($limit, $primes) {
my @h = (1);
foreach my $p (@$primes) {
say "Prime: $p";
foreach my $n (@h) {
if ($n * $p <= $limit and check_valuation($n, $p)) {
push @h, $n * $p;
}
}
}
return \@h;
}
my $h = smooth_numbers(10**10, [2, 3, 5, 7, 11, 13, 19, 31, 83]);
say "\nFound: ", scalar(@$h), " terms";
my %table;
foreach my $n (@$h) {
valuation($n, 2) >= 6 or next;
valuation($n, 3) >= 2 or next;
valuation($n, 5) >= 1 or next;
valuation($n, 7) >= 1 or next;
#say "Generating: $n";
method_2($n);
}