# Efficiently getting bits of N! ?

Given $N$ and $M$, is it possible to get the $M$'th bit (or digit of any small base) of $N!$ in time/space of $O( p( ln(N), ln(M) ) )$, where $p(x, y)$ is some polynomial function in $x$ and $y$?

i.e. Given $N = 2^\eta$, $M = 2^\mu$ (with $N$, $M \in \mathbb{Z}$), find bit $2^\mu$ of $(2^\eta)!$ in $O( p(\eta, \mu) )$.

Note: I have asked this on mathoverflow.net here and have not been getting any answers so I have cross-posted.

From the comment on the other site, Gene Kopp points out that one can efficiently compute the lower order bits by doing modular arithmetic and higher order bits using Stirling's approximation, so this question is really 'how efficiently can one compute the middle order bits?'.

Dick Lipton has a beautiful post from 2009 on the relationship between the factorial function and factoring. There's a lot there that is unrelated to this question, but one salient point is this theorem:

If $n!$ can be computed by a straight-line arithmetic computation in $O(\log^{c} n)$ steps, then factoring has polynomial size circuits.

I suspect that this is evidence that your question, especially within the time bounds you mention, is going to be difficult to answer.

• Thank you, this is exactly the type of answer I was looking for. This doesn't directly answer my question and I don't see exactly how to connect the two, but it's close enough to put my mind at rest. – user834 Oct 15 '10 at 14:59

Suresh's answer probably answers the question for you, but I thought I would point out a special case. You can always work out the result for the less significant digits for any base. Take $p$ as our base.

Clearly, every pth term in the factorial is a multiple of $p$. Every $(p^2)$th term is a multiple of $p^2$, etc. Thus the highest power of $p$ that is a factor of $N!$ is $X_p = \sum_{i=1}^{\lfloor \log_p(N!) \rfloor} \lfloor \frac{N}{p^i}\rfloor$. $\log_p(N!)$ is easy to approximate by Stirlings approximation: $\ln N! \approx N \ln N - N$. Further, $p^{N \lceil \log_p (N) \rceil} > N!$, so the sum can always be computed efficiently by summing instead for $1 \leq i \leq {N \lceil \log_p (N) \rceil}$ (since $\lfloor \frac{N}{p^i}\rfloor = 0$ for $i > {\lfloor \log_p(N!) \rfloor}$).

Thus the last $X_p$ digits of $N!$ are zero in base $p$.