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