Created
August 5, 2012 05:29
-
-
Save 2GMon/3262014 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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