I am looking for an efficient way to find the exact sum (not an asymptotic estimation) of all the squarefree numbers $\leq x$. As an example, when $x=10$, the sum is $1+2+3+5+6+7+10=34$
I guess an eratosthenes-like sieve could work but it is inefficient for large values of $x$, as it has time complexity of $\mathcal{O}(x\log\log x)$ and memory requirement of $\mathcal{O}(\sqrt x)$ (using segmented sieve), slightly altered for this problem. I can't find this problem properly answered on the internet, although the count of squarefree integers $\leq x$ is well addressed and admits very fast calculations.
Call this function $S(x)$. It can be easily shown that:
$$S(x) = \sum_{n=1}^x |\mu(n)|\cdot n$$
Some sample values:
$S(10) = 34 \qquad\text{(order of $10^1$)}\\ S(4\cdot 10^{5}) = 48\,630\,980\,209 \qquad \text{(order of $10^{10}$)}\\ S(2\cdot 10^{9}) = 1\,215\,854\,235\,246\,230\,121 \qquad \text{(order of $10^{18}$)}$
Is there any efficient way (possibly sublinear algorithm) to find the sum of the squarefree integers $\leq x$? Perhaps using Mertens function $M(x)$ or an alternation of the counting function of these integers?
The following method uses inclusion exclusion method it needs around $\sqrt{N}$ operations: Start with $$ 1+2+\dots+N = \frac{N(N+1)}2 $$ Now for every integer $1< x \le \sqrt{N}$, $x$ squarefree compute the number $k$ of prime factors of $x$ and add $$ (-1)^k x^2\left( 1 + \dots + \bigl\lfloor\tfrac N{x^2}\bigr\rfloor\right) $$ to the sum. In other words the sum you look for is: $$ S = \frac 12\sum_{x\le \sqrt{N}} \mu(x)x^2\bigl\lfloor \tfrac N{x^2}\bigr\rfloor\bigl(\bigl\lfloor \tfrac N{x^2}\bigr\rfloor+1\bigl) $$ where $\omega(x)$ is the number of different prime factors of $x$, and $\mu(x)$ is the mobius function.
In Pari the program would be: