Calculate Euler Number with Recursion
I recently went through several posts I had made on one of the development forums back then in 2005. I've found one of them interesting and wanted to repost it here. The question went like this:
"How can I calculate euler number with recursion ?"
Euler number (or e) can be calculated with the following formula in math:
sum = 1/0! + 1/1! + 1/2! + ... + 1/n!
Here one can notice the use of the Factorial function which can be implemented via recursion itself like so:
int Factorial(int n) { if (n == 0) { return 1; } return n * Factorial(n - 1); }
After we know how to implement Factorial we can implement Euler number using recursion as well:
double EulerNumber(int n) { if (n == 0) { return 1; } return 1.0/Factorial(n) + EulerNumber(n - 1); }
Enjoy :)
Tuesday, March 11, 2008 4:01 AM