euler/014.hs
2011-11-09 16:15:32 +01:00

39 lines
1.2 KiB
Haskell

import Data.List
type YInt = Int
collatz :: YInt -> YInt
collatz n = if n<0 then 1 else if even n then n `quot` 2 else 3*n+1
-- The l `seq` is necessary to not be lazy (non-strict)
-- And not to fill the stack
lencollatz' :: YInt -> YInt -> YInt
lencollatz' l 1 = l
lencollatz' l n = l `seq` lencollatz' (l+1) (collatz n)
lencollatz = lencollatz' 0
-- The j `seq` is necessary to not be lazy (non-strict)
-- And not to fill the stack
maximalIndex :: (YInt,YInt) -> YInt -> [YInt] -> (YInt,YInt)
maximalIndex (m,i) j [] = (m,i)
maximalIndex (m,i) j (x:xs) = j `seq` maximalIndex res (j+1) xs
where res = if x>m then (x,j) else (m,i)
showCollatz :: YInt -> String
showCollatz 1 = "1"
showCollatz n = show n ++ "" ++ showCollatz ( collatz n )
pure (Just x) = x
main = do
putStrLn $ "max len: " ++ show m
putStrLn $ "max value: " ++ show i
-- print mrec
-- print irec
-- putStrLn $ showCollatz $ toInteger i
where
array = map lencollatz [1..1000000]
m = maximum array
i = 1 + pure (elemIndex m array)
(mrec,irec) = maximalIndex (0,0) 1 array