Here is a recursive divide-and-conquer. There are probably nicer ways to code it.
trailingZeros[n_, b_] := Module[
{scale=Log[b,N[n]], sqrt, ndigits},
If [scale<1, Return[0]];
sqrt = Ceiling[scale/2];
ndigits = IntegerDigits[n, b^sqrt, 2];
If [Last[ndigits]==0,
sqrt + trailingZeros[First[ndigits],b],
trailingZeros[Last[ndigits], b]]
]
In[39]:= Timing[trailingZeros[4234567!, 33]]
Out[39]= {6.740000, 423454}
In terms of speed, it is essentially identical to J.M.'s approach. It just shows how one might do this were there no IntegerExponent
function available.
--- edit 2017-06-25 ---
Since this showed up again I decided to time the various responses, all special-cased to digits base 10. The second and fifth are also specialized for factorials and are, as noted in respective responses and comments, hugely faster than the rest. The input for them is the non-factorialed value while for the rest it is the factorial.
zero1[n_] :=
StringCases[
ToString[n], {Longest[x : "0" ..] ~~ EndOfString} :> StringLength@x]
zero2[n_] := Tr@Floor@NestWhileList[#/5` &, #/5`, # > 1 &] &@n
zero3[n_] := LengthWhile[Reverse@IntegerDigits[n], # == 0 &]
zero4[n_] :=
Module[{scale = Log[10, N[n]], sqrt, ndigits},
If[scale < 1, Return[0]];
sqrt = Ceiling[scale/2];
ndigits = IntegerDigits[n, 10^sqrt, 2];
If[Last[ndigits] == 0, sqrt + zero4[First[ndigits]],
zero4[Last[ndigits]]]]
zero5[n_Integer] := (n - Total[IntegerDigits[n, 5]])/4
zero6[n_] := IntegerExponent[n, 10]
Here is a big test. It shows that the "slow" methods are all comparable though there are modest factors separating them: best to worst is around a factor of 4 with this test.
nn = 12345678;
mm = nn!;
AbsoluteTiming[zero1[mm]]
AbsoluteTiming[zero2[nn]]
AbsoluteTiming[zero3[mm]]
AbsoluteTiming[zero4[mm]]
AbsoluteTiming[zero5[nn]]
AbsoluteTiming[zero6[mm]]
(* Out[114]= {78.1305, {3086416}}
Out[115]= {0.0000988615, 3086416}
Out[116]= {95.3831, 3086416}
Out[117]= {22.2577, 3086416}
Out[118]= {0.0000522287, 3086416}
Out[119]= {45.3224, 3086416} *)
I was a bit surprised that IntegerExponent
was not fastest and maybe more surprised that it is almost exactly a factor of 2 off in terms of speed. Something to look into on a rainy day.
--- end edit ---