Created
August 31, 2014 20:28
-
-
Save fada21/10690cb0e48a8a01e66a to your computer and use it in GitHub Desktop.
findPrimes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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