Finding the last nonzero digit of the factorial of a large number

385 Views Asked by At

This problem is from projecteuler problem 160. I am not looking for an answer or anything like that I just got stuck on some of the mathematics and am looking for some help. Instead of solving the problem as it is given I first tried to solve a simpler problem:

Let $f(N)$ be the function that assigns to $N$ the last non zero digit of $N!$. For instance $f(9) = f(10) = 8$. Now I was looking for some kind of recursive formula for $f(10^n)$ but got stuck. For notational convenienve, denote $\pi (n)$ to be the last non zero digit of a number $n$.

My thoughts are as follows: Note that $f(10)$ is the last non zero digit of $1\cdot 2\ldots\cdot 10$. Now $f(11)$ is given by

\begin{align} \pi(11\cdot (1\cdot 2\ldots\cdot 10))&=\pi[10\cdot (1\cdot 2\ldots\cdot 10) + 1\cdot 2\ldots\cdot 10]\\ &=\pi[ 1\cdot 2\ldots\cdot 10]\\ &= f(10). \end{align}

The second equality follows from the fact that the first term in square brackets is 10 times the second part, and thus cannot contribute to the last digit. Similarly $f(12)$ is equal to the last digit of $12 \cdot f(11)$ which is then equal to the last digit of $2\cdot f(11)$ which is 6. This is correct so far as $12!=479001600$. Continuing this line of reasoning it would follow that

\begin{align} f(19) &= \pi[(11\cdot 12\ldots\cdot 19)\cdot (1\cdot 2\ldots\cdot 10)]\\ &= \pi[(1\cdot 2\ldots\cdot 9)\cdot (1\cdot 2\ldots\cdot 9)]\\ &=\pi [(1\cdot 2\ldots\cdot 9)^2]\\ &=\pi (f(9)^2)\\ &= \pi (8^2)\\ &= 4. \end{align}

However, no such luck as $f(19)=2$, as can be seen by working out the actual factorial of 19. Can someone help me understand what is going on?

EDIT: One of the main reason I ask questions on Stackexchange is that it forcces me to write out clearly what I know and more often than not I will see where the mistake lies. I found one problem, namely that I mistakenly assumed that $\pi(a\cdot b) = \pi(\pi(a)\cdot \pi(b))$, which is not the case, particularly if $\pi(a)=2$ and $\pi(b)=5$. I feel like there is still something to salvage here though.

Thanks in advance for any help!

1

There are 1 best solutions below

7
On

Ignore all multiples of 5, since they add a 0 to the product. You just have to look at the final digit, so for $1\times 2\times 3\dots$ that would only be 1, 2, 3, 4, 6, 7, 8, 9, and then starting again with 1, 2... For the final digit of the product this quickly results in a repeating pattern.