Skip to content

Instantly share code, notes, and snippets.

@fada21
Created August 31, 2014 20:28
Show Gist options
  • Save fada21/10690cb0e48a8a01e66a to your computer and use it in GitHub Desktop.
Save fada21/10690cb0e48a8a01e66a to your computer and use it in GitHub Desktop.
findPrimes
def findPrimes(toInt: Int): Seq[Int] = {
def eras(l: Seq[Int]): Seq[Int] = {
val size = l.size
val mlist = collection.mutable.IndexedSeq(l:_*)
def crossout(x: Int): Unit = {
def crossout(xbase: Int, x: Int):Unit = {
if (x<size) {
mlist(x) = 0
crossout(xbase, x+xbase)
}
else ()
}
crossout(x,x)
}
def eras() : List[Int] = {
mlist.find(_>0) match {
case Some(x) =>
if (x<size) {
crossout(x)
x::eras
}
else Nil
case None => Nil
}
}
if (size<2)
Seq[Int]()
else
mlist(1) = 0;
eras
}
if (toInt < 0)
Seq[Int]()
else {
val primes = eras (0 to toInt)
primes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment