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?
efficient? Python and Swfit are big monster for those kind of calculations. You need to calculate $\sum_{n\le x} |\mu(n)|n$ – luisfelipe18 Jul 10 '19 at 21:01