partial_sums_of_prime_bigomega_function.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 27 November 2018
  4. # https://github.com/trizen
  5. # A nice algorithm in terms of the prime-counting function for computing partial sums of the generalized bigomega(n) function:
  6. # B_m(n) = Sum_{k=1..n} Ω_m(k)
  7. # For `m=0`, we have:
  8. # B_0(n) = bigomega(n!)
  9. # OEIS related sequences:
  10. # https://oeis.org/A025528
  11. # https://oeis.org/A022559
  12. # https://oeis.org/A071811
  13. # https://oeis.org/A154945 (0.55169329765699918...)
  14. # https://oeis.org/A286229 (0.19411816983263379...)
  15. # Example for `B_0(n)`:
  16. # B_0(10^1) = 15
  17. # B_0(10^2) = 239
  18. # B_0(10^3) = 2877
  19. # B_0(10^4) = 31985
  20. # B_0(10^5) = 343614
  21. # B_0(10^6) = 3626619
  22. # B_0(10^7) = 37861249
  23. # B_0(10^8) = 392351272
  24. # B_0(10^9) = 4044220058
  25. # B_0(10^10) = 41518796555
  26. # B_0(10^11) = 424904645958
  27. # Example for `B_1(n)`:
  28. # B_1(10^1) = 30
  29. # B_1(10^2) = 2815
  30. # B_1(10^3) = 276337
  31. # B_1(10^4) = 27591490
  32. # B_1(10^5) = 2758525172
  33. # B_1(10^6) = 275847515154
  34. # B_1(10^7) = 27584671195911
  35. # B_1(10^8) = 2758466558498626
  36. # B_1(10^9) = 275846649393437566
  37. # B_1(10^10) = 27584664891073330599
  38. # B_1(10^11) = 2758466488352698209587
  39. # Example for `B_2(n)`:
  40. # B_2(10^1) = 82
  41. # B_2(10^2) = 66799
  42. # B_2(10^3) = 64901405
  43. # B_2(10^4) = 64727468210
  44. # B_2(10^5) = 64708096890744
  45. # B_2(10^6) = 64706281936598588
  46. # B_2(10^7) = 64706077322294843451
  47. # B_2(10^8) = 64706058761567362618628
  48. # B_2(10^9) = 64706056807390376400359474
  49. # B_2(10^10) = 64706056632561375736945155965
  50. # B_2(10^11) = 64706056612919470606889256184409
  51. # Asymptotic formulas:
  52. # B_1(n) ~ 0.55169329765699918... * n*(n+1)/2
  53. # B_2(n) ~ 0.19411816983263379... * n*(n+1)*(2*n+1)/6
  54. # In general, for `m>=1`, we have the following asymptotic formula:
  55. # B_m(n) ~ (Sum_{k>=1} primezeta((m+1)*k)) * F_m(n)
  56. #
  57. # where F_n(x) is Faulhaber's formula and primezeta(s) is the prime zeta function.
  58. # The prime zeta function is defined as:
  59. # primezeta(s) = Sum_{p prime >= 2} 1/p^s
  60. # OEIS sequences:
  61. # https://oeis.org/A022559 -- Sum of exponents in prime-power factorization of n!.
  62. # https://oeis.org/A071811 -- Sum_{k <= 10^n} number of primes (counted with multiplicity) dividing k.
  63. # See also:
  64. # https://en.wikipedia.org/wiki/Prime_zeta_function
  65. # https://en.wikipedia.org/wiki/Prime_omega_function
  66. # https://en.wikipedia.org/wiki/Prime-counting_function
  67. # https://trizenx.blogspot.com/2018/11/partial-sums-of-arithmetical-functions.html
  68. use 5.020;
  69. use strict;
  70. use warnings;
  71. use experimental qw(signatures);
  72. use Math::AnyNum qw(faulhaber_sum ipow);
  73. use ntheory qw(logint sqrtint rootint prime_count is_prime_power forprimes prime_power_count divint);
  74. sub prime_bigomega_partial_sum ($n, $m) {
  75. my $s = sqrtint($n);
  76. my $u = divint($n, $s+1);
  77. my $total = 0;
  78. my $prev = prime_power_count($n);
  79. for my $k (1 .. $s) {
  80. my $curr = prime_power_count(divint($n, $k+1));
  81. $total += faulhaber_sum($k, $m) * ($prev - $curr);
  82. $prev = $curr;
  83. }
  84. forprimes {
  85. for (my $q = $_; $q <= $u; $q *= $_) {
  86. $total += faulhaber_sum(divint($n, $q), $m);
  87. }
  88. } $u;
  89. return $total;
  90. }
  91. sub prime_bigomega_partial_sum_2 ($n, $m) {
  92. my $s = sqrtint($n);
  93. my $total = 0;
  94. for my $k (1 .. $s) {
  95. $total += ipow($k, $m) * prime_power_count(divint($n,$k));
  96. $total += faulhaber_sum(divint($n,$k), $m) if is_prime_power($k);
  97. }
  98. $total -= prime_power_count($s) * faulhaber_sum($s, $m);
  99. return $total;
  100. }
  101. sub prime_bigomega_partial_sum_test ($n, $m) { # just for testing
  102. my $total = 0;
  103. foreach my $k (1 .. $n) {
  104. if (is_prime_power($k)) {
  105. $total += faulhaber_sum(divint($n,$k), $m);
  106. }
  107. }
  108. return $total;
  109. }
  110. for my $m (0 .. 10) {
  111. my $n = int rand 100000;
  112. my $t1 = prime_bigomega_partial_sum($n, $m);
  113. my $t2 = prime_bigomega_partial_sum_2($n, $m);
  114. my $t3 = prime_bigomega_partial_sum_test($n, $m);
  115. die "error: $t1 != $t2" if ($t1 != $t2);
  116. die "error: $t1 != $t3" if ($t1 != $t3);
  117. say "Sum_{k=1..$n} bigomega_$m(k) = $t1";
  118. }
  119. __END__
  120. Sum_{k=1..64129} bigomega_0(k) = 217697
  121. Sum_{k=1..80658} bigomega_1(k) = 1794616247
  122. Sum_{k=1..14117} bigomega_2(k) = 182041102184
  123. Sum_{k=1..42256} bigomega_3(k) = 64820877399946967
  124. Sum_{k=1..94333} bigomega_4(k) = 54949545016977768030431
  125. Sum_{k=1..67787} bigomega_5(k) = 280074038628976042168758675
  126. Sum_{k=1..35346} bigomega_6(k) = 82191526450425222986408201316
  127. Sum_{k=1..26871} bigomega_7(k) = 138516432841564488200009700415893
  128. Sum_{k=1..37827} bigomega_8(k) = 35383863032817120893574255077390725080
  129. Sum_{k=1..75109} bigomega_9(k) = 568264668321999976994584691196910905310669837
  130. Sum_{k=1..86486} bigomega_10(k) = 90982066598399530764623907560522017063257428908802