Factorial Trailing Zeroes, factorialtrailing
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/42417535
Given an integer n, return the number of trailing zeroes in n !.
Note: Your solution shocould be in logarithmic time complexity.
Ideas:
(1) the question is how many zeros are obtained by factorial calculation for an integer.
(2) We know that the number of 0 is related to 2 and 5, and the number of 2 is much more than the number of 5, so the number of 5 is the number of 0.
(3) but for a combination of 25 and 125, Which is multiplied by a number of 5, the number of Integers to be solved must be calculated.
(4) For example, 1000 = 1000/5/25 = 40, 20,100/125 = 625/1250 =/= 0, that is, the number of zeros is 20 + 40 + 8 + 1 = 69.
(5) I hope this article will help you.
The algorithm code is implemented as follows:
Public static int trailingZeroes (int n) {int count = 0; while (n> 0) {count = count + n/5; n = n/5;} return count ;}