Exponents with Recursion

I'm trying to to do exponets with conversion somewhere i went wrong and cant seem to figure out why I keep getting errors

these were the formulas given

a^n = a^n/2(a^n/2) even

This formula only works for even exponents. To accommodate odd exponent values as well, we simply multiply one more time by a:

a^n = a^n/2(a^n/2) * a if a is odd

import java.util.*;

publicclass recursion3

{

publicstaticvoid main (String[] args)

{

int base;

int power;

Scanner sc =new Scanner(System.in);

System.out.println ("Please enter a non negative number");

base = sc.nextInt();

System.out.println ("Please enter a power");

power = sc.nextInt();

System.out.println(convert(base,power));

}

publicstaticdouble convert(int a,int n)

{

int ans;

ans = a^n/2*a^n/2;

if (n == 0)

{

return convert(a,n);

}

else

{

return (convert(a,n) * a);

}

}

}

Message was edited by:

jaydon34

[2008 byte] By [jaydon34a] at [2007-11-26 19:52:58]
# 1
Are you expecting this to be Java syntax:ans = a^n/2*a^n/2;Java doesn't have an exponent operator. The operator ^ does bitwise exclusive or -- which is completely different.Also, isn't a^0 =1, for a>0? You seem to have an infinite recursion in this case.
DrLaszloJamfa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 2
In general, you just aren't implementing what you just described. Where is n/2 in your code, for example?
DrLaszloJamfa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 3
What about this formulation?a^n = 1when n is 0= b*bwhen n is even, > 0= b*b*a when n is oddwhere b is a^(n/2)
DrLaszloJamfa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 4
So how would I go about using formulating an exponent with out using the Math.pow(base,exponent)
jaydon34a at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 5

try this

public static double convert(int a,int n) {

if (n % 2 == 1 && n > 1){

return convert(a, 0 ) * convert(a, 1);

}

if (n % 2 == 0){ //even

return Math.pow(a,n/2) * Math.pow(a,n/2);

} else {

return a;

}

}

p_epia at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 6
somebody tell me he's kidding please
ejpa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 7

>>So how would I go about using formulating an exponent with out using the >>Math.pow(base,exponent)

Algorithms my friend.

http://www.osix.net/modules/article/?id=696

Create your own method that calculates the number to a certain number (x^n)

Then just call this method to calculate the power...

Of course, the Math class has already created this method called "power" and all you have to do is use it (Instead of creating it by yourself ! Which would probably be less efficient than the method from the Math class)

lethalwirea at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 8
imigod another one. All you need for the solution is to create your own method that implements the solution?
ejpa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 9

Let this be over and done with:int power(int a, int n) {

if (n < 0) return 0; // just in case

if (n == 0) return 1; // the easy part

if (n == 1) return a; // also easy

//

int half= power(a, n/2);

if (n%2 == 0) return half*half;

return half*half*a;

}

kind regards,

Jos

JosAHa at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 10
isn't a^(-1) = 1/a?maybe my maths is rusty?
Icycoola at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 11
> isn't a^(-1) = 1/a?> maybe my maths is rusty?lol.
fastmikea at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 12

Darn mike you made me wiki it out. ok I was right, so

replace if (n < 0) return 0;

with if (n == -1) return 1/a;

I think it "should" work.

Edit: Sorry it won't work for odd negative power, but I won't solve it ;p

Message was edited by:

Icycool

Icycoola at 2007-7-9 22:44:15 > top of Java-index,Java Essentials,New To Java...
# 13

> replace if (n < 0) return 0;

> with if (n == -1) return 1/a;

> I think it "should" work.

Both n and a are ints. btw, take n == -2 for an example: the results will

be turtles all the way down, that's why I used n < 0 as a sentinel value.

kind regards,

Jos

JosAHa at 2007-7-9 22:44:16 > top of Java-index,Java Essentials,New To Java...
# 14

>replace

>if (n < 0) return 0;

>with

>if (n == -1) return 1/a;

You can catch negative powers by saying

if (n < 0) return 1/convert(a, -n);

This will actually result in 0 in any possible case, so Jos provided the most efficient way by simply returning 0 instead of executing useless recursions.

Peetzorea at 2007-7-9 22:44:16 > top of Java-index,Java Essentials,New To Java...
# 15

But it's a fact that negative powers do exist.

You should make the program produce correct result, but not ignoring a certain domain.

For instance power(2,-4) should produce 1/16, if you can't do it, at least print "sorry this program don't know negative" instead of 0.

I'm not understand why -2 wont work.

power(3,-2) will become power(3,-1)*power(3,-1), then become 1/3*1/3

Edit: oh i see it now, the result is not capable of floating type. Well, I got a silly workaround, if(n<0) print "1/"+result ;p

Message was edited by:

Icycool

Icycoola at 2007-7-21 17:45:12 > top of Java-index,Java Essentials,New To Java...
# 16
<nevermind>Message was edited by: jsalonen
jsalonena at 2007-7-21 17:45:12 > top of Java-index,Java Essentials,New To Java...
# 17
> Let this be over and done with:Jos, man, you caved! This chappie was fishing with an assignment and you did it for him!
DrLaszloJamfa at 2007-7-21 17:45:12 > top of Java-index,Java Essentials,New To Java...
# 18

> > Let this be over and done with:

>

> Jos, man, you caved! This chappie was fishing with an

> assignment and you did it for him!

Yeah I guess so, but this thread was also full of incorrect answers and

the entire thread sort of made me feel itchy ;-)

kind regards,

Jos

JosAHa at 2007-7-21 17:45:12 > top of Java-index,Java Essentials,New To Java...
# 19
Yeah I was asking for help with an assignment but not the whole answer just some direction
jaydon34a at 2007-7-21 17:45:12 > top of Java-index,Java Essentials,New To Java...