Created
June 16, 2010 05:56
-
-
Save Synesso/440216 to your computer and use it in GitHub Desktop.
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
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