-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted_prime_power_count.sf
More file actions
54 lines (45 loc) · 2.53 KB
/
Copy pathweighted_prime_power_count.sf
File metadata and controls
54 lines (45 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/ruby
# Counts prime powers with weight 1 for primes, 1/2 for their squares, 1/3 for cubes, ...
# OEIS sequences:
# https://oeis.org/A322713 (numerators for 10^n)
# https://oeis.org/A322714 (denominators for 10^n)
# See also:
# https://mathworld.wolfram.com/RiemannPrimeCountingFunction.html
# 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
func weighted_prime_count(n) {
n.primes.sum {|p|
1..n.ilog(p) -> sum {|k|
1/k
}
}
}
func weighted_prime_count_fast(n) {
sum(1..n.ilog2, {|k|
n.iroot(k).prime_count / k
})
}
say weighted_prime_count(100) #=> 28.53333333333333333333333333333333333333333333333
say weighted_prime_count(1000) #=> 176.69563492063492063492063492063492063492063492063
say weighted_prime_count(10000) #=> 1247.09799089799089799089799089799089799089799089799
say "\n=> Weighted prime counting function for 10^n: ";
for n in (1..14) {
var r = weighted_prime_count_fast(10**n)
say ("Π(10^#{n}) = #{r.as_dec} = #{r.as_rat}")
}
__END__
=> Weighted prime counting function for 10^n:
Π(10^1) = 5.33333333333333333333333333333333333333333333333 = 16/3
Π(10^2) = 28.5333333333333333333333333333333333333333333333 = 428/15
Π(10^3) = 176.695634920634920634920634920634920634920634921 = 445273/2520
Π(10^4) = 1247.0979908979908979908979908979908979908979909 = 56175529/45045
Π(10^5) = 9633.76922105672105672105672105672105672105672106 = 991892879/102960
Π(10^6) = 78597.1116646210686458364476940328333517188006352 = 18296822833013/232792560
Π(10^7) = 664827.299345901141230256858201399862457878341577 = 3559637526370229/5354228880
Π(10^8) = 5762113.05304192733725645288439742605848407436777 = 6427431691337929/1115464350
Π(10^9) = 50849310.4437027127242081684365186576929002929625 = 14804074778750628149/291136195350
Π(10^10) = 455057444.27504104638639582043741344296799117077 = 9387415960571046321167/20629078984800
Π(10^11) = 4118068710.93062615465692608585258934407739611649 = 594663752918349842404169/144403552893600
Π(10^12) = 37607951741.7757315060084074682937241071935833813 = 200936708396848319452718531/5342931457063200
Π(10^13) = 346065651568.069547135033906034348160836616132429 = 296345083061712053722716462103/856326196254765600
Π(10^14) = 3204942084844.48423782137396505648719560091721094 = 30189234512048649753828116713823/9419588158802421600
Π(10^15) = 29844571402088.8168168616245330808277351246061783 = 92489654985220588144991271054976597/3099044504245996706400