Pure Programmer
Blue Matrix


Cluster Map

Project: Calculate Euler's Number

You can calculate the value of [[E_(mathematical_constant)|Euler's Number]] (𝑒) using an infinite series that uses the factorial function.

`e = sum_(n=0)^oo 1/(n!)`

or

`e = 1 + 1/(1!) + 1/(2!) + 1/(3!) + 1/(4!) + 1/(5!) + ...`

where the factorial n! is:

`n! = 1*2*3*4*5*...*n`

Write a function to compute Euler's Number to a specified number of terms N that is passed to the function. Test the function with various values of N.

Output
$ javac -Xlint CalculateEulersNumber.java $ java -ea CalculateEulersNumber ℯ(1) = 2.0 ℯ(2) = 2.5 ℯ(3) = 2.6666666666666665 ℯ(5) = 2.7166666666666663 ℯ(10) = 2.7182818011463845

Solution