Transcript from the "Factorial with Loop" Lesson

[00:00:00]

>> Bianca Gandolfo: Now we're gonna talk about factorial. I have to talk about factorial because this is when everyone has to talk about factorial. Blah, blah, blah, recursion. You ready? Our math person will be happy.

>> Speaker 2: [LAUGH]

>> Bianca Gandolfo: All right, so what is a factorial? You guys remember what that one is?

[00:00:18] It's the one with the [SOUND]. It's got the bang on it.

>> Bianca Gandolfo: So,

>> Bianca Gandolfo: If we, I don't know, did 6 factorial.

>> Bianca Gandolfo: What is the equivalent of that, does anyone remember?

>> Speaker 3: That's 6.

>> [INAUDIBLE]

>> Speaker 3: 5 times 4 times 3.

>> Bianca Gandolfo: Mm-hm.

>> Bianca Gandolfo: Okay, not times 0 cuz that wouldn't make sense.

[00:00:53]

>> Speaker 2: [LAUGH] zero.

>> Bianca Gandolfo: Yeah, exactly. So this is a factorial. Multiply, multiply, multiply, this is just a very classic example of recursion. It hurts me to use this example but I feel like it's a necessary evil. Yeah, okay, I'm sorry, I wanted it to be about food and cute cats and stuff, but maybe tomorrow.

[00:01:22] Okay, so we have a pattern here, where's my thing? So the pattern that's recursion, or that's factorial, is this. Is it's the results of, the results times equal basically n minus 1. We could see here if we're looping through this, we would say result times equals starting at 2.

[00:01:56] Again, remember why we can't start at 0. Starting at 1's not helpful either, right, because 1 times anything is the same. So we start at 2, we count up, and then we return the result. Clear on how this works? But we have a pattern. Here's our pattern. Maybe it's not super obvious at first, but there we are.

[00:02:21] And so, what we hope to do is use this pattern to inform our recursion, cuz we can do recursion with loops, and so here we go. So, we have a base case somewhere hidden in this loop. All right, the base case is the point in which we are gonna stop.

[00:02:47]

>> Bianca Gandolfo: We're probably gonna pass a recursive function a number like 5.

>> Bianca Gandolfo: And then we wanna get to a certain base case that's a stopping point. And we also want to actually do the computation of the factorial. Yeah, so what is our base case?

>> Speaker 3: Zero.

>> Speaker 2: When i is less than or equal to the number that you passed in?

[00:03:19]

>> Bianca Gandolfo: I guess it depends on where you start.

>> Bianca Gandolfo: Right, because the number that you, so if we're imagining this loop and you have i. So you're starting at, so you're saying when it's less, so 4, 3, 2, 1, it would stop on all of those?

>> Speaker 2: Yeah, yeah, yeah.

[00:03:38]

>> Bianca Gandolfo: On all of them or just one?

>> Speaker 2: No, no, no.

>> Speaker 2: Wait, sorry.

>> Bianca Gandolfo: So the base case is a stopping case. You're talking about the work step. We want to do work on those steps.

>> Speaker 2: Right.

>> Speaker 3: Suggestion from the chat. i equals num.

>> Bianca Gandolfo: When i equals num, yeah, if we're counting up.

[00:03:58] Or, if we're counting down, when i equals what?

>> Speaker 3: One.

>> Bianca Gandolfo: One, yeah or two. Exactly, great. So it depends on which order we're going in, either one is fine. In this case, this example is counting up. So once we reach num, awesome. So we had our base case.

[00:04:16] We can imagine in our head now, in a recursive function, we have some like if, n equals, equals, equals 5 since we're counting up in this example, break.