Creating pow in Java: Learn Recursive Solution

  • Comp Sci
  • Thread starter cgrumiea
  • Start date
  • Tags
    Java
In summary, the conversation discusses the creation of a recursive method called pow that takes two integers, x and n, and returns the result of raising x to the n-th power. The method should account for non-negative values of n and the base case when n is equal to 0 or 1. The method can be implemented by using the formula pow(n,k)=pow(n,k-1)*n.
  • #1
cgrumiea
4
0

Homework Statement


Define a recursive method named pow that takes two integers, x and n, and returns the result of raising x to the n-th power. Assume that n is non-negative.

pow(5, 0) ==> 1
pow(2, 10) ==> 1024
pow(-3, 2) ==> 9



Homework Equations





The Attempt at a Solution


I'm totally fresh to java, and am not used to or comfortable with recursion.
public static int pow(int x, int n) {

I don't know what to do. Should I have some increasing value to count up to when x has been multiplied by itself n times? I know scheme which seems to be a bit of a different ball game here. Any help would be appreciated.
 
Physics news on Phys.org
  • #2
pow(n,k)=pow(n,k-1)*n, isn't it? pow(n,0)=1. Isn't that a pretty recipe for recursion? You don't have to count anything at all.
 
  • #3
yep, and remember to check the base case first:
Code:
if(k==1) return n;
or:
Code:
if(k==0) return 1;
if you will.
 

What is pow in Java?

pow in Java stands for power and is a mathematical function that calculates the result of raising a number to a given power.

How do I use pow in Java?

To use pow in Java, you can use the Math.pow() method. It takes two parameters, the base number and the exponent, and returns the result of the calculation.

What is a recursive solution?

A recursive solution is a programming technique where a function calls itself repeatedly until a base case is reached. It is often used to solve problems that can be broken down into smaller, simpler versions of the original problem.

Why is recursion useful for creating pow in Java?

Recursion is useful for creating pow in Java because the power function can be broken down into smaller, simpler versions of itself. By using recursion, we can repeatedly solve these smaller versions until we reach the base case, which is when the exponent is equal to 0.

What are the advantages of using a recursive solution for creating pow in Java?

One advantage of using a recursive solution for creating pow in Java is that it can make the code more concise and easier to understand. Additionally, it allows for a more elegant and efficient solution compared to using iterative methods.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
937
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top