Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explain this recursive solution from Chap 3 Eloquent js? [duplicate]

I am going through chapter 3 in eloquent javascript and recursion was just introduced and I am trying wrap my mind around this concept.

Also can someone explain the second parameter history in the function find ?

Has history already been defined ?

Consider this puzzle: by starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. How would you write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number? For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice, whereas the number 15 cannot be reached at all.

function findSolution(target) {
  function find(start, history) {
    if (start == target)
      return history;
    else if (start > target)
      return null;
    else
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)");
  }
  return find(1, "1");
}

console.log(findSolution(24));
like image 969
mrbaseball Avatar asked Oct 20 '22 18:10

mrbaseball


1 Answers

console.log will print the value returned by findSolution(24) where 24 is the target.

now, when the function findSolution() is executed, it invokes (calls) the function "find" with 2 parameters : start = 1, history = "1" then returns something to be printed.

"return" sends back to the place the call was set. meaning find() is called within findSolution(), hence return will send back a value where it was called within findSolution.

in our case :

if start === target

return history //(to findSolution. let's call it "H")
return H to console.log() // prints 1

else if start > target

return null //(to findSolution, let's call it N)
return N to console.log() // prints null

otherwise if start is lesser than target, return A || B

A = find(start + 5, "(" + history + " + 5)")
B = find (start * 3, "(" + history + " * 3)")

meaning return A if A is truthy otherwise return B; A or B are evaluated before the function returns.

let's go through it step by step :

find will be invoked again and again with start parameter incremented by 5 ie :

return find(1, "1")    //  start = 1,   history = "(1 + 5)"    
return find(6, history)   //   start = 6,   history = "(1 + 5 ) + 5"   
return find(11, history)    //  start = 11,   history = "(1 + 5) + 5 ) + 5"   
return find(16, history)    //  start = 16,   history = "(1 + 5) + 5 ) + 5 ) + 5"   
return find(21, history) start = 21,   history = "(1 + 5) + 5 ) + 5 ) + 5 ) + 5"   

now the next value will be 26. since 26 > 22, "find" will return B instead

return find(63, history) start = 21 * 3 =  63,      
//since 63 > 24,  "null" is returned where start = 16,    

start is 16 and not 63 because it is a parameter. It is not changed and knows nothing about the incrementation by 5 that occurred during the invocation.

find(21*3, history) , 48 > 24 so return null where start = 11
find(11 *3, history) , 33 > 24 so return null where start = 6
find(6*3, history) , start = 18 and 18 < 24 so A will be truthy start will be incremented by 5
return find( 18 + 5, history)
return find (23 + 5, history) 28 > 24, A is null, B is evaluated

... and so on and so on until start is equal 24

like image 150
jfab fab Avatar answered Oct 22 '22 09:10

jfab fab