21
\$\begingroup\$

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:

Maclaurin series expansion of e^x

By letting x equal 1, we obtain

Maclaurin series expansion of e

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.

\$\endgroup\$
  • 7
    \$\begingroup\$ 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? \$\endgroup\$ – FryAmTheEggman Jun 10 '16 at 20:12
  • 12
    \$\begingroup\$ Some test cases would be neat. \$\endgroup\$ – Lynn Jun 10 '16 at 20:15
  • 7
    \$\begingroup\$ (This kind of issue is preferably resolved in the sandbox – if you post your challenges there first, golfers will give useful feedback.) \$\endgroup\$ – Lynn Jun 10 '16 at 20:27
  • 2
    \$\begingroup\$ Is x^n the nth term or the (n+1)th? \$\endgroup\$ – msh210 Jun 10 '16 at 20:31
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis Jun 11 '16 at 0:04

39 Answers 39

1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 44 bytes

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

Stax, 5 bytes

òo⌠├p

Run and debug it

Produces output as a fraction.

\$\endgroup\$
1
\$\begingroup\$

cQuents, 7 bytes

;1/f_-$

Try it online!

Explanation

;           Series - output sum of first n terms in sequence
            Each term is 
 1/                      1 / 
   f   )                     factorial (           )
    _-                                         - 1
      $                                  index
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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> **) \$\endgroup\$ – caird coinheringaahing Sep 25 at 16:15
  • \$\begingroup\$ @cairdcoinheringaahing Thank you very much! I appreciate! \$\endgroup\$ – Motanovici Sep 25 at 16:17
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Keg -rr, 18 15 bytes

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

Try it online!

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

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.