Project Euler Problem 432

Let S(n,m) = sumphi(n times i) for 1 leq i leq m.

Project Euler Problem 432

Solution

Answer: 754862080

Let

$$S(n,m)=\sum_{i=1}^{m}\phi(ni)$$

with $n=510510=2\cdot3\cdot5\cdot7\cdot11\cdot13\cdot17$.

The key observation is that $510510$ is squarefree.

For squarefree $n$,

$$\phi(ni) = \phi(n)\phi(i) \prod_{p\mid \gcd(n,i)} \left(1-\frac1p\right)^{-1}.$$

The correction factor depends only on $\gcd(n,i)$, which ranges over the $2^7=128$ divisors of $510510$. By Möbius inversion on these divisors, one can rewrite the sum as a linear combination of quantities of the form

$$G(d,m)=\sum_{k\le m}\phi(dk),$$

leading to a recursion over divisors of $n$:

$$G(d,m) = \phi(d),F(m) + \sum_{\substack{e\mid d\ e>1}} \phi(d)\alpha(e), G!\left(e,\left\lfloor \frac{m}{e}\right\rfloor\right),$$

where

$$F(x)=\sum_{k\le x}\phi(k)$$

is the summatory totient function.

To evaluate $F(x)$ for $x\le 10^{11}$, use the standard divide-and-conquer identity

$$F(n) = \frac{n(n+1)}{2} - \sum_{l=2}^{n} (r-l+1), F!\left(\left\lfloor\frac{n}{l}\right\rfloor\right),$$

grouping equal quotients $\lfloor n/l \rfloor$, which runs in roughly $O(n^{2/3})$ with memoization.

Verifying against the given check:

$$S(510510,10^6)=45480596821125120,$$

the implementation reproduces the exact provided value.

For $m=10^{11}$,

$$S(510510,10^{11}) = 454805540037378968754862080.$$

The last $9$ digits are:

Answer: 754862080