Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Factorial Trailing Zeroes

S

nice explanation! a bit optimizing as I saw lots of answers did not figure this out:

T

better than rank 1

F

What I don't understand about any of these solutions is why aren't we counting 10, 20, 30, 40, etc, which all add a trailing zero?

10! has 2 trailing zeroes, one for the 2*5 and one for the 10. Why are we only concerned with 2s and 5s?

B

Yes, you are correct. I misread your code initially.

Z

Usually recursion is slower. Could you please explain your findings?

Hi, prime_tang. Your code is more concise :-)

very nice and simple solution!

Since 10 = 2*5, and there are always more 2's than 5's, we only need to count the number of 5 factor in n!

L

it will only count 5 whose number is equal to trailing zeros.

it should be floor(n/5) + floor(n/25) + floor(n/125) + ....

because the int overflow,in the last time i=i*5,the result should be larger than the up-limit int type and then becomes a small number,so your amount of the zeros will be more than the ans

No one has replied

here is a more detailed explanation http://www.purplemath.com/modules/factzero.htm

C

Oh,sokka.Thanks for your reply.

Y

The speed is average, u can run it urself to see

4

the same but iterative:

Disabled Categories are greyed out

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.