-2

I faced an interview. where I was asked the following question

Write a function in any of programming language that computes the nth power of a number w/o using + * or ^ or declaring a new variable inside the function or using any library function (eg Math lib in java).

I have used pow function of java Math.pow(a, b)

Thanks

nicael
  • 18,550
  • 13
  • 57
  • 90
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243

4 Answers4

3

They're asking whether you understand recursion. Considering x ^ k for some integer k,

  • when k < 0, xk = xk+1 / x
  • when k = 0, xk = 1
  • when k > 0, xk = xk-1 * x

Turning this into code shouldn't be too bad. Let's use multiplication for now, and take it out later.

double recursivePower(double x, int k) {
    if (k < 0) {
        return power(x, ++k) / x;
    } else if (k == 0) {
        return 1;
    } else {
        return power(x, --k) * x;
    }
}

Now, to get rid of the multiplication. Since n * m = n / (1/m), we can rewrite the last calculation as power(x, --k) / (1/x):

double recursivePower(double x, int k) {
    if (k < 0) {
        return recursivePower(x, ++k) / x;
    } else if (k == 0) {
        return 1;
    } else {
        return recursivePower(x, --k) / (1 / x);
    }
}

Fractional exponents could probably be done in the same style. If they want irrational exponents to be handled in the same way, I'd ask for Google and a fair amount of time to think about the problem.

GargantuChet
  • 5,691
  • 1
  • 30
  • 41
0
static public int power(int value, int pow){
    if(pow == 0) return 1;

    return value * power(value, pow -1);
}
Dapeng
  • 1,704
  • 13
  • 25
0

Done in JavaScript:

function power(num,pow){
  if (pow == 0) return 1
  num /= 1/(power(num,--pow))
  return num
}

Call it like:

power(2,0) // -> 1
power(5,2) // -> 25
power(7,3) // -> 343

I feel like inverse division is cheating the no * operator rule, but eh, maybe that's what they were looking for.

jaredbranum
  • 171
  • 3
-2

I am using java programming language. The interviewer restricted you to declare a new variable inside the method better you pass it to the function. The interviewer didnt restrict you to use division operator (/) so you can use that.

static double getNthPowerOfNumber(double originalNumber,
        int power) {


    if (power == 0) {
        return 1;
    }
    if (originalNumber == 0) {
        return 0;
    } else {
        originalNumber/=1/getNthPowerOfNumber(originalNumber, --power);

        return originalNumber;
    }



}

if you want to get 5th power of a number 3 then write System.out.println("4..double..." + getNthPowerOfNumber(4, 1));

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • May I know the reason of -ve voting? Donot hide yourself after -ve voting. try to write the reason of -ve voting – Sunil Kumar Sahoo Jul 06 '11 at 06:20
  • This defines a new variable -- `int i`. – GargantuChet Jul 06 '11 at 06:21
  • I didn't down-vote, but this solution doesn't work. `1 / originalNumber` is zero. Integer arithmetic. – Stephen C Jul 06 '11 at 06:29
  • @Stephen, thanks for your reply. check my edited answer. it may work for you. and let me know if it does not work for you – Sunil Kumar Sahoo Jul 06 '11 at 06:58
  • I think you're gaming for points here. I mean its NOT coincidental at all that you both have the same middle and last name AT ALL. – Jesus Ramos Jul 06 '11 at 07:50
  • @Jesus, search in stack overflow you will get lots of people with the same middle name and surname. I am working in a company in the mean while I am trying to help others by posting answers in stackoverflow and also i aks questions here. I am not sitting ideal. What is the benefit that that I will get to gain point. I am not employer of stack-overflow. neither i am getting revenue from this. – Sunil Kumar Sahoo Jul 06 '11 at 08:30
  • @I found alot of persons with your firname also with your last name. I think you donot have all those stackoverflow accounts. How all these persons have similar name like ur? – Sunil Kumar Sahoo Jul 06 '11 at 08:36
  • nope im the only Jesus Ramos on SO nice defence though. – Jesus Ramos Jul 06 '11 at 08:38
  • also all of your friends questions are asked by him and answered by you or the other way around even if your answers are downvoted so tell me im wrong? – Jesus Ramos Jul 06 '11 at 08:40
  • @JesusRamos let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1176/discussion-between-sunil-kumar-sahoo-and-jesus-ramos) – Sunil Kumar Sahoo Jul 06 '11 at 09:20
  • yes all programmers are my friends. so i always give answers to any of questions if i can solve. – Sunil Kumar Sahoo Jul 06 '11 at 10:35