Algorithm 如何生成表示';e';

Algorithm 如何生成表示';e';,algorithm,haskell,clojure,lazy-evaluation,Algorithm,Haskell,Clojure,Lazy Evaluation,我试图在“e”的展开式中找到前100000个二进制数字。是否有一种算法可以将“e”的二进制数字生成为一个无限列表?这是Haskell中的e的一个无界插口: main = print $ stream (1,0,1) [(n, a*d, d) | (n,d,a) <- map f [1..]] where f k = (1, k, 1) stream z (x:xs) | lbound == approx z 2 = lbound : stream (mul

我试图在“e”的展开式中找到前100000个二进制数字。是否有一种算法可以将“e”的二进制数字生成为一个无限列表?

这是Haskell中的
e
的一个无界插口:

main = print $ stream (1,0,1) [(n, a*d, d) | (n,d,a) <- map f [1..]]
    where
        f k = (1, k, 1)

stream z (x:xs)
    | lbound == approx z 2 = lbound : stream (mul (10, -10*lbound, 1) z) (x:xs)
    | otherwise            =          stream (mul z x) xs
  where
    lbound = approx z 1

approx (a,b,c) n = (a*n + b) `div` c

mul (a,b,c) (d,e,f) = (a*d, a*e + b*f, c*f)

如果你对这些有趣的算法感兴趣,我推荐吉本的论文。

你可能会对此感兴趣。对于100000个二进制数字,30200个十进制数字就足够了:

Prelude> 100000 * logBase 10 2
30102.999566398114
Prelude> :m + Data.Number.CReal
Prelude> :set +s
Prelude Data.Number.CReal> last $ showCReal 1000 (exp 1)
'4'
(0.34 secs, 34061824 bytes)
Prelude Data.Number.CReal> last $ showCReal 2000 (exp 1)
'4'
(1.25 secs, 104478784 bytes)
Prelude Data.Number.CReal> last $ showCReal 4000 (exp 1)
'7'
(5.96 secs, 355775928 bytes)
Prelude Data.Number.CReal> last $ showCReal 8000 (exp 1)
'2'
(20.89 secs, 1298942504 bytes)
这个模式在我看来是二次的,所以在我的机器上计算exp 1的头30200个数字可能需要五到六分钟。可能会接受直接以二进制输出的修补程序(从而避免转换为十进制和十进制)

编辑:投影满意,计算时间不到六分钟

Prelude Data.Number.CReal> showCReal 30200 (exp 1)
"2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334...middle snipped due to StackOverflow message limit...39106913376148418348845963656215266103322394174671"
(349.44 secs, 17096829912 bytes)

请看这个问题:请看例如“无界插口算法”例如,Pi,为什么不从其他地方(例如,此处:)获取数字,并在运行时将其转换为二进制表示形式?如果需要将其更改为输出二进制,是否将10替换为2?
Prelude Data.Number.CReal> showCReal 30200 (exp 1)
"2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334...middle snipped due to StackOverflow message limit...39106913376148418348845963656215266103322394174671"
(349.44 secs, 17096829912 bytes)