sum_of_number_of_unitary_divisors.sf 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/ruby
  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. func asymptotic_formula(n) {
  20. # c = 2*Zeta'(2)/Zeta(2) = (12 * Zeta'(2))/π^2 = 2 (-12 log(A) + γ + log(2) + log(π))
  21. const c = -1.13992198618906561279972872003946000480696456161386195911639472087583455473348121357
  22. # Asymptotic formula based on Merten's theorem (1874) (see: https://oeis.org/A064608)
  23. (n/zeta(2)) * (log(n) + 2*Num.EulerGamma - 1 - c)
  24. }
  25. func usigma0_partial_sum_1 (n) { # O(sqrt(n)) complexity
  26. var total = 0
  27. var s = n.isqrt
  28. var u = idiv(n, s+1)
  29. var prev = squarefree_count(n)
  30. for k in (1..s) {
  31. var curr = squarefree_count(idiv(n, k+1))
  32. total += (prev - curr)*k
  33. prev = curr
  34. }
  35. u.each_squarefree {|k|
  36. total += idiv(n, k)
  37. }
  38. return total
  39. }
  40. func usigma0_partial_sum_2 (n) { # based on formula by Jerome Raulin (https://oeis.org/A064608)
  41. var total = 0
  42. n.isqrt.each_squarefree {|k|
  43. var v = moebius(k)
  44. var t = 2*sum(1..isqrt(idiv(n, k*k)), {|j|
  45. idiv(n, j*k*k)
  46. })
  47. total += v*(t - isqrt(idiv(n, k*k))**2)
  48. }
  49. return total
  50. }
  51. func usigma0_partial_sum_3(n) { # O(sqrt(n)) complexity, using Dirichlet's hyperbola method
  52. n.dirichlet_sum({1}, {.mu.abs}, {_}, {.squarefree_count})
  53. }
  54. say 20.of(usigma0_partial_sum_1)
  55. say 20.of(usigma0_partial_sum_2)
  56. say 20.of(usigma0_partial_sum_3)
  57. for k in (0..7) {
  58. var n = 10**k
  59. var t = usigma0_partial_sum_1(n)
  60. var u = asymptotic_formula(n)
  61. printf("a(10^%s) = %10s ~ %-15s -> %s\n", k, t, u.round(-2), t/u)
  62. }
  63. __END__
  64. [0, 1, 3, 5, 7, 9, 13, 15, 17, 19, 23, 25, 29, 31, 35, 39, 41, 43, 47, 49]
  65. [0, 1, 3, 5, 7, 9, 13, 15, 17, 19, 23, 25, 29, 31, 35, 39, 41, 43, 47, 49]
  66. a(10^0) = 1 ~ 0.79 -> 1.27085398285349342897812915198984638968899591751
  67. a(10^1) = 23 ~ 21.87 -> 1.05182461403816051734935994402113331145060974294
  68. a(10^2) = 359 ~ 358.65 -> 1.00098140095602073835866744824992972185806123685
  69. a(10^3) = 4987 ~ 4986.28 -> 1.00014357239778054254970740667091143421188177813
  70. a(10^4) = 63869 ~ 63860.88 -> 1.00012715302552355451250212258735392366329621935
  71. a(10^5) = 778581 ~ 778589.19 -> 0.999989484576929013867264739526374966823956960403
  72. a(10^6) = 9185685 ~ 9185695.75 -> 0.99999882923368455522780513812504287278271814501
  73. a(10^7) = 105854997 ~ 105854996.37 -> 1.00000000598372061072117962943109677794267023891
  74. a(10^8) = 1198530315 ~ 1198530351.90 -> 0.999999969211002320383540850995519903094748492418
  75. a(10^9) = 13385107495 ~ 13385107401.37 -> 1.00000000699496540213133746406895764726726792391
  76. a(10^10) = 147849112851 ~ 147849112837.28 -> 1.00000000009281141854332921757852421030396550125