# Brun's theorem and sieve theory

Brun's theorem is a relatively famous result in analytic number theory that says the sum of the reciprocals of the twin primes converges to a finite value. In other words, we have

\\[ \sum_{p, p+2 \, \text{prime}} \frac{1}{p} + \frac{1}{p+2} = B \\]

for some finite constant \\( B \\). This is in contrast to the same sum taken over *all primes*, which is divergent:

\\[ \sum_{p \, \textrm{prime}} \frac{1}{p} = \infty \\]

In this post, I'll use Brun's theorem as an illustration of sieve theoretic arguments in analytic number theory. I'll try to explain relevant results as I go along to minimize the background necessary to understand the arguments, but some background in real analysis and number theory is needed to understand the post. If you don't have such a background, most of the post will probably be gibberish.

I'm writing this post mostly because I think there's some lack of good explanations of sieve theory in general and the Brun sieve in particular. Hopefully this post will be helpful to a handful of people who are interested in or trying to understand such matters.

Note that in the post I'll not always mention that a sum or a product runs over the prime numbers explicitly. If the sum or product is indexed by the letter \\( p \\), you should assume that it runs over the primes and not e.g. over the natural numbers. Sometimes \\( p \\) will run only over *odd* primes, because there is a degenerate case with the prime \\( 2 \\) when we work with twin primes coming from the fact that \\( 0 \\) and \\( 2 \\) are in the same residue class mod \\( 2 \\). This will often be obvious from the surrounding context.

## Background

First, let's discuss some background results that will be useful to know throughout the post.

