Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)!

share|cite|improve this question
Project Euler, problem #160 ? – Sasha Nov 9 '11 at 20:16
For least significant non-zero digit see – 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
related:… – Man Jul 30 '13 at 13:49
up vote 0 down vote accepted

I solved this just now--a crucial fact is that there are more factors of 2 than of 5.

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?

share|cite|improve this answer
hey, you got it? That's great! – Scaramouche Jan 18 '12 at 3:27

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...

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.