-
Notifications
You must be signed in to change notification settings - Fork 7
/
516 5-smooth totients -- v2.pl
75 lines (55 loc) · 1.58 KB
/
516 5-smooth totients -- v2.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 03 October 2017
# https://github.com/trizen
# https://projecteuler.net/problem=516
# Runtime: ~9 minutes.
# Based on Dana Jacobsen's code from Math::Prime::Util,
# which in turn is based on invphi.gp v1.3 by Max Alekseyev.
# See also:
# https://en.wikipedia.org/wiki/Euler%27s_totient_function
# https://github.com/danaj/Math-Prime-Util/blob/master/examples/inverse_totient.pl
use 5.010;
use strict;
use warnings;
use ntheory qw(is_prime divisors valuation addmod);
use constant LIMIT => 1e12;
use constant MOD => 2**32;
sub inverse_euler_phi {
my ($n) = @_;
my %r = (1 => [1]);
foreach my $d (divisors($n)) {
if (is_prime($d + 1)) {
my %temp;
foreach my $k (1 .. (valuation($n, $d + 1) + 1)) {
my $u = $d * ($d + 1)**($k - 1);
my $v = ($d + 1)**$k;
foreach my $f (divisors($n / $u)) {
if (exists $r{$f}) {
push @{$temp{$f * $u}}, grep { $_ <= LIMIT } map { $v * $_ } @{$r{$f}};
}
}
}
while (my ($i, $v) = each(%temp)) {
push @{$r{$i}}, @$v;
}
}
}
return if not exists $r{$n};
return @{$r{$n}};
}
my @smooth = (1);
foreach my $n (2, 3, 5) {
foreach my $k (@smooth) {
if ($n * $k <= LIMIT) {
push @smooth, $n * $k;
}
}
}
my $sum = 0;
foreach my $k (@smooth) {
foreach my $n (inverse_euler_phi($k)) {
$sum = addmod($sum, $n, MOD);
}
}
say $sum;