Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: question about recursion..

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default question about recursion..

    public class Recursion {
     
       public static double power(double a, int n){
    double tmp,result,answer;
     
           if(n % 2 == 0){
        tmp = power(a, n-1);
        result = (tmp*(n/2));
        answer = result * result;
     
     
        return answer;
    }
     else if (n % 2 == 1){
        tmp = power(a, n-1);
        result = (tmp*((n-1)/2));
        answer = result * result;
     
     
        return result;
     }
     
     
           return 1;
     
    }
        public static void main (String [] args){
            double tmp;
            tmp = power(2,10);
            System.out.println(tmp);
        }
    }
    it keeps printing 0...

    its supposed to print 2 to the power of 10 but i dont know what im doing wrong..
    its supposed to be, if n is even solution would be

    a^n = (a^n/2)^2 if n is even
    a^n = (a^((n-1)/2))^2 if n is odd.... any comments? thanks..
    Last edited by copeg; March 15th, 2011 at 09:48 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: question about recursion..

    I'm a little surprised that code doesn't result in a stack overflow (it looks like it should from visual inspection).

    In a recursive algorithm, you must check for the base case(s) first. I don't see a base case before the recursive call, thus you have infinite recursion.

    What's the base case? It's when n=0.

    Additionally, I think you're recursive logic a little incorrect.
    a^n = (a^(n/2))^2 if n is even
    a^n = a*(a^((n-1)/2))^2 if n is odd

    Lastly, you're code implementing the recursion calls are off.

    You're calling power(a,n-1), but the recursive logic requires a^(n/2), not a^(n-1). This needs to be fixed for both the even and odd cases. Also, that strange multiplication by temp*(...) stuff needs to be removed.
    Last edited by helloworld922; March 16th, 2011 at 02:13 AM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    gonfreecks (March 16th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: question about recursion..

    that helped thank you

Similar Threads

  1. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 04:42 PM
  2. recursion problem, please help
    By nil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2010, 01:59 PM
  3. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 2nd, 2010, 12:27 AM
  4. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  5. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 21st, 2009, 12:02 AM