JavaScript Algorithms - 13 - Recursive Factorial of a Number

Поділитися
Вставка
  • Опубліковано 3 січ 2025

КОМЕНТАРІ • 49

  • @pmburu0
    @pmburu0 Рік тому +3

    This is the breakdown of the function flow. I wasn't getting it till i traced it on paper
    5 * recursiveFactorial(5-1)
    4 * recursiveFactorial(4 - 1)
    3 * recursiveFactorial(3 -1)
    2 * recursiveFactorial(2 -1 )
    1 * recursiveFactorial(1-1) return 1 when it get to the base case

  • @olalekanmohammed512
    @olalekanmohammed512 6 місяців тому

    I am most grateful for the work you have put here sir, God bless you

  • @nqobizithampande4894
    @nqobizithampande4894 5 місяців тому

    Awesome tutorials that make a complex concept easy. Thumbs up Sir

  • @susantdasari2385
    @susantdasari2385 2 роки тому +2

    Good work! Really loved the simplification 👍

  • @nsikakessien
    @nsikakessien 2 роки тому

    Very Interesting Series. The way you break it down makes it simpler for me, thanks Vishwas.

  • @vaibhavsharma4743
    @vaibhavsharma4743 Рік тому +4

    function factorial(n){
    return n

  • @sudharanirajasekar3409
    @sudharanirajasekar3409 Рік тому

    I really love your way of teaching.

  • @szymon5811
    @szymon5811 2 роки тому +2

    There is a mistake at 5:20 . The code from video always calls the function n+1 times . If the if condition was written as "if(n===0 || n===1)" it would always be n times.

  • @prachi_arora
    @prachi_arora Місяць тому

    Thank you so much🙏

  • @Squarit1
    @Squarit1 2 роки тому +1

    What a amazing channel :ooo

  • @khusnijafar587
    @khusnijafar587 2 роки тому +1

    Great work sir 👍

  • @Buggs26
    @Buggs26 4 місяці тому

    If I put a negative number (although clearly it says positive integers only) the function ends up in the endless loop causing maximum call stack size error. This is the solution i came up with although negative integer will always resort to 1 yet it wont resort to endless loop (perhaps we add 1-line code to throw Error for negative integers). Anyway, In terms of space complexity, my proposed solution doesn't add more stack to the memory as I incorporated the tail recursion than a regular one. Hence, the final result is accumulated till the very end call.
    function recursiveFactorial(n, product = 1) {
    if (n

  • @aakash6238
    @aakash6238 2 роки тому +2

    If (n < 1) then solution never give error whenever input is of negative integer.

  • @m_97__
    @m_97__ Рік тому +2

    How does the computer knows that we are calculating factorial while
    N * recursivefactorial(N-1)
    is that recursivefactorial() is a built-in function in JavaScript ?
    Because vishwas didn't code anything for factorial in base case.
    He just coded
    n === 0:
    return = 1
    Then how the factorial calculation done automatically 😅😢

    • @funworld8379
      @funworld8379 11 місяців тому +2

      The computer calculates factorial(4) by recursively applying the defined function factorial(n) and repeatedly breaking down the problem into smaller subproblems until it reaches the base case. Let me clarify how the process works:
      Lets take example of factorial(4)
      factorial(4) Calculation:
      The initial call is factorial(4).
      It checks the base case: if (n === 0), which is false (as n is 4).
      It enters the recursive call: 4 * factorial(3).
      factorial(3) Calculation:
      Now, it needs to calculate factorial(3).
      It checks the base case: if (n === 0), which is false.
      It enters the recursive call: 3 * factorial(2).
      factorial(2) Calculation:
      Again, it needs to calculate factorial(2).
      Checks the base case: if (n === 0), which is false.
      Enters the recursive call: 2 * factorial(1).
      factorial(1) Calculation:
      Needs to calculate factorial(1).
      Checks the base case: if (n === 0), which is false.
      Enters the recursive call: 1 * factorial(0).
      factorial(0) Calculation:
      Base case is true: if (n === 0).
      Returns 1.
      Backtracking:
      Backtracking starts from the base case:
      factorial(0) returns 1.
      factorial(1) returns 1 * 1 = 1.
      factorial(2) returns 2 * 1 = 2.
      factorial(3) returns 3 * 2 = 6.
      factorial(4) returns 4 * 6 = 24.
      So, the computer calculates factorial(4) by repeatedly applying the function and evaluating smaller subproblems until it reaches the base case, and then it combines the results during the backtracking process. The result is indeed 24, and this is achieved through recursion and the mathematical definition of factorial.

    • @funworld8379
      @funworld8379 11 місяців тому

      Now for one more question on how does computer know the previous value to multiply:
      Here's the key part of the factorial function:
      return n * factorial(n - 1);
      This line of code is what triggers the recursive call, and it contains the multiplication operation. Let's analyze how it works:
      Current Value (n):
      The function receives the current value n.
      Multiplication:
      It multiplies the current value n by the result of the recursive call factorial(n - 1).
      Recursive Call (factorial(n - 1)):
      This is the key to understanding the multiplication with the previous value.
      The recursive call is made with the value n - 1, which is the previous value.
      Repetition:
      The process repeats for the recursive call with n - 1.
      At each step, the function multiplies the current value (n) by the result of the recursive call with the previous value (n - 1).
      Base Case:
      The recursion continues until it reaches the base case (if (n === 0) return 1;).
      At the base case, the function returns 1, and the recursion starts unwinding.
      Backtracking:
      During the backtracking process, each multiplication result is combined to calculate the overall result.
      In summary, the computer knows to multiply with the previous value because the recursive call involves passing the previous value (n - 1). This process continues until it reaches the base case, and then it combines the results during the backtracking process.
      Let's trace factorial(4) to illustrate further:
      factorial(4) = 4 * factorial(3)
      = 4 * (3 * factorial(2))
      = 4 * (3 * (2 * factorial(1)))
      = 4 * (3 * (2 * (1 * factorial(0))))
      = 4 * (3 * (2 * (1 * 1)))
      = 4 * (3 * (2 * 1))
      = 4 * (3 * 2)
      = 4 * 6
      = 24

    • @oscargm1979
      @oscargm1979 4 місяці тому

      @@funworld8379 Master

  • @oscargm1979
    @oscargm1979 2 роки тому

    Really good work.I appreciate your teachings a lot :)

  • @sayedulkarim474
    @sayedulkarim474 8 місяців тому

    function rfn(n) {
    if (n < 1) return 1;
    return n * rfn(n - 1);
    }
    When you call rfn(5), it doesn't just multiply 5 by (5 - 1), it actually calls the rfn function again with the argument (5 - 1), which is 4. So it's more like this:
    ```
    5 * rfn(4)
    ```
    Now, the rfn(4) part will further break down into
    ```
    5 * (4 * rfn(3))
    // here '4' comes because of the first "n" =====> [ n * rfn(n-1) ]
    ```
    ..... at last 5 * (4 * (3 * (2 * rfn(1)))) === 5 * (4 * (3 * (2 * 1)))

  • @ajitoriginal
    @ajitoriginal Рік тому +1

    The code will not work if user passes -ve integer. So, I think (n < 1) is better condition instead of (n === 0). What do you think ?

  • @rajnandanibhawsar3105
    @rajnandanibhawsar3105 2 роки тому

    Nice series please make a series on react native unit testing

  • @NEELKAMAL-t9q
    @NEELKAMAL-t9q Рік тому

    Great!! , Sir

  • @ronaldngarombo1026
    @ronaldngarombo1026 Рік тому

    What is meant by tracing the function execution?

  • @MrBruh-xc1qy
    @MrBruh-xc1qy 2 місяці тому

    i have been trying to figure out a way to recursively solve the prime number problem state ment aswell and this works
    function recursivePrimeNumber(n,div=2){
    if(n==0|| n==1)
    {
    return false
    }
    if(n%div!=0 && div

  • @karunyamohan5866
    @karunyamohan5866 2 роки тому

    What if the n is negative? how shall we test it?

  • @ogucharles2800
    @ogucharles2800 2 роки тому

    Does this mean that the base code always starts the function?

  • @codeedict3919
    @codeedict3919 2 роки тому +1

    If i input 6000 as an argument, it says call stack exceeded. How can we resolve this issue?? And optimised solution for that.?

    • @oscargm1979
      @oscargm1979 2 роки тому +1

      It is because that number is really long,because it is 6000*5999*5998*5997,etc

  • @olaoluwaanigboro-napoleon6484
    @olaoluwaanigboro-napoleon6484 2 роки тому +2

    So, I feel like I'm the dumbest here...lol
    I say this because though Vishwas simplified this but I still can't bring myself to understand and I'd be glad to get an assistance in where I'm not clear.
    So firstly, I get when we break the recursive factorial to a simple equation (n! = n x (n - 1)!) but it is in the code I get confused because we did
    return n * recursiveFactorial(n-1)
    and this is working but I do not understand why because if n = 5, then it will be
    5 * recursiveFactorial (5-1). This should be 5 * 4 equals to 20 but the answer is 120. Please I'd appreciate a guide on the logic behind this and I hope my question was understood because I feel like I have complicated it more...Thank you

    • @pastuh
      @pastuh 2 роки тому

      5*4 its only first call. You forgot after first call, same function is called again.
      Last video showed diagram.
      Just take paper and pencil and try to calculate
      Edit: simplest answer.. start calculate from last call and go up till your mentioned 5*4 .. and you will find out its 5*24 , because previous calls changed function result. Changed, because it returns number which is used in another call

    • @prabhakaranishere
      @prabhakaranishere 2 роки тому +2

      Assume I give number: 5
      for this line "return n * recursive_Factorial(n - 1)"
      as per above return statement (this code run continuously till it come to 1)
      5x4 = 20 // 5 * recursive_Factorial(5-1)
      20*3 = 60 // 20 * recursive_Factorial(4-1)
      60*2 = 120 // 60 * recursive_Factorial(3-1)
      120x1 = 120 // 120 * recursive_Factorial(5-1)

    • @HuntsDown
      @HuntsDown 2 роки тому

      @@prabhakaranishere How does the code know that it is suppose to do that? When I am reading it, it looks like it is n * n-1. If n is 5 then n-1 would be 4.
      5*4= 20
      what part of the code is telling it to go through all numbers from 5 to 1?

    • @Shaktish-kumar
      @Shaktish-kumar Рік тому

      @@HuntsDown return n * factorialRec(n - 1); -> we are calling the factorialRec function, thats how its being called until 1

    • @m_97__
      @m_97__ Рік тому

      ​@@Shaktish-kumar is that factorialrecurision is a built in function?
      So it is calling until 1?

  • @dataplusanimation3HD
    @dataplusanimation3HD 2 роки тому

    Hey vishwas could you please cover any react based animation library

  • @sharjeelfaiq16
    @sharjeelfaiq16 6 місяців тому

    function generateFibonacciSeq(n, ubound, seq = []) {
    if (n >= ubound) {
    return seq;
    } else {
    let int = n;
    if (seq.length >= 2) {
    int = seq[n - 1] + seq[n - 2];
    }
    seq.push(int);
    return generateFibonacciSeq(n + 1, ubound, seq);
    }
    }
    const ubound = 10;
    const int = 0;
    const fibonacciSeq = generateFibonacciSeq(int, ubound);
    console.log(fibonacciSeq);

  • @wellness_and_wealth_forever
    @wellness_and_wealth_forever 2 роки тому

    Big O = O(n!), small correction, i think.

    • @oscargm1979
      @oscargm1979 2 роки тому +1

      I think he is right, for n entries the funcion is called just n times

  • @KiraRu-pf6qq
    @KiraRu-pf6qq Рік тому

    function recursiveFactorial(n) {
    if (n === 1 || n === 0) {
    return 1;
    }
    for (var i = n - 1; i >= 1; i--)
    n *= i;
    return n;
    }
    Hi, can you please tell me the Big O of my solution? I prefer yours, but I'm still learning, and I'm just curious.

  • @narendra3385
    @narendra3385 2 роки тому

    I will personally contact with you !!.How to Contact with You ?? ..I have some doubts regarding your front end code evolution blog ????

  • @theideadude
    @theideadude 22 дні тому

    const findFactorial = n => n < 1 ? 1 : n * findFactorial(n-1);

  • @sudharanirajasekar3409
    @sudharanirajasekar3409 Рік тому

    👏👏👏

  • @mrrishiraj88
    @mrrishiraj88 2 роки тому

    🙏

  • @369-davian
    @369-davian 2 роки тому

    Internal Error: too much recursion: this is my response, I not understand anything

    • @Codevolution
      @Codevolution  2 роки тому +1

      Please check with the code in the description down below.

    • @shyamsundarjha5913
      @shyamsundarjha5913 2 роки тому +1

      implement the base case carefully, that's what terminates the recursiion cycle