The [prime number theorem](https://en.wikipedia.org/wiki/Prime_number_theorem) says that the number of primes less than \\( N \\), denoted \\( \pi(N) \\), is well approximated by \\( N/\log N \\). Concretely, it says that

\\[ \lim_{N \to \infty} \frac{\pi(N)}{N/\log N} = 1 \\]

Roughly, the prime number theorem says that the density of the prime numbers "around \\( N \\)" is roughly \\( 1/\log N \\) when \\( N \\) is large. This is a rather difficult theorem to prove and we won't actually need it to prove Brun's theorem. However, the result will be useful to know in heuristic arguments for why we might expect the theorem to be true, and to motivate our method of proof.

We will also need the [Mertens theorems](https://en.wikipedia.org/wiki/Mertens%27_theorems). These theorems are "weaker" versions of the prime number theorem. Specifically, we will need to know the fact that

\\[ A(x) = \sum_{p \leq x, \, p \, \text{prime}} \frac{1}{p} = \log \log x + O(1) \\]

This is a stronger, quantitative version of the result that the sum of the reciprocals of the prime numbers is divergent. This theorem is not as difficult to prove. Here is a possible proof strategy: we know that each prime \\( p \leq n \\) occurs in the prime factorization of \\( n! \\) "roughly" \\( n/p \\) times. So we have a rough approximation

\\[ \log(n!) \approx \sum_{p \leq n} \frac{n \log p}{p} \\]

where the sum over \\( p \\) runs over prime numbers - I'll stick to this convention for the rest of the post for the sake of brevity. On the other hand, Stirling's theorem gives the approximation \\( \log(n!) = n \log n - n + O(\log n) \\). Combining these immediately gives 

\\[ B(n) = \sum_{p \leq n} \frac{\log p}{p} = \log n + O(1) \\]

once we take account of the error term in this approximation.

To pass from this to the sum we care about, we employ [partial summation](https://en.wikipedia.org/wiki/Summation_by_parts). This is the discrete analog of integration by parts. The idea is to write the sum we care about as

\\[ A(x) = \sum_{2 \leq n \leq x} \frac{\textbf 1_{\textrm{prime}}(n) \log n}{n \log n} = \sum_{2 \leq n \leq x} \frac{B(n) - B(n-1)}{\log n} \\]

where \\( \textbf 1_{\textrm{prime}} \\) is the indicator function of the primes, and then use summation by parts, which allows us to shift the "differencing operator" from acting on \\( B \\) to acting on \\( 1/\log n \\). Approximating the first difference of \\( 1/\log n \\) by its derivative gives us the main term

\\[ \approx \sum_{2 \leq n \leq x} \frac{A(n)}{n \log^2(n)} \\]

which we can now approximate using the result \\( A(n) = \log n + O(1) \\) from above as

\\[ \approx \sum_{2 \leq n \leq x} \frac{1}{n \log(n)} \approx \log \log x \\]

where the last approximation follows from replacing the sum by an integral and noting that \\( d (\log \log x)/dx = (x \log x)^{-1} \\). If we carefully keep track of all the error terms in the approximations, we again recover the precise result that \\( A(x) = \log \log x + O(1) \\).

That's all the background knowledge we'll need for the post. Moving on:

## Heuristics

Let's think about why we might *expect* Brun's theorem to be true.

We know from the prime number theorem that the density of the primes around \\( n \\) is \\( \sim 1/\log n \\), so if we cheat and assume that the events of \\( n \\) being prime and \\( n+2 \\) being prime are "independent", the density of the twin primes should be roughly \\( 1/\log^2 (n) \\). So we should expect

\\[ \pi_2(N) \sim \frac{N}{\log^2 (N)} \\]

where \\( \pi_2(N) \\) counts the twin primes less than or equal to \\( N \\). If so, we can simply turn the crank of partial summation as we did before: letting \\( P_2 \\) be the set of twin primes and \\( \textbf 1_{P_2} \\) its indicator function, we compute

\\[ \sum_{p \in P_2, p \leq x} \frac{1}{p} = \sum_{2 \leq n \leq x} \frac{\textbf 1_{P_2}(n)}{n} \approx \sum_{2 \leq n \leq x} \frac{n}{n^2 \log^2(n)} = \sum_{2 \leq n \leq x} \frac{1}{n \log^2(n)} \\]

This final sum is convergent. One way to see it is by an integral test: if we consider the integral

\\[ \int_{2}^{\infty} \frac{dx}{x \log^2(x)} \\]

then the substitution \\( x = e^u, \, dx = e^u \, du \\) turns the integral into

\\[ \int_{\log 2}^{\infty} \frac{du}{u^2} \\]

which is obviously finite. So the initial sum is convergent as well.

This heuristic suggests that Brun's theorem should be true if there is "nothing wrong": if there's no unexpected correlation between the events of \\( n \\) being prime and \\( n+2 \\) being prime. The task of the proof is therefore to show that this correlation can't get bad enough to make this sum divergent.

One key insight is that we actually have some substantial amount of room to work with in this argument: we don't actually need to get anywhere close to \\( \pi_2(N) \sim N/\log ^2 N \\). If we could show, for instance, that

\\[ \pi_2(N) \leq \frac{N}{\log^{1.5}(N)} \\]

for all sufficiently large \\( N \\), which is a much weaker bound than what the heuristic suggests; we would still prove that the sum we care about is convergent. This fact is what makes Brun's theorem a relatively "easy" result: we don't actually need to show that there's strictly *no* correlation between \\( n \\) being prime and \\( n+2 \\) being prime, just that the correlation doesn't get too bad.

## The sieve of Eratosthenes

Now that we understand what we have to do, it's time to think about concrete proof strategies. Our goal is to prove a "nontrivial" upper bound on the twin prime counting function \\( \pi_2(N) \\). We know that the key objective is to bound the dependence of the events of \\( n \\) being prime and \\( n+2 \\) being prime. We need to make something work with these ingredients.

Given that our goal is to prove an *upper bound*, we'll still have succeeded if we find a set \\( S \\) that *contains* the twin primes and for which we can prove a similar result, as if we have an upper bound on \\( |S \cap \{1, 2, \ldots, x \}| \\) we'll obviously have an upper bound on \\( \pi_2(x) \\) as well. This suggests one immediate way to attempt a proof: the sieve of Eratosthenes.

The sieve of Eratosthenes characterizes the primes by the following: a prime is a number that's either not divisible by, or gives \\( 1 \\) when divided by, \\( 2, 3, 5, 7, 11, 13, \ldots \\). In other words, we obtain a sequence of sets \\( S_k \\) which consists of the integers not divisible by the first \\( k \\) primes (except the primes themselves), and as \\( k \\) grows this gives us an increasingly refined superset of the prime numbers. It's called a sieve because we're gradually "sieving out" composite numbers from the integers.

To apply this to our problem, we need to modify the sieve a little bit, as we care about twin primes. Instead of sieving out integers that are \\( 0 \pmod{p} \\) for each prime \\( p \\), we'll sieve out integers that are *either* \\( 0 \pmod{p} \\) *or* \\( 2 \pmod{p} \\). This is because the bigger prime in a tuple of twin primes can't belong to either of these congruence classes when \\( p \\) is chosen to be smaller than the numbers in the tuple.

Now, we know that for a prime \\( p \\), the number of natural numbers less than \\( n \\) in *any* residue class mod \\( p \\) is equal to \\( n/p + O(1) \\). This approximation is quite good when \\( p \\) is much less than \\( n \\). So we can roughly think that applying the sieve for a prime \\( p \\) removes roughly a fraction \\( 2/p \\) of the remaining integers for odd primes, and \\( 1/2 \\) for the prime \\( 2 \\). For this to work nicely it's also more convenient to sieve out by the entire congruence class, so we don't make an exception for the case when we're sieving out a twin prime as we would with the traditional sieve of Eratosthenes.

Given the above condition, we can't sieve by all primes up to \\( x \\) as obviously this will leave us with nothing left, but if we sieve by the primes up to \\( \sqrt{x} \\), we'll only have removed at most \\( \sqrt{x} \\) twin primes, which is not going to be important as the upper bound we want to prove is much bigger than \\( \sqrt{x} \\). So we can ideally imagine to get a result like

\\[ \frac{\pi_2(x)}{x} \leq \prod_{p \leq \sqrt{x}} \left(1 - \frac{2}{p} \right) \approx \exp \left(-\sum_{p \leq \sqrt{x}} \frac{2}{p} \right) = O \left( \frac{1}{\log^2 x} \right) \\]

where the approximation uses the fact that \\( \exp(-x) \approx 1-x \\) when \\( x \\) is small and the last equality uses the Mertens' theorems. Note that \\( p \\) here runs over the odd primes. If we could prove this, we would be very happy.

However, in fact we can't prove this, and the reason is that we're again assuming that the events we're dealing with are independent. If we could *prove* the independence in some formal sense, the argument would be fine, but in fact this turns out to be very difficult to prove when we're taking all primes up to \\( \sqrt{x} \\) in the product expansion.

The reason is that the fundamental result about patching together different congruence classes modulo different primes, [the Chinese remainder theorem](https://en.wikipedia.org/wiki/Chinese_remainder_theorem), only allows us to control the number of solutions \\( \leq N \\) to a system of congruences over different prime moduli when the product of those primes is substantially less than \\( N \\): concretely, it means the exact asymptotic we can give on the above expression is only

\\[ \frac{\pi_2(x)}{x} \leq \prod_{p \leq \sqrt{x}} \left(1 - \frac{2}{p} \right) + O \left( \frac{\prod_{p \leq \sqrt{x}} p}{x} \right) \\]

 It turns out that another implication of the prime number theorem is that

\\[ \sum_{p \leq x, \, p \, \text{prime}} \log(p) = x + o(x) \\]

so we expect the product of all primes up to \\( x \\) to behave like the exponential of \\( x \\). When we apply this to the primes less than \\( \sqrt{x} \\), we get that the product is \\( \approx e^{\sqrt x} \\), which is much larger than \\( x \\) asymptotically. So the error term becomes much larger than the main term and the approximation fails.

The best we can hope out of this is to take \\( p \\) up to \\( (\log x)/2 \\) or so. This is sometimes called the Legendre sieve, and gives us the bound

\\[ \frac{\pi_2(x)}{x} \leq \prod_{p \leq (\log x)/2} \left(1 - \frac{2}{p} \right) + O(x^{-1/2}) \approx \exp \left(-\sum_{p \leq (\log x)/2} \frac{2}{p} \right) = O \left( \frac{1}{(\log \log x)^2} \right) \\]

which turns out to be too weak to do what we want. Indeed, this is even weaker than the prime number theorem bound \\( \pi_2(N) \ll \pi(N) \approx N/\log N \\) and we know the reciprocals of the prime numbers have a divergent sum, so the Legendre sieve gets us nothing in this situation: it's far too weak.

We obviously need to be more clever. However, this shouldn't be *too* hard: intuitively, it should be possible for us to go past the \\( \log x \\) threshold, because it's not that we don't understand *anything* about primes that are bigger than \\( \log x \\). The only information we don't have is about the joint correlations that involve a very large number of primes, and we understand the low order correlations very well even at thresholds much bigger than \\( \log x \\), because those only involve the product of a small number of primes. For instance, up to \\( x^{1/10} \\) we understand all correlations that only involve \\( 10 \\) or less primes very well from the same Chinese remainder theorem bound.

This suggests the following proof strategy: since higher order correlations are causing all the problems, we should try to find some way to decompose the quantity we want to calculate into "lower order" and "higher order" terms, and then see if we can bound the "higher order" part well enough that we can get some nontrivial result.

## The Brun sieve

The Brun sieve is essentially an implementation of the vague idea discussed above.

Let's say that event \\( E_p \\) for a prime \\( p \\) is defined as above: it's the event that a natural number in \\( [1, x] \\) is either \\( 0 \\) or \\( 2 \\) modulo \\( p \\). Then, we can see our above computation as noting that, ignoring the primes less than \\( \sqrt x \\) (which pose no problems, as discussed above), we have

\\[ \pi_2(x) \ll \left| \bigcap_{p \leq \sqrt x} (E_p)^c \right| \\]

where the exponent \\( c \\) denotes set complements. If we assume the events are actually independent, we can do a computation that looks like

\\[ \frac{\pi_2(x)}{x} \ll \mathbb P \left( \bigcap_{p \leq \sqrt x} (E_p)^c \right) = \prod_{p \leq \sqrt x} \mathbb (1 - \mathbb P(E_p)) \\]

and since we know \\( \mathbb P(E_p) = 2/p + O(x^{-1/2}) \\) we would be able to do the same proof we did above. However, as we know, the independence assumption is actually wrong, so this doesn't really work.

However, there is an *unconditional* way to simplify expressions of this kind that doesn't assume independence: to use [inclusion-exclusion](https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle). This looks like

\\[ \mathbb P \left( \bigcap_{p \leq \sqrt x} (E_p)^c \right) = 1 - \mathbb P\left( \bigcup_{p \leq \sqrt x} E_p \right) = 1 - \sum_{p \leq \sqrt x} \mathbb P(E_p) + \sum_{p_1, p_2 \leq \sqrt{x}, p_1 < p_2} \mathbb P(E_{p_1} \cap E_{p_2}) - \ldots \\]

Overall it's a big mess, but the hope is that this is exactly what we need to use our better understanding of the low order correlations between the congruence classes of different primes: inclusion-exclusion gives us a series expansion for the quantity we want that's increasing in the complexity of the correlations we want to understand!

So our hope might be the following: we truncate this sum at some threshold \\( t \\), as in, we truncate at the term that involves the correlations of \\( t \\) primes. We pick \\( t \\) to be even, as inclusion-exclusion has the property that the partial sums are always upper bounds for even \\( t \\) and lower bounds for odd \\( t \\). We then try to understand this truncated sum better.

The square root is not ideal for this, as we already don't understand three prime correlations when we work with the square root. So let's also take the threshold at which we cut off the primes as a free parameter \\( m \\). In the above, we were using \\( m = \sqrt x \\), but we want to generalize this now.

One immediate observation is that if we cut the sum off at \\( m \\), we only have a good understanding of correlations that involve at most \\( \log x/\log m \\) primes. To be safe, let's truncate the sum at (or choose \\( t \\) to be) the even number closest to \\( c \log x/\log m \\) where \\( c \ll 1 \\) is a small number that we'll fix later. In this case, the Chinese remainder approximations will be very good, and we don't have to worry about the failure of independence anymore. (I'll equivocate between \\( t \\) the even integer and \\( t \\) the real number \\( c \log x/\log m \\) sometimes - rest assured that this causes no problems for the argument and everything goes into the error terms.) So we'll get the sum

\\[ \approx 1 - \sum_{p \leq m} \frac{2}{p} + \sum_{p_1 < p_2 \leq m} \frac{4}{p_1 p_2} - \sum_{p_1 < p_2 < p_3 \leq m} \frac{8}{p_1 p_2 p_3} - \ldots + \sum_{p_1 < p_2 < \ldots < p_t \leq m} \frac{2^t}{p_1 p_2 \ldots p_t} \\]

after truncation. It's important to keep track of the error term of this properly, so we can note that the error we make whenever we approximate the probability of an intersection with an independence assumption is \\( O(x^{c-1}) \\): this is because each correlation involves at most \\( t \\) primes and each prime is \\( \leq m \\), so the product is bounded from above by \\( m^t \\) which is equal to \\( x^c \\) given our choice of \\( t \\). Since when \\( t < m \\) the number of such approximations we have to make is bounded from above by \\( t \cdot C(m, t) < m^t = x^c \\), we're safe as long as \\( c \\) is small. For instance, \\( c \leq 1/3 \\) will be enough for the error \\( O(x^{2c - 1}) \\) to be negligible relative to the main term here.

Now that we know the error term is negligible, so long as \\( c \\) is small, let's think about approximating this sum. Bounding from above with the triangle inequality gives very poor results here because the sum has a lot of cancellation. However, we can use a trick: we know that the *untruncated sum*, i.e. the sum for \\( t = \infty \\), is equal to the infinite product

\\[ \prod_{p \leq m} \left( 1 - \frac{2}{p} \right) \\]

This is the expression we were working with earlier when assuming independence, so it makes sense to bring it back here. Instead of bounding the sum above directly, we'll write it as this "main term" plus an error term that comes from the "high order correlations" when the number of primes exceeds \\( t = c \log x/\log m \\). This fits in naturally with our proof strategy: try to bound the contribution coming from higher order correlations.

This will give us

\\[ = \prod_{p \leq m} \left( 1 - \frac{2}{p} \right) + \sum_{p_1 < p_2 < \ldots < p_{t+1} \leq m} \frac{2^{t+1}}{p_1 p_2 \ldots p_{t+1}} - \sum_{p_1 < p_2 < \ldots < p_{t+2} \leq m} \frac{2^{t+2}}{p_1 p_2 \ldots p_{t+2}} + \ldots \\]

The triangle inequality works much better on the error term now. Indeed, it's fairly straightforward to see that if we recall the definition

\\[ A(m) = \sum_{p \leq m} \frac{1}{p} \\]

then the "error term", as in, all terms aside from the product term here, can be bounded in absolute value by

\\[ \sum_{k=t+1}^{\infty} \frac{2^k A(m)^k}{k!} \\]

This is an elementary exericse and I leave it to the reader to see why it's true. Taking this for granted, though, we can approximate this sum up to a constant by just its first term if \\( t \gg A(m) \approx \log \log m \\), as the rest of the series can be bounded from above by a geometric series in the parameter \\( A(m)/t \\) which converges quickly when the above condition is true. So as long as we pick \\( t \gg \log \log m \\) asymptotically, say if \\( t \\) is always greater than \\( D \log \log m \\) for some \\( D > 2 \\), we can approximate this sum by just its first term, which is roughly

\\[ \frac{2^{t} (\log \log m)^{t}}{t!} \\]

This is not exactly right but the difference doesn't affect the proof - it just goes into the error term, as usual. The main term is also easy to approximate: we use the old idea of writing it as

\\[ \prod_{p \leq m} \left( 1 - \frac{2}{p} \right) = O \left(\exp \left( -\sum_{p \leq m}\frac{2}{p} \right) \right) = O \left( \frac{1}{\log^2 m} \right) \\]

using the Mertens theorems.

## Finishing the proof

Now we're almost at the end of the proof. We have a bound of the form

\\[ O \left( \frac{1}{\log^2 m} \right) + O \left( \frac{2^{t} (\log \log m)^{t}}{t!} \right) \\]

We can use Stirling's formula to replace the factorial and end up with some bound of the form

\\[ \frac{1}{\log^2 m} + \left(\frac{2e \log \log m}{t} \right)^t \\]

where I've dropped the big O notation for simplicity, as this bound is not exactly right even up to a constant. However, the differences it has with a bound that works are inconsequential, so I'll illustrate the rest of this argument on this bound.

What can we pick for \\( m \\)? We know that we can't pick it to be a constant, which would be \\( x^{1/\log x} \\), and we can't pick it to be \\( x \\). As a compromise, let's try something of the form \\( m = x^{1/(g \log \log x)} \\) for some constant \\( g > 0 \\). With this expression, we'll get

\\[ t = \frac{c \log x}{\log m} = gc \log \log x \\]

Now, let's work out what this says. Our bound becomes, to top order,

\\[ \frac{g^2 (\log \log x)^2}{\log^2 x} + \left(\frac{2e}{gc} \right)^{gc \log \log x} \\]

Now, we need to make the choices of the constants. \\( c \\) is small, so we need to pick \\( g \\) big enough such that \\( t \gg \log \log x > 2 \log \log m \\) for our approximations to have negligible error terms. Choosing \\( g \gg 2/c \\) is good enough for this purpose. At the same time, we need to ensure that \\( gc \gg 2e \\), as otherwise the error term will dominate the main term here. Specifically, we have

\\[ \left(\frac{2e}{gc} \right)^{gc \log \log x} = \exp( \log(2e/gc) \cdot gc \cdot \log \log x) = (\log x)^{gc \log(2e/gc)} \\]

and we want the main term to dominate, so we want the exponent here to be less than \\( -2 \\). If we choose \\( c = 1/3 \\) and \\( g = 27 \\), it's easy to check that both of these conditions hold. So everything checks out, and we're finally able to recover a bound of the form

\\[ \pi_2(x) \leq O \left( \frac{x (\log \log x)^2}{\log^2 x} \right) \\]

Is this good enough? Yes! Earlier, we had said that even a bound of the form

\\[ \pi_2(x) \ll \frac{x}{\log^{1.5}(x)} \\]

would've been good enough, and this bound is far tighter. It's not even \\( (\log x)^{\varepsilon} \\) from the independence bound, which is in fact quite impressive.

## Conclusion

The fundamental nature of the above proof is that we understand the behavior of primes relative to modular congruence classes quite well, at least when the modulus is small compared to the primes in question. We then try to leverage our understanding here into saying something about the distribution of primes in other sets.

This is the heart of sieve theory: it's used in cases when you understand the behavior of some objects well in some substructures and you want to "piece that together" to gain a broader understanding of how the objects behave. Here, the substructures are arithmetic progressions or modular congruence classes, and the objects are the prime numbers, but the fundamental ideas of sieve theory may be applied in other domains as well.

The Brun sieve itself is a fairly basic sieve that was introduced over a hundred years ago for the exact purpose of proving tight upper bounds on \\( \pi_2(x) \\), though it can also be used for other purposes as a general way to prove upper bounds on various quantities involving primes using combinatorial methods. Today, more sophisticated sieves are able to prove nontrivial lower bounds as well, which is often a more tricky problem than proving upper bounds in sieve theory.

For more on sieve theory, you might want to check out the [associated tag](https://terrytao.wordpress.com/tag/sieve-theory/) on Terence Tao's blog. It contains material ranging from introductory to research-level in sophistication.