• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 122
  • Last Modified:

powerN challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p158888


Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared).

powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27
i was not clear on how to find the powerN using recursion? please advise
0
gudii9
Asked:
gudii9
  • 2
1 Solution
 
rrzCommented:
This challenge is very similar to the other challenges that you started to the same time. I can give you two hints about the base case here.  Every number to the power of 0 is equal to one.  Furthermore, every number multiplied by 1 is equal to that same number.
0
 
gudii9Author Commented:
public int powerN(int base, int n) {
   if(n==0) return 1;
//  if(n==1) return base;
  else{
   return base*powerN(base,n-1);
  }
}

Open in new window

above passes all test. any alternate approaches or improvements?
0
 
rrzCommented:
My solution is the same as yours.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now