Solved

powerN  challenge

Posted on 2016-09-09
3
62 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
detecting  the added row index in a datagridview 3 70
simplest php form 3 77
use lov values 2 49
sp_configure 'external scripts enabled', 1; 7 49
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

805 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