### Approximating the value of e I am close but I need help. The problem is: "Interestingly, the value of e can also be approximated using the following expression:

2+1/(1+1/(2+2/(3+3/(4+4/(5+5/(n+n))))))

Write a program that uses this formula to approximate the value of e.

The program should prompt the user to input a value for n and then output the approximate value of e. Use setprecision(15) to format the output for the tests.

Test your program for n = 3, 5, 10, 50, and 100."

I can get the answer for when n=3 that is rounded (aka e=3 not 2.700000000000), but I do not know how to loop these values or proceed forth in increments.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include using namespace std; int main() { int n; int value; int x = 0; int t = 3; int factorial; cout << fixed << showpoint << setprecision(15); cout << "Please input the value of n: "; cin >> n; while (n >= t) { if ((n = t)) {factorial = t+t; x = 1 / (1+1/(2+2/(factorial))); value = 2 + x; cout << t; break;} else if ((n > t)) {t = 5; factorial += (t+t); x = 1 / (1+1/(2+2/(3+3/(4+4/(factorial))))); value = 2 + x; t++;} else {cout << "Nothing.";} } cout << "This is the value of e: " << value; return 0; }``````
Last edited on it may be by design but 100 factorial is gigantic and may not fit into int (i don't recall if it fits in unsigned 64 bit or not, but you can check it). Some professors will throw this out and let you get an incorrect value to debug, so fair warning.

if(n=t) says "assign n to the value of t and if the new n is not zero, proceed else skip"
you probably meant n==t which says if n and t are the same.

the 'simple and dumb way'

 ``123456789101112131415`` `````` int main() { uint64_t fact = 1; double e = 1.0; for(int i = 1; i < 30; i++) { fact *= i; e += 1.0/fact; } cout <

that is the answer until factorial gets too large. You can re-arrange it to avoid the factorials, which will eventually underflow a double but that is ok, you would just be adding zero a bunch of times doing nothing.
the formula they gave you can be done as well if you are not allowed to use the simple form (its the same equation, really, but its a bit to show that to be true!) You are trying to hard code the formula in your loop, and that won't work. You need to understand the formula and do one iteration worth each loop, building a running total. You need to unravel the MATH part of this before you try to CODE it. The code will be simple, I promise. The math may take you a few min fooling with it on paper to get to where you need to be to see what to do.
Last edited on The exercise has nothing whatsoever to do with factorials.

 ``1234567891011121314`` ``````#include #include using namespace std; double continuedFraction( int n, int nmax ) { return n + ( n == nmax ? nmax : n / continuedFraction( n + 1, nmax ) ); } int main() { cout << fixed << setprecision( 12 ); for ( int n : { 3, 5, 10, 50, 100 } ) cout << n << ": " << 2 + 1.0 / continuedFraction( 1, n ) << '\n'; }``````

 ```3: 2.700000000000 5: 2.717770034843 10: 2.718281834730 50: 2.718281828459 100: 2.718281828459```

Last edited on OP line 11 and 18 are trying to use them (in a weird way that I could not follow), so I gave him the factorial approach. But you are correct, the question does not require them. If you want to do this continued fraction non-recursively then you can run the loop backwards.

For more similar ones see
https://en.wikipedia.org/wiki/Continued_fraction

 ``12345678910111213141516`` ``````#include #include using namespace std; double continuedFraction( int nmax ) { double sum = 1.0; for ( int i = nmax; i; i-- ) sum = i + i / sum; return sum; } int main() { cout << fixed << setprecision( 12 ); for ( int n : { 3, 5, 10, 50, 100 } ) cout << n << ": " << 2 + 1.0 / continuedFraction( n ) << '\n'; }``````
Last edited on I had to tweak the code so that it would allow input from the bot that grades it, but other than that, it was fine. I get so frustrated that my book and my teacher don't really help me to understand how to do these coding assignments. All in all, you guys rock. Thank you for helping me with this.
Topic archived. No new replies allowed.