 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over another problem called it factorial, which is on spож Basically, we are given a number n and we want to find the number of trailing zeros of n factorial So what does that mean? Basically, we are given a number n and we want to find the number of trailing zeros of n factorial Okay, so like let's say we have five is our n I'm going to find n factorial Which is going to be five factorial, which is going to be five times four times three times two times one and in this case It would be twenty times six, which is going to give me 120 and the number of trailing zeros is the number of zeros after number right so here this would have a number trailing zeros would be one so Z of n factorial Will be equal to one and the reason why Z of n factorial is equal to one is because this has a last trailing zero of zero But there's only one zero one trailing zero when we sing trailing trailing zeros This is not a trailing zero, right? I could add a bunch of zeros in front of it. That's not a trailing zero, right? It's only a number of zeros after a certain number, right because this technically It's the trailing zero is still going to be one because there's only one zero after a certain number, okay? So how do you do this problem? Okay, let's suppose we're trying to get twenty three factorial, right? And we want to find the number trailing so one way you could just compute the factor and find how many zeros there are That takes too long though. So to find the number trailing zeros, right? So let's say twenty three times twenty two To one right this is twenty three factorial How do I know if a number has a zero at the end of it or not? That's a big basic question So if I have like ten, how do I know this number has a Zero after it or like let's say I have a hundred. How do I know this has two trailing zeros, right? You guys first have to answer these two basic questions And the reason for that is that it's divisible by ten if I were to divide by ten. It's divisible by it Right. This is divisible by ten So that's why the number of trailing zeros is one and these two are divisible by ten like 100 is divisible by ten So if you divide by 100 by ten you get two, right? So that's why there's two trailing zeros So if you were to think about this in twenty three factorial We just need to find how many number of tens there are in this factorial of twenty three Well, that means that how many how many numbers in this twenty three are a factor of ten So this is a little tricky. So if you if you want to find how many numbers in twenty three are a factor of ten One way you could do is you could just keep let's just keep keep counting and then soon I get down to twenty right twenty This is we know this is a factor of ten and I keep going now I'm gonna get to ten and then so on down to one right so these two these two are factors of ten right now, how do I find Factors of ten, right? So what is a factor of ten? Well a factor of ten is Actually two numbers, right? It's five times two Right a factor of ten if I want to find something is divisible by ten I know if it's divisible by five and it's a visible by two then I know that it's a factor of ten Right, so if I'm going to find the factor of ten And I know if this is by five divisible by two Let's just see how many factors of two. Let's write down factors of two So let's go back and write down factors of two like so. Let's say I have two four six eight ten twelve fourteen sixteen Eighteen twenty right. Let's say these are factors of two Right now. Let's look at factors of five five ten fifteen as it turns out that Finding factors of two is doesn't really help you that much because there's more factors of two in this case right every two Numbers in a factor of two That doesn't help you to see if it's a factor of ten or not, right? Because like if you look at this like four that doesn't necessarily mean it's a factor of ten six doesn't necessarily mean eight doesn't necessarily mean ten does right ten does 12 does not necessarily mean anything 14 doesn't As it turns out five actually helps you a lot because if you know about five five Five every second place of five tells you the factor of ten. It's a factor of ten or not right five ten to twenty 25 30 so five actually knowing about the factors of five Actually, it helps you a lot to determine the factors of ten Right. It's much easier to find factors of five than is for ten because two you have to search through over and over again I just have to find the number of factors of five between one to twenty three and let's list them out There's five ten fifteen and twenty Right, so now let's think about the values of five and one twenty three, so if we were doing a a To actually list out twenty three factor we have like twenty three times twenty two times twenty one yada yada yada Down to one. We know that I take any even number Let's say this even number twenty two and if I were to multiply it by a factor of five this one Let's see twenty two times five. I'm actually going to get a trailing zero Right trailing zero right here and That's because any even number multiplied by a factor of five is going to give me a It's going to give me a number with trailing zeros So technically all we just have to do is just count the number of multiples of fat Factors of five between one to twenty three and I'll just be our answer because these This five is going to get paired with like an even number for twenty three factor oil and that will give us a number that's Divisible by ten right and also this fifteen is going to get paired with another even number and that's going to give us a number That's visible by ten also right so Yeah, like like if we were to list out two four six eight ten 12 yada yada we're gonna have an even number that's going to get paired up with this five Right if you were to multiply them out like if I multiply the factors of twenty three out I'm going to have like an even number that's going to end up getting paired with a five ten or fifty right So these two my numbers actually do matter These two numbers do count right five and fifteen they do count because they're going to get paired with an even number if You multiply do like a factorization of twos on the factorial twenty three factorial right so that's why To the number would just be the numbers of factors of five Between one to n so that would all basically be the answer Okay, so there's actually some test cases where you might not actually get what you want So let's go over that let's say I have one hundred and one and I want to find the number trailing zeros of this Well one oh one if I want to find them multiples of five between one to one oh one It's actually way easier to just take the highest number that's the physical by five which is one hundred and I divide by five and I'll actually give us the Number of fives between one oh one oh one oh one right so these are multiples of five and that's a going to give us 20 right But there's also another situation scenario 101 25 is a number between one and one oh one So we actually have to count account for that also right the factors of five that Also factors of five so to do that to find the factors of five now between one to 25 Would actually be this 20 divided by five right and That's gonna give us four right because 25 is a factor that is Gets counted twice because we have to double count that as well So we have to divide by five again so that gives four so if we add all these up We could get 20 plus four to equal 24 Right, so if you want to find the number of factors of five Between one and one one one oh one and you want to find the number trailing zeros You have to keep dividing by five and then add up that to a counter Right, and that'll be the number of your answer because sometimes you have to double count Some some other factors of five. So yeah, so I'm gonna show you the code now So in this case, I created a red in the test cases T Right, and I read an N and I have a number of numbers zeros while n is greater than zero I'm going to take num zeros plus equal to n and I'm going to Divide by five each time so then after the end I'm gonna see out num this you have to divide five first and then add it Yeah, so yeah, let's actually try testing this first and then we'll see And I got a seed so yeah, that's basically how you do this problem ray com subscribe. I'll check you guys later. Peace