test
Summary
Details
test.hs
1 -- The Computer Language Benchmarks Game
2 -- http://shootout.alioth.debian.org/
3 -- contributed by Bryan O'Sullivan
4 -- modified by Eugene Kirpichov: pidgits only generates
5 -- the result string instead of printing it. For some
6 -- reason, this gives a speedup.
7
8 import System.Environment (getArgs)
9
10 pidgits n = 0 % (0 # (1,0,1)) where
11 i%ds
12 | i >= n = []
13 | True = (concat h ++ "\t:" ++ show j ++ "\n") ++ j%t
14 where k = i+10; j = min n k
15 (h,t) | k > n = (take (n`mod`10) ds ++ replicate (k-n) " ",[])
16 | True = splitAt 10 ds
17 j # s | n>a || r+n>=d = k # t
18 | True = show q : k # (n*10,(a-(q*d))*10,d)
19 where k = j+1; t@(n,a,d)=k&s; (q,r)=(n*3+a)`divMod`d
20 j&(n,a,d) = (n*j,(a+n*2)*y,d*y) where y=(j*2+1)
21
22 main = putStr.pidgits.read.head =<< getArgs