# On the regularity of the alternating sum of prime numbers

Let's define $(p_n)_{n\in \mathbb N}$ the ordered list of prime numbers ($p_0=2$, $p_1=3$, $p_2=5$...).

I am interested in the following sum:

$$S_n:=\sum_{k=1}^n (-1)^kp_k$$

Since the sequence $(S_n)$ is related to the gaps between prime numbers, I would expect it to be quite irregular.

But if we plot $(S_n)_{1\leqslant n\leqslant N}$ for $N\in \{50,10^3,10^5,10^6,10^7\}$, we obtain the following:

We can observe a great regularity.

So my questions are:

• Why is there so much regularity?

• Can we find the equations of the two lines forming $(S_n)$?

• Is there a proof that it will continue to be that regular forever?

Any contribution, even partial, will be greatly appreciated.

Thanks to mixedmath and Daniel Fischer, here is more curves:

• in blue, you have $S_n$;

• in red, you have $\displaystyle 2^{1/6}\displaystyle \sum (-1)^k k\log k$;

• in green, you have $\displaystyle \sum (-1)^k k\log k$;

• in purple, you have $\displaystyle \sum (-1)^k k(\log k+\log\log k)$;

• in yellow, you have $\displaystyle \sum (-1)^k k(\log k+\log\log k-1)$.

My question seems quite related to this one.

• Did you take $1$ as a prime? It seems you did, because in the first diagram, $S_2<0$. Jun 8, 2017 at 11:12
• for the third question you have to show that $|\sum \limits_{k=1}^n (-1)^k p_k| \approx \frac{p_n}{2}$ Jun 8, 2017 at 15:25
• Ah, I hadn't noticed that you'd edited in the additional graphs. Indeed, the extra terms from Dusart's bounds (including the $-1$) look very good. Jun 8, 2017 at 20:52
• The sequence $\{|S_n|\}_{n\in\omega}$ is in the on-line encyclopedia of integer sequences, and there are provided several conjectures and a few links to papers. Oct 6, 2023 at 12:04
• Shouldn't the sum begin from $0$? Oct 7, 2023 at 13:05

This is a great question. Unfortunately, this is an incomplete answer. But I thought about this a bit and I noticed something interesting, but which I do not know how to explain.

With $$S_n = \sum_{k \leq n} (-1)^k p_k,$$ where $p_n$ is the $n$th prime, some patterns are immediately clear. It is obvious that the sequence of $S_n$ alternates in sign for example. But some patterns are not obvious or clear.

By the prime number theorem, we expect that $p_n \approx n \log n$. If we plot $\sum_{k \leq n} (-1)^k k \log k$ against $S_n$ for all primes up to one million, we get

This is apparently a bit too small, it seems. This sort of makes sense, as deviations from the approximation $p_n \approx n \log n$ compound here.

However, I noticed that $$1.12 \sum_{k \leq n} (-1)^k k \log k$$ is actually a very good (experimental) estimate of what's going on, as can be seen in the following plot.

Perhaps $1.12$ is an incorrect choice --- it just happened to be a very nearby reasonable seeming number, and it does appear to reflect what's going on. I do not know why, though.

If we conjecture for a moment that $1.12 \sum (-1)^k k \log k$ is a good estimator, then we can write a good asymptotic for this series using partial summation. Namely

\begin{align} \sum_{k \leq n} (-1)^k k \log k &= \left( \sum_{k \leq n} (-1)^k k \right) \log n - \int_1^n \left( \sum_{k \leq t} (-1)^k k \right) \frac{1}{t} dt \\ &= (-1)^n \left \lfloor \frac{n+1}{2} \right \rfloor \log n - \int_1^n (-1)^{\lfloor t \rfloor} \left \lfloor \frac{\lfloor t \rfloor+1}{2} \right \rfloor \frac{1}{t} dt \\ &= (-1)^n \left \lfloor \frac{n+1}{2} \right \rfloor \log n + O \left( \int_1^n \left( \frac{t+1}{2t} + \frac{2}{t} \right) dt\right) \\ &= (-1)^n \left \lfloor \frac{n+1}{2} \right \rfloor \log n + O(n). \end{align}

So I conjecture that $$S_n \approx 1.12 (-1)^n \left \lfloor \frac{n+1}{2} \right \rfloor \log n + O(n).$$ For comparison, the size of the alternating sum of the first 1001 primes is $3806$, where this estimate gives about $3876.6$. For $10001$, the actual is $52726$, compared to the estimated $51588.7$. These are both close, although apparently not super accurate.

It may be possible to describe the actual behavior of $S_n$ a bit more by using secondary terms in the prime number theorem, but I was not successful in my back-of-the-envelope computations. Nor do I know how to explain the $1.12$ that appears in this answer (or how to determine if it is $1.12$ as opposed to, say, $1.15$). Perhaps someone else will see how to fill in these gaps.

(Edited in after Daniel Fischer's comment)

Here are updated images, including plots of $\sum (-1)^n n (\log n + \log \log n)$.

As we can see, $\sum (-1)^n n (\log n + \log \log n)$ grows in magnitude just a little bit more quickly. Focusing a bit on just the upper half, we get

• Thank you for this great work! For the minus sign, I defined $p_0=2$ so I would avoid the first negative sign, but this is not really important. I found it strange how $\sqrt 5$ pop here. I wish someone will see how to fill these gaps! Thanks again for this work, it gave me a lot to think about. Jun 8, 2017 at 16:24
• I have tried to replicate your results for the last $20$ minutes, but it can't seem to work. It seems that $\sqrt 5\sum (-1)^k k \log k$ is way superior to $S_n$. I don't understand why we obtain such different results. Jun 8, 2017 at 16:52
• @E.Joseph Ah, you are correct. I made a (very silly) mistake in my code. I will change it and update accordingly. Jun 8, 2017 at 17:22
• It would be interesting to see the plot of $\sum (-1)^k k(\log k + \log \log k)$ in there too. Jun 8, 2017 at 18:11
• There's no reason to believe that the constant $1.12$ is relevant in the long run. The approximation $p_n \sim n\log n$ is noticeably imperfect for small $n$ yet holds with leading constant $1$ for large $n$. Oct 7, 2023 at 7:22

Since $$S_n=\sum_{k=1}^n (-1)^k p_k$$ alternates, it's behaviour may become clearer by expressing $$(-1)^n S_n$$ in terms of the prime gaps $$p_k-p_{k-1}$$. So let's instead analyse $$s_n = (-1)^n\cdot 2 S_n = 2\cdot\sum_{k=1}^n (-1)^{n-k} p_k = p_n + \sum_{k=1}^n (-1)^{n-k}(p_k-p_{k-1}) + (-1)^n p_0$$ where either $$p_0=2$$ as stated or $$p_0=0, p_1=2$$ as seems to be used in the plots.

From this, we see that $$p_n$$ is the dominant term of $$s_n=(-1)^n\cdot2S_n$$. So the asymptotics is just the asymptotics of $$p_n$$.

The prime number theorem gives multiple approximations of the prime number count. The simplest is just that the number of primes up to $$n$$ is $$\pi(n)\approx n/\ln n$$, which leads to $$p_n\approx n\ln n$$. However, this is not very accurate.

A somewhat better approximation is $$p_n/n\approx\ln n+\ln\ln n$$, and an even better one being $$p_n/n\approx\ln n + \ln\ln n - 1$$, with these two providing upper and lower bounds for $$n\ge6$$.

While these different approximations are all asymptotically equivalent in the sense that their ratios approach 1 as $$n$$ increases, convergence is slow due to the logarithmic terms. Plotting the ratios $$\text{prime}(n)=p_n$$ divided by $$\text{simple}(n)=n\ln n$$, $$\text{appr2}(n)=n(\ln n+\ln\ln n)$$, $$\text{appr3}(n)=n(\ln n+\ln\ln n-1)$$, and using $$\log_{10} n$$ on the $$x$$-axis illustrates this:

Due to the logarithmic $$x$$-axis, it is more clear that the ratios are approaching 1: when using a linear scale, it approaches so slowly that it may appear to converge to some number other than 1.

The similar ratios for $$s_n$$, which I plot instead of the ratios for $$S_n$$, show some additional fluctuation which is due to the alternating sum of the prime gaps: this basically acts as noise similar to a Brownian motion. The plot contains $$s_n/p_n$$ (prime) as well as ratios $$s_n/\text{appr}(n)$$ for the same functions as above.

• Agreed. Furthermore, we expect the prime gaps near $n$ to act like Poisson random variables with parameter $\log n$. Therefore the alternating sum should act like a mean-$0$ random variable with variance about $n\log n$. In other words, $S_n$ should act like $(-1)^n p_n/2 + O(\sqrt{n\log n})$ most of the time (and the larger deviations should only be more logarithmic favtors, by the law of the iterated logarithm). Oct 7, 2023 at 7:25

Just a remark and I shall delete this :

Some some months ago I made a conjecture let's try it :

Let $$x\geq 10^{10}$$ then it seems we have :

$$\bigg{\lfloor}\frac{\pi{(x)}}{\pi{(\pi{(x)}})}\bigg{\rfloor}\pm 0.5=\lfloor \ln(x)-\ln(\ln(x))-1\rfloor\pm 0.5\tag{I}$$

I'm not a specialist in number theory so give me your feedback about it and how it enlighten the great question here.

@davidlowryduda $$2e^{-\gamma}\simeq 1.12$$

EDIT:

We can do better than the prime number theorem if in $$(I)$$ and for a sufficiently large $$x$$ we replace $$-1$$ by $$-\left(1+C\left(\frac{\operatorname{li}\left(x\right)}{x}\ln\left(x\right)-1\right)^{2}\right)$$ where $$C$$ is a positive constant.

• Using Firoozbakht's conjecture we have $$p_{n+1}-p_n\le \ln^2(p_n)-\ln(p_n)-1,n\geq 9$$ . Now we can get an estimate using the product math.stackexchange.com/questions/204902/… at infinity and jensen's inequality for $\ln^2(x)$ and $p_n\simeq n\ln n$ Oct 8, 2023 at 8:10