Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- for eg 10! = 1×2×3×4×5×6×7×8×9×10
- In factorial of n, only 2*5 is needed for a trailing zeros and 2 will be always more than 5 because 2 you can get in 4,6,8 also but 5 you can only get in 5,10,25... so we have to find how many 5 we can get.
- so now our problem reduced to :
- n!= ..5..10...15..20...25(which is 5*5)...30...35
- so we are finding how many 5 by dividing it by 5,then we will find how many 25 then 125 and so on
- '''
- class Solution:
- def trailingZeroes(self, n: int) -> int:
- ans=0
- k=5
- while k<=n: #k will be 5,25,125...
- ans+=(n//k)
- k=k*5 #first we will get the count of 5's(which also includes 10,15,20 and one 5 of 25) then we will find the count of other 5 of 25 and so goes on.
- return ans
Advertisement
Add Comment
Please, Sign In to add comment