Skip to content

Instantly share code, notes, and snippets.

@neizod
Created September 2, 2012 19:45
Show Gist options
  • Save neizod/3603862 to your computer and use it in GitHub Desktop.
Save neizod/3603862 to your computer and use it in GitHub Desktop.
Compute mathematical constant `e`.
e n = 1 + (foldr1 (\ a x -> a + 1 / x) $ take n [i ^ j | i <- [2,4..], j <- [0,0,1]])
-- sample:
-- e 1 -> 2.0
-- e 2 -> 3.0
-- e 6 -> 2.71875
-- e 100 -> 2.7182818284590455
-- see more:
-- <http://en.wikipedia.org/wiki/E_(mathematical_constant)#Representations>
e n = (\(e:es) -> (e:'.':es)) $ show $ (\(a,l) -> (10^n * sum l) `div` a) $ (\(a,l) -> (a, [a `div` i | i <- l])) $ (\l -> (product l, l)) [factorial n | n <- [0..4*n`div`3]] where factorial 0 = 1; factorial n = n * factorial (n-1)
-- sample:
-- e 100 -> "2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment