Created
June 30, 2013 01:22
-
-
Save outsideris/5893384 to your computer and use it in GitHub Desktop.
euler problem 12
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
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