weighted_prime_power_count.sf 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/ruby
  2. # Counts prime powers with weight 1 for primes, 1/2 for their squares, 1/3 for cubes, ...
  3. # OEIS sequences:
  4. # https://oeis.org/A322713 (numerators for 10^n)
  5. # https://oeis.org/A322714 (denominators for 10^n)
  6. # See also:
  7. # https://mathworld.wolfram.com/RiemannPrimeCountingFunction.html
  8. # https://en.wikipedia.org/wiki/Arithmetic_function#%CF%80(x),_%CE%A0(x),_%CE%B8(x),_%CF%88(x)_%E2%80%93_prime_count_functions
  9. func weighted_prime_count(n) {
  10. n.primes.sum {|p|
  11. 1..n.ilog(p) -> sum {|k|
  12. 1/k
  13. }
  14. }
  15. }
  16. func weighted_prime_count_fast(n) {
  17. sum(1..n.ilog2, {|k|
  18. n.iroot(k).prime_count / k
  19. })
  20. }
  21. say weighted_prime_count(100) #=> 28.53333333333333333333333333333333333333333333333
  22. say weighted_prime_count(1000) #=> 176.69563492063492063492063492063492063492063492063
  23. say weighted_prime_count(10000) #=> 1247.09799089799089799089799089799089799089799089799
  24. say "\n=> Weighted prime counting function for 10^n: ";
  25. for n in (1..14) {
  26. var r = weighted_prime_count_fast(10**n)
  27. say ("Π(10^#{n}) = #{r.as_dec} = #{r.as_rat}")
  28. }
  29. __END__
  30. => Weighted prime counting function for 10^n:
  31. Π(10^1) = 5.33333333333333333333333333333333333333333333333 = 16/3
  32. Π(10^2) = 28.5333333333333333333333333333333333333333333333 = 428/15
  33. Π(10^3) = 176.695634920634920634920634920634920634920634921 = 445273/2520
  34. Π(10^4) = 1247.0979908979908979908979908979908979908979909 = 56175529/45045
  35. Π(10^5) = 9633.76922105672105672105672105672105672105672106 = 991892879/102960
  36. Π(10^6) = 78597.1116646210686458364476940328333517188006352 = 18296822833013/232792560
  37. Π(10^7) = 664827.299345901141230256858201399862457878341577 = 3559637526370229/5354228880
  38. Π(10^8) = 5762113.05304192733725645288439742605848407436777 = 6427431691337929/1115464350
  39. Π(10^9) = 50849310.4437027127242081684365186576929002929625 = 14804074778750628149/291136195350
  40. Π(10^10) = 455057444.27504104638639582043741344296799117077 = 9387415960571046321167/20629078984800
  41. Π(10^11) = 4118068710.93062615465692608585258934407739611649 = 594663752918349842404169/144403552893600
  42. Π(10^12) = 37607951741.7757315060084074682937241071935833813 = 200936708396848319452718531/5342931457063200
  43. Π(10^13) = 346065651568.069547135033906034348160836616132429 = 296345083061712053722716462103/856326196254765600
  44. Π(10^14) = 3204942084844.48423782137396505648719560091721094 = 30189234512048649753828116713823/9419588158802421600
  45. Π(10^15) = 29844571402088.8168168616245330808277351246061783 = 92489654985220588144991271054976597/3099044504245996706400