-1

Problem Statement:

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

Example

powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27

The function signature is public int powerN(int base, int n)

I'm finding it to difficult to solve this? Help me out.
EDIT: I need a solution which does not uses built in mathematical formulas and recursion

Deepak
  • 2,094
  • 8
  • 35
  • 48
  • 3
    If it's homework, please tag it as such. – MByD Apr 01 '11 at 11:48
  • 3
    Have you tried `Math.pow(base, n)`? For homework, you have to solve a problem a given way, in the real world, you usually just want the simplest/best. This is why you should tag [homework] as such. – Peter Lawrey Apr 01 '11 at 11:49
  • i should not use built in expressions,No its not homework... – Deepak Apr 01 '11 at 11:50
  • 1
    `i should not use built in expressions` why? – Peter Lawrey Apr 01 '11 at 11:51
  • if you should not use this or that, who gave you this task? it really sounds like homework ;) – Tobias Apr 01 '11 at 11:51
  • Thats what the interviewer told me – Deepak Apr 01 '11 at 11:51
  • @Deepak, I wonder, why you shouldn't? – Vladimir Ivanov Apr 01 '11 at 11:51
  • What are you finding difficult? Do you know how to work out the power of something on paper? If so, do you know how to do this in java? It doesn't get much simpler. I understand that a lot of people have bought "java for dummies"; perhaps you'd find it useful? –  Apr 01 '11 at 11:52
  • For homework, this is useually solved using either a loop or recursion. Which one are you trying, can you post the code you have and the situtation you believe is not working? – Peter Lawrey Apr 01 '11 at 11:52
  • For recursion, you can do this in one to four lines of code, for a loop, you might use two to five. Perhaps the problem is you imagine its much harder than it should be, try to simplify your solution. I can only guess what it is you are trying as you haven't posted it. – Peter Lawrey Apr 01 '11 at 11:54
  • Sorry, but you have to answers, but both are not ok, because you add the additional requirement that no built in and no recursion. So if you have such restrictions, post it in the first place. – RoflcoptrException Apr 01 '11 at 11:54
  • See the EDIT of original question – Deepak Apr 01 '11 at 11:56
  • @Roflcoptr, I am not sure the OP mentioned recusion. ;) – Peter Lawrey Apr 01 '11 at 11:56
  • 1
    Is it just me or does this sound as a thoroughly counterproductive question to ask at an interview? – biziclop Apr 01 '11 at 11:57
  • @Peter Lawrey He mentioned it in a comment to an answer. – RoflcoptrException Apr 01 '11 at 11:57
  • @Roflcoptr, I wasn't keeping up with the edits, thanks. ;) – Peter Lawrey Apr 01 '11 at 11:59
  • @biziclop, counter productive in the sense that you shouldn't be doing this yourself in any real environment, but useful for the interviewer as its simple to explain and write the answer in a short period of time. – Peter Lawrey Apr 01 '11 at 12:06
  • If I was the interview I would just ask for all three possible approaches. (And any others you can think of, two others come to mind ;) – Peter Lawrey Apr 01 '11 at 12:07
  • Its the bit like the how-would-you-implement-linked-list question. They don't like it when I say, you wouldn't, you would use the built in one unless there is a very good reason not to. ;) – Peter Lawrey Apr 01 '11 at 12:38
  • @Peter Lawrey That's what I meant: asking for as many implementations as you can think of is a good question, setting up arbitrary constraints to get the exact answer you had in mind isn't. – biziclop Apr 01 '11 at 12:57
  • @biziclop, If I asked this I would expect them to know this is a bad to implement and what might go wrong. e.g. edge cases, performance, maintainability issues. – Peter Lawrey Apr 01 '11 at 13:02

6 Answers6

7
public int powerN(int base, int n) {
    int result = 1;
    for (int i = 0; i < n; i++) {
        result *= base;
    }

    return result;
}
James.Xu
  • 8,249
  • 5
  • 25
  • 36
3

The most popular method of doing this is like this:

sum = base
while (power > 1)
    if (power is even)
        sum = sum * sum
        power = power / 2
    else
        sum = sum * base
        power = power - 1
return sum

You can convert to Java, I just wanted to give you the general idea.

Jonathan
  • 13,354
  • 4
  • 36
  • 32
2

You can use recursion:

public int powerN(int base, int n)
{
    if (n == 0) {
       return 1;
    } else {
       return (powerN(base, n-1) * base);
    }
}
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

assuming your power stays under int limit

int out = 1;
for(int i=n; i>0; i--)
   out = out*base;
return out;
Nishant
  • 54,584
  • 13
  • 112
  • 127
0
public int powerN(int base, int n) {
   return Double.intValue(Math.pow ( base, n));
}

Ok I saw you can't use built in functions:

public int powerN(int base, int n) {
    if (n == 0) {
        return 1;
    } else {
        int result = 1;
        for (int i = 0; i < n; i++) {
            result = result * base;
        }

        return result;
    }
}
CSchulz
  • 10,882
  • 11
  • 60
  • 114
0

Fast way the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int from Elias Yarrkov

Exponentiation by squaring.

 public static long powerN(long base, long n)
 {
   long result = 1;
    while (n > 0)
    {
        if ((n & 1) == 1)
            result *= base;
        n >>= 1;
        base *= base;
    }

    return result;
 }
Community
  • 1
  • 1
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112