Math CentralQuandaries & Queries


Question from mukesh, a student:

Sir my problem is i have to find last five non zero digits of integer which can be very large (up to 10^12) .

I searched on net and i found this <>
very helpful and i can find last non zero digit of of any factorial.

Now my problem is that i have to find last five non zero digit of factorial and also i want to general method for last K non zero digits of factorial n. For example 10!=3628800 so last non zero digit is 8, last two non zero digit is 88 ..... and last five non zero digit is 36288.

thankx in advance .

Hi Mukesh,

Here is a suggestion. I am not sure if this is exactly what you want, but it might help get you started.

First write n! as 2a x 5b x t, where t is divisible by neither 2 nor 5. The exponent of any prime in the prime factorization of n! is known to be the sum from i=1 to infinity of the floor of n/pi, so it is possible to compute a and b. Notice that once pi exceeds n all remaining tems in the sum are zero.

Let m be the minimum of a and b. The integer n! ends with a sequence of m zeros. The last non-zero digit of n! is n! modulo 10m+1. The two digits preceding the sequence of m zeros are given by n! modulo 10m+2. There is a small problem though, the leftmost of these two digits could be a zero.

In general the K digits preceding the final sequence of m zeros of n! are given n! modulo 10m+K, but there is no reason why some of these cannot be zero (except for the last). I don't know how often there is a zero among them.

Papers written by Peter Borwein might be a good place to look for better ideas than these.


About Math Central


Math Central is supported by the University of Regina and The Pacific Institute for the Mathematical Sciences.
Quandaries & Queries page Home page University of Regina PIMS