[Math] Find last 5 significant digits of 2017!

factorialmodular arithmeticnumber theory

Since there are less powers of $5$ than of $2$ and since $10 = 2 \cdot 5$, I counted the number of zeros in $2017!$:
$\left \lfloor{ \frac{2017}{5^1}}\right \rfloor + \left \lfloor{ \frac{2017}{5^2}}\right \rfloor +\left \lfloor{ \frac{2017}{5^3}}\right \rfloor +\left \lfloor{ \frac{2017}{5^4}}\right \rfloor = 502$

Now I need to find $\frac{2017!}{10^{502}} \pmod{10^5}$

Using Chinese remainder theorem I split the $\pmod{10^5}$ into $\pmod{2^5}$ and $\pmod{5^5}$. Since there are a lot more powers of $2$ left, $\frac{2017!}{10^{502}}\equiv 0 \pmod{2^5}$

I am having trouble with getting $\frac{2017!}{10^{502}} \pmod{5^5}$. What would be the best way to compute it?

Clarification: significant digits – the ones that follow up before the zeros at the right side of the number.

Best Answer

OK, for what it's worth, the answer can be derived by casting out all factors of $5$ and multiplying the resulting numbers incrementally $\bmod 100000$, which also allows us to also divide out from the running product the same power of $2$ as the power of $5$ that we just cast out.

As a way of checking any more elegant mathematical approach, therefore, the result is

$15968$



As an aside, I'll share the process I used to calculate this and justify a shortcut that I used.

The basic idea here was to multiply successive numbers into a running product, excluding powers of $5$ and adjusting powers of $2$. The process for each number was:

  • find the highest power of $5$ that divides the new number
  • divide that out of the number
  • multiply the adjusted number into the running product and divide by an equivalent power of $2$
  • find the residue $\bmod 100000$

Note that this gives identical running $\bmod 100000$ values to @skyking's approach.

There is a intermittent problem with this approach, in that the running product will potentially be wrong for a couple of numbers after a large power of $5$ is divided out. Effectively, the value should be saturated with factors of $2$ from $8!$ onwards, but dividing out a suitable power of two may disturb this briefly. For example, the value for $15!$ is out in this process - $24368$ instead of the correct value, $74368$. Nevertheless, once a suitable number of factors of two in subsequent numbers are multiplied back into the product, the $\bmod 100000$ value gets back on track.

$2017$, occurring as it does directly after $2016$ = $2^5\cdot 63$, has an accurate running product by this method.

An continuously accurate running value can be produced, if necessary, by building up a "reserve" of a few powers of $2$ separately from a running product calculation, by stripping out powers of two from the successive values until the reserve is full. This is then used to compensate for powers of $5$ encountered, and multiplying these back in to the reduced running product. This correct gives the value for $15$ etc.

Related Question