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
@olalekanmohammed5126 ай бұрын
I am most grateful for the work you have put here sir, God bless you
@nqobizithampande48945 ай бұрын
Awesome tutorials that make a complex concept easy. Thumbs up Sir
@sudharanirajasekar3409 Жыл бұрын
I really love your way of teaching.
@susantdasari23852 жыл бұрын
Good work! Really loved the simplification 👍
@vaibhavsharma4743 Жыл бұрын
function factorial(n){ return n
@nsikakessien2 жыл бұрын
Very Interesting Series. The way you break it down makes it simpler for me, thanks Vishwas.
@prachi_aroraАй бұрын
Thank you so much🙏
@Squarit12 жыл бұрын
What a amazing channel :ooo
@khusnijafar5872 жыл бұрын
Great work sir 👍
@szymon58112 жыл бұрын
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.
@Buggs264 ай бұрын
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
@oscargm19792 жыл бұрын
Really good work.I appreciate your teachings a lot :)
@sayedulkarim4747 ай бұрын
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)))
@NEELKAMAL-t9q Жыл бұрын
Great!! , Sir
@m_97__11 ай бұрын
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 😅😢
@funworld837910 ай бұрын
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.
@funworld837910 ай бұрын
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
@oscargm19794 ай бұрын
@@funworld8379 Master
@aakash62382 жыл бұрын
If (n < 1) then solution never give error whenever input is of negative integer.
@rajnandanibhawsar31052 жыл бұрын
Nice series please make a series on react native unit testing
@ajitoriginal Жыл бұрын
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 ?
@sharjeelfaiq166 ай бұрын
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);
@ronaldngarombo1026 Жыл бұрын
What is meant by tracing the function execution?
@codeedict39192 жыл бұрын
If i input 6000 as an argument, it says call stack exceeded. How can we resolve this issue?? And optimised solution for that.?
@oscargm19792 жыл бұрын
It is because that number is really long,because it is 6000*5999*5998*5997,etc
@olaoluwaanigboro-napoleon64842 жыл бұрын
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
@pastuh2 жыл бұрын
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
@prabhakaranishere2 жыл бұрын
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)
@HuntsDown2 жыл бұрын
@@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 Жыл бұрын
@@HuntsDown return n * factorialRec(n - 1); -> we are calling the factorialRec function, thats how its being called until 1
@m_97__11 ай бұрын
@@Shaktish-kumar is that factorialrecurision is a built in function? So it is calling until 1?
@MrBruh-xc1qy2 ай бұрын
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
@dataplusanimation3HD2 жыл бұрын
Hey vishwas could you please cover any react based animation library
@ogucharles28002 жыл бұрын
Does this mean that the base code always starts the function?
@karunyamohan58662 жыл бұрын
What if the n is negative? how shall we test it?
@wellness_and_wealth_forever2 жыл бұрын
Big O = O(n!), small correction, i think.
@oscargm19792 жыл бұрын
I think he is right, for n entries the funcion is called just n times
@sudharanirajasekar3409 Жыл бұрын
👏👏👏
@mrrishiraj882 жыл бұрын
🙏
@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.
@narendra33852 жыл бұрын
I will personally contact with you !!.How to Contact with You ?? ..I have some doubts regarding your front end code evolution blog ????
@eelguneezmemmedov16712 жыл бұрын
What kind of doubts do u have?
@theideadude12 күн бұрын
const findFactorial = n => n < 1 ? 1 : n * findFactorial(n-1);
@369-davian2 жыл бұрын
Internal Error: too much recursion: this is my response, I not understand anything
@Codevolution2 жыл бұрын
Please check with the code in the description down below.
@shyamsundarjha59132 жыл бұрын
implement the base case carefully, that's what terminates the recursiion cycle