# Trailing Number of Zeros

Have you ever wondered how many zeroes are present in the end of a factorial of a huge number? Then you have come to the right place! Let us learn how to calculate the so-called "trailing zeroes" of big numbers, and understand how it works. Here's a teaser:

\[15!=15\times14\times13\times\cdots\times1 = 1307674368\color{green}{000}.\]

The above number has three 0's at the end of its decimal representation.

We say that \(15!\) has 3 trailing zeros.

How do we calculate the number of trailing zeros of other large numbers like \(100!\)?

If you don't know about the floor function, see floor function. You'll need to know what it is for this.

#### Contents

## Finding Trailing Zeroes

Problems often ask you to find the number of zeroes at the end of a number, usually a factorial. There is a method to find trailing zeroes of any factorial \(n!\) using the floor function.

Knowing that zeroes at the end of a number mean having factor pairs of 2 and 5, we can count the number of times these pairs appear to find the number of zeroes. We count the 5's because they signify the number of pairs, whereas counting 2's would be counting many 2's not in 2-5 pairs. In short, we count the number of times 5 is a factor in \(n!\)

## Formula

There's a fancy way to express the above strategy:

\[f(n) = \displaystyle \sum_{i=1}^k \left\lfloor{\frac{n}{5^i}}\right\rfloor,\]

which ends up looking like

\[\left\lfloor{\frac{n}{5}}\right\rfloor+\left\lfloor{\frac{n}{5^2}}\right\rfloor+\left\lfloor{\frac{n}{5^3}}\right\rfloor+\dots+\left\lfloor{\frac{n}{5^k}}\right\rfloor.\]

The first term counts the number of times 5 appears, the second counts for 25, and so on until \(5^k\) is too large for \(n\) and anything after that \(\left\lfloor{\frac{n}{5^k}}\right\rfloor\) term will be 0. Note that \(\lfloor k \rfloor\) denotes the smallest integer less than or equal to \(k\).

Another simple formula is that

\[ { v }_{ p }(n) = \frac { n - { S }_{ p }(n) }{ p - 1 }. \]

Here, \({ S }_{ p }(n)\) is the sum of digits of \(n\) in base \(p\) and \({ v }_{ p }(n)\) is the highest power of prime \(p\) in \(n\).

## Determine the number of trailing zeros of \(777!\).

We plug in 777 to the formula and get

\[\begin{eqnarray} \left\lfloor{\frac{777}{5}}\right\rfloor+\left\lfloor{\frac{777}{25}}\right\rfloor+\left\lfloor{\frac{777}{125}}\right\rfloor+\left\lfloor{\frac{777}{625}}\right\rfloor &= & 155+31+6+1 \\ &=&193. \ _\square \end{eqnarray} \]

Note that the sequence stopped after \(\left\lfloor{\frac{777}{625}}\right\rfloor\) because everything after that would be 0. You don't have to memorize fancy formulas to do this kind of problem consistently, but just know how and why it works.

## Prove that the number of trailing zeroes of \(6!,7!,8!,9!\) are all the same.

Let \(n\) denote the number of trailing zeroes of \(6!\).

Factorizing \(6!\) gives \(1\times2\times3\times4\times5\times6=2^4\times3^2\times5\).

Similarly, factorizing \(7!,8!,9!\) gives\[\begin{eqnarray}7!&=& 7\times6! = 2^4\times3^2\times5\times 7\\ 8!&= &8\times 7! = 2^4\times3^2\times5 \times 7 \times 2^3 \\ 9!&= &9\times 8! = 2^4\times3^2\times5 \times 7 \times 2^3 \times 3^2. \end{eqnarray} \]

Because the highest power of 5 that divides \(6!,7!,8!,9!\) is 1, they all have the same number of trailing zeroes. \(_\square \)

## Determine the number of trailing zeros of \(10005!\).

We plug in 10005 to the formula and get

\[\begin{eqnarray} \left\lfloor { \frac { 10005 }{ 5 } } \right\rfloor +\left\lfloor { \frac { 10005 }{ 25 } } \right\rfloor +\left\lfloor { \frac { 10005 }{ 125 } } \right\rfloor +\left\lfloor { \frac { 10005 }{ 625 } } \right\rfloor +\left\lfloor { \frac { 10005 }{ 3125 } } \right\rfloor & = & 2001+400+80+16+3 \\ & = & 2500. \end{eqnarray} \]

Note that the sequence would stop after \(\left\lfloor{\frac{10005}{3125}}\right\rfloor\) because everything after that would be 0. So the answer \(2500\). \(_\square\)

## Determine the number of trailing zeros of \(180!!\).

We have

\[\begin{eqnarray}180!! & = & 180 \times 178 \times \cdots \times 4 \times 2 \\ & = & 2^{90} \times (90 \times 89 \times \cdots \times 2 \times 1) \\ & = & 2^{90} \times 90!. \end{eqnarray}\]

Plugging \(90\) into the formula gives

\[\begin{eqnarray} \left\lfloor \frac {90}{5} \right\rfloor + \left\lfloor \frac {90}{25} \right\rfloor & = & 18 + 3 \\ & = & 21. \ _\square \end{eqnarray}\]

For how many positive integral values of \(n\) does \(n!\) end with precisely 23 trailing zeros?

**Cite as:**Trailing Number of Zeros.

*Brilliant.org*. Retrieved from https://brilliant.org/wiki/trailing-number-of-zeros/