Let $$S_n = \sum_{m=1}^{n} \frac{n \bmod m}{m!}$$
be the $n$-th partial sum of a series with remainder $$R_{n}=\sum_{m=n+1}^{\infty}\frac{n \bmod m}{m!}=n\cdot e - \frac{\lfloor e\cdot n\rfloor}{(n-1)!}.$$
This series occurs when embedding the profinite integers $\hat{\mathbb{Z}}$ into the unit interval $\left[0,1\right]$ using the fractional part of the factorial number system. $S_n$ is a rational number.
Computing $n \% m$ for each $m$ and summing is $\mathcal{O}(n)$, so exponential in the number of digits. I'm wondering if there is a faster way to compute these numbers. Programmatically, arbitrary precision arithmetic is needed as $\frac{1}{m!}$ is tiny.
Initial thoughts:
Factor $n$ and compute the residues modulo prime powers, $n \bmod p^{r_i}$, use divisibility criteria to reduce the number of summands, and the Chinese Remainder Theorem to reconstruct.
Rewrite $n \bmod m=n−m\lfloor n/m \rfloor$, and rewrite the floor using a Fourier series to get $\displaystyle n−m\left(n/m−1/2+\frac{1}{\pi}\sum_{k=1}^{\infty}\frac{\sin(2\pi k n/m)}{k}\right)=m/2−\frac{m}{\pi}\sum_{k=1}^{\infty}\frac{\sin(2\pi k n/m)}{k}$. Rearranging to get an exponential sum would be the next guess, but not confident w/o abs. conv.
See also: https://mathoverflow.net/questions/195325/how-to-calculate-the-sum-of-remainders-of-n
The only thing I "optimized" is the calculation of the factorial term, which is updated each step. Will let you know how long it takes. If I have to stop it for some reason, I'll run it again tonight.
– Charlie S Dec 28 '20 at 14:08