-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverse_phi.pl
342 lines (250 loc) · 6.7 KB
/
inverse_phi.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
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
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl
# Numbers n such that phi(n) divides n+1, where phi is Euler's totient function (A000010).
# https://oeis.org/A203966
# Related sequence:
# https://oeis.org/A050474
# See also:
# https://projecteuclid.org/journals/bulletin-of-the-american-mathematical-society/volume-38/issue-10/On-Eulers-totient-function/bams/1183496203.pdf
# Almost a term:
# 48901526933832864378258473353215
use utf8;
use 5.020;
use strict;
use warnings;
use Math::GMPz;
use ntheory qw(:all);
use experimental qw(signatures);
sub dynamicPreimage ($N, $L, %opt) {
my %r = (1 => [1]);
foreach my $l (@$L) {
my %t;
foreach my $pair (@$l) {
my ($x, $y) = @$pair;
foreach my $d (divisors(divint($N, $x))) {
if (exists $r{$d}) {
my @list = @{$r{$d}};
if ($opt{unitary}) {
@list = grep { gcd($_, $y) == 1 } @list;
}
push @{$t{mulint($x, $d)}}, map { mulint($_, $y) } @list;
}
}
}
while (my ($k, $v) = each %t) {
push @{$r{$k}}, @$v;
}
}
return if not exists $r{$N};
sort { $a <=> $b } @{$r{$N}};
}
sub dynamicLen ($N, $L) {
my %r = (1 => 1);
foreach my $l (@$L) {
my %t;
foreach my $pair (@$l) {
my ($x, $y) = @$pair;
foreach my $d (divisors(divint($N, $x))) {
if (exists $r{$d}) {
$t{mulint($x, $d)} += $r{$d};
}
}
}
while (my ($k, $v) = each %t) {
$r{$k} += $v;
}
}
$r{$N} // 0;
}
sub dynamicMin ($N, $L) {
my %r = (1 => 1);
foreach my $l (@$L) {
my %t;
foreach my $pair (@$l) {
my ($x, $y) = @$pair;
foreach my $d (divisors(divint($N, $x))) {
if (exists $r{$d}) {
my $k = mulint($x, $d);
my $v = $r{$d} * $y;
if (not defined($t{$k})) {
$t{$k} = $v;
}
else {
$t{$k} = $v if ($v < $t{$k});
}
}
}
}
while (my ($k, $v) = each %t) {
if (not defined($r{$k})) {
$r{$k} = $v;
}
else {
$r{$k} = $v if ($v < $r{$k});
}
}
}
$r{$N};
}
sub dynamicMax ($N, $L) {
my %r = (1 => 1);
foreach my $l (@$L) {
my %t;
foreach my $pair (@$l) {
my ($x, $y) = @$pair;
foreach my $d (divisors(divint($N, $x))) {
if (exists $r{$d}) {
my $k = mulint($x, $d);
my $v = $r{$d} * $y;
if (not defined($t{$k})) {
$t{$k} = $v;
}
else {
$t{$k} = $v if ($v > $t{$k});
}
}
}
}
while (my ($k, $v) = each %t) {
if (not defined($r{$k})) {
$r{$k} = $v;
}
else {
$r{$k} = $v if ($v > $r{$k});
}
}
}
$r{$N};
}
sub cook_sigma ($N, $k) {
my %L;
foreach my $d (divisors($N)) {
next if ($d == 1);
foreach my $p (map { $_->[0] } factor_exp(subint($d, 1))) {
my $q = addint(mulint($d, subint(powint($p, $k), 1)), 1);
my $t = valuation($q, $p);
next if ($t <= $k or ($t % $k) or $q != powint($p, $t));
push @{$L{$p}}, [$d, powint($p, subint(divint($t, $k), 1))];
}
}
[values %L];
}
sub cook_phi ($N) {
my %L;
foreach my $d (divisors($N)) {
my $p = addint($d, 1);
is_prime($p) || next;
my $v = valuation($N, $p);
push @{$L{$p}}, map { [mulint($d, powint($p, $_ - 1)), powint($p, $_)] } 1 .. $v + 1;
}
[values %L];
}
sub cook_psi ($N) {
my %L;
foreach my $d (divisors($N)) {
my $p = subint($d, 1);
is_prime($p) || next;
my $v = valuation($N, $p);
push @{$L{$p}}, map { [mulint($d, powint($p, $_ - 1)), powint($p, $_)] } 1 .. $v + 1;
}
[values %L];
}
sub cook_usigma ($N) {
my @list;
foreach my $d (divisors($N)) {
if (is_prime_power(subint($d, 1))) {
push @list, [[$d, subint($d, 1)]];
}
}
return \@list;
}
sub cook_uphi ($N) {
my @list;
foreach my $d (divisors($N)) {
if (is_prime_power(addint($d, 1))) {
push @list, [[$d, addint($d, 1)]];
}
}
return \@list;
}
# Inverse of sigma function
sub inverse_sigma ($N, $k = 1) {
dynamicPreimage($N, cook_sigma($N, $k));
}
sub inverse_sigma_min ($N, $k = 1) {
dynamicMin($N, cook_sigma($N, $k));
}
sub inverse_sigma_max ($N, $k = 1) {
dynamicMax($N, cook_sigma($N, $k));
}
sub inverse_sigma_len ($N, $k = 1) {
dynamicLen($N, cook_sigma($N, $k));
}
# Inverse of Euler phi function
sub inverse_phi ($N) {
dynamicPreimage($N, cook_phi($N));
}
sub inverse_phi_min ($N) {
dynamicMin($N, cook_phi($N));
}
sub inverse_phi_max ($N) {
dynamicMax($N, cook_phi($N));
}
sub inverse_phi_len ($N) {
dynamicLen($N, cook_phi($N));
}
# Inverse of Dedekind psi function
sub inverse_psi ($N) {
dynamicPreimage($N, cook_psi($N));
}
sub inverse_psi_min ($N) {
dynamicMin($N, cook_psi($N));
}
sub inverse_psi_max ($N) {
dynamicMax($N, cook_psi($N));
}
sub inverse_psi_len ($N) {
dynamicLen($N, cook_psi($N));
}
# Inverse of unitary sigma function
sub inverse_usigma ($N) {
dynamicPreimage($N, cook_usigma($N), unitary => 1);
}
# Inverse of unitary phi function
sub inverse_uphi ($N) {
dynamicPreimage($N, cook_uphi($N), unitary => 1);
}
sub check_valuation ($n, $p) {
if ($p == 2) {
return valuation($n, $p) < 63;
}
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;
}
foreach my $p (@{primes(30, 500)}) {
my $h = smooth_numbers(~0, [2, 11, 29, $p]);
say "Terms: ", scalar(@$h);
foreach my $phi (@$h) {
valuation($phi, 2) >= 35 or next;
$phi % 2 == 0 or next;
$phi % 11 == 0 or next;
$phi % 29 == 0 or next;
$phi % $p == 0 or next;
foreach my $n (inverse_phi($phi)) {
if (modint(addint($n, 1), $phi) == 0) {
say $n;
}
}
}
}