Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Created November 9, 2012 17:40
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 bgreenlee/4047063 to your computer and use it in GitHub Desktop.
Save bgreenlee/4047063 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Scala #scala #algorithms
# sieve of eratosthenes
def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0))
def from(n: Int): Stream[Int] = n #:: from(n + 1)
def primes = sieve(from(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment