Skip to content

Instantly share code, notes, and snippets.

@dcsobral
Created July 5, 2011 11:52
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 dcsobral/1064703 to your computer and use it in GitHub Desktop.
Save dcsobral/1064703 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes One-Liner
(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))
// Forward-pipe courtesy of Scalaz, or look here: http://stevegilham.blogspot.com/2009/01/pipe-operator-in-scala.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment