# Approximation of e

We all know that the Euler's number, denoted by e, to the power of some variable x, can be approximated by using the Maclaurin Series expansion:

By letting x equal 1, we obtain

## Challenge

Write a program in any language which approximates Euler's number by taking in an input N and calculates the series to the N-th term. Note that the first term has denominator 0!, not 1!, i.e. N=1 corresponds to 1/0!.

## Scoring

Program with least amount of bytes wins.

• Given large enough N the results will be the same if using a finite precision floating point number. Is that behaviour acceptable or does the result have to progressively get more accurate as N approaches infinity? – FryAmTheEggman Jun 10 '16 at 20:12
• Some test cases would be neat. – Lynn Jun 10 '16 at 20:15
• (This kind of issue is preferably resolved in the sandbox – if you post your challenges there first, golfers will give useful feedback.) – Lynn Jun 10 '16 at 20:27
• Is x^n the nth term or the (n+1)th? – msh210 Jun 10 '16 at 20:31
• I personally despise it, but there are people who refer to the term at index 0 as zeroth term. Independently of our thoughts on that matter, the question should be as clear as possible. Also, a few test cases to verify that the solutions are working correctly, would be very helpful. – Dennis Jun 11 '16 at 0:04

## F#, 87 bytes

let rec f x=if x<2 then 1 else f(x-1)*x
let e N=Seq.sumBy(fun x->1./float(f x)){0..N-1}


Try it online!

A direct implementation of the Maclaurin expansion. Unfortunately over 35 terms the denominator is too small, so the sum becomes infinity.

# JavaScript (ES6), 44 bytes

n=>{for(a=d=i=1;i<=n;a+=1/d)d/=i++;return a}


# Stax, 5 bytes

òo⌠├p


Run and debug it

Produces output as a fraction.

;1/f_-$ Try it online! # Explanation ; Series - output sum of first n terms in sequence Each term is 1/ 1 / f ) factorial ( ) _- - 1$                                  index


# Mathematica, 26 bytes

Sum[1/n!,{n,0,Input[]}]//N


Pretty straight forward: takes the sum of the inverse of n factorial, with n starting from 0 and all the way to the desired number. The //N is used to give an approximate value of the fraction yielded by the sum. The code can be checked online , for free, via the wolfram programming labs:

Wolfram Labs

• Welcome to the site, and nice first answer! I've edited your post ever so slightly to improve clarity and to format it in the standard manner for answers on the site (# <language name> rather than ** <language> **) – caird coinheringaahing Sep 25 at 16:15
• @cairdcoinheringaahing Thank you very much! I appreciate! – Motanovici Sep 25 at 16:17

# k4, 16 15 bytes

{+/%*/'1_',\!x}


{             } / lambda
!x  / enumerate x; (!4 -> 0 1 2 3)
,\    / join scan; joins successive elements and returns intermediate results (,0;0 1;0 1 2;0 1 2 3)
1_'      / 1 drop each; drops leading 0s (();,1;1 2;1 2 3)
*/'         / multiply over each; 1 1 2 6
%            / reciprocal; 1 1 0.5 0.1666667
+/             / sum


# Wren, 81 bytes

Fn.new{|N|
var e=0
for(i in 1..N)e=e+1/(1..i).reduce{|a,b|a*b}
return e+1
}


Try it online!

# Keg-rr, 18 15 bytes

11(¿|:¡1\$/"+"1+


Try it online!

This took a whole bunch of fiddling to figure out. It works!!

# Ruby, 43 42 bytes

-1 byte by calculating using Rational instead of Float.

->n{(0..n).sum{|i|1/(1..i).reduce(1r,:*)}}


Try it online!