How does recursion work?

0

Can someone tell me how the recursion works? Step by step if it is possible, I do not get to understand it, how the function can not be executed, as it takes new values until it reaches zero, there it will return 1 and again to the other function? Thanks

function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    // This is it! Recursion!!
    return n * factorial(n - 1);
  }
}
console.log(factorial(4)); // OUTPUT => 24
    
asked by fran 09.12.2017 в 16:06
source

3 answers

1

A recursive function is a function that calls itself .

But what are they for? , is not more or less efficient since it depends on the programming language and the implementation of the algorithm, then?

They make a cleaner and more elegant code.

And how is a recursive function conformed?

A recursive function conforms to a base condition, which you probably have heard, but what does it mean?

It means that obviously the function has to stop at some point, otherwise the function will be called endless times ..

And it will also be conformed by its algorithm itself.

p.e:

Factorial of a number:

Recursive v / s Cycle

Recursive:

function _f(number){
 if(number === 1) return number;
 return number * _f(number -1);
}
console.log(_f(5));

function _fFor (number){
 var acc = 1;
 while (number > 1) {
 acc *= number;
 number--;
 }
 return acc;
}
console.log(_fFor(5));

In the case of taking the factorial of a number, how does it work?

The factorial of a number works like this:

 n = n * (n-1) + (n-1) * (n-2) .... 1 Hasta llegar a 1

Until you reach 1, because if you multiplied by 0, it would always give 0 (any number * 0 is = 0)

We analyze your function:

function factorial(n) { // 1° Recibe el numero al cual calculara el factorial
  /* Supondremos que necesitamos el factorial de 4 */
  if (n === 0) { // 3° cuando sea 0, NO multiplica por 0, osino anularia todo, sino por 1 que es el neutro multiplicativo
    return 1;
  } else {
    // This is it! Recursion!!
    return n * factorial(n - 1); // 2° Retornara 4 * 4-1 , ya que la funcion factorial en si retorna el n -1 y asi hasta llegar a 0
  }
}

If you ask, because it returns n * factorial (n-1) is because it must "acumularlo" , then, the value is accumulated internally and then in the base case, in this case when it is 0, returns the value fully accumulated.

    
answered by 10.12.2017 в 02:12
0

The most important thing to use recursion is to have a clear exit condition in your case

  

n === 0    thus you will avoid entering an infinite cycle.

as a second part you must perform the same function in your case return n * factorial(args);

and finally you have to do something so that your condition n === 0 is fulfilled in your case (n - 1)

To finish the recursion is to execute the same function until something is fulfilled Your example in pseudocode would read like this:

While my entry is different from 0 multiply it by n and subtract 1

So what your function would be doing is

  • Yes 4 < > 0   4 * function (4-1) = 4
  • Yes 3 < > 0 3 * function (3-1) = 3
  • Yes 2 < > 0 2 * function (2-1) = 2
  • Yes 1 < > 0    1 * function (1-1) = 1

Then we see that what the function does is multiply the results first from the lowest since the function is nested

  • 1 * 2 = 2
  • 2 * 3 = 6
  • 6 * 4 = 24
answered by 09.12.2017 в 16:44
0

Definition

  

The factorial of a number is the product of that number for all the previous ones less than it until reaching zero. By definition, the factorial of the number 0 is 1.

The result is represented by an abbreviated notation that is the exclamation mark symbol (!).

That said, the factorial of number 5 would be:

5! = 5 * 4 * 3 * 2 * 1

Javascript factorial without thinking too much ...

Quickly from the definition we could code the Javascript factorial function in the following way, with a decreasing loop of the argument until we reach 1 or ascending from 1 to the desired number that we receive as the argument from which we want to calculate the Javascript factorial. The zero would not be necessary to consider it because its factorial is 1

 function factorial (n) {
var total = 1; 
for (i=1; i<=n; i++) {
    total = total * i; 
}
return total; 
}

Let's look at the definition

If we read well the definition of factorial as a result of multiplying with all previous ones. In the example and being the multiplication an associative operation (it is also commutative, but it does not matter for what we want to point out):

So we can calculate the Javascript factorial as a recursive function, that is, it refers to itself multiplying the argument by the javascript factorial of the preceding number:

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

Source Code Line

    
answered by 09.12.2017 в 18:26