Results 1 to 10 of 10
Thread: problem with recursive method
- 09-27-2012, 04:17 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
problem with recursive method
My problem is with this exercise:
Write a recursive method called power that takes a double x, and an integer n and that returns x^n. Hint: a recursive definition of this operation is x^n = x*x^(n-1). Also, remember that anything raised to the zeroeth power is 1. Optional challenge: you can make this method more efficient, when n is even, using x^n=(x^(n/2))^2.
Now call me crazy but I just can't see why recursion is necessary or how it could be implemented into this, here is the code I wrote:
It works, and returns x^n just as the exercise said, but it specified to use a recursive method so if any of you guys could give me a hand in how recursion would work with this problem it would be appreciated.Java Code:package edu.vtc.aav10260.cis2261; import java.util.Scanner; /** * @author andre * */ public class Power { /** * @param args * @return */ public static double main(String[] args) { Scanner kbd = new Scanner(System.in); System.out.println("Enter the values: "); System.out.print("x= "); double x = kbd.nextDouble(); System.out.print("n= "); int n = kbd.nextInt(); return Math.pow(x, n); } }
- 09-27-2012, 06:27 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: problem with recursive method
A recursive method calls itself, so if you need to calculate the power of a number, how do you think you can do so using a method that calls itself? Break the problem down....what is the definition of power a ^ b ? Use an example where a = 3, b = 4: 3 ^ 4 = 3 * 3 * 3 * 3. In other words, 3 times itself 4 times...so what could you put inside a method that calls itself to calculate a times itself b times?
- 09-28-2012, 01:24 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: problem with recursive method
bump
- 09-28-2012, 01:33 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: problem with recursive method
Evidently you don't care for advice given, in fact completely ignored it, which doesn't help your chances of receiving help here or elsewhere. And neither does cross-posting.
Cross posted at help with recursive method
- 09-28-2012, 01:37 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: problem with recursive method
i didnt really understand what you were saying
- 09-28-2012, 03:17 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
- 09-28-2012, 03:47 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: problem with recursive method
ok well my question is, will you please show me how you would write this code?
-
Re: problem with recursive method
- 09-28-2012, 03:53 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: problem with recursive method
i just don't see how recursion can be used in this problem as I said the code that I wrote did exactly what the exercise says to and doesn't need recursion
-
Re: problem with recursive method
Just because you can solve it non-recursively doesn't mean that there isn't a recursive solution. The key as has been mentioned before is to work it out on paper first. But before you do that, see how other recursive methods, like the one for calculating Fibonacci numbers works.
Similar Threads
-
Recursive Method
By emschorsch in forum New To JavaReplies: 11Last Post: 04-18-2011, 06:01 AM -
recursive method
By michail in forum New To JavaReplies: 0Last Post: 01-31-2010, 01:50 PM -
recursive method problem
By melody in forum New To JavaReplies: 1Last Post: 10-29-2009, 07:15 AM -
Java Recursive method problem
By kj2009 in forum Advanced JavaReplies: 2Last Post: 02-25-2009, 03:19 PM -
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11-29-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks