Results 1 to 2 of 2
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default 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!)
    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:
    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. :(

    Anyhow, feedbacks and comments appreciated.
    Last edited by Lil_Aziz1; 03-25-2010 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)

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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("[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));
    	}
    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-24-2010 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

  1. Current week number method
    By levent in forum New To Java
    Replies: 2
    Last Post: 02-01-2010, 05:33 AM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  3. implement binary search method using recursion?
    By chopo1980 in forum New To Java
    Replies: 1
    Last Post: 12-12-2009, 04:58 PM
  4. Replies: 1
    Last Post: 08-07-2009, 01:14 PM
  5. Replies: 1
    Last Post: 11-03-2008, 03:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •