Skip to content

Instantly share code, notes, and snippets.

@2GMon
Created August 5, 2012 05:29
Show Gist options
  • Save 2GMon/3262014 to your computer and use it in GitHub Desktop.
Save 2GMon/3262014 to your computer and use it in GitHub Desktop.
import Data.List
collatz :: Int -> [Int]
collatz 1 = [1]
collatz x
| even x = x : collatz (x `div` 2)
| odd x = x : collatz (3 * x + 1)
collatzLength :: Int -> (Int, Int)
collatzLength x = (x, length (collatz x))
maxCollatzLength :: [(Int, Int)] -> (Int, Int)
maxCollatzLength xs = foldl' (\acc x -> if snd acc > snd x then acc else x) (0, 0) xs
main = print $ fst $ maxCollatzLength $ map collatzLength [1..(1000000 - 1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment