Comparing iterative and recursive factorial functions
Пікірлер: 55
@DJ-wu9yi9 жыл бұрын
Just had one of those "Oooooooh" moments. Thanks Khan!
@sanketkumar90584 жыл бұрын
Ohhhhhh, not ooooh
@Silver-nm2if3 жыл бұрын
lol same
@shreyashkashyap3 жыл бұрын
Thanks a ton Khan Academy. Idk why your videos don't get much views but your quality of teaching is remarkable! This is much better than the colleges/universities, that's why Elon Musk us with you guys😉
@vinlacey8 жыл бұрын
Absolutely wonderful explanation of recursion. Well done!
@gexwing12 жыл бұрын
it works this way: the function is still running while it calls the next function, so it builds up a stack of function calls (thus: call stack), then when it reached the base case, it returns a value, then the above returns, etc. etc. until the call stack has been reduced back to the original function call, which then returns the result, done. the variable number is NOT the same for all the function calls, each function call has its own variables.
@Sauravkumar-co7hg3 жыл бұрын
Thank you for ur 8 y/o comment..
@slowcreep69784 ай бұрын
This was the piece I was missing, thanks.
@RUCER3Al6 жыл бұрын
I love the concept of Programming. But I really hate the method my college book is explaining this. Thank you Khan Academy!!!
@rockingamingwiththesahit21454 жыл бұрын
Textbooks always show thing in the hard way and not really helpful except for problems,in contrary to SK
@jakeambrose42947 жыл бұрын
the way you write it and show it here was enlightening for me
@myne413 жыл бұрын
Recursion blows my mind.
@thepr1ntgnome4303 жыл бұрын
Yeah I know what you mean, but the process in real life is just too slow. Iteration must be better.
@shreyashkashyap3 жыл бұрын
Same here.
@shreyashkashyap3 жыл бұрын
@@thepr1ntgnome430 but it's more efficient and it requires much less code lines as compared to iteration method. It sure has its own advantages at many points.
@nawfaljafri2 жыл бұрын
so basically (i think, idk cuz I'm just watching this video for fun, i dunno much about coding so correct me if I'm wrong) for 0, we specify that if product = 0 or less, then return product = 1 since a 0 in this formula would mean 0(0+1) which is 0x1=0; what's happening is basically we take an integer (integers start from 0 unlike counting numbers that start from 1) and we put it in the formula 1(i+1) where i is the integer and the 1 outside of the parenthesis is the previous product/factorial since the product of 0 factorial is 1, we start with 1; as it iterates, the previous factorial product becomes the new product outside of the parenthesis we're trying to find so we continue doing this for the number of times we want a factorial for so 3! would be 6 because 1(0+1) = 1, 1(1+1) = 2, 2(2+1) = 6 so the to calculate the factorial we could also do: def factorial(number): if number
@mohchoudhary12 жыл бұрын
YOU'RE AWESOME, THANKS!!!
@candichiu78502 жыл бұрын
Thank you! very clear.
@SB-ig1rg3 жыл бұрын
Veeeryy cool job man, big thanks to you✌️
@spirituelconnexion12 жыл бұрын
Sal, watching this playlist makes me want to switch my major to CS :(:(:(
@bir_deb4 жыл бұрын
Boom boom! Why is it so cutely explained!
@DualSignal9 жыл бұрын
This video doesn't provide any comparison like the title says. It's more of a walkthrough of an iterative and recursive example.
@JollyBeJolly4 жыл бұрын
An Iterative solution uses a loop and computes as it goes. Recursive breaks a problem down into smaller problems and calls itself for each of the smaller problems.
@Huncho03 жыл бұрын
@@JollyBeJolly good one - imma add that to my notes thanks!
@xWaterTwist2 жыл бұрын
@@JollyBeJolly That was such a good explanation thanks
@petruciucur7 жыл бұрын
Very well explanation. So the program stacks the numbers n in memory then go back and solve each multiplication.
@k12_hnbuihuyhoang516 жыл бұрын
Thanks god you're here.
@RiyadhElalami13 жыл бұрын
I love this guy
@serenity32706 жыл бұрын
note, that i + i should be i + 1? yes?
@Nopalesasados8 жыл бұрын
Thank you.
@sophia02822 жыл бұрын
Thank you sir!
@majesty13968 жыл бұрын
thanks a lot
@lolk1917 жыл бұрын
hey tech me if we put njmber=1 in if statement so ans is 1 how
@kerrywsmyth12 жыл бұрын
I would never guess recursion would ever work because it APPEARS as if you are redefining the value of the variable "number" and returning a result every time the function recurs. The thing that needs explaining for me is how Python knows to remember each of the returned results for each recurring function call.
@B1GL0NGJ0HN9 ай бұрын
You need to learn about the “stack”. When new functions get ran, all of those variables are put in a new place in the “stack”. Stack and heap. Different memory allocations.
@tubex1300 Жыл бұрын
can u recommend the books about the iteration
@AlderDragon13 жыл бұрын
@radiator0505 I think it has to do with the definition of a factorial. It works fine when the numbers are decreasing, but the definition of a factorial given a negative number is different (since the numbers are increasing each time rather than decreasing).. this results in a divide-by-zero error when you get to 1. Why the program returns 1 I am not sure.
@ferhatakbulut65724 жыл бұрын
You are absolutely right sir, also going to zero makes no sense since you are multiplying because after that your value would be always 0 since you multiplied by a zero once.
@victornnah39207 жыл бұрын
Must I always return 1
@djbvon67698 жыл бұрын
I have to admit, almost went to another video after hearing a mistake in the "iterative" pronunciation within the very first verbal audible of this video.
@weiliantan43867 жыл бұрын
What does return do ?
@hassanshabbir82546 жыл бұрын
It is a python keyword referring to the output of the function.
@riteshsaha97294 жыл бұрын
which function is faster?
@gardenhouseNemurin3 жыл бұрын
Late response but iteration is generally faster, although the difference only becomes noticeable as you increase the number of iterations/recursive calls you need to make. Recursion creates unnecessary overhead as you need to keep multiple calls open until you reach a base case, which can easily deplete the resources of a less powerful machine. A well optimized iterative solution always beats a well optimized recursive one (even if you use memoization), because it does not have to deal with the extra overhead of keeping recursive calls open.
@bara7979 Жыл бұрын
Missed your sound❤
@moliv89273 жыл бұрын
not the comparison i expected ...
@Lundburgerr13 жыл бұрын
python is the shit!
@ujjvalw26842 жыл бұрын
Who is this Teacher?
@danielford28982 жыл бұрын
Chaos
@phazon10013 жыл бұрын
second. lol
@SarinaSaman4 жыл бұрын
recursion is a billion times better than iteration tbh
@Sauravkumar-co7hg3 жыл бұрын
How is that so.?? Recursion takes a lot of memory
@gardenhouseNemurin3 жыл бұрын
It's sometimes easier to come up with recursive solutions for some problems (e.g., given a positive integer n, finding the n-th element of the Fibonacci sequence.) However, as the number of recursive calls increase, so does the overhead of keeping those calls open, which makes your program use more resources than it really needs to. Even if you use memoization, iteration is generally faster and consumer less resources as everything is contained in a single call.
@MarsTheProgrammer12 жыл бұрын
you can't explain recursions with functions in 2 minutes...