base
becoming multiplied by 5 got lost in the second version.
public static int trailingZeroesV2(int n) {
return trailingZeroesV2(n, 5);
}
private static int trailingZeroesRec(int n, int base) {
return n == 0 ? 0 : n / base + trailingZeroesRec(n / 5, base * 5);
}
(Typically a recursive function uses an extra parameter,)
The correctness of both versions I leave to your ingenuity.
It obviously uses that the number of factors of 5 in the factorial has at least the same number of factor 2. So the factors of 10 can be determined as such.
I would however consider % 5
, modulo 5. But I will not seriously delve into your algorithm.
4
solved return different result to find Factorial Trailing Zero