Legendre prime counting function: Difference between revisions

Content added Content deleted
m (put Java contribution in alphabetic order...)
m (→‎Non Memoized Haskell Versions: Haskell: improve comments...)
Line 1,370: Line 1,370:
'''The Non-Recursive Partial Sieving Algorithm'''
'''The Non-Recursive Partial Sieving Algorithm'''


Just substitute the following Haskell code for the `countPrimes` function above to enjoy the further gain in speed:
Just substitute the following Haskell code for the `countPrimes` function above to enjoy the further gain in speed; this version may appear to be recursive to those unfamiliar with Functional Programming (FP) implementations but FP generally turns all function recursions in tail-call position (which all of these are) into the same compiled code as a simple loop in other languages:
<syntaxhighlight lang="haskell">countPrimesxx :: Word64 -> Int64
<syntaxhighlight lang="haskell">countPrimes :: Word64 -> Int64
countPrimesxx n =
countPrimes n =
if n < 3 then (if n < 2 then 0 else 1) else
if n < 3 then (if n < 2 then 0 else 1) else
let
let