Skip to content

Instantly share code, notes, and snippets.

@kazua
Created January 13, 2013 19:17
Show Gist options
  • Save kazua/4525748 to your computer and use it in GitHub Desktop.
Save kazua/4525748 to your computer and use it in GitHub Desktop.
Project Euler Problem 14
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2014
//K.A
object problem14 {
def getCollatzDgt(nm : Long, acl : Int) : Int = nm match {
case a if a == 1 => acl + 1
case a => {
val nn = if (nm % 2 == 0) nm / 2 else (3 * nm) + 1
getCollatzDgt(nn, acl + 1)
}
}
def getCollatzDgtMax(mn : Int) : Int = {
(1 until mn).map(i => List(i, getCollatzDgt(i, 0))).reduceLeft((a, b) => if (a.tail.head > b.tail.head) a else b).head
}
def main(args : Array[String]) {
val mn = 1000000
println(getCollatzDgtMax(mn))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment