5

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?

2 Answers2

2

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:

> S(n) = sum(x=1,sqrt(n),moebius(x)*x^2*(floor(n/x^2)*(floor(n/x^2)+1)/2))
> S(10)
%2 = 34
> S(4*10^5)
%3 = 48630980209
> S(2*10^9)
time = 62 ms.
%4 = 1215854235246230121
> S(2*10^12)
time = 2,313 ms.
%5 = 1215854204028733201986723
0

If you need to test, here is a simple python code which returns the sum of non-square numbers lower than $x$:

def squarefree(N):
    if N%4==0:
        return False
    else:
        for j in range(3,int(N**0.5)+1,2):
            if N%j**2==0:
                return False
    return True

def sumation(x):
    s=0
    for j in range(1,int(x)+1):
        if squarefree(j):
            s+=j
    return s

# test:
print(sumation(10))

You can run this script( copy paste) at this link

luisfelipe18
  • 3,690