Legendre prime counting function: Difference between revisions
Legendre prime counting function (view source)
Revision as of 10:12, 17 December 2023
, 5 months ago→{{header|Wren}}: Changed to Wren S/H
(Added solution for Pascal) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
Line 3,830:
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.
<syntaxhighlight lang="
var pi = Fn.new { |n|
Line 3,874:
The memoized version requires a cache of around 6.5 million map entries, which at 8 bytes each (all numbers are 64 bit floats in Wren) for both the key and the value, equates in round figures to memory usage of 104 MB on top of that needed for the prime sieve. The following version strips out memoization and, whilst somewhat slower at 5.4 seconds, may be preferred in a constrained memory environment.
<syntaxhighlight lang="
var pi = Fn.new { |n|
Line 3,905:
{{trans|Vlang}}
This non-memoized, non-recursive version is far quicker than the first two versions, running in about 114 milliseconds. Nowhere near as quick as V, of course, but acceptable for Wren which is interpreted and only has one built-in numeric type, a 64 bit float.
<syntaxhighlight lang="
var masks = [1, 2, 4, 8, 16, 32, 64, 128]
|