# Computing Non-zero End Digits of Large Factorials

Any large factorial will have a number of zero behind it, and one could write an expression to compute the number of trailing zeros, but how would one go about computing the non-zero end digits?

E.g. compute first 5 non-zero end digits of (10^12)!

-
Project Euler, problem #160 ? – Sasha Nov 9 '11 at 20:16
For least significant non-zero digit see :mathpages.com/home/kmath489.htm – Ganesh Dec 9 '11 at 22:58
How about in a base like 12 instead of 10? – marty cohen Jan 9 '12 at 6:14
– Man Jul 30 '13 at 13:49

To formalize notation, let $v(N)$ be the largest positive integer such that $\large 10^{v(N))} | N$, and let $M(N) := \dfrac{N}{10^{v(N)}}$ denote the number of which we are trying to find the last 5 digits if $N = (10^{12})!$.
Hint: What can you say about $M(N) \mod 2^5$? How about $M(N) \mod 5^5$? How about $$\Large M \left ( \prod_{\substack{ 1 \leq n \leq 10^{12} \\ 5 \not \, \,| \, n } } n \right ) \mod 5^5?$$ How about $$\Large M \left ( \prod_{\substack{ 1 \leq n \leq 10^{12} \\ 25 \not \, \,| \, n } } n \right ) \mod 5^5?$$ Can you see a pattern?
Hint: if you wanted just the last digit, a naive approach would be to do all the multiplication mod 10, skipping the 0's. $(1*2*3*4*5*6*7*8*9)^{10^{11}}$. A second thought is that the $5$ swallows the $2$, so we can skip those. This works fine for all the factors besides $2$ and $5$, which need some more thought...