Programming Help - Solving for e(n)

281 Views Asked by At

I've been wrestling with this issue for a week and I just need some guidance on the math part of it. If I could just understand the math behind it I could piece together the functions to make it work. The assignment is;


Design and develop a C++ program for Calculating e(n) when delta <= 0.000001

  • e(n-1) = 1 + 1/1! + 1/2! + 1/3! + 1/4! + … + 1/(n-1)!
  • e(n) = 1 + 1/1! + 1/2! + 1/3! + 1/4! + … + 1/(n)!

delta = e(n) – e(n-1)

You do not have any input to the program. Your output should be something like this:

  • N = 2 e(1) = 2 e(2) = 2.5 delta = 0.5
  • N = 3 e(2) = 2.5 e(3) = 2.565 delta = 0.065 ...

You must use recursive function calls.


My first issue is the math and the variables that would contain them.

  • the delta, e(n), and e(n-1) variable must doubles
  • if e(n) = 1 + 1 / 1! = 2 then e(n-1) must equal 1, which means delta = 1 (that's my thinking anyway) I'm just not sure of the math behind the .5 delta the first time and the 0.065 in the second iteration.

Can someone point me in the right direction on this problem?

Thank you,

T

3

There are 3 best solutions below

0
On

your e(n) series is the Tylor series for $e^x$ evaluated at $x=1$, so it will converge to $e$. As you keep adding terms, the error between your current sum and $e$ will decrease (since this series converges), and so will the differences between a term and the previous one. $e=2.718281828...$

'the delta, e(n), and e(n-1) variable must doubles' ok

'if e(n) = 1 + 1 / 1! = 2 then e(n-1) must equal 1, which means delta = 1' ok

'I'm just not sure of the math behind the .5 delta the first time and the 0.065 in the second iteration.' In each iteration the errors will keep decreasing, since your program will converge to $e$ as I said above, so don't be troubled by it.

It seems you get it all right, just go ahead and code!

0
On

e(1) = 1 + 1/1! = 1 + 1 = 2

e(2) = 1 + 1/1! + 1/2! = 1 + 1 + 0.5 = 2.5

e(3) = 1 + 1/1! + 1/2! + 1/3! = 1 + 1 + 0.5 + 0.167 = 2.667

e(4) = 1 + 1/1! + 1/2! + 1/3! + 1/4! = 1 + 1 + 0.5 + 0.167 + 0.042 = 2.709

For N = 2, delta = e(2) - e(1), or 0.5

For N = 3, delta = e(3) - e(2), or 0.167

The above have accumulated rounding errors, but the idea is there. The value of e(3) = 2.565 in your question does not seem correct.

As for how to approach the problem, you're looking for a delta <= 0.000001

Look at the difference between terms e(n) and e(n-1). Each time you increase n, you add a new term of 1/(new n)!

As such, you'll find e to your required accuracy when 1/n! <= 0.000001

In other terms, you'll find that you reach your required accuracy when n! >= 1,000,000

This first occurs when n = 10. A double will be sufficient to hold this equation.

0
On

$e(n)$ is an approximation to the Euler's number $e=1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \ldots$.

You have $e(n) = 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \ldots + \frac{1}{n!}$ and $e(n-1) = 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \ldots + \frac{1}{(n-1)!}$, therefore, $$e(n) - e(n-1) = 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \ldots + \frac{1}{n!} - \Big(1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \ldots + \frac{1}{(n-1)!}\Big) = \frac{1}{n!}.$$

Because $e(n) - e(n-1) = \frac{1}{n!}$, you have that delta$= \frac{1}{n!}$, using double you will get not too far, but maybe will be enough for your goal.

Finally, if $e(n) - e(n-1) = \frac{1}{n!}$, then $e(n) = e(n-1) + \frac{1}{n!}$, so there is your recursion formula.