Home > Number theory, Olympiad, Problem Solving > Last non-zero digit of a factorial

## Last non-zero digit of a factorial

Find a method to calculate the last non-zero digit of $n!$, where $n \in \mathbb{N}^*$ and $n!=1\cdot 2\cdot ... \cdot n$.
Solution: We have the following formula $\displaystyle (5q)! ={10}^{q}q!\prod_{i=0}^{q-1}\frac{(5i+1)(5i+2)(5i+3)(5i+4)}{2}$, where this is proved by removing from $(5q)!$ terms divisible by $5 : \ 5, 10,...,5q$.
Since $\displaystyle \frac{(5i+1)(5i+2)(5i+3)(5i+4)}{2} \equiv 2 \text{ mod }10$ we can obtain the following reccurence $L(n) \equiv 2^q L(q)L(r) \text{ mod }10$, where $L(n)$ is the last non-zero digit of $n!$ and $n=5q+r$ by the integer remainder theorem.
This enables us to calculate the last digit of $n!$ very fast, descending exponentially at every step to reach small numbers, for which we can easily calculate that digit.

1. September 2, 2011 at 5:12 pm

Hi,
Thanks for the method. I can use this as long as r is not equal to 0. What will be the answer when r = 0? Or in other terms, what to do when n is a multiple of 5?
Regards.

2. July 27, 2012 at 6:12 am

why is the residue L(r) ? I suspect there’s a problem when r = 1 since 1 != 6 mod 10, even though 1 * 2 = 6 * 7 , 1 * 2 * 3 = 6 * 7 * 8, 1 * 2 * 3 * 4 = 6 * 7 * 8 * 9 mod 10

• July 27, 2012 at 11:08 am

I don’t understand your point. Please detail a bit what you mean. (there is a small error in your comment: $1!=1 \mod 10$)