|
| 1 | +Number of trailing zeros in the factorial of an integer |
| 2 | +======================================================= |
| 3 | + |
| 4 | +:date: 2020-01-02 19:50 |
| 5 | +:category: tips |
| 6 | +:slug: trailing-zeros-factorial |
| 7 | +:summary: Use Python to find the number of trailing zeros in the factorial of an integer |
| 8 | + |
| 9 | + |
| 10 | +Hi all, I recently learned about a cool formula to calculate the number of |
| 11 | +trailing zeros in the factorial of a number. It has been a while since I |
| 12 | +wrote a program to do something like this. So, I decided to change that and |
| 13 | +write this blog post. In the spirit of wring various "calculators", we will |
| 14 | +write a "number of trailing zero" calculator. First up though, let's refresh |
| 15 | +some key relevant concepts. |
| 16 | + |
| 17 | +**Factorial**: The factorial of a number, ``n`` denoted by ``n!`` is the product ``n*(n-1)*(n-2)...*1``. |
| 18 | +For example, ``5! = 5*4*3*2*1 = 120``. |
| 19 | + |
| 20 | +**Trailing zeros**: The trailing zeros of a number is the number of zeros at the end of a number. For example, |
| 21 | +the number 567100 has **two** trailing zeros. |
| 22 | + |
| 23 | +**Floor**: The floor of a number is the greatest integer less than or equal to x. That is floor of 3.2 is 3 |
| 24 | +and that of 3.5 is 3 and the floor of 3 is 3 as well. |
| 25 | + |
| 26 | + |
| 27 | +Now, coming back to the focus of this post, this document at brilliant.org wiki |
| 28 | +explains the process in `detail <https://brilliant.org/wiki/trailing-number-of-zeros/>`__. |
| 29 | + |
| 30 | +The key bit there in is this formula: |
| 31 | + |
| 32 | +.. math:: |
| 33 | +
|
| 34 | + \sum_{i=0}^{k}\floor*{\frac{n}{5^i}} |
| 35 | +
|
| 36 | +where, ``n`` is the number for whose factorial we want to find the number of trailing zeros in and ``k`` |
| 37 | + |
| 38 | + |
0 commit comments