Factorial Trailing Zeroes
Problem:
Given an integer n, return the number of trailing zeroes in N!.
Note:your solution should is in logarithmic time complexity.
Ideas:
You know the idea, you don't really understand.
Others code:
Public class Solution { publicint trailingzeroes (int n) { if return 0; int k = 0; while (n > 0) { + =N/5 ; /= 5; } return k; }}
View Code
Why??
Others explain: After reading only then dawned, Taichetaiwu ....
Then, since can not traverse, can only change a thought, can constitute the end of 0, in fact, the determining factor is the number of 1 to n a total of how many 5 factors. So we consider this: for 5 then divisible by him is 5 10 15 25 30 ... In fact, there is a total of N/5, for 25 50 such a number includes two 5 factors, which we will calculate later, when considering 5, the result is N/5. (This time has been considered 25 125 but only considered once) for 25 can be divisible is 25 50 75 ... So, in fact, there are a total of N/25, this time 25 of the two 5 factors, the change is counted. (On the basis of the above, in consideration of a 25 this completes 252 times the count N/5/5 = = N/25 Ah can be counted 25 how many times) for 125 can also be divisible by it is 125 625 ... (as the above method is similar to the deduction can be)in this way, the result is actually:N/5 + N/25 + n/125 ...
Factorial Trailing Zeroes