package factorialtrailingzeroes;

public class Solution {
	public int trailingZeroes(int n) {
		int countFor2 = 0;
		int countFor5 = 0;
		for (int i = 1; i <= n; i++) {
			countFor2 += countFor2(i);
			countFor5 += countFor5(i);
		}
		return Math.min(countFor2, countFor5);
	}

	private int countFor2(int n) {
		int ret = 0;
		while (n > 0 && n % 2 == 0) {
			ret++;
			n = n / 2;
		}
		return ret;
	}

	private int countFor5(int n) {
		int ret = 0;
		while (n > 0 && n % 5 == 0) {
			ret++;
			n = n / 5;
		}
		return ret;
	}
}

package factorialtrailingzeroes;

public class Solution {
	public int trailingZeroes(int n) {
		// n! = (1) * (2) * ... (n)
		// only concern the number of '5' -> (5*1) * (5*2) *(5*3) * ...*(5*n/5)
		// = (5^(n/5))* (1*2*3*...*n/5) = 5 * (n/5)!
		//

		int count = n / 5;
		n = n / 5;
		if (n >= 5) {
			count += trailingZeroes(n);
		}
		return count;
	}

}

update factorial trailing zeroes