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.
If ⌊5n⌋! is sufficiently small, then the units digit of D(n!) gives us the value of the rightmost non-zero digit of n!.
Otherwise, we find the units digit of 2⌊5n⌋×(nmod5)!, which we call d1. Now we solve for D(⌊5n⌋!). Again, if we arrive at a sufficiently small value of ⌊51⋅⌊5n⌋⌋!, then we evaluate the units digit of D(⌊5n⌋!)(call this d) and the final answer is d1×d.
If such a case doesn't arise, then we keep on iterating for D(⌊51⋅⋯⌊51⋅⌊51⋅⌊51⋅⌊5n⌋⌋⌋⌋⋯⌋!value of expression =m!) and record the units digit of the product 2ai×(bimod5)!(where ai and bi are to be followed from the original definition of D(n!)) and call them {d1,d2,⋯,dk}. Once we arrive at a sufficiently small value for m!, we find the units digit of D(m)(call this d) and the final answer is
i=1∏kdi×d.
(Here sufficiently small value for m! refers to m∈{1,2,3,4}.)
We first expand n! as
n!=1×2×3×4×5×6×⋯×(n−2)×(n−1)×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 ⌊5n⌋ multiples of 5 in n!.
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⌊5n⌋ (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
Now k4+k2 is an even integer for both even and odd k. This shows that 625(k4+k2) will be divisible by 10 for all non-negative integers k and thus the expression shown above in 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 (which are also ⌊5n⌋ in number), we know that each group will now contribute to a product whose units digit is 2.
Thus
(Rightmost non-zero digit of n!)=5⌊5n⌋×⌊5n⌋!×2⌊5n⌋×(2×2×2×2×⋯⌊5n⌋ times)×(nmod5)!=⌊5n⌋!×2⌊5n⌋×(nmod5)!.
(Note that if ⌊5n⌋! is not sufficiently small, then iterations are to be followed.)
The final term (shown in 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 (nmod5). 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≡2mod5). 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 (nmod5)!. This result can be proved using a similar method as in (∗).
D(501!)=(Units digit of 2100)×(Units digit of 220)×(Units digit of 4!⋅24)=(Units digit of 6×6×4)=4.□
Add an example here.
Unsolved Problems
2
4
6
8
It is a common exercise to determine the number of trailing zeros of a factorial. For example,
20!=2432902008176640000
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!.