 What's up guys, Mike the coder here today. We are going to go over trailing zeros. So essentially is that given a number N and our task is to calculate the number of trailing zeros in the factor of N factorial. So in this case we have 20 factorial and they have four trailing zeros. And when they mean by trailing zeros, it's the number of zeros at the end. So there's four zeros for the zeros at the end. That's why there's four trailing zeros. So our task is just to find calculate the number of trailing zeros. Okay, so how do you do this problem? Essentially is, let's say N is equal to 20, given that we need to find the number of trailing zeros at the factorial, factorial event factorial. Okay, so yeah. Okay, so what we could do first is we could list out the factorial of 20 factorial. So N is equal to 20 and then 20 factorial. What is that? It's equal to 20 times 19, times 18, times 17, times 16, times 15, times 14, times 13, times 12, times 11, 10, nine, eight, seven, six, five, four, three, two, one. Okay. Okay, now to find trailing zeros, essentially is that if we look at this, right? Trailing zeros, we have like, what does it mean by having trailing zeros? Well, it means that essentially we have this last number of 20 factorial, all these values here. They have four zeros at the end, four zeros at the end. So that means there's four zeros at the end. That means it could, it's divisible by 10, right? You know, like it's divisible by 10. So if you were to divide this last number by 10, right? And you keep dividing it, you're gonna get four, right? Because there's four zeros at the end. Therefore that, because there's four zeros at the end, if you divide by 10 repeatedly, right? We're gonna keep getting, we'll get four in the end, right? So because of this, essentially what they're doing is they're dividing by 10 over and over and over again. Okay, so if you have to zero, zero, zero, zero, zero, there's 10, right, it's gonna divide it. So essentially is that it's like 10 to the fourth, right? 10 to the fourth, right? Maybe I should list out this number. I'll list out this number. Just to help you guys see what I'm talking about. So here, can I paste this? Yeah, right here, I'll paste it here. Okay, so this number. So this number, this number, right? There's four trailing zeros, four trailing zeros. There's four trailing zeros at the end. So these four zeros are at the end, right? So essentially what we could do is how we know, how we count these number trailing zeros, right? Is you could keep dividing it by 10, right? So we could divide 10, 10, 10, 10, 10. So essentially this number, this first number, this whole number, which I'm gonna just copy this whole number down, this whole number. I'll just copy this because I'm too lazy to write it again. So copy this. This number is multiplied. So this whole number that we have, we're given here after calculating the factorial, right? Is equal to this number, multiply 10 to the fourth, right? Because there's one, two, three, four, four of them here. So essentially what we're trying to find is like the number of 10s we could divide after, number of 10s we could divide after after we calculate the factorial, right? So this factorial, remember 20 factorial is equal to this number that they gave us, right? This factorial, 20 factorial is equal to this giant number that they gave us, right? So we just need to find how many times we could divide by 10 in order to get this number, okay? To get four. So to do that, what you could do essentially is that 10 is basically just two times five, right? 10 is two times five, right? So if you wanna look at this, 10 is two times five. We could do two times five and there is the fourth power, right? Because this is 10 to the fourth, right? So we have this number multiplied 10 to the fourth, which is equal to the same number, the same number, let me paste this again. The same number multiplied by two times five to the fourth, right? Because literally I'm just rewriting this is equal to this. This is equal to this, okay? I'm just rewriting that. And then this is equal to two to the fourth times five to the fourth, right? Okay, so essentially is that as long as you count the number of times we're divided by five, we get our answer, okay? So instead of evaluating 20 factorial and then doing the number of times doing all this stuff, right? What we could do is we could loop through from 20 and then going down to one and then we just add up how many times we're dividing by five, okay? Keep dividing by five, add up how many times that happens and then that should give us the answer. So what I'm gonna do right now is I'm going to essentially just split all the numbers in 20 factorial. So I wrote down all the 20 factorial and I'm just gonna try to split them into powers of five and two. And then maybe you'll guys see the pattern for that. So 20 is five times four, right? 20 is five times four, which is five times two square, right? 19 is just 19. 18 is nine times two, right? That's what 18 is. 17, we leave 17. 16 is two to the fourth power, right? 16 is two to the fourth power. 15 is five times three, right? 15 is five times three. 14 is two times seven, 14 is two times seven. 13 is, what is 13? 13 is just 13. 12 is two times, 12 is four times three, which is three times two squared, right? 14 times three is 12, right? Which is a three times two squared. But what I'm doing is I'm just rewriting all the powers. Rewriting all the numbers in powers of two and five, okay? Yeah, and then, yeah. So we're at 12, then we have 11, which is times 11, times 11. 10 is two times five, which is just, I'll just five times two, there. 10 is five times two. Nine is three times three. Actually, I'll just leave nine as it is because you can't really do anything there. Eight is two to the third, right? Two four, two times four, times two is eight, yep. Times is just seven. Six is three times two. Five is just five. Four is two squared. Three is just three, and then two is two, and then we have one, okay? So then, now let's just count how many twos, powers of twos, and powers of fives we could have, right? How many powers of twos? Powers of fives. So if I just go here and just count how many powers of twos and powers of fives, I should be able to get my answer. Okay, so we have one power of five, one, two, so there's two fives, three, four. There's four fives, there's four fives, so there's four fives, so five to the fourth. How many powers of twos do I have? Well, we have two to the two, right? So two to the two. So we have two to the two, and we have two to just two also. So two to the two times two is two to three times two. Okay, I'll just write them all out down because this is really hard to count them in my head. So I have this one, right? Two to the two, and I have this one, this two times two. I have this one to the fourth. I have this one, this two, I have this one. Two to the second. I have this one, right? I have this two also, this two. I have this one, this two, two to the third. I have this two, which is one, two. I have this two to the second, and I have this two. Okay, I think I've listed out most of the powers of twos and the power of fives. The rest we don't have to care about because who cares about the rest of the numbers, right? We're mostly focusing on powers of fives and twos because that's how you make 10, right? Okay, so then, now that we have that. Yeah, now that we have that, let's just add up all the number of powers of twos to make this more simplified. So this is equal to five to the fourth. Times two plus one is, let's just add up all the powers. So two plus one is three. Three plus four is seven. Seven plus one is eight. Eight plus two is 10. Plus one is 11. 11 plus three is 14. 14 plus one is 15. 15 plus two, 17, 17 plus one is 18. So it is two to the 18th. Okay, so as you could see here, in the end here, after evaluating doing all the primes, listing the powers of twos and fives, we got five to the fourth times two to the 18th, okay? So if we go back to the original number, oh, whoops, not this one. Go back to the original number of this. It only had four trailing zeroes, right? It only had four trailing zeroes. So essentially is that no matter how many twos you have, the rest of these twos don't matter, right? Because in order to make 10, in order to make 10 is that we need fives, right? You have to pair a five with a two, right? So essentially is it's just like the maximum number of fives you could have because the twos don't matter because without the fives, without the, yeah, without the fives you can't really do anything. You can't pair them up, right? So essentially all we have to do is find the number of fives that is divisible by this power and then we'll just get, we'll just get like, essentially we just get the answer, right? Five to the fourth, okay? So just keep dividing by five and then while you keep dividing by five, just add them up and then you'll get your answer. That'll be the answer, essentially the number of zeros you have. So yeah, I can show you what I wrote for the code for mine. It's a little difficult to understand. Actually it's not that difficult. Okay, so essentially is we need to find the number of powers of fives, right? Powers of fives. So what I did was I created a counter here, count as equal to zero and I start my count from equal to five, right? And every time through this loop, I'm gonna multiply by five, right? Because then I'll get like five to the first power, five to the second, five to the third, five to the fourth, five to the fifth, right? So on and so forth, right? And then what I'm doing is I'm just taking my number and dividing it by this number of, my counter of I, of I, right? So essentially it's I'm taking my current number and I just keep dividing it by powers of fives. That's what I'm doing here, okay? And then I'm just adding it up with my counter. So as long as we keep adding up, keep dividing it by powers of fives and adding it up, that'll get our answer. And I do this while n divided by I is greater than equal to one. So while it's not zero, essentially, like while it's greater than equal to one, yeah. So while my number is like repeatedly greater than or equal to one, I'm just gonna keep dividing it by powers of fives and then keep adding it up to my counter and I just print it out. So yeah, that's pretty much just this problem. I hope you guys understand what's going on. Here we have main, which is reading in the number n and then it's called solve of n and then it just keeps doing it like powers of fives, adding those things up while I divide by my number of n of powers of fives, right here. Multiply by five. Yeah, this is a way to do it. There's other ways to do it. You could probably figure, there are other ways to do it. But yeah, essentially it's powers of fives and figuring that out, yeah. But I hope you guys enjoyed this video. Don't forget to like, comment, and subscribe. I'll check you guys later. Peace.