2

I've an exercice to complete, and i should use a recursive function (basic one).

I'm using nodejs to avoid using JS Console in browsers.

When i try this function here :

function factorial(n) {
  if (n === 0) {
    return 0;
  }else {
      return (n * factorial((n-1)*(n-1)));
  }
}

So when i try : console.log(factorial(0)); it outputs "0". When i try to log any numbers superior to 0, it displays :

Maximum call stack size exceeded.

Thanks.

1
  • btw, 0! === 1. Commented Dec 21, 2018 at 10:24

3 Answers 3

0

You need to reduce the value by one foir the next recursion, instead of taking the product of the reduced value.

And you need to return one, if you have the value zero, because of the definition of 0!

function factorial(n) {
    if (n === 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

console.log(factorial(0));
console.log(factorial(1));
console.log(factorial(2));
console.log(factorial(3));
console.log(factorial(10));

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, yes i understand now, otherwise it would do an infinite loop. Thnaks to you for replying!
0

It is because factorial((n-1)*(n-1)) will return even more number when you input n more than 2. Therefore, it will be an infinite recursive function calls from that point on. It will work, though, if you put 0, 1, or 2.

function factorial(n) {
  if (n === 0) {
    return 0;
  }else {
      return (n * factorial((n-1)*(n-1)));
  }
}

console.log("fac0: "+factorial(0));
console.log("fac1: "+factorial(1));
console.log("fac2: "+factorial(2));
console.log("fac3: "+factorial(3));

Comments

0

Normally for a recursive function to work it needs to have a base case where the recursion stops. It also needs the recursive step to be simpler than the original problem, thus less work, such that eventually it will hit the base case.

So looking at your code:

function factorial(n) {
  if (n === 0) {
    return 0;
  }else {
      return (n * factorial((n-1)*(n-1))); // problem is here
  }
}

It has a base case where it returns 0 when n is zero. Thus factorial(0) // ==> 0. Now lets say we try it with 3.

factorial(3);                   // ==
3 * factorial((3-1)*(3-1));     // ==
3 * factorial(4);               // ==
3 * 4 * factorial((4-1)*(4-1)); // ==
3 * 4 * factorial(9);           // == 

The requirement that the recursion is a simpler problem. factorial(4) is not simpler than factorial(3) so this recursion will never ever hit the base case.

If you want to implement factorial the base case should return 1 and not 0 and the recursion is always one less then the current value. factorial(2) is a simpler problem than factorial(3) since it will hot the base case such that the result is 3 * 2 * 1 * 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.