21 lines
580 B
Plaintext
21 lines
580 B
Plaintext
module Hailstone where
|
|
|
|
import Data.List (maximumBy)
|
|
|
|
hailstone :: Int -> [Int]
|
|
hailstone 1 = [1]
|
|
hailstone n | even n = n : hailstone (n `div` 2)
|
|
| otherwise = n : hailstone (n * 3 + 1)
|
|
|
|
withResult :: (t -> t1) -> t -> (t1, t)
|
|
withResult f x = (f x, x)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
let h27 = hailstone 27
|
|
putStrLn $ show $ length h27
|
|
let h4 = show $ take 4 h27
|
|
let t4 = show $ drop (length h27 - 4) h27
|
|
putStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
|
|
putStrLn $ show $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) [1..100000]
|