a*****e 发帖数: 1700 | 2 你这个算法写得不是特别好,造成最后要 take N, 然后还要预估一个 N
为什么不从 most significant digit 开始排呢?这样也用不着 [0,0..]
我给你改了一下
initial = [1]
tworaise :: [[Int]]
tworaise = initial : map double tworaise
double :: [Int] -> [Int]
double = dropWhile (==0) . foldr f [0]
where
f !d ((!x):xs) =
let (q, r) = (d * 2) `quotRem` 10
in q:r+x:xs
main = print $ sum $ tworaise !! 10000
运行起来是这样的:
bash-4.2$ ghc -XBangPatterns -O2 test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
Linking test ...
bash-4.2$ time .... 阅读全帖 |
|