Solved

powerN  challenge

Posted on 2016-09-09
3
46 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
Comment Utility
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
Comment Utility
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
Comment Utility
My solution is the same as yours.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This is about my first experience with programming Arduino.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now