Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- n!=1*2*3*4*5*6..*10*....15*....*20*...*25*....n
- Trailing zeros will be only generated by 2*5,no other factors exist for 10.
- so minimum of count of 2,count of 5 in factorial of n will be the answer.
- but there is one cache that,2 factors we can get after every alternate number
- eg 2,4,6,8,10 but 5 factor we get after every 5 numbers so obviously n! will
- have more no. of 2 in their factorial so just count the 5 in n!.
- after every 5 there will be one 5 do divide n by 5 but 25 have two 5 so
- divide n by 25 also because numbers which are divisible by 25 will give one five
- when divided by 5 and one five when divided by 25 and same goes on.
- */
- class Solution {
- public:
- int trailingZeroes(int n) {
- int ans=0;
- int k=5;
- while(k<=n){
- ans+=(n/k);
- k*=5;
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement