hello, i just want to ask a simple question: does somebody have or know where to find a haskell program that calculates the number e, that is the list of infinite digits? Because i think it may be possible to do it, but i haven´t find the way to do it. what i am looking for is something like the ertostenes sifts, that prints every prime number until it run out of memory... thank you, Luis
You might want to use e n = sum [1 % fak x | x <- [0..n]] and convert it to floating point representation via fromRational (e l) Bernd Holzmüller Luis Pablo Michelena schrieb:
hello, i just want to ask a simple question: does somebody have or know where to find a haskell program that calculates the number e, that is the list of infinite digits? Because i think it may be possible to do it, but i haven´t find the way to do it. what i am looking for is something like the ertostenes sifts, that prints every prime number until it run out of memory... thank you, Luis
On Wed, Aug 15, 2001 at 02:15:31AM -0300, Luis Pablo Michelena wrote:
hello, i just want to ask a simple question: does somebody have or know where to find a haskell program that calculates the number e, that is the list of infinite digits? Because i think it may be possible to do it, but i haven't find the way to do it.
Here's a solution that uses continued fractions and uses much less memory than the other solutions proposed, though it is slow. Best, Dylan Thurston ------
module E where type ContFrac = [Integer]
Compute the decimal representation of e progressively. A continued fraction expansion for e is [2,1,2,1,1,4,1,1,6,1,...]
eContFrac :: ContFrac eContFrac = 2:aux 2 where aux n = 1:n:1:aux (n+2)
We need a general function that applies an arbitrary linear fractional transformation to a legal continued fraction, represented as a list of positive integers. The complicated guard is to see if we can output a digit regardless of what the input is; i.e., to see if the interval [1,infinity) is mapped into [k,k+1) for some k.
-- ratTrans (a,b,c,d) x: compute (a + bx)/(c+dx) as a continued fraction ratTrans :: (Integer,Integer,Integer,Integer) -> ContFrac -> ContFrac -- Output a digit if we can ratTrans (a,b,c,d) xs | ((signum c == signum d) || (abs c < abs d)) && -- No pole in range (c+d)*q <= a+b && (c+d)*q + (c+d) > a+b -- Next digit is determined = q:ratTrans (c,d,a-q*c,b-q*d) xs where q = b `div` d ratTrans (a,b,c,d) (x:xs) = ratTrans (b,a+x*b,d,c+x*d) xs
Finally, we convert a continued fraction to digits by repeatedly multiplying by 10.
toDigits :: ContFrac -> [Integer] toDigits (x:xs) = x:toDigits (ratTrans (10,0,0,1) xs)
e = toDigits eContFrac
hello, i just want to ask a simple question: does somebody have or know where to find a haskell program that calculates the number e, that is the list of infinite digits?
Hugs includes such a function in its demos. Look at eFactBase in the file hugs98/demos/Examples.hs There's a lot of other functions that exploit lazy lists in that file. -- Alastair Reid reid@cs.utah.edu http://www.cs.utah.edu/~reid/
participants (4)
-
Alastair David Reid -
Bernd Holzmüller -
Dylan Thurston -
Luis Pablo Michelena