{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy.Char8 as C import Data.Char (isDigit, digitToInt) -- | Generates as many digits of pi. -- Based on . pi_ :: Integral a => [a] pi_ = g (1, 0, 1, 1, 3, 3) where g (q, r, t, k, n, l) | 4 * q + r - t < n * t = n : g (10 * q, 10 * (r - n * t), t, k, div (10 * (3 * q + r)) t - 10 * n, l) | otherwise = g (q * k, (2 * q + r) * l, t * l, k + 1, div (q * (7 * k + 2) + r * l) (t * l), l + 2) correct :: C.ByteString -> [Int] correct = correct_ pi_ correct_ ps ss | (not . isDigit) hs = correct_ ps ts | digitToInt hs == hp = correct_ tp ts | otherwise = [digitToInt hs, hp] where hp = head ps hs = C.head ss tp = tail ps ts = C.tail ss main :: IO () main = putStrLn . unwords . map show . correct =<< C.getContents