Wednesday, April 13, 2016

number theory - Last non Zero digit of a Factorial



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(N5)D(Units digit of N)(If tens digit of N is odd)


D(N)=6D(N5)D(Units digit of N)(If tens digit of N is even)



Where is greatest integer function.



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


Answer



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 ϕ:N(Z/5Z) by ϕ(n)=ˉ3v5(n)×d(n) where v5(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 ϕ(n!).



It turns out that ϕ is a group morphism from (N,×) to ((Z/5Z),×) :
If n=5k(a+5b) and m=5k(a+5b) with a and a coprime with 5, then nm=5k+k(aa+5(ab+ba+5bb)), thus ϕ(nm)=3k+kaa=(3ka)(3ka)=ϕ(n)ϕ(m).



Therefore we only need to find ϕ(k) for k=1n and multiply them together to get ϕ(n!).
If k is coprime with 5, then ϕ(k)=ˉk, and ϕ(k)=3ϕ(k/5) otherwise.
Furthermore, 1234=4 in (Z/5Z) so if n=5a :



ϕ(n!)=(a1i=0ϕ(5i+1)ϕ(5i+5))=(a1i=031234ϕ(i+1))=(a1i=02ϕ(i+1))=2aϕ(a!)




If n=5a+b, then ϕ(n!)=ϕ((5a)!)ϕ(5a+1)ϕ(5a+b)=ϕ((5a)!)ϕ(1)ϕ(b)=ϕ((5a)!)ϕ(b!)
Therefore, we have the recurrence relation ϕ(n!)=2[n/5]ϕ([n/5]!)ϕ((nmod5)!)






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



In fact, ((Z/5Z)={1;2;3;4},×) is isomorphic to ({6;2;8;4},×) where the multiplication is modulo 10.

Rewriting the recurrence relation in this context, we get :
D(n)=2[n/5]D([n/5])D(nmod5) where the multiplications are all modulo 10.



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


No comments:

Post a Comment

analysis - Injection, making bijection

I have injection f:AB and I want to get bijection. Can I just resting codomain to f(A)? I know that every function i...