Skip to content

Instantly share code, notes, and snippets.

@Synesso
Created June 16, 2010 05:56
Show Gist options
  • Save Synesso/440216 to your computer and use it in GitHub Desktop.
Save Synesso/440216 to your computer and use it in GitHub Desktop.
def collatz(start: Long) = {
def collatz_i(chain: List[Long]): List[Long] = {
if (chain.head == 1) chain
else if (chain.head % 2 == 0) collatz_i((chain.head / 2) :: chain)
else collatz_i((chain.head * 3 + 1) :: chain)
}
collatz_i(start :: Nil)
}
(2 until 1000000).foldLeft((1,collatz(1).size)){(acc,next) =>
val chainSize = collatz(next).size
if (chainSize > acc._2) (next, chainSize) else acc
}._1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment