Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 8: File Searcher App
Lecture: Recursion factorial example
0:00 So let's take a step back from application for a moment and just open this empty file called play for play around,
0:07 where we can just play around with some ideas. We'll do this for recursion but we also do this for our generator methods later.
0:13 Let's take a really simple case something like creating the factorial of a number. Now remember, factorials are sort of iterative processes.
0:22 The factorial written with like a number and an exclamation point right so like let's say 5! = 120 and the way that you get that is you take 5*4*3*2*1
0:38 and each step if I start here the thing I need to multiply is the number minus 1 times the rest of the factorial
0:47 and then form here I need to multiply it by that smaller number minus 1 times the rest of the factorial
0:53 and I keep doing that until I get down to 1 and then I stop. So this turns out to be really easily modeled with this concept called recursion.
1:01 So let's look at this in code and then we'll look at this conceptually and we'll go back and apply this to our searching app.
1:08 So here we have this factorial function, empty, but it's going to return the number which is the factorial
1:14 so here I have pre loaded the few, we'll compute 5, 3 and 11 factorial and these numbers grow tremendously large in a hurry.
1:23 So remember the way you take the factorial, is you want to take the number in this case we are going to have n
1:30 and we want to multiply it by the factorial of the smaller piece of the stuff to the right, and we kind of keep building that up and it turns out that
1:39 we can say we want those factorial to be n times and we can call the same method within itself with different parameters,
1:47 so for this step from here is just going to be whatever that is times 5, so n*factorial (n-1) now if we do it like this
1:55 we are going to end up in a stack overflow situation, it's going to keep calling this function over and over and over until it just runs out of space.
2:05 So once we get down to one we are going to stop and we just say 1! is defined to be 1, so we'll say something like if n is 1 return 1
2:16 otherwise we are going to come down here and do this we'll just say return n*factorial (n-1) so let's run this, now this one this should be 120,
2:31 this one I know should be 6, this one should be huge, I don't know what factorial of 11 is but I know it is much larger than you expect,
2:41 so let's run this and see how it works. Awesome, 5! is 120, 3! is 6 and 11! is some big number, now we don't like to look at numbers like this, right,
2:50 we like separators, so we can actually do that by putting colon comma on all of these, will make a difference on the small ones but on the larger one
3:00 you can see we now have separated those with digit grouping. So this is a recursive algorithm, let's take a moment
3:07 and look at this as it's one of our core concepts.