-
Notifications
You must be signed in to change notification settings - Fork 0
/
moebius_transform.sf
37 lines (27 loc) · 1.48 KB
/
moebius_transform.sf
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
#!/usr/bin/ruby
# Möbius inversion formula.
# See also:
# https://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula
func moebius_transform(n, f={.sigma}) {
n.divisor_sum {|d|
moebius(d) * f(n/d)
}
}
func inverse_moebius_transform(n, f={.sigma}) {
n.divisor_sum {|d|
f(d)
}
}
say "=> Möbius transform of Jordan's J_2(k) totient function:"
say 20.of { moebius_transform(_, { .jordan_totient(2) }) } # Möbius transform applied once to the Jordan J_2(k) function.
say 20.of { moebius_transform(_, { moebius_transform(_, { _**2 }) }) } # Möbius transform applied twice to squares.
say "\n=> Inverse Möbius transform of sigma_2(k) function:"
say 20.of { inverse_moebius_transform(_, { .sigma(2) }) } # Inverse Möbius transform applied once to the sigma_2(k) function.
say 20.of { inverse_moebius_transform(_, { inverse_moebius_transform(_, { _**2 }) }) } # Inverse Möbius transform applied twice to squares.
__END__
=> Möbius transform of Jordan's J_2(k) totient function:
[0, 1, 2, 7, 9, 23, 14, 47, 36, 64, 46, 119, 63, 167, 94, 161, 144, 287, 128, 359]
[0, 1, 2, 7, 9, 23, 14, 47, 36, 64, 46, 119, 63, 167, 94, 161, 144, 287, 128, 359]
=> Inverse Möbius transform of sigma_2(k) function:
[0, 1, 6, 11, 27, 27, 66, 51, 112, 102, 162, 123, 297, 171, 306, 297, 453, 291, 612, 363]
[0, 1, 6, 11, 27, 27, 66, 51, 112, 102, 162, 123, 297, 171, 306, 297, 453, 291, 612, 363]