test :: Int -> Int test x | even x = x `div` 2 | otherwise = 3*x + 1 collatz :: Int -> [Int] collatz 1 = [1] collatz x = x : collatz (test x) numCollatz = map (length . collatz) [1..] -- Convert into a table for gnuplot format x y = show x ++ "\t" ++ show y table x = unlines (zipWith format [1..] (take x numCollatz)) main = writeFile "collatz.dat" (table 100001)