Skip to content

Instantly share code, notes, and snippets.

@outsideris
Created June 30, 2013 01:22
Show Gist options
  • Save outsideris/5893384 to your computer and use it in GitHub Desktop.
Save outsideris/5893384 to your computer and use it in GitHub Desktop.
euler problem 12
import scala.annotation.tailrec
@tailrec
def countDivisors(i: Int, n: Int, count:Int = 0):Int = {
val end = math.sqrt(n).toInt
(n % i) match {
case x if n == 1 => 1
case x if i >= end && end * end == n => count + 2
case x if i >= end => count
case 0 => countDivisors(i + 1, n, count + 2)
case _ => countDivisors(i + 1, n, count)
}
}
@tailrec
def loop(n:Int, limit:Int, i:Int = 1):Int = {
countDivisors(1, n) match {
case x if x > limit => n
case _ => loop(n + i + 1, limit, i + 1)
}
}
loop(1, 500) // 76576500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment