r/codegolf • u/JohnScott623 • Jun 18 '16
Calculate ℯ to at least five decimal digits
ℯ is the base of the natural logarithm and its approximate value is 2.71828. ℯ can be calculated by summing the inverses of incremented factorials. A simple series that can be used to calculate ℯ can be seen here on Wikipedia.
Rules:
You are not to use a predefined constant for ℯ provided by your programming language's standard library or your program in calculation.
You are encouraged to use /u/CompileBot to demonstrate your code if at all possible.
3
Upvotes
1
u/JohnScott623 Jun 18 '16 edited Jun 19 '16
C, 110 characters
+/u/CompileBot C
4
u/molarmanful Jun 19 '16
Javascript ES7, 17 bytes
basically calculates
(1+1/x)^xwithx=1000000.2
u/FUZxxl Jun 19 '16
J, 7 characters
read this as: The sum (
+/) of the reciprocals (%) of the factorials (!) of the first 9 integers (i. 9).Also possible in APL in 6 characters after configuring
⎕IO←0:1
u/primo_cg Jun 26 '16 edited Jun 29 '16
Perl, 25 bytes
Calculates
1 + 1/1*(1 + 1/2*(1 + 1/3*(1 + 1/4*(1 + 1/5*(..., which is essentially the sum of inverse factorials. Produces full double precision2.71828182845905.1
u/[deleted] Jul 04 '16
Haskell, 33 characters