Factorial Zeros
16.5 Factorial Zeros: Write an algorithm which computes the number of trailing zeros in n factorial.
I was thinking of using dynamic programming with factorial recursion. The issue is storing 32 bit numbers, which factorial can exceed very quickly.
The better approach would be split up the factorial into its components, and find its prime factors. Any pair of 10, (5 and 2, as building blocks) should move us on to the next base. Therefore the solution would be min(num(5's), num(2's)). Since there will always be more 2's than 5's we can just count the 5's.
Last updated