Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created May 1, 2015 06:01
Show Gist options
  • Save zaltoprofen/31d4a29252cdbf698ff2 to your computer and use it in GitHub Desktop.
Save zaltoprofen/31d4a29252cdbf698ff2 to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
object Prog {
def primes(n: Int): List[Int] = {
@tailrec
def loop(cs: List[Int], ps: List[Int]): List[Int] = {
cs.headOption match {
case Some(n: Int) => loop(cs.tail.filterNot(_ % n == 0), n::ps)
case None => ps
}
}
loop((2 until n).toList, Nil).reverse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment