Legendre prime counting function: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(→{{header|Ruby}}: Add Ruby) |
||
Line 3,221: | Line 3,221: | ||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="ruby">require 'prime' |
|||
def pi(n) |
|||
@pr = Prime.each(Integer.sqrt(n)).to_a |
|||
a = @pr.size |
|||
case n |
|||
when 0,1 then 0 |
|||
when 2 then 1 |
|||
else phi(n,a) + a - 1 |
|||
end |
|||
end |
|||
def phi(x,a) |
|||
case a |
|||
when 0 then x |
|||
when 1 then x-(x>>1) |
|||
else |
|||
pa = @pr[a-1] |
|||
return 1 if x <= pa |
|||
phi(x, a-1)- phi(x/pa, a-1) |
|||
end |
|||
end |
|||
(0..9).each {|n| puts "10E#{n} #{pi(10**n)}" } |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10E0 0 |
|||
10E1 4 |
|||
10E2 25 |
|||
10E3 168 |
|||
10E4 1229 |
|||
10E5 9592 |
|||
10E6 78498 |
|||
10E7 664579 |
|||
10E8 5761455 |
|||
10E9 50847534 |
|||
</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang="ruby">func legendre_phi(x, a) is cached { |
<syntaxhighlight lang="ruby">func legendre_phi(x, a) is cached { |