Created
January 4, 2014 07:30
-
-
Save mikamix/8252719 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Data.Array | |
| import Data.List | |
| import Data.Ord (comparing) | |
| main = print $ maximumBy (comparing snd) $ assocs $ collatz 1000000 | |
| collatz size = memo | |
| where | |
| memo = listArray (1, size) $ 1 : [1 + count n | n <- [2..size]] | |
| count x | |
| | x' <= size = memo ! x' | |
| | otherwise = 1 + count x' | |
| where x' | |
| | even x = x `div` 2 | |
| | odd x = 3 * x + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment