31

I ran into a cool trick for last non zero digit of a factorial. This is actually a recurrent relation which states that:

If $D(N)$ denotes the last non zero digit of factorial, then

$$D(N)=4D\left(\left\lfloor{\frac N5}\right\rfloor\right)\cdot D(\mbox{Units digit of $N$}) \qquad \mbox{(If tens digit of $N$ is odd)}$$ $$D(N)=6D\left(\left\lfloor{\frac N5}\right\rfloor\right)\cdot D(\mbox{Units digit of $N$}) \qquad \mbox{(If tens digit of $N$ is even)}$$

Where $\left\lfloor\cdots\right\rfloor$ is greatest integer function.

I was wondering, if anybody could explain why this works?

MJD
  • 65,394
  • 39
  • 298
  • 580
Quixotic
  • 22,431

2 Answers2

32

First, we know that (except for n=0), there are more factors of 2 than factors of 5 in $n!$, so that the first non zero digit has to be even, and we only need to know what it is modulo $5$.

Define $\phi : \mathbb{N}^* \to (\mathbb{Z}/5\mathbb{Z})^*$ by $\phi(n) = {\bar3}^{v_5(n)} \times d(n)$ where $v_5(n)$ is the number of $5$ in the factorisation of $n$ and $d(n)$ is the class of the last nonzero digit of $n$ when written in base $5$. The goal is to find $\phi(n!)$.

It turns out that $\phi$ is a group morphism from $(\mathbb{N}^*,\times)$ to $((\mathbb{Z}/5\mathbb{Z})^*,\times)$ :
If $n = 5^k(a+5b)$ and $m = 5^{k'}(a'+5b')$ with $a$ and $a'$ coprime with $5$, then $nm = 5^{k+k'}(aa'+5(ab'+ba'+5bb'))$, thus $\phi(nm) = 3^{k+k'}aa' = (3^k a)(3^{k'}a') = \phi(n)\phi(m)$.

Therefore we only need to find $\phi(k)$ for $k=1 \ldots n$ and multiply them together to get $\phi(n!)$.
If $k$ is coprime with $5$, then $\phi(k) = \bar{k}$, and $\phi(k) = 3 \phi(k/5)$ otherwise. Furthermore, $1*2*3*4 = 4$ in $(\mathbb{Z}/5\mathbb{Z})^*$ so if $n=5a$ :

$$\phi(n!) = \left(\prod_{i=0}^{a-1} \phi(5i+1)\ldots\phi(5i+5)\right) = \left(\prod_{i=0}^{a-1} 3 \cdot 1 \cdot 2 \cdot 3 \cdot 4 \cdot \phi(i+1)\right) \\ = \left(\prod_{i=0}^{a-1} 2 \phi(i+1)\right) = 2^a\phi(a!)$$

If $n=5a+b$, then $\phi(n!) = \phi((5a)!)\phi(5a+1)\ldots\phi(5a+b) = \phi((5a)!)\phi(1)\ldots\phi(b) = \phi((5a)!)\phi(b!)$ Therefore, we have the recurrence relation $\phi(n!) = 2^{[n/5]} \phi([n/5]!) \phi((n \mod 5)!)$


Now to get the digit in base $10$ : if $n! = 10^k (a + 10 \ldots)$ with $a \in \{2;4;6;8\}$ then, in base $5$, we get $n! = 5^k ((2^k a) + 5 \ldots)$, so $\phi(n!) = 3^k*2^k * a = a$ in $(\mathbb{Z}/5\mathbb{Z})^*$ So we simply need to look at $\phi(n!)$ and pick the corresponding even digit :

In fact, $((\mathbb{Z}/5\mathbb{Z})^*= \{1;2;3;4\},\times)$ is isomorphic to $(\{6;2;8;4\},\times)$ where the multiplication is modulo $10$. Rewriting the recurrence relation in this context, we get : $D(n) = 2^{[n/5]} D([n/5]) D(n \mod 5)$ where the multiplications are all modulo $10$.

To recover the recurrence relation you have, we only need to prove that $2^{[n/5]} D(n \mod 5) = 4^{[n/10]} D(n \mod 10)$ :
If the last digit of n is less than $5$, then $[n/5] = 2[n/10]$ and $n \mod 5 = n \mod 10$, so they are equal. If not, then $[n/5] = 2[n/10] +1$ and $D(n \mod 10) = D(5) D(n \mod 5) = 2 D(n \mod 5)$ so they are equal again.

mercio
  • 50,180
1

For optimizing the calculations instead of the

$$ D(n)=(2[n/5]∗D([n/5])∗D(n \mod5)) \mod10 $$

we can calculate

$$ D(n)=(LastDigitOf(2⌊n/5⌋)∗D(⌊n/5⌋)∗D(n \mod5)) \mod10 $$

Modified formula is:

$$ D(0) = 1 \\ D(1) = 1 \\ D(2) = 2 \\ D(3) = 6 \\ D(4) = 4 \\ D(5) = 2 \\ D(6) = 2 \\ D(7) = 4 \\ D(8) = 2 \\ D(9) = 8 \\ D(n) = (LastDigitOf(2^{\lfloor n/5 \rfloor}) * D(\lfloor n/5 \rfloor) * D(n \mod 5)) \mod 10, \;where \;n > 9 $$

How?

The original formula is $D(n) = (2^{[n/5]} * D([n/5]) * D(n \mod 5)) \mod 10$

But there is a problem: we need to calculate $2^{[n/5]}$. For example if $n = 10 000 $ then we need to calculate $2^{2000}$ which is already too hard (in JS it will output Infinity). But I discovered that we don't need to calculate it instead of this we just need to calculate the last digit of $2^{[n/5]}$. Why? Because each time we just need to calculate the last digit of $2^{[n/5]} * D([n/5]) * D(n \mod 5)$

Theorem

The last digit of multiplication is the last digit of multiplication of last digits of each multiplier

Proof

Let's say we have two numbers $a$ and $b$. We can represent each of them to the power of 10:

$$ a = x_n * 10^n + x_{n - 1} * 10^{n - 1} + \ldots + x_1 * 10^1 + x_0 \\ b = y_n * 10^m + y_{m - 1} * 10^{m - 1} + \ldots + y_1 * 10^1 + y_0 $$

or

$$ a = (x_n * 10^{n - 1} + x_{n - 1} * 10^{n - 2} + \ldots + x_1) * 10 + x_0 \\ b = (y_n * 10^{m - 1} + y_{m - 1} * 10^{m - 2} + \ldots + y_1) * 10 + y_0 $$

When we multiply them we will get:

$$ a * b = (...) * 10 + x_0 * y_0 $$

Now it is clear that the last digit of multiplication is the last digit of $x_0 * y_0$, where the $x_0$ and $y_0$ are the last digits of $a$ and $b$ respectively. So we proved the theorem

Now we need to find the last digits of powers of 2. Let's write some of them:

$$ 2^0 = 1 \\ 2^1 = 2 \\ 2^2 = 4 \\ 2^3 = 8 \\ 2^4 = 16 \\ 2^5 = 32 \\ 2^6 = 64 \\ 2^7 = 128 \\ 2^8 = 256 $$

Now we can see the pattern 1 (2 4 8 6) (2 4 8 6) and can write it $(k = [n / 5])$:

if (k == 0) return 1;
else return [6, 2, 4, 8][k % 4];

We can ignore the first case when k == 0 and here is why:

k % 4 == 0 when k is one of [0, 4, 8, 12, 16, ...] but we need to consider cases when k is one of [0, 4, 8] because for bigger values we will run recursion. The second multiplier in our formula is $D(k)$ and for k is on of [0, 4, 8] we get [1, 4, 2] respectively. We had to multiply these values by 1 but instead of this we will multiply them by 6 so we will get [1, 24, 12], the last digits of these values are [1, 4, 2]. Now we can see that it doesn't matter to multiply them by 1 or 6 the last digits will be the same

The last thing we need it is to prove that $[n / 5] = \lfloor n / 5 \rfloor$ for $n \ge 0$. I think it is obvious

Here is the implementation in JS:

const lastDigigtsOfPowerOfTwo = [6, 2, 4, 8];
const initialValuesOfD_n = [1, 1, 2, 6, 4, 2, 2, 4, 2, 8];

function D(n) { if (n > 9) { const k = Math.floor(n / 5); const lastDigitOfPowerOfTwo = lastDigigtsOfPowerOfTwo[k % 4];

return (lastDigitOfPowerOfTwo * D(k) * D(n % 5)) % 10

} else { return initialValuesOfD_n[n]; } }

// TESTS

;[ 0, // 1 1, // 1 2, // 2 3, // 6 4, // 4 5, // 2 6, // 2 7, // 4 8, // 2 9, // 8 10, // 8 20, // 4 100, // 4 1000, // 2 10000, // 8 100000, // 6 1000000, // 4 ].forEach(n => console.log(n, D(n))) ```