Solved

powerN  challenge

Posted on 2016-09-09
3
76 Views
Last Modified: 2016-09-11
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
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 41792204
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
 
LVL 7

Author Comment

by:gudii9
ID: 41793661
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
 
LVL 27

Expert Comment

by:rrz
ID: 41793740
My solution is the same as yours.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hashmap order 17 40
incorrect syntax near the order by 10 56
learn programming 8 66
batch file or script 4 47
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question