partial_sums_of_euler_totient_function_times_k.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 05 April 2022
  4. # https://github.com/trizen
  5. # A sublinear algorithm for computing the partial sums of the Euler totient function times k.
  6. # The partial sums of the Euler totient function is defined as:
  7. #
  8. # a(n,m) = Sum_{k=1..n} k * phi(k)
  9. #
  10. # where phi(k) is the Euler totient function.
  11. # Example:
  12. # a(10^1) = 217
  13. # a(10^2) = 203085
  14. # a(10^3) = 202870719
  15. # a(10^4) = 202653667159
  16. # a(10^5) = 202643891472849
  17. # a(10^6) = 202642368741515819
  18. # a(10^7) = 202642380629476099463
  19. # a(10^8) = 202642367994273571457613
  20. # a(10^9) = 202642367530671221417109931
  21. # a(10^10) = 202642367286524384080814204093
  22. # General asymptotic formula:
  23. #
  24. # Sum_{k=1..n} k^m * phi(k) ~ F_(m+1)(n) / zeta(2).
  25. #
  26. # where F_m(n) are the Faulhaber polynomials.
  27. # OEIS sequences:
  28. # https://oeis.org/A011755 -- Sum_{k=1..n} k*phi(k).
  29. # https://oeis.org/A002088 -- Sum of totient function: a(n) = Sum_{k=1..n} phi(k).
  30. # https://oeis.org/A064018 -- Sum of the Euler totients phi for 10^n.
  31. # https://oeis.org/A272718 -- Partial sums of gcd-sum sequence A018804.
  32. # See also:
  33. # https://en.wikipedia.org/wiki/Faulhaber's_formula
  34. # https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
  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 ntheory qw(:all);
  41. sub triangular ($n) {
  42. divint(mulint($n, $n + 1), 2);
  43. }
  44. sub square_pyramidal ($n) {
  45. divint(vecprod($n, $n + 1, mulint(2, $n) + 1), 6);
  46. }
  47. sub partial_sums_of_euler_totient ($n) {
  48. my $s = sqrtint($n);
  49. my @euler_sum_lookup = (0);
  50. my $lookup_size = int(2 * rootint($n, 3)**2);
  51. my @euler_phi = euler_phi(0, $lookup_size);
  52. foreach my $i (1 .. $lookup_size) {
  53. $euler_sum_lookup[$i] = addint($euler_sum_lookup[$i - 1], mulint($i, $euler_phi[$i]));
  54. }
  55. my %seen;
  56. sub ($n) {
  57. if ($n <= $lookup_size) {
  58. return $euler_sum_lookup[$n];
  59. }
  60. if (exists $seen{$n}) {
  61. return $seen{$n};
  62. }
  63. my $s = sqrtint($n);
  64. my $T = square_pyramidal($n);
  65. foreach my $k (2 .. divint($n, $s + 1)) {
  66. $T = subint($T, mulint($k, __SUB__->(divint($n, $k))));
  67. }
  68. my $prev = triangular($n);
  69. foreach my $k (1 .. $s) {
  70. my $curr = triangular(divint($n, $k + 1));
  71. $T = subint($T, mulint(subint($prev, $curr), __SUB__->($k)));
  72. $prev = $curr;
  73. }
  74. $seen{$n} = $T;
  75. }->($n);
  76. }
  77. foreach my $n (1 .. 8) { # takes ~5 seconds
  78. say "a(10^$n) = ", partial_sums_of_euler_totient(powint(10, $n));
  79. }