# Thread: My first recursion method: the number e

1. ## 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:

Java Code:
`the number e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + 1/(1*2*3*4) + ... 1/(n!)`
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:
Java 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:
Java 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. :(

Last edited by Lil_Aziz1; 03-25-2010 at 10:14 PM.  Reply With Quote

2. ## I did some more research on binary numbers and I learned how to convert decimal numbers to binary numbers. Kudos to me! anyhow, I wanted to make a static method that would do this for me. First, I started off with a for loop:

Java Code:
```	public static int decimalToBinary(int num) {
/*
assume num = 52.
2^x = 52
multiply with log base of 10 on both sides
log(2^x) = log(52)
use the exponent log rule
xlog(2) = log(52)
x = log(52) / log(2)
x = 5.7004 = 5
*/
int pow = (int) (Math.log10(num) / Math.log10(2));
String binaryNumber = "";
int y;
for (; pow >= 0; pow--) {
y = (int) Math.pow(2, pow);
if ((num-y) >= 0) {
binaryNumber += "1";
num -= y;
}
else
binaryNumber += "0";
}
return Integer.parseInt(binaryNumber);
}```
Then I decided to use a recursion method. This required me two methods:
Java Code:
```	public static int decimalToBinary2(int num) {
int pow = (int) (Math.log10(num) / Math.log10(2));
return Integer.parseInt(decimaltoBinary(pow, num));
}
public static String decimaltoBinary(int pow, int num) {

int y;
if (pow == -1)
return "";
else {
y = (int) Math.pow(2, pow);
if ((num-y) >= 0)
return "1" + decimaltoBinary(pow-1,num-y);
else
return "0" + decimaltoBinary(pow-1,num);
}

}```
Question: is it possible to make the one recursion method (decimalToBinary(int pow, int num)) and one normal method (decimalToBinary2(int pow)) into a recursion method?

And finally, binaryToDecimal method (also recursive):

Java Code:
```	public static int binaryToDecimal(String num) {
if (!num.matches("+")) return -1;
if (num.length() == 1)
return Integer.parseInt(num) * (int) Math.pow(2, 0);
else
return (Integer.parseInt(num.substring(0,1)) * (int) Math.pow(2, num.length() -1)) + binaryToDecimal(num.substring(1));
}```
I realize my decimalToBinary methods are not perfect (they do not handle any number that's less than or equal to 0).

Java Code:
```	public static void main(String[] args) {
System.out.println("Decimal 55 to binary: " + MyMathClass.decimalToBinary(55));
System.out.println("Binary 110111 to decimal: " +  MyMathClass.binaryToDecimal("110111"));

}```
Output:
Java Code:
```Decimal 55 to binary: 110111
Binary 110111 to decimal: 55```
Last edited by Lil_Aziz1; 01-23-2010 at 11:27 PM.  Reply With Quote

log, math, number, recursion 