Skip to content

Instantly share code, notes, and snippets.

@moznion
Created July 29, 2012 12:52
Show Gist options
  • Save moznion/3198524 to your computer and use it in GitHub Desktop.
Save moznion/3198524 to your computer and use it in GitHub Desktop.
スタートHaskell 2 第2回目演習 コラッツ数列
import Data.List
import Data.Maybe
collatz :: Int -> [Int]
collatz 1 = [1]
collatz x
| even x = [x] ++ (collatz $ x `div` 2)
| otherwise = [x] ++ (collatz $ 3 * x + 1)
longestLengthOfCollatz :: Int
longestLengthOfCollatz = fromJust (elemIndex (maximum lengthList) lengthList) + 1
where
lengthList = [length $ collatz x | x <- [1..100]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment