Legendre prime counting function: Difference between revisions

m
syntax highlighting fixup automation
(PicoLisp version)
m (syntax highlighting fixup automation)
Line 23:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
#include <unordered_map>
Line 83:
for (int i = 0, n = 1; i < 10; ++i, n *= 10)
std::cout << "10^" << i << "\t" << counter.prime_count(n) << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 100:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">
<lang CoffeeScript>
sorenson = require('sieve').primes # Sorenson's extensible sieve from task: Extensible Prime Generator
 
Line 135:
console.log "10^#{i}\t#{pi(n)}"
n *= 10
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 150:
</pre>
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-mode(native).
 
Line 203:
true -> M + 1
end.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 225:
 
The alternative of sieving a billion numbers and then counting them takes about 4.8 seconds using the present library function though this could be cut to 1.2 seconds using a third party library such as primegen.
<langsyntaxhighlight lang="go">package main
 
import (
Line 270:
fmt.Printf("10^%d %d\n", i, pi(n))
}
}</langsyntaxhighlight>
 
{{out}}
Line 287:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class LegendrePrimeCounter {
Line 341:
return primes;
}
}</langsyntaxhighlight>
 
{{out}}
Line 360:
 
Memoization utilities:
<langsyntaxhighlight lang="haskell">data Memo a = Node a (Memo a) (Memo a)
deriving Functor
 
Line 385:
split [] = ([],[])
split [x] = ([x],[])
split (x:y:xs) = let (l,r) = split xs in (x:l, y:r)</langsyntaxhighlight>
 
Computation of Legendre function:
<langsyntaxhighlight lang="haskell">isqrt :: Integer -> Integer
isqrt n = go n 0 (q `shiftR` 2)
where
Line 411:
where a = legendrePi (floor (sqrt (fromInteger n)))
 
main = mapM_ (\n -> putStrLn $ show n ++ "\t" ++ show (legendrePi (10^n))) [1..7]</langsyntaxhighlight>
 
<pre>λ> main
Line 427:
This is "almost a primitive" in J:
 
<langsyntaxhighlight Jlang="j"> require'format/printf'
{{echo '10^%d: %d' sprintf y;p:inv 10^y}}"0 i.10
10^0: 0
Line 438:
10^7: 664579
10^8: 5761455
10^9: 50847534</langsyntaxhighlight>
 
A complete implementation of the legendre prime counting function would be <tt>(1&p: + p:inv)</tt>:
 
<langsyntaxhighlight Jlang="j"> (1&p: + p:inv) 10
4
(1&p: + p:inv) 11
5</langsyntaxhighlight>
 
But we can simplify that to p:inv when we know that primes are not being tested.
Line 465:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># For the sake of the accuracy of integer arithmetic when using gojq:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
Line 483:
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i))
| map(select(.)) ;
</syntaxhighlight>
</lang>
'''Phi and the Legendre Counting Function'''
<langsyntaxhighlight lang="jq">def legendre:
(sqrt | floor + 1 | eratosthenes) as $primes
 
Line 522:
;
 
task</langsyntaxhighlight>
 
{{out}}
Line 540:
=== Using the Julia Primes library ===
This would be faster with a custom sieve that only counted instead of making a bitset, instead of the more versatile library function.
<langsyntaxhighlight lang="julia">using Primes
 
function primepi(N)
Line 550:
println("10^", rpad(power, 5), primepi(10^power))
end
</langsyntaxhighlight>{{out}}
<pre>
10^0 0
Line 567:
{{trans|CoffeeScript}}
Run twice to show the benefits of precomputing the library prime functions.
<langsyntaxhighlight lang="julia">using Primes
 
const maxpi = 1_000_000_000
Line 594:
end
 
</langsyntaxhighlight>{{out}}
<pre>
10^0 1
Line 621:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Phi,pi]
$RecursionLimit = 10^6;
Phi[x_, 0] := x
Phi[x_, a_] := Phi[x, a] = Phi[x, a - 1] - Phi[Floor[x/Prime[a]], a - 1]
pi[n_] := Module[{a}, If[n < 2, 0, a = pi[Floor[Sqrt[n]]]; Phi[n, a] + a - 1]]
Scan[Print[pi[10^#]] &, Range[0,9]]</langsyntaxhighlight>
{{out}}
<pre>0
Line 641:
=={{header|Nim}}==
The program runs in 2.2 seconds on our laptop. Using int32 instead of naturals (64 bits on our 64 bits computer) saves 0.3 second. Using an integer rather than a tuple as key (for instance <code>x shl 32 or a</code>) saves 0.4 second.
<langsyntaxhighlight Nimlang="nim">import math, strutils, sugar, tables
 
const
Line 674:
for i in 0..9:
echo "π(10^$1) = $2".format(i, π(n))
n *= 10</langsyntaxhighlight>
 
{{out}}
Line 689:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Legendre_prime_counting_function
Line 717:
{
printf "%d %12d %10d %10d\n", $_, 10**$_, pi(10**$_), prime_count(10**$_);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 734:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">--
Line 782:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
The commented-out length(get_primes_le()) gives the same results, in about the same time,
and in both cases re-running the main loop a second time finishes near-instantly.
Line 803:
=={{header|Picat}}==
Performance is surprisingly good, considering that the Picat's library SoE is very basic and that I recompute the base primes each time pi() is called. This version takes 23 seconds on my laptop, outperforming the CoffeeScript version which takes 30 seconds.
<syntaxhighlight lang="picat">
<lang Picat>
table(+, +, nt)
phi(X, 0, _) = X.
Line 819:
writef("10^%w %w%n", K, pi(N)),
N := N * 10.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 836:
=={{header|PicoLisp}}==
Uses code from the Sieve of Eratosthenes to generate primes.
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
(setq Legendre-Max (** 10 9))
 
Line 862:
 
(bye)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 879:
=={{header|Python}}==
Using <code>primesieve</code> module (<code>pip install primesieve</code>).
<langsyntaxhighlight lang="python">from primesieve import primes
from math import isqrt
from functools import cache
Line 902:
 
for e in range(10):
print(f'10^{e}', legpi(10**e))</langsyntaxhighlight>
{{out}}
<pre>10^0 0
Line 917:
=={{header|Raku}}==
Seems like an awful lot of pointless work. Using prime sieve anyway, why not just use it?
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
 
my $sieve = Math::Primesieve.new;
Line 923:
say "10^$_\t" ~ $sieve.count: exp($_,10) for ^10;
 
say (now - INIT now) ~ ' elapsed seconds';</langsyntaxhighlight>
{{out}}
<pre>10^0 0
Line 939:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func legendre_phi(x, a) is cached {
return x if (a <= 0)
__FUNC__(x, a-1) - __FUNC__(idiv(x, a.prime), a-1)
Line 957:
legendre_prime_count(10**n), prime_count(10**n))
assert_eq(legendre_prime_count(10**n), prime_count(10**n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 973:
Alternative implementation of the Legendre phi function, by counting k-rough numbers <= n.
 
<langsyntaxhighlight lang="ruby">func rough_count (n,k) {
 
# Count of k-rough numbers <= n.
Line 1,028:
legendre_prime_count(10**n), prime_count(10**n))
assert_eq(legendre_prime_count(10**n), prime_count(10**n))
}</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension Numeric where Self: Strideable {
Line 1,109:
 
print("π(10^\(i)) = \(π(n: n))")
}</langsyntaxhighlight>
 
{{out}}
Line 1,129:
 
To sieve a billion numbers and then count the primes up to 10^k would take about 53 seconds in Wren so, as expected, the Legendre method represents a considerable speed up.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
 
var limit = 1e9.sqrt.floor
Line 1,155:
System.print("10^%(i) %(pi.call(n))")
n = n * 10
}</langsyntaxhighlight>
 
{{out}}
10,351

edits