Copyright 2012 Ken Takusagawa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . > import System.Environment(getArgs) > import Codec.Compression.GZip(compress) > import qualified Data.ByteString.Lazy as B > type Item = (Integer,(Integer,Integer,Integer,Integer)) > g :: (Integer,Integer,Integer,Integer) -> [Item] > g(q,r,t,i) = let (u,y)=(3*(3*i+1)*(3*i+2),div(q*(27*i-12)+5*r)(5*t)) > in (y,(q,r,t,i)) : g(10*q*i*(2*i-1),10*u*(q*(5*i-2)+r-y*t),t*u,i+1) > piG3 :: [Item] > piG3 = g(1,180,60,2) > main = do > args <- getArgs > case args of > ["run",skipdigits] -> do > putStrLn $ "skipping " ++ skipdigits ++ "." > mapM_ myprint $ seqdrop (read skipdigits) $ piG3 > ["readstate"] -> getContents >>= (mapM_ myprint . g . read) mapM_ colprint $ seqdrop (read$ head args) $ piG3 > myprint :: Item -> IO () > myprint (digit,state@(q,r,t,i)) = do > print digit start at 2 to get the initial state > if (0==(mod i 10000)) then > B.writeFile "streaming-pi-spigot.state.gz" $ (stategz state) > else return () > stategz :: (Integer,Integer,Integer,Integer) -> B.ByteString; > stategz state = compress $ B.pack $ map (fromIntegral . fromEnum) $ show state Avoid the giant thunk from being created if you use regular "drop". (works for infinite lists) > seqdrop 0 l = l > seqdrop n (h:t) = (seq (fst h) (seqdrop (pred n) t)) The digits seem to float up. > colprint :: Integer -> IO () > colprint i = let di :: Int > di = fromInteger i > in do putStr $(concat $ replicate di " ") > print di 1.5 million digits (3 million bytes) in 21h 14min, 133 MB ram