Skip to content

Instantly share code, notes, and snippets.

@motylwg
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motylwg/9788f95f0ac9bb147e02 to your computer and use it in GitHub Desktop.
Save motylwg/9788f95f0ac9bb147e02 to your computer and use it in GitHub Desktop.
let rec sieve (xs : int seq) =
seq {
let p = xs |> Seq.take 1 |> Seq.exactlyOne
yield p
yield! xs |> Seq.skip 1 |> Seq.filter (fun x -> x % p <> 0) |> sieve
}
let ints = Seq.initInfinite (fun x -> x + 2)
let primes = sieve ints
@motylwg
Copy link
Author

motylwg commented Feb 17, 2015

Note: This is not the genuine Sieve of Eratosthenes but rather the inefficient naive sieve.

See http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment