Created
July 29, 2012 12:52
-
-
Save moznion/3198524 to your computer and use it in GitHub Desktop.
スタートHaskell 2 第2回目演習 コラッツ数列
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.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