partial_sums_of_jordan_totient_function_times_k_to_the_m.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 07 February 2019
  4. # https://github.com/trizen
  5. # A sublinear algorithm for computing the partial sums of the Jordan totient function times k^m.
  6. # The partial sums of the Jordan totient function is defined as:
  7. #
  8. # a(n,j,m) = Sum_{k=1..n} k^m * J_j(k)
  9. #
  10. # where J_j(k) is the Jordan totient function.
  11. # Example:
  12. # a(10^1, 2, 1) = 2431
  13. # a(10^2, 2, 1) = 21128719
  14. # a(10^3, 2, 1) = 208327305823
  15. # a(10^4, 2, 1) = 2080103011048135
  16. # a(10^5, 2, 1) = 20798025097513144783
  17. # a(10^6, 2, 1) = 207977166477794042245831
  18. # a(10^7, 2, 1) = 2079768770407248541815183631
  19. # a(10^8, 2, 1) = 20797684646417657386198683679183
  20. # a(10^9, 2, 1) = 207976843496387628847025371255443991
  21. # General asymptotic formula:
  22. #
  23. # Sum_{k=1..n} k^m * J_j(k) ~ F_(m+j)(n) / zeta(j+1).
  24. #
  25. # where F_m(n) are the Faulhaber polynomials.
  26. # OEIS sequences:
  27. # https://oeis.org/A321879 -- Partial sums of the Jordan function J_2(k), for 1 <= k <= n.
  28. # https://oeis.org/A002088 -- Sum of totient function: a(n) = Sum_{k=1..n} phi(k).
  29. # https://oeis.org/A064018 -- Sum of the Euler totients phi for 10^n.
  30. # https://oeis.org/A272718 -- Partial sums of gcd-sum sequence A018804.
  31. # See also:
  32. # https://en.wikipedia.org/wiki/Faulhaber's_formula
  33. # https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
  34. # https://en.wikipedia.org/wiki/Jordan%27s_totient_function
  35. # https://trizenx.blogspot.com/2018/11/partial-sums-of-arithmetical-functions.html
  36. use 5.020;
  37. use strict;
  38. use warnings;
  39. use experimental qw(signatures);
  40. use Math::AnyNum qw(faulhaber_sum ipow);
  41. use ntheory qw(jordan_totient sqrtint rootint);
  42. sub partial_sums_of_jordan_totient ($n, $j, $m) {
  43. my $s = sqrtint($n);
  44. my @jordan_sum_lookup = (0);
  45. my $lookup_size = 2 * rootint($n, 3)**2;
  46. foreach my $i (1 .. $lookup_size) {
  47. $jordan_sum_lookup[$i] = $jordan_sum_lookup[$i - 1] + ipow($i, $m) * jordan_totient($j, $i);
  48. }
  49. my %seen;
  50. sub ($n) {
  51. if ($n <= $lookup_size) {
  52. return $jordan_sum_lookup[$n];
  53. }
  54. if (exists $seen{$n}) {
  55. return $seen{$n};
  56. }
  57. my $s = sqrtint($n);
  58. my $T = faulhaber_sum($n, $m + $j);
  59. foreach my $k (2 .. int($n / ($s + 1))) {
  60. $T -= ipow($k, $m) * __SUB__->(int($n / $k));
  61. }
  62. foreach my $k (1 .. $s) {
  63. $T -= (faulhaber_sum(int($n / $k), $m) - faulhaber_sum(int($n / ($k + 1)), $m)) * __SUB__->($k);
  64. }
  65. $seen{$n} = $T;
  66. }->($n);
  67. }
  68. my $j = 2;
  69. my $k = 1;
  70. foreach my $n (1 .. 7) { # takes ~2.9 seconds
  71. say "a(10^$n, $j, $k) = ", partial_sums_of_jordan_totient(10**$n, $j, $k);
  72. }