imashutosh51

Factorial Trailing Zeroes

Oct 6th, 2022 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. '''
  2. for eg 10! = 1×2×3×4×5×6×7×8×9×10
  3. 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.
  4. so now our problem reduced to :
  5. n!= ..5..10...15..20...25(which is 5*5)...30...35
  6. so we are finding how many 5 by dividing it by 5,then we will find how many 25 then 125 and so on
  7. '''
  8.  
  9. class Solution:
  10.     def trailingZeroes(self, n: int) -> int:
  11.         ans=0
  12.         k=5
  13.         while k<=n: #k will be 5,25,125...
  14.             ans+=(n//k)
  15.             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.
  16.         return ans
  17.        
Advertisement
Add Comment
Please, Sign In to add comment