-
Notifications
You must be signed in to change notification settings - Fork 7
/
248 Numbers for which Euler s totient function equals 13.pl
57 lines (40 loc) · 1.38 KB
/
248 Numbers for which Euler s totient function equals 13.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# 29 September 2017
# https://github.com/trizen
# https://projecteuler.net/problem=248
# Runtime: 0.788s
use 5.010;
use strict;
use warnings;
use ntheory qw(is_prime divisors valuation factorial);
# Given a number `n`, the algorithm finds all the numbers such that for each number `k` in the list, φ(k) = n.
# 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
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}}, map { $v * $_ } @{$r{$f}};
}
}
}
while (my ($i, $v) = each(%temp)) {
push @{$r{$i}}, @$v;
}
}
}
return if not exists $r{$n};
return sort { $a <=> $b } @{$r{$n}};
}
say((inverse_euler_phi(factorial(13)))[150_000 - 1]);