The sieve of Eratosthenes in F#

Posted by IVlad on Stack Overflow See other posts from Stack Overflow or by IVlad
Published on 2011-01-07T20:01:30Z Indexed on 2011/01/09 5:53 UTC
Read the original article Hit count: 299

Filed under:
|

I am interested in an implementation of the sieve of eratosthenes in purely functional F#. I am interested in an implementation of the actual sieve, not the naive functional implementation that isn't really the sieve, so not something like this:

let rec PseudoSieve list =
    match list with
    | hd::tl -> hd :: (PseudoSieve <| List.filter (fun x -> x % hd <> 0) tl)
    | [] -> []

The second link above briefly describes an algorithm that would require the use of a multimap, which isn't available in F# as far as I know. The Haskell implementation given uses a map that supports an insertWith method, which I haven't seen available in the F# functional map.

Does anyone know a way to translate the given Haskell map code to F#, or perhaps knows of alternative implementation methods or sieving algorithms that are as efficient and better suited for a functional implementation or F#?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about F#