12345678910111213141516171819202122 |
- #!/usr/bin/ruby
- # Dirichlet convolution of two functions.
- # See also:
- # https://oeis.org/wiki/Dirichlet_convolution
- # https://en.wikipedia.org/wiki/Dirichlet_convolution
- func dirichlet_convolution(n, f, g) {
- n.divisor_sum {|d|
- f(d) * g(n/d)
- }
- }
- # Example:
- # Dirichlet convolution of sigma(n) (A000203) with phi(n) (A000010).
- var f = { .sigma }
- var g = { .euler_phi }
- say 20.of {|n| dirichlet_convolution(n, f, g) } # https://oeis.org/A038040
|