-
Notifications
You must be signed in to change notification settings - Fork 33
/
inverse_of_multiplicative_functions.pl
314 lines (230 loc) · 6.58 KB
/
inverse_of_multiplicative_functions.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
#!/usr/bin/perl
# Computing the inverse of some multiplicative functions.
# Translation of invphi.gp ver. 2.1 by Max Alekseyev.
# See also:
# https://home.gwu.edu/~maxal/gpscripts/
use utf8;
use 5.020;
use strict;
use warnings;
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 !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);
}
## Usage example
say join ', ', inverse_sigma(120); #=> [54, 56, 87, 95]
say join ', ', inverse_usigma(120); #=> [60, 87, 92, 95, 99]
say join ', ', inverse_uphi(120); #=> [121, 143, 144, 155, 164, 183, 220, 231, 240, 242, 286, 310, 366, 462]
say join ', ', inverse_phi(120); #=> [143, 155, 175, 183, 225, 231, 244, 248, 286, 308, 310, 350, 366, 372, 396, 450, 462]
say join ', ', inverse_psi(120); #=> [75, 76, 87, 95]
say join ', ', inverse_sigma(22100, 2); #=> [120, 130, 141]
say '';
say inverse_sigma_min(factorial(10)); #=> 876960
say inverse_sigma_max(factorial(10)); #=> 3624941
say inverse_sigma_len(factorial(10)); #=> 1195
say '';
say inverse_phi_min(factorial(10)); #=> 3632617
say inverse_phi_max(factorial(10)); #=> 19969950
say inverse_phi_len(factorial(10)); #=> 3802
say '';
say inverse_psi_min(factorial(10)); #=> 1160250
say inverse_psi_max(factorial(10)); #=> 3624941
say inverse_psi_len(factorial(10)); #=> 1793
say '';