fork download
  1. import Data.Array
  2. import Data.Word
  3. import Data.List
  4.  
  5. collatz_array =
  6. let
  7. upperbound = 1000000
  8. a = array (1, upperbound) [(i :: Word64, f i :: Int) | i <- [1..upperbound]]
  9. f i = i `seq`
  10. let
  11. check_f i = i `seq` if i <= upperbound then a ! i else f i
  12. in
  13. if (i == 1) then 0 else (check_f ((if (even i) then i else 3 * i + 1) `div` 2)) + 1
  14. in a
  15.  
  16. main =
  17. foldl1' (\(x1,x2) (y1,y2) -> if (x2 >= y2) then (x1, x2) else (y1, y2)) $! (assocs collatz_array)
  18.  
Success #stdin #stdout 2.4s 35376KB
stdin
Standard input is empty
stdout
(837799,329)