Skip to content

Instantly share code, notes, and snippets.

@outsideris
Created June 1, 2013 17:38
Show Gist options
  • Save outsideris/5691126 to your computer and use it in GitHub Desktop.
Save outsideris/5691126 to your computer and use it in GitHub Desktop.
euler problem 7
import scala.language.implicitConversions
import scala.language.postfixOps
class Prime(val i: Int) {
def isPrime = {
if (i <= 1) false
else if (i == 2) true
else { !(2 to (i-1)).exists(i % _ == 0)}
}
}
implicit def intWrapper(i: Int) : Prime = new Prime(i)
def loop(num: Int, count: Int, target:Int): Int = {
num match {
case n if (n isPrime) && count == target - 1 => num
case n if n isPrime => loop(n + 1, count + 1, target)
case _ => loop(num + 1, count, target)
}
}
println( loop(2, 0, 10001) ) // 104743
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment