My first recursion method: the number e
So I was reading about binary numbers and I got to thinking I need to review on log and natural log. I was skimming through my notes from last year and I found out this:
Code:
the number e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + 1/(1*2*3*4) + ... 1/(n!)
And just last night, I was reading about recursion methods. Instantaneously
I had an epiphany. The epiphany was simple: I'm gonna make a recursion method that returns the value of e. Here it is. :D
The MyMathClass class:
Code:
public class MyMathClass {
public static double e(int num) {
if (num == 0)
return 1;
else
return ((double) 1 / factorial(num)) + e(num -1);
}
public static int factorial(int num) {
if (num == 1)
return 1;
else
return num * factorial(num -1);
}
}
The RecursionMethodsDriver class:
Code:
public class RecursionMethods {
public static void main(String[] args) {
System.out.println(e(33));
}
}
I realize that there is a method that returns the value of e in the math class.
Unfortunately, if num > 33 in e(num), it returns "infinity." Is there anyway way I can get a more precise number?
p.s: so jaunty right now because I get recursion methods. ;) Although, there's a lot more to recursion, like mazes and stuff which I'm not getting. :(
Anyhow, feedbacks and comments appreciated.