# Rightmost Non-zero Digit of $n!$

Finding the number of trailing zeroes in $n!$ is an elementary and standard approach which gives us the number of zeroes at the end of $n!$. But finding the **rightmost non-zero digit** is one such commonly asked problem that many fewer people know about.

## Rightmost Non-zero Digit of $n!$

Let

$\mathfrak{D} (n!) = {\left\lfloor \frac n5 \right\rfloor}! \times 2^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times { ( n \bmod{5} ) }!.$

If ${\left\lfloor \frac n5 \right\rfloor}!$ is sufficiently small, then the units digit of $\mathfrak{D} (n!)$ gives us the value of the rightmost non-zero digit of $n!$.

Otherwise, we find the units digit of $2^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times ( n \bmod{5} )!$, which we call $\mathfrak{d}_1$. Now we solve for $\mathfrak{D} \left( {\left\lfloor \frac n5 \right\rfloor}! \right)$. Again, if we arrive at a sufficiently small value of ${\left\lfloor \frac 15 \cdot {\big\lfloor \frac n5 \big\rfloor} \right\rfloor}!$, then we evaluate the units digit of $\mathfrak{D} \left( {\left\lfloor \frac n5 \right\rfloor}! \right)$ $($call this $\mathfrak{d})$ and the final answer is $\mathfrak{d}_1 \times \mathfrak{d}$.

If such a case doesn't arise, then we keep on iterating for $\mathfrak{D} \bigg( \overbrace{ {\bigg\lfloor \frac 15 \cdot {\cdots \left\lfloor \frac 15 \cdot {\left\lfloor \frac 15 \cdot {\left\lfloor \frac 15 \cdot {\big\lfloor \frac n5 \big\rfloor} \right\rfloor} \right\rfloor} \right\rfloor } \cdots \bigg\rfloor}! }^{\text{value of expression } = \, m!} \bigg)$ and record the units digit of the product $2^{a_i} \times (b_i \bmod{5}) !$ $\big($where $a_i$ and $b_i$ are to be followed from the original definition of $\mathfrak{D} (n!)\big)$ and call them $\{\mathfrak{d}_1 , \mathfrak{d}_2, \cdots, \mathfrak{d}_k\}$. Once we arrive at a sufficiently small value for $m!$, we find the units digit of $\mathfrak{D} (m)$ $($call this $\mathfrak{d})$ and the final answer is

$\prod_{i=1}^k \mathfrak{d_i} \times \mathfrak{d}.$

$\big($Here sufficiently small value for $m!$ refers to $m \in \{1,2,3,4\}.\big)$

We first expand $n!$ as

$n! = 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times \cdots \times (n-2) \times (n-1) \times n.$

Now our objective is to collect all the multiples of 5 contained in $n!$ and group the rest of the terms in groups of four consecutive integers. Note that there will be $\left\lfloor \frac n5 \right\rfloor$ multiples of 5 in $n!$.

Thus

$\begin{aligned} n! &= \bigg( 5 \times 10 \times 15 \times 20 \times \cdots \times \left(5 \left\lfloor \dfrac n5 \right\rfloor \right) \bigg) \times (1 \times 2 \times 3 \times 4) \times (6 \times 7 \times 8 \times 9) \times \cdots \\ &= \bigg( 5^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times {\left\lfloor \dfrac n5 \right\rfloor}! \bigg) \times (1 \times 2 \times 3 \times 4) \times (6 \times 7 \times 8 \times 9) \times \cdots. \end{aligned}$

We know that in the value of $n!$ the distribution of lower primes are always greater than or equal to the distribution of higher primes. Thus we know that we can find a multiple $2^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}}$ (or some higher exponent of 2) from each of the groups of the four consecutive integers which are non-integer multiples of 5.

Also note that for consecutive non-multiples of 5, we have

$(5k+1)(5k+2)(5k+3)(5k+4) = {\color{#3D99F6} 625\big(k^4+k^2\big) + 10\big(125k^3+25k^2+25k\big)} + 24. \qquad {\color{#20A900} (*)}$

Now $k^4+k^2$ is an even integer for both even and odd $k$. This shows that $625\big(k^4+k^2\big)$ will be divisible by $10$ for all non-negative integers $k$ and thus the expression shown above in $\color{#3D99F6} \text{blue}$ will be divisible by $10$ for all $k$. Hence, the product of four consecutive non-integer multiples of 5, when formulated, will always have 4 in the units place.

So, if we extract all multiples of 2 from each of the given groups of four $\big($which are also ${\big\lfloor \frac n5 \big\rfloor}$ in number$\big),$ we know that each group will now contribute to a product whose units digit is 2.

Thus

$\begin{aligned} \big(\text{Rightmost non-zero digit of } n!\big) &= 5^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times {\left\lfloor \dfrac n5 \right\rfloor}! \times 2^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times \bigg( 2 \times 2 \times 2 \times 2 \times \cdots {\left\lfloor \dfrac n5 \right\rfloor} \text{ times} \bigg) \times {\color{#D61F06} \big( n \bmod{5} \big)!} \\\\ &= {\left\lfloor \dfrac n5 \right\rfloor}! \times 2^{{}^{\big\lfloor {\large \frac{n}{5}} \big\rfloor}} \times {\color{#D61F06} \big( n \bmod{5} \big)!}. \end{aligned}$

$\big($Note that if ${\big\lfloor \frac n5 \big\rfloor}!$ is not sufficiently small, then iterations are to be followed.$\big)$

The final term $($shown in ${\color{#D61F06} \text{red}})$ comes from the fact that $n$ can be of any of the forms $5k,5k+1,5k+2,5k+3,5k+4$ and hence may or may not be a part of a group of four. In such a case when it is not in a group of 4, it will be in a group of $(n \bmod{5} )$. For example, if $n=12,$ then the groups of 4 are $(1,2,3,4)$ and $(6,7,8,9)$. The remaining numbers are $(11,12),$ in which 12 is included in the group of $( 12 \equiv 2 \bmod{5} )$. The multiplication of the integers in the final group that contains $n$ will yield a product whose units digit will be equal to the units digit of $( n \bmod{5} )!$. This result can be proved using a similar method as in ${\color{#20A900} (*)}$.

This completes our proof. $_\square$

## Solved Examples

Find the rightmost non-zero digit of $501!$.

Using our method of continued iterations, we have

$\begin{aligned} \mathfrak{D} (501!) &=& {\left\lfloor \dfrac{501}{5} \right\rfloor}! \times 2^{\lfloor {501}/{5} \rfloor} \times \big( 501 \bmod{5} \big)! &=& 100! \times 2^{100} \times 1! \\ \mathfrak{D} (100!) &=& {\left\lfloor \dfrac{100}{5} \right\rfloor}! \times 2^{\lfloor {100}/{5} \rfloor} \times \big( 100 \bmod{5} \big)! &=& 20! \times 2^{20} \\ \mathfrak{D} (20!) &=& {\left\lfloor \dfrac{20}{5} \right\rfloor}! \times 2^{\lfloor {20}/{5} \rfloor} \times \big( 20 \bmod{5} \big)! &=& 4! \times 2^{4}. \end{aligned}$

Combining the above three results, we have

$\begin{aligned} \mathfrak{D} (501!) &= \big(\text{Units digit of } 2^{100}\big) \times \big(\text{Units digit of } 2^{20}\big) \times \big(\text{Units digit of } 4! \cdot 2^4\big)\\ & = \big(\text{Units digit of } 6 \times 6 \times 4 \big) \\ &= 4.\ _\square \end{aligned}$

Add an example here.

## Unsolved Problems

It is a common exercise to determine the number of trailing zeros of a factorial. For example, $20! = 2\; 432\; 902\; 008\; 176\; 64{\color{#20A900}{0}}\; {\color{#20A900}{000}}$ has 4 trailing zeros (as highlighted in green above).

However, finding the rightmost non-zero digit of a factorial is much harder. For example, the rightmost non-zero digit of $20!$ is 4, as shown above.

**Problem:** Find the rightmost non-zero digit of $10000!$.

**Cite as:**Rightmost Non-zero Digit of $n!$.

*Brilliant.org*. Retrieved from https://brilliant.org/wiki/rightmost-non-zero-digit-of-n/