Matt Andrzejczuk
Matt Andrzejczuk

Reputation: 2066

Recursive Exponent Method

public static int exponent(int baseNum) {
    int temp = baseNum *= baseNum;                

        return temp * exponent(baseNum);             
}

Right now the method above does n * n into infinity if I debug it, so it still works but I need this recursive method to stop after 10 times because my instructor requires us to find the exponent given a power of 10.

The method must have only one parameter, here's some examples of calling exponent:

                System.out.println ("The power of 10 in " + n + " is " + 
                    exponent(n));

So output should be:

The power of 10 in 2 is 1024

OR

The power of 10 in 5 is 9765625

Upvotes: 5

Views: 38291

Answers (7)

Jekyll
Jekyll

Reputation: 1434

I came here accidentally, and I think one could do better, as one would figure out easily that if exp is even then x^2n = x^n * x^n = (x^2)^n, so rather than computing n^2-1 recursions, you can just compute xx and then call pow(x,n) having n recursions and a product. If instead the power is odd, then we just do xpow(x, n-1) and make the power even again. But, as soon as now n-1 is even, we can directly write xpow(xx, (n-1)/2) adding an extra product and using the same code as for the even exponent.

int pow_( int base, unsigned int exp ) {
        if( exp == 0 )
                return 1;

        if( exp & 0x01 ) {
                return base * pow_( base*base, (exp-1)/2 );
        }
        return pow_( base*base, exp/2 );
}

Upvotes: 0

JavaStudent
JavaStudent

Reputation: 11

The following is what my instructor, Professor Penn Wu, provided in his lecture note.

public class Exp
{
public static int exponent(int a, int n)
{
if (n==0) { return 1; } // base
else // recursion
{
a *= exponent(a, n-1);
return a;
}
}
public static void main(String[] args)
{
System.out.print(exponent(2, 10));
}
}

Upvotes: 1

bachstein
bachstein

Reputation: 23

Don't forget , for each recursive function , you need a base case. A stop condition` static double r2(float base, int n) {

    if (n<=0) return 1;
    return  base*r2(base,n-1);

}

Upvotes: 0

user1500049
user1500049

Reputation: 1003

For recursion function, we need to :

  1. check stopping condition (i.e. when exp is 0, return 1)
  2. call itself with adjusted condition (i.e. base * base^(n-1) )

Here is the code.

public class Test
{
    public static int exponent(int baseNum, int exp)
    {
        if (exp<=0)
            return 1;

        return baseNum * exponent(baseNum, --exp);
    }

    public static void main(String a[])
    {
        int base=2;
        int exp =10;

        System.out.println("The power of "+exp+" in "+base+" is "+exponent(base,exp));
    }

}

Upvotes: 0

mu_sa
mu_sa

Reputation: 2725

Do something like

public static int exp(int pow, int num) {
    if (pow < 1) 
        return 1; 
    else
        return num * exp(pow-1, num) ;
}

public static void main (String [] args) {     
    System.out.println (exp (10, 5));
}

and do not forget the base case (i.e a condition) which tells when to stop recursion and pop the values from the stack.

Upvotes: 5

Yogendra Singh
Yogendra Singh

Reputation: 34367

Shouldn't it have 2 parameter and handle exit condition like below?

public static int exponent(int baseNum, int power) {
   if(power == 0){
      return 1;
   }else{ 
      return baseNum * exponent(baseNum, power-1);  
   }           
}

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

Create an auxiliary method to do the recursion. It should have two arguments: the base and the exponent. Call it with a value of 10 for the exponent and have it recurse with (exponent-1). The base case is exponent == 0, in which case it should return 1. (You can also use exponent == 1 as a base case, in which case it should return the base.)

Upvotes: 1

Related Questions