Comparing Iterative and Recursive Factorial Functions

  Рет қаралды 158,223

Khan Academy

Khan Academy

Күн бұрын

Comparing iterative and recursive factorial functions

Пікірлер: 55
@DJ-wu9yi
@DJ-wu9yi 9 жыл бұрын
Just had one of those "Oooooooh" moments. Thanks Khan!
@sanketkumar9058
@sanketkumar9058 4 жыл бұрын
Ohhhhhh, not ooooh
@Silver-nm2if
@Silver-nm2if 3 жыл бұрын
lol same
@shreyashkashyap
@shreyashkashyap 3 жыл бұрын
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😉
@vinlacey
@vinlacey 8 жыл бұрын
Absolutely wonderful explanation of recursion. Well done!
@gexwing
@gexwing 12 жыл бұрын
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-co7hg
@Sauravkumar-co7hg 3 жыл бұрын
Thank you for ur 8 y/o comment..
@slowcreep6978
@slowcreep6978 4 ай бұрын
This was the piece I was missing, thanks.
@RUCER3Al
@RUCER3Al 6 жыл бұрын
I love the concept of Programming. But I really hate the method my college book is explaining this. Thank you Khan Academy!!!
@rockingamingwiththesahit2145
@rockingamingwiththesahit2145 4 жыл бұрын
Textbooks always show thing in the hard way and not really helpful except for problems,in contrary to SK
@jakeambrose4294
@jakeambrose4294 7 жыл бұрын
the way you write it and show it here was enlightening for me
@myne4
@myne4 13 жыл бұрын
Recursion blows my mind.
@thepr1ntgnome430
@thepr1ntgnome430 3 жыл бұрын
Yeah I know what you mean, but the process in real life is just too slow. Iteration must be better.
@shreyashkashyap
@shreyashkashyap 3 жыл бұрын
Same here.
@shreyashkashyap
@shreyashkashyap 3 жыл бұрын
@@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.
@nawfaljafri
@nawfaljafri 2 жыл бұрын
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
@mohchoudhary
@mohchoudhary 12 жыл бұрын
YOU'RE AWESOME, THANKS!!!
@candichiu7850
@candichiu7850 2 жыл бұрын
Thank you! very clear.
@SB-ig1rg
@SB-ig1rg 3 жыл бұрын
Veeeryy cool job man, big thanks to you✌️
@spirituelconnexion
@spirituelconnexion 12 жыл бұрын
Sal, watching this playlist makes me want to switch my major to CS :(:(:(
@bir_deb
@bir_deb 4 жыл бұрын
Boom boom! Why is it so cutely explained!
@DualSignal
@DualSignal 9 жыл бұрын
This video doesn't provide any comparison like the title says. It's more of a walkthrough of an iterative and recursive example.
@JollyBeJolly
@JollyBeJolly 4 жыл бұрын
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.
@Huncho0
@Huncho0 3 жыл бұрын
@@JollyBeJolly good one - imma add that to my notes thanks!
@xWaterTwist
@xWaterTwist 2 жыл бұрын
@@JollyBeJolly That was such a good explanation thanks
@petruciucur
@petruciucur 7 жыл бұрын
Very well explanation. So the program stacks the numbers n in memory then go back and solve each multiplication.
@k12_hnbuihuyhoang51
@k12_hnbuihuyhoang51 6 жыл бұрын
Thanks god you're here.
@RiyadhElalami
@RiyadhElalami 13 жыл бұрын
I love this guy
@serenity3270
@serenity3270 6 жыл бұрын
note, that i + i should be i + 1? yes?
@Nopalesasados
@Nopalesasados 8 жыл бұрын
Thank you.
@sophia0282
@sophia0282 2 жыл бұрын
Thank you sir!
@majesty1396
@majesty1396 8 жыл бұрын
thanks a lot
@lolk191
@lolk191 7 жыл бұрын
hey tech me if we put njmber=1 in if statement so ans is 1 how
@kerrywsmyth
@kerrywsmyth 12 жыл бұрын
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.
@B1GL0NGJ0HN
@B1GL0NGJ0HN 9 ай бұрын
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
@tubex1300 Жыл бұрын
can u recommend the books about the iteration
@AlderDragon
@AlderDragon 13 жыл бұрын
@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.
@ferhatakbulut6572
@ferhatakbulut6572 4 жыл бұрын
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.
@victornnah3920
@victornnah3920 7 жыл бұрын
Must I always return 1
@djbvon6769
@djbvon6769 8 жыл бұрын
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.
@weiliantan4386
@weiliantan4386 7 жыл бұрын
What does return do ?
@hassanshabbir8254
@hassanshabbir8254 6 жыл бұрын
It is a python keyword referring to the output of the function.
@riteshsaha9729
@riteshsaha9729 4 жыл бұрын
which function is faster?
@gardenhouseNemurin
@gardenhouseNemurin 3 жыл бұрын
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
@bara7979 Жыл бұрын
Missed your sound❤
@moliv8927
@moliv8927 3 жыл бұрын
not the comparison i expected ...
@Lundburgerr
@Lundburgerr 13 жыл бұрын
python is the shit!
@ujjvalw2684
@ujjvalw2684 2 жыл бұрын
Who is this Teacher?
@danielford2898
@danielford2898 2 жыл бұрын
Chaos
@phazon100
@phazon100 13 жыл бұрын
second. lol
@SarinaSaman
@SarinaSaman 4 жыл бұрын
recursion is a billion times better than iteration tbh
@Sauravkumar-co7hg
@Sauravkumar-co7hg 3 жыл бұрын
How is that so.?? Recursion takes a lot of memory
@gardenhouseNemurin
@gardenhouseNemurin 3 жыл бұрын
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.
@MarsTheProgrammer
@MarsTheProgrammer 12 жыл бұрын
you can't explain recursions with functions in 2 minutes...
Exercise - Write a Fibonacci Function
4:15
Khan Academy
Рет қаралды 105 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Recursive vs Iterative Solutions
8:24
MrBrownCS
Рет қаралды 7 М.
Programming Loops vs Recursion - Computerphile
12:32
Computerphile
Рет қаралды 1,5 МЛН
recursion finding factorial of a number
7:10
Satpute Academy
Рет қаралды 55 М.
Magnus Carlsen, Hans Niemann Drama Just Got Much Worse
31:16
GothamChess
Рет қаралды 251 М.
This is a Better Way to Understand Recursion
4:03
Alex Hyett
Рет қаралды 34 М.
Tail Recursion Explained - Computerphile
16:05
Computerphile
Рет қаралды 170 М.
Towers of Hanoi: A Complete Recursive Visualization
21:13
Reducible
Рет қаралды 452 М.
You WILL Understand Recursion After Watching This
3:46
15 4 Convert recursion to iteration
6:54
KEC Computer Science
Рет қаралды 7 М.