Note why all shootout benchmarks couldn't be added
[nofib.git] / fibon / Shootout / Pidigits / pidigits.hs
1 --
2 -- The Great Computer Language Shootout
3 -- http://shootout.alioth.debian.org/
4 -- by Don Stewart, Einar Karttunen, Branimir Maksimovic and Bertram Felgenhauer
5 --
6
7 import System.Environment
8
9 data F = F !Integer !Integer !Integer !Integer
10
11 main :: IO ()
12 main = loop 10 0 . flip take (str (F 1 0 0 1) ns) . read . head =<< getArgs
13
14 ns = [ F k (4*k+2) 0 (2*k+1) | k <- [1..] ]
15
16 loop :: Int -> Integer -> [Integer] -> IO ()
17 loop n s [] = putStrLn $ replicate n ' ' ++ "\t:" ++ show s
18 loop 0 s xs = putStrLn ("\t:"++show s) >> loop 10 s xs
19 loop n s (x:xs) = putStr (show x) >> loop (n-1) (s+1) xs
20
21 flr x (F q r s t) = (q*x + r) `div` (s*x + t)
22 comp1 (F q r s t) (F u v w x) = F (q*u+r*w) (q*v+r*x) (t*w) (t*x)
23 comp2 (F q r s t) (F u v w x) = F (q*u) (q*v+r*x) (s*u) (s*v+t*x)
24
25 str z (x:xs) | y == flr 4 z = y : str (comp1 (F 10 (-10*y) 0 1) z) (x:xs)
26 | otherwise = str (comp2 z x) xs where y = flr 3 z