sum_of_the_number_of_unitary_divisors.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 10 January 2019
  4. # https://github.com/trizen
  5. # Two fast algorithms for computing the sum of number of unitary divisors from 1 to n.
  6. # a(n) = Sum_{k=1..n} usigma_0(k)
  7. # Based on the formula:
  8. # a(n) = Sum_{k=1..n} moebius(k)^2 * floor(n/k)
  9. # See also:
  10. # https://oeis.org/A034444 -- Partial sums of A034444: sum of number of unitary divisors from 1 to n.
  11. # https://oeis.org/A180361 -- Sum of number of unitary divisors (A034444) from 1 to 10^n
  12. # https://oeis.org/A268732 -- Sum of the numbers of divisors of gcd(x,y) with x*y <= n.
  13. # Asymptotic formula:
  14. # a(n) ~ n*log(n)/zeta(2) + O(n)
  15. # Better asymptotic formula:
  16. # a(n) ~ (n/zeta(2))*(log(n) + 2*γ - 1 - c) + O(sqrt(n) * log(n))
  17. #
  18. # where γ is the Euler-Mascheroni constant and c = 2*zeta'(2)/zeta(2) = -1.1399219861890656127997287200...
  19. use 5.020;
  20. use strict;
  21. use warnings;
  22. use ntheory qw(:all);
  23. use experimental qw(signatures);
  24. use Math::AnyNum qw(:overload zeta EulerGamma round);
  25. sub squarefree_count ($n) {
  26. my $count = 0;
  27. my $k = 1;
  28. foreach my $mu (moebius(1, sqrtint($n))) {
  29. if ($mu) {
  30. $count += $mu * divint($n, $k * $k);
  31. }
  32. ++$k;
  33. }
  34. return $count;
  35. }
  36. sub asymptotic_formula($n) {
  37. # c = 2*Zeta'(2)/Zeta(2) = (12 * Zeta'(2))/π^2 = 2 (-12 log(A) + γ + log(2) + log(π))
  38. my $c = -1.13992198618906561279972872003946000480696456161386195911639472087583455473348121357;
  39. # Asymptotic formula based on Merten's theorem (1874) (see: https://oeis.org/A064608)
  40. ($n / zeta(2)) * (log($n) + 2 * EulerGamma - 1 - $c);
  41. }
  42. sub unitary_divisors_partial_sum_1 ($n) { # O(sqrt(n)) complexity
  43. my $total = 0;
  44. my $s = sqrtint($n);
  45. my $u = divint($n, $s + 1);
  46. my $prev = squarefree_count($n);
  47. for my $k (1 .. $s) {
  48. my $curr = squarefree_count(divint($n, $k + 1));
  49. $total += $k * ($prev - $curr);
  50. $prev = $curr;
  51. }
  52. forsquarefree {
  53. $total += divint($n, $_);
  54. } $u;
  55. return $total;
  56. }
  57. sub unitary_divisors_partial_sum_2 ($n) { # based on formula by Jerome Raulin (https://oeis.org/A064608)
  58. my $total = 0;
  59. my $k = 1;
  60. foreach my $mu (moebius(1, sqrtint($n))) {
  61. if ($mu) {
  62. my $t = 0;
  63. foreach my $j (1 .. sqrtint(divint($n, $k * $k))) {
  64. $t += divint($n, $j * $k * $k);
  65. }
  66. my $r = sqrtint(divint($n, $k * $k));
  67. $total += $mu * (2 * $t - $r * $r);
  68. }
  69. ++$k;
  70. }
  71. return $total;
  72. }
  73. say join(', ', map { unitary_divisors_partial_sum_1($_) } 1 .. 20);
  74. say join(', ', map { unitary_divisors_partial_sum_2($_) } 1 .. 20);
  75. foreach my $k (0 .. 7) {
  76. my $n = 10**$k;
  77. my $t = unitary_divisors_partial_sum_2($n);
  78. my $u = asymptotic_formula($n);
  79. printf("a(10^%s) = %10s ~ %-15s -> %s\n", $k, $t, round($u, -2), $t / $u);
  80. }
  81. __END__
  82. [0, 1, 3, 5, 7, 9, 13, 15, 17, 19, 23, 25, 29, 31, 35, 39, 41, 43, 47, 49, 53]
  83. [0, 1, 3, 5, 7, 9, 13, 15, 17, 19, 23, 25, 29, 31, 35, 39, 41, 43, 47, 49, 53]
  84. a(10^0) = 1 ~ 0.79 -> 1.27085398285349342897812915198984638968899591751
  85. a(10^1) = 23 ~ 21.87 -> 1.05182461403816051734935994402113331145060974294
  86. a(10^2) = 359 ~ 358.65 -> 1.00098140095602073835866744824992972185806123685
  87. a(10^3) = 4987 ~ 4986.28 -> 1.00014357239778054254970740667091143421188177813
  88. a(10^4) = 63869 ~ 63860.88 -> 1.00012715302552355451250212258735392366329621935
  89. a(10^5) = 778581 ~ 778589.19 -> 0.999989484576929013867264739526374966823956960403
  90. a(10^6) = 9185685 ~ 9185695.75 -> 0.99999882923368455522780513812504287278271814501
  91. a(10^7) = 105854997 ~ 105854996.37 -> 1.00000000598372061072117962943109677794267023891
  92. a(10^8) = 1198530315 ~ 1198530351.90 -> 0.999999969211002320383540850995519903094748492418
  93. a(10^9) = 13385107495 ~ 13385107401.37 -> 1.00000000699496540213133746406895764726726792391
  94. a(10^10) = 147849112851 ~ 147849112837.28 -> 1.00000000009281141854332921757852421030396550125