in
Welcome to Lionhead Community Sign in to Windows Live ID | Help

Java Help

Last post 01-31-2007, 23:16 by StaticKyle. 6 replies.
Sort Posts: Previous Next
  •  01-31-2007, 23:16 2472094

    Java Help

    Well, I was told by my professor that I had to use recursion on our next project and for our project we have to use the exponential formula. I think I have most of it correct which was pretty hard considering I had no idea on how to do a recursive method. Anyways, I am having a huge problem with the odd numbers and no idea how to do x to the negative n. With the odd numbers it is usually multiplying it one too many times, so I took out the extra x and it did it too few times.. any help would be appreciated. Here is my code so far. public class Power { //Methods public static double powerOf(int x,int n) { //will compute x to the power of n. double m = 0; int k = n % 2; if (n == 0) return 1; else if (k == 0 && n > 0) { m = x * powerOf(x, (n/2)); return m; } else if (k == 1 && n > 0) { m = x *x * powerOf(x, (n/2)); //This is where I have been having the problems. System.out.println(m); return m; } else return 1; } }
  •  01-31-2007, 23:21 2472100 in reply to 2472094

    Re: Java Help

    You need to know three things: x^0 == 1 (except when x == 0, then you should probably throw an exception or return -1 or whatever you want) x^n == x * x^(n-1), when n is positive. x^(-n) == 1 / (x^n), when n is positive. So, for example, x^-2 == 1 / (x^2) == 1 / (x * x^1) == 1 / (x * x * x^0) == 1 / (x * x * 1). Now all is left is for you to figure out how to make that algorithm into a program, using recursion. If you still can't figure it out, all I can do is just give you the answer ... but you won't learn half as much from that, and it's a lot less fun too. Silly [:p] Your current approach is way too complicated though.
  •  02-01-2007, 0:52 2472219 in reply to 2472094

    Re: Java Help

    Well, okay, I do understand that, but here is what my professor wants us to do.. I didn't much like it when I first saw it either because the iterative solution isn't that bad. He gave us these instructions: Write a recursive method, power, to compute x^n by using the following recursive formula: x^0 = 1 x^n = (x^(n/2))^2 if n>0 and n is even. x^n = x * (x^(n/2))^2 if n>0 and n is odd Write a driver program which will prompt the user to input a real number x and an integer n and uses your power method to compute xn. If n is negative, note that x ^-n = 1/x^n, and then use that to compute the result correctly. Print the result with an approproate message. This is all he gave me and it is pretty vague considering I had no idea how to do recursive methods. Thanks in advance.
  •  02-01-2007, 1:20 2472250 in reply to 2472094

    Re: Java Help

    Oh, okay, I didn't realise you had to do it that crazy way. Silly [:p] [code]float power(int x, int n) { if (n == 0) { if (x == 0) return Float.NaN; return 1; } if (x == 0) return 0; if (n < 0) return 1 / power(x, -n); if (n % 2 == 0) { return power(x, n / 2) * power(x, n / 2); } return power(x, n/2) * power(x, n/2) * x; }[/code] This would be my first implementation. You got the most part, but you incorrectly multiply by x in stead of the power function. Oh, you don't need all those elses by the way, since if you didn't reach that else you already returned from the function. Of course this can easily be simplified a bit: [code]float power(int x, int n) { if (n == 0) { if (x == 0) return Float.NaN; return 1; } if (x == 0) return 0; if (n < 0) return 1 / power(x, -n); float root = power(x, n / 2); if (n % 2 == 0) return root * root; return root * root * x; }[/code] If must admit though, that since this method reduces the argument by a factor of two each time, it only calls itself 1 + lg n times, which is a lot less than the iterative method, which calls itself n times. It effectively does a binary search on the problem, which is of course a lot faster than a linear search.
  •  02-01-2007, 1:33 2472257 in reply to 2472094

    Re: Java Help

    I'm just a little confused at this part.. "return Float.NaN;" what does it do exactly?
  •  02-01-2007, 2:27 2472344 in reply to 2472094

    Re: Java Help

    It signifies "Not A Number".
  •  02-01-2007, 3:38 2472422 in reply to 2472094

    Re: Java Help

    Ah, yes, since 0^0 doesn't exist, it is costumary to return Not a Number. Just like 0/0 or infinity/infinity.
View as RSS news feed in XML