Results 1 to 2 of 2
 01232010, 07:48 PM #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); } }
Java Code:public class RecursionMethods { public static void main(String[] args) { System.out.println(e(33)); } }
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.Last edited by Lil_Aziz1; 03252010 at 11:14 PM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
 01232010, 11:28 PM #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 ((numy) >= 0) { binaryNumber += "1"; num = y; } else binaryNumber += "0"; } return Integer.parseInt(binaryNumber); }
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 ((numy) >= 0) return "1" + decimaltoBinary(pow1,numy); else return "0" + decimaltoBinary(pow1,num); } }
And finally, binaryToDecimal method (also recursive):
Java Code:public static int binaryToDecimal(String num) { if (!num.matches("[01]+")) 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)); }
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")); }
Java Code:Decimal 55 to binary: 110111 Binary 110111 to decimal: 55
Last edited by Lil_Aziz1; 01242010 at 12:27 AM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
Similar Threads

Current week number method
By levent in forum New To JavaReplies: 2Last Post: 02012010, 05:33 AM 
recursion and tailrecursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12282009, 07:26 PM 
implement binary search method using recursion?
By chopo1980 in forum New To JavaReplies: 1Last Post: 12122009, 04:58 PM 
Geting Mobile Number, Mobile Operator, Location and Mobile Serial Number by J2ME.
By maruffaiz in forum CLDC and MIDPReplies: 1Last Post: 08072009, 01:14 PM 
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11032008, 03:38 PM
Bookmarks