Skip to content

Instantly share code, notes, and snippets.

@mikamix
Created January 4, 2014 07:30
Show Gist options
  • Select an option

  • Save mikamix/8252719 to your computer and use it in GitHub Desktop.

Select an option

Save mikamix/8252719 to your computer and use it in GitHub Desktop.
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