partial_sums_of_gcd-sum_function_fast.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 04 February 2019
  4. # https://github.com/trizen
  5. # A sublinear algorithm for computing the partial sums of the gcd-sum function, using Dirichlet's hyperbola method.
  6. # The partial sums of the gcd-sum function is defined as:
  7. #
  8. # a(n) = Sum_{k=1..n} Sum_{d|k} d*phi(k/d)
  9. #
  10. # where phi(k) is the Euler totient function.
  11. # Also equivalent with:
  12. # a(n) = Sum_{j=1..n} Sum_{i=1..j} gcd(i, j)
  13. # Based on the formula:
  14. # a(n) = (1/2)*Sum_{k=1..n} phi(k) * floor(n/k) * floor(1+n/k)
  15. # Example:
  16. # a(10^1) = 122
  17. # a(10^2) = 18065
  18. # a(10^3) = 2475190
  19. # a(10^4) = 317257140
  20. # a(10^5) = 38717197452
  21. # a(10^6) = 4571629173912
  22. # a(10^7) = 527148712519016
  23. # a(10^8) = 59713873168012716
  24. # a(10^9) = 6671288261316915052
  25. # OEIS sequences:
  26. # https://oeis.org/A272718 -- Partial sums of gcd-sum sequence A018804.
  27. # https://oeis.org/A018804 -- Pillai's arithmetical function: Sum_{k=1..n} gcd(k, n).
  28. # See also:
  29. # https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
  30. # https://trizenx.blogspot.com/2018/11/partial-sums-of-arithmetical-functions.html
  31. use 5.020;
  32. use strict;
  33. use warnings;
  34. use experimental qw(signatures);
  35. use ntheory qw(euler_phi moebius sqrtint rootint);
  36. sub partial_sums_of_gcd_sum_function($n) {
  37. my $s = sqrtint($n);
  38. my @mertens_lookup = (0);
  39. my @euler_sum_lookup = (0);
  40. my $lookup_size = 2 + 2 * rootint($n, 3)**2;
  41. my @moebius = moebius(0, $lookup_size);
  42. my @euler_phi = euler_phi(0, $lookup_size);
  43. foreach my $i (1 .. $lookup_size) {
  44. $mertens_lookup[$i] = $mertens_lookup[$i - 1] + $moebius[$i];
  45. $euler_sum_lookup[$i] = $euler_sum_lookup[$i - 1] + $euler_phi[$i];
  46. }
  47. my %mertens_cache;
  48. my sub moebius_partial_sum ($n) {
  49. if ($n <= $lookup_size) {
  50. return $mertens_lookup[$n];
  51. }
  52. if (exists $mertens_cache{$n}) {
  53. return $mertens_cache{$n};
  54. }
  55. my $s = sqrtint($n);
  56. my $M = 1;
  57. foreach my $k (2 .. int($n / ($s + 1))) {
  58. $M -= __SUB__->(int($n / $k));
  59. }
  60. foreach my $k (1 .. $s) {
  61. $M -= $mertens_lookup[$k] * (int($n / $k) - int($n / ($k + 1)));
  62. }
  63. $mertens_cache{$n} = $M;
  64. }
  65. my %euler_phi_sum_cache;
  66. my sub euler_phi_partial_sum($n) {
  67. if ($n <= $lookup_size) {
  68. return $euler_sum_lookup[$n];
  69. }
  70. if (exists $euler_phi_sum_cache{$n}) {
  71. return $euler_phi_sum_cache{$n};
  72. }
  73. my $s = sqrtint($n);
  74. my $A = 0;
  75. foreach my $k (1 .. $s) {
  76. my $t = int($n / $k);
  77. $A += $k * moebius_partial_sum($t) + $moebius[$k] * (($t * ($t + 1)) >> 1);
  78. }
  79. my $C = moebius_partial_sum($s) * (($s * ($s + 1)) >> 1);
  80. $euler_phi_sum_cache{$n} = ($A - $C);
  81. }
  82. my $A = 0;
  83. foreach my $k (1 .. $s) {
  84. my $t = int($n / $k);
  85. $A += $k * euler_phi_partial_sum($t) + $euler_phi[$k] * (($t * ($t + 1)) >> 1);
  86. }
  87. my $C = euler_phi_partial_sum($s) * (($s * ($s + 1)) >> 1);
  88. return ($A - $C);
  89. }
  90. foreach my $n (1 .. 8) { # takes less than 1 second
  91. say "a(10^$n) = ", partial_sums_of_gcd_sum_function(10**$n);
  92. }