prime_functions_in_terms_of_zeros_of_zeta.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/perl
  2. # Approximate the Chebyshev function and the weighted prime counting function, using zeros of the Riemann zeta function.
  3. # See also:
  4. # https://oeis.org/A267712
  5. # https://en.wikipedia.org/wiki/Chebyshev_function
  6. # https://en.wikipedia.org/wiki/Logarithmic_integral_function
  7. # https://en.wikipedia.org/wiki/Riemann_zeta_function
  8. use utf8;
  9. use 5.020;
  10. use strict;
  11. use warnings;
  12. binmode(STDOUT, ':utf8');
  13. use ntheory qw(forprimes prime_count);
  14. use experimental qw(signatures);
  15. use Math::AnyNum qw(:overload gamma complex tau ilog iroot log Li harmreal);
  16. my @zeta_ρ = map { chomp; complex(1 / 2, $_) } <DATA>;
  17. sub Li_approx ($x) {
  18. my $sum = 0;
  19. foreach my $k (0 .. 0) {
  20. $sum += gamma($k + 1) / log($x)**$k;
  21. }
  22. return ($sum * ($x / log($x)));
  23. }
  24. sub chebyshev_ψ ($x) {
  25. my $sum = 0;
  26. forprimes {
  27. $sum += ilog($x, $_) * log($_)
  28. } $x;
  29. return $sum;
  30. }
  31. sub weighted_prime_count ($x) {
  32. my $sum = 0;
  33. foreach my $k (1 .. ilog($x, 2)) {
  34. $sum += Math::AnyNum->new(prime_count(iroot($x, $k))) / $k;
  35. }
  36. return $sum;
  37. }
  38. sub weighted_prime_count_from_zeta_zeros ($x) {
  39. my $sum = Li($x);
  40. foreach my $ρ (@zeta_ρ) {
  41. $sum -= Li_approx($x**$ρ);
  42. }
  43. return abs($sum - log(2));
  44. }
  45. sub chebyshev_ψ_from_zeta_zeros($x) {
  46. my $sum = $x - log(tau) - log(1 - $x**(-2)) / 2;
  47. foreach my $ρ (@zeta_ρ) {
  48. $sum -= $x**$ρ / $ρ;
  49. }
  50. return abs($sum);
  51. }
  52. my $x = 10**3;
  53. say "ψ($x) = ", chebyshev_ψ($x); # 996.680912247175240263021765666421541665778436902
  54. say "ψ($x) ≅ ", chebyshev_ψ_from_zeta_zeros($x); # 996.068434632130345546023799228964726756917555651
  55. say "\n=> Weighted prime count approximation: ";
  56. foreach my $k (10 .. 14) {
  57. my $exact = weighted_prime_count(10**$k);
  58. my $approx = weighted_prime_count_from_zeta_zeros(10**$k);
  59. say "Π(10^$k) = ", $exact->as_dec, " ≅ ", $approx, ' -> ', abs($exact - $approx);
  60. }
  61. __DATA__
  62. 14.1347251417346937904572519835624702707842571157
  63. 21.0220396387715549926284795938969027773343405249
  64. 25.0108575801456887632137909925628218186595496726
  65. 30.4248761258595132103118975305840913201815600237
  66. 32.9350615877391896906623689640749034888127156035
  67. 37.5861781588256712572177634807053328214055973508
  68. 40.9187190121474951873981269146332543957261659628
  69. 43.3270732809149995194961221654068057826456683718
  70. 48.0051508811671597279424727494275160416868440011
  71. 49.7738324776723021819167846785637240577231782997
  72. 52.9703214777144606441472966088809900638250178888
  73. 56.4462476970633948043677594767061275527822644717
  74. 59.3470440026023530796536486749922190310987728065
  75. 60.8317785246098098442599018245240038029100904512
  76. 65.1125440480816066608750542531837050293481492952
  77. 67.0798105294941737144788288965222167701071449517
  78. 69.5464017111739792529268575265547384430124742096
  79. 72.0671576744819075825221079698261683904809066215
  80. 75.7046906990839331683269167620303459228119035307
  81. 77.1448400688748053726826648563046370157960324492
  82. 79.3373750202493679227635928771162281906132467431
  83. 82.9103808540860301831648374947706094975088805938
  84. 84.7354929805170501057353112068277414171066279342
  85. 87.4252746131252294065316678509192132521718864013
  86. 88.8091112076344654236823480795093783954448934098
  87. 92.4918992705584842962597252418106848787217940277
  88. 94.6513440405198869665979258152081539377280270157
  89. 95.870634228245309758741029219246781695256461225
  90. 98.8311942181936922333244201386223278206580390634
  91. 101.317851005731391228785447940292308906332866384
  92. 103.725538040478339416398408108695280834481173069
  93. 105.446623052326094493670832414111808997282753929
  94. 107.168611184276407515123351963086191213476707881
  95. 111.02953554316967452465645030994435041534596839
  96. 111.874659176992637085612078716770594960311749873
  97. 114.320220915452712765890937276191079809917657724
  98. 116.226680320857554382160804312064755127329851232
  99. 118.790782865976217322979139702699824347306210593
  100. 121.370125002420645918945532970499922723001310632
  101. 122.946829293552588200817460330770016496214389874
  102. 124.256818554345767184732007966129924441573538775
  103. 127.516683879596495124279323766906076268088309882
  104. 129.578704199956050985768033906179973608640953265
  105. 131.087688530932656723566372461501349059203547503
  106. 133.497737202997586450130492042640607664974174944
  107. 134.756509753373871331326064157169736178396068614
  108. 138.116042054533443200191555190282447859835274624
  109. 139.736208952121388950450046523382460846790052565
  110. 141.12370740402112376194035381847535509030066088
  111. 143.11184580762063273940512386891392996623310243
  112. 146.000982486765518547402507596424682428975741233
  113. 147.42276534255960204952118501043150616877277525
  114. 150.05352042078488035143246723695937062303732156
  115. 150.925257612241466761852524678305627602426770473
  116. 153.024693811198896198256544255185446508590434904
  117. 156.112909294237867569750189310169194746535308501
  118. 157.597591817594059887530503158498765730723899519
  119. 158.849988171420498724174994775540271414335083049
  120. 161.188964137596027519437344129369554364915790327
  121. 163.030709687181987243311039000687994896964461416
  122. 165.537069187900418830038919354874797328367251745
  123. 167.184439978174513440957756246210378736460769243
  124. 169.09451541556882148950587118143183479666764858
  125. 169.911976479411698966699843595821792288394437125
  126. 173.411536519591552959846118649345595254156066063
  127. 174.754191523365725813378762455866917938755717621
  128. 176.441434297710418888892641057860933528118497109
  129. 178.377407776099977285830935414184426183132361461
  130. 179.916484020256996139340036612051237453687607553
  131. 182.207078484366461915407037226987798690797457778
  132. 184.874467848387508800960646617234258413351022912
  133. 185.59878367770747146652770426839264661293471765
  134. 187.228922583501851991641540586131243016810734604
  135. 189.416158656016937084852289099845324491357103023
  136. 192.026656360713786547283631425583430105839920298
  137. 193.079726603845704047402205794376054604020615811
  138. 195.265396679529235321463187814862250926905052452
  139. 196.876481840958316948622263914696207735746028692
  140. 198.015309676251912424919918702208867155062695439
  141. 201.264751943703788733016133427548173222402863639
  142. 202.493594514140534277686660637864315821020244899
  143. 204.189671803104554330716438386313685136534529229
  144. 205.394697202163286025212379390693090923722914772
  145. 207.906258887806209861501967907753644268659403769
  146. 209.576509716856259852835644289886752175390783181
  147. 211.690862595365307563907486730719294253394030983
  148. 213.347919359712666190639122021072608821897183277
  149. 214.547044783491423222944201072590691045599888053
  150. 216.169538508263700265869563354498128575453714274
  151. 219.067596349021378985677256590437241245149182927
  152. 220.714918839314003369115592633906339656761145078
  153. 221.430705554693338732097475119276077950222331077
  154. 224.00700025460433521172887552850489535608598995
  155. 224.983324669582287503782523680528656772090054486
  156. 227.421444279679291310461436160659639963969148322
  157. 229.337413305525348107760083306055740082752341388
  158. 231.250188700499164773806186770010372606708495843
  159. 231.987235253180248603771668539197862205419833995
  160. 233.693404178908300640704494732569788179537227755
  161. 236.524229665816205802475507955662978689529495212