dirichlet_convolution.sf 474 B

12345678910111213141516171819202122
  1. #!/usr/bin/ruby
  2. # Dirichlet convolution of two functions.
  3. # See also:
  4. # https://oeis.org/wiki/Dirichlet_convolution
  5. # https://en.wikipedia.org/wiki/Dirichlet_convolution
  6. func dirichlet_convolution(n, f, g) {
  7. n.divisor_sum {|d|
  8. f(d) * g(n/d)
  9. }
  10. }
  11. # Example:
  12. # Dirichlet convolution of sigma(n) (A000203) with phi(n) (A000010).
  13. var f = { .sigma }
  14. var g = { .euler_phi }
  15. say 20.of {|n| dirichlet_convolution(n, f, g) } # https://oeis.org/A038040