Find the trailing number of zeros of
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The following C code solves the problem:
Let x = i = 1 ∏ n i ! , note that we can rewrite it as x = i = 1 ∏ n i n − i + 1 . We need to find the highest k such that 1 0 k ∣ x . But 1 0 = 2 × 5 , and since 2 will appear more times than 5 in the prime factorization of x , we only need to find the highest k such that 5 k ∣ x .
So, we iterate from i = 1 to i = n , count the number of times that 5 can divide i and multiply that by n − i + 1 . Finally, we sum all of these results and that will be the answer.