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;
	}
}

@@ -93,416 +93,242 @@
 %7B%0A%09%09
-int countFor2 = 0;%0A%09%09int countFor5 = 0;%0A%09%09for (int i = 1; i %3C= n; i++) %7B%0A%09%09%09countFor2 += countFor2(i);%0A%09%09%09countFor5 += countFor5(i);%0A%09%09%7D%0A%09%09return Math.min(countFor2, countFor5);%0A%09%7D%0A%0A%09private int countFor2(int n) %7B%0A%09%09int ret = 0;%0A%09%09while (n %3E 0 && n %25 2 == 0) %7B%0A%09%09%09ret++;%0A%09%09%09n = n / 2;%0A%09%09%7D%0A%09%09return ret;%0A%09%7D%0A%0A%09private int countFor5(int n) %7B%0A%09%09int ret = 0;%0A%09%09while (n %3E 0 && n %25 5 == 0
+// n! = (1) * (2) * ... (n)%0A%09%09// only concern the number of '5' -%3E (5*1) * (5*2) *(5*3) * ...*(5*n/5)%0A%09%09// = (5%5E(n/5))* (1*2*3*...*n/5) = 5 * (n/5)!%0A%09%09//%0A%0A%09%09int count = n / 5;%0A%09%09n = n / 5;%0A%09%09if (n %3E= 5
 ) %7B%0A%09%09%09
-ret++;%0A%09%09%09n = n / 5
+count += trailingZeroes(n)
 ;%0A%09%09
@@ -342,14 +342,17 @@
 urn 
-re
+coun
 t;%0A%09%7D%0A
+%0A
 %7D%0A

update factorial trailing zeroes