[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Recursion

Posted on 2005-05-15
23
Medium Priority
?
1,484 Views
Last Modified: 2013-11-18
working on a past paper for study and am stuck on a recursion problem:

==============================================================
The Java code below contains a recursive method to calculate the power of a number given the base and exponent. (Eg. power (3, 4) = 3 x 3 x 3 x 3 and should use the relationship base exponent = base x base exponent - 1 and base 1 = base.

public class Maths
{
public static void main(String[] args)
{
     System.out.println(“The power of base 3 to exponent 4 = “ + power(3, 4));
}
 // power method should be here
 }


The power of base 3 to exponent 4 = 81 1

Write the Java code for the recursive method power(). (9 marks)
===========================================================

my attempt at the power method:
      public int power(int base, int exp)
      {
            if (base<=1)
                  return base;
            else
                  return power(base * base, exp--);
      }

======================================================

thanks
pete
0
Comment
Question by:pete420
  • 10
  • 5
  • 5
  • +1
23 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 14005506
Hi pete420,
I think u need the power method to:

     public int power(int base, int exp)
     {
          if (exp<=1)
               return base;
          else
               return power(base * base, exp--);
     }


Cheers!
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14005511
sorry misstypoo I mean:

the power method to be changed to:

     public int power(int base, int exp)
     {
          if (exp<=1) //this is the correct conditon
               return base;
          else
               return power(base * base, exp--);
     }

0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005512
    public int power(int base, int exp)
     {
          if (exp<=1)
               return base;
          else
               return (base * power(base, exp - 1));
     }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:petmagdy
ID: 14005515
mgh_mgharish,
I already provided this answer :)
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005520
pete420,
    Consider power(4,3) which implies 4*power(4*2) which implies 4*4*power(4*1) which implies 4*4*4.

    So the correct function is like the one posted by me which contains

               return power(base * base, exp--);


petmagdy,

    Your function will do this...
    power(4,3) = power(4*4,3)  ... 3 is not decremented since it is post decrement
                                 
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14005521
mgh_mgharish,

opppsssss sorryyy u r right
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005523
Sorry...

    So the correct function is like the one posted by me which contains
               return power(base * base, exp-1);

    Note the change in my and petmagdy's comments
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 300 total points
ID: 14005525
pete420,
    The correct function will be

    public int power(int base, int exp)
     {
          if (exp<=1)
               return base;
          else
               return (base * power(base, exp - 1));
     }
0
 

Author Comment

by:pete420
ID: 14005550
ahhh i see,

however when i try to compile the prog. now it says:
non-static method power(int, int) cannot be referenced from a static context (line 5)

pete
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14005557
u need to do this in main method:

public static void main(String[] args)
{
Maths mths = new Maths();    
System.out.println(“The power of base 3 to exponent 4 = “ + mths.power(3, 4));
}
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005560
pete420,
> public int power(int base, int exp)

Try
public static int power(int base, int exp)
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005569
public class Maths
{
    public int power(int base, int exp)
    {
        if (exp<=1)
            return base;
        else
            return (base * power(base, exp - 1));
    }


    public static void main(String[] args)
    {
        System.out.println(“The power of base 3 to exponent 4 = “ + Maths.power(3, 4));
    }
 
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14005573
You will get incorrect results for n^0 with the above. It should be:

      public static power(int base, int exp) {
            if (exp == 0)
                  return 1;
            else if (exp <= 1)
                  return base;
            else
                  return (base * power(base, exp - 1));
      }
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005577
CEHJ,
    You are right.. but a better code would be

public class Maths
{
    public int power(int base, int exp)
    {
        if (exp<=0)
            return 1;
        else
            return (base * power(base, exp - 1));
    }


    public static void main(String[] args)
    {
        System.out.println(“The power of base 3 to exponent 4 = “ + Maths.power(3, 4));
    }
 
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14005580
The function should be ranged-check actually, or power(2, -1) would return 1, which is incorrect
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005583
pete420,
    A perfect code would be...

public class Maths
{
    public float power(float base, int exp)
    {
        if (exp==0)
            return 1;
        else if(exp > 0)
            return (power(base, exp - 1) * base);
        else
            return (power(base, exp + 1) / base);
    }


    public static void main(String[] args)
    {
        System.out.println(“The power of base 3 to exponent 4 = “ + Maths.power(3.0, 4));
    }
}

0
 

Author Comment

by:pete420
ID: 14005634
thanks for all the help, its much appreciated

:)
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005638
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14005980
Unfortunately you've accepted the wrong answer pete420. Please see the guidelines
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 14005993
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14006000
And some of my comments may not have been entirely out of place in the acceptance either  ;-)
0
 

Author Comment

by:pete420
ID: 14010414
apoligies for that, i picked the one that best suited the needs of what i was doing. i know the "proper" code would have been much better but i was selecting what i thought would have been the answer of the past paper exam.

sorry again,

pete
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14010435
Well it would have been a wrong answer ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question