A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
19
Task/Hailstone-sequence/Haskell/hailstone-sequence.hs
Normal file
19
Task/Hailstone-sequence/Haskell/hailstone-sequence.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Data.List (maximumBy)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
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
|
||||
print $ length h27
|
||||
let h4 = show $ take 4 h27
|
||||
let t4 = show $ drop (length h27 - 4) h27
|
||||
putStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
|
||||
print $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) [1..100000]
|
||||
Loading…
Add table
Add a link
Reference in a new issue