-1

I have a small problem I want to build a function that returns with recursive function the nn expression with one parameter in the calling function. Can someone help me? My thought so far :

int powerThroughRecursion(int n) {      
    if (n == 0) {
        return 1;
    }
    if (n <= 1) {
        return n;
    }
    return n * powerThroughRecursion(n - 1);
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 1
    Can you please tell us what's wrong with the function you show? For some specific input, what is the actual and expected result? What is the actual problem with the code you show? – Some programmer dude Jan 10 '16 at 12:37
  • 1
    Ok I ''ll show you with an example , if you''ll call the function with n=3 it should return 27 but it returns 6.I hope I helped you , i tried also with a variable in the function but it didn't help . – Patryk Wajs Jan 10 '16 at 12:39

2 Answers2

2

Yes, you are actually computing n! there. One way to do it is the following:

#include <iostream>
#include <string>

int powerThroughRecusion(int n, int step) {   
    if (step < 1)
        return 1;

    return n * powerThroughRecusion(n, step - 1);
}

int main()
{
  std::cout << powerThroughRecusion(4, 4);
}

You multiply by n, but a step will tell you how much multiplications to be done.

[edit] using a single parameter function

#include <iostream>
#include <string>

int powerThroughRecusionInternal(int n, int step) {   
    if (step < 1)
        return 1;

    return n * powerThroughRecusionInternal(n, step - 1);
}

int powerThroughRecusion(int n)
{
   return powerThroughRecusionInternal(n, n);
}

int main()
{
  std::cout << powerThroughRecusion(2) << "\n";
  std::cout << powerThroughRecusion(4);
}
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • no sorry I want to call the function with oneparameter – Patryk Wajs Jan 10 '16 at 12:45
  • 1
    Well, @PatrykWajs do not use a recursive function then. – Maxime Chéramy Jan 10 '16 at 12:46
  • I need a recursive function , It is an excercise , I can solve the function with two parameters but I lost the past 3 hours to solve it with one parameters i thinkit is impossible, I just wanted an opinion on the fact – Patryk Wajs Jan 10 '16 at 12:50
  • 2
    @PatrykWajs Can you build a function `f` such that `n^n = f((n-1)^(n-1))` ? if the answer is yes, then it's possible, otherwise it's not. – Maxime Chéramy Jan 10 '16 at 12:58
  • Sorry my friend but it is entirely wrong , thank you for the support thow ^^ – Patryk Wajs Jan 10 '16 at 13:07
  • I don't think it is possible with one parameter in the recursive function since the exponent and the base of the exponential are 2 different variables, changes in the exponent should not change the base. The closest thing you can do is to wrap the recursive function. – krato Jan 10 '16 at 13:18
2

If you want a one parameter function. just hide and wrap the two parameter implementation version with another function call.

static int powerThroughRecusionImpl(int n, int power) {       
    if (power == 0) {
        return 1;
    }
    if (power == 1) {
        return n;
    }
    return n * powerThroughRecusionImpl(n ,power - 1);
}

int powerThroughRecusion(int n) {       
    return powerThroughRecusionImpl(n ,n);
}

Or if you want to be non thread-safe.

int powerThroughRecusion(int n) {       
    static int base = 0;

    if (!base) {
      base = n;
      n = powerThroughRecusion(n);
      base = 0;
      return n;
    } else {
      if (n == 0) {
        return 1;
      } else if (n == 1) {
        return base;
      }
      return base * powerThroughRecusion(n - 1);
    }
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Wow really amazing , Thank you , can you really explain me what does the program and what static mean? I would really appreciate if you could , Thank you – Patryk Wajs Jan 10 '16 at 15:59
  • @PatrykWajs, You can read about `static` [here](http://stackoverflow.com/a/572550/817643). The program simply stores a value where all invocations of the function can see it. I basically use this common variable to treat `n` first as the base, and then as the exponent. – StoryTeller - Unslander Monica Jan 10 '16 at 16:02
  • Wow really amazing I lost 5 hours to do this program with one parametre , thank you really I'll study the program to understand it , Thank you really ^^ – Patryk Wajs Jan 10 '16 at 16:04
  • @PatrykWajs, NP. However note that this is a hack, and not quality code. This suffers from all the problems of global variables. – StoryTeller - Unslander Monica Jan 10 '16 at 16:05