moebius_transform.sf 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/ruby
  2. # Möbius inversion formula.
  3. # See also:
  4. # https://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula
  5. func moebius_transform(n, f={.sigma}) {
  6. n.divisor_sum {|d|
  7. moebius(d) * f(n/d)
  8. }
  9. }
  10. func inverse_moebius_transform(n, f={.sigma}) {
  11. n.divisor_sum {|d|
  12. f(d)
  13. }
  14. }
  15. say "=> Möbius transform of Jordan's J_2(k) totient function:"
  16. say 20.of { moebius_transform(_, { .jordan_totient(2) }) } # Möbius transform applied once to the Jordan J_2(k) function.
  17. say 20.of { moebius_transform(_, { moebius_transform(_, { _**2 }) }) } # Möbius transform applied twice to squares.
  18. say "\n=> Inverse Möbius transform of sigma_2(k) function:"
  19. say 20.of { inverse_moebius_transform(_, { .sigma(2) }) } # Inverse Möbius transform applied once to the sigma_2(k) function.
  20. say 20.of { inverse_moebius_transform(_, { inverse_moebius_transform(_, { _**2 }) }) } # Inverse Möbius transform applied twice to squares.
  21. __END__
  22. => Möbius transform of Jordan's J_2(k) totient function:
  23. [0, 1, 2, 7, 9, 23, 14, 47, 36, 64, 46, 119, 63, 167, 94, 161, 144, 287, 128, 359]
  24. [0, 1, 2, 7, 9, 23, 14, 47, 36, 64, 46, 119, 63, 167, 94, 161, 144, 287, 128, 359]
  25. => Inverse Möbius transform of sigma_2(k) function:
  26. [0, 1, 6, 11, 27, 27, 66, 51, 112, 102, 162, 123, 297, 171, 306, 297, 453, 291, 612, 363]
  27. [0, 1, 6, 11, 27, 27, 66, 51, 112, 102, 162, 123, 297, 171, 306, 297, 453, 291, 612, 363]