Prime Density and the Chinese Remainder Theorem


Prime Density and the Chinese Remainder Theorem

Hope
Let’s pick up where we left off. We briefly revisited the Chinese remainder theorem: for a system of congruences in one variable, with pairwise coprime moduli, there is a unique solution modulo the product of those moduli. Now we’re going to finish a different question: how many primes are there among the numbers from 1 to x?

Michael
We write that count as pi of x. The estimate we’re aiming for is that pi of x is on the scale of x divided by log x. In this discussion, log means logarithm base 2. More specifically, the lecture’s target is an upper bound of 5x over log x and a lower bound of about x over log x, up to a small additive error.

Hope
So the fraction of numbers up to x that are prime is roughly pi of x divided by x, and that fraction is on the scale of 1 over log x. Gauss famously noticed this pattern by studying tables of primes in the nineteenth century. He conjectured the relationship from the data before it was proved.

Michael
And the point here isn’t to give the full historical proof of the prime number theorem. We’re building an elementary estimate that is useful in computer science, including applications involving cryptography and coding theory. To get there, we need three lemmas.

Hope
Lemma one is about the central binomial coefficient: the quantity '2n choose n' is at least 4 to the n divided by 2n plus 1. Why should that be true? The sum of all the binomial coefficients in row 2n is 4 to the n, and there are 2n plus 1 terms in that row.

Michael
The middle term, '2n choose n', is the largest term in the row. A largest term must be at least the average, so it’s at least the total, 4 to the n, divided by the number of terms, 2n plus 1. That gives the inequality.

Hope
The second lemma says that in the prime factorization of '2n choose n', each complete prime power, p to the exponent v, is at most 2n. The binomial coefficient itself can be enormous, but no individual prime power in its factorization can be larger than 2n.

Michael
One clean way to see the bound is to use the formula for the exponent of p in a factorial. In the exponent of p in '2n choose n', each contribution has the form floor of 2n over p to the i, minus twice floor of n over p to the i. Each such contribution is either zero or one, and it can only be nonzero while p to the i is at most 2n.

Hope
So there are at most floor of log base p of 2n nonzero contributions. That means the exponent v is at most that number, and therefore p to the v is at most 2n. That is the useful conclusion: every prime-power factor is individually small.

Michael
The third lemma bounds the product of all primes up to n: that product is at most 4 to the n. It’s stronger than merely saying there are at most n primes, and it’s what will let us turn a bound on a product into a bound on the number of primes.

Hope
Let’s prove it by induction. Start with an odd number, written as 2m plus 1. Split the primes up to 2m plus 1 into two groups: primes at most m plus 1, and primes strictly larger than m plus 1.

Michael
By induction, the product in the first group is at most 4 to the power m plus 1. For a prime in the second group, it’s larger than m, so it does not divide m factorial. But it does divide one of the numerator factors from m plus 1 through 2m plus 1 in the expression for '2m plus 1 choose m'.

Hope
Because those primes are distinct, their product divides that binomial coefficient, and so their product is at most '2m plus 1 choose m'. The two middle coefficients in row 2m plus 1 are equal and are the largest. Since the whole row sums to 2 to the power 2m plus 1, either middle coefficient is at most half that total: 2 to the power 2m, or 4 to the power m.

Michael
Multiplying the two group bounds gives 4 to the power m plus 1 times 4 to the power m, which is 4 to the power 2m plus 1. That completes the odd case. For an even number, 2m, the prime product is twice the product up to 2m minus 1, because the only even prime is 2. The induction bound gives at most 2 times 4 to the power 2m minus 1, which is no more than 4 to the power 2m.

Hope
Now we can prove the upper bound on pi of x. Look just at the primes greater than the square root of x and at most x. There are pi of x minus pi of the square root of x of them, and each is greater than the square root of x.

Michael
Their product is therefore at least the square root of x raised to that count. But it is no larger than the product of all primes up to x, which Lemma three bounds by 4 to the x. So we have square root of x to the power pi of x minus pi of square root x, at most 4 to the x.

Hope
Take base-2 logarithms. Since log of the square root of x is half of log x, the result is that pi of x minus pi of the square root of x is at most 4x divided by log x. And pi of the square root of x is at most the number of integers in that range, namely the square root of x.

Michael
So pi of x is at most 4x over log x plus the square root of x. For x at least 2, the square root term is at most x over log x, giving the stated upper bound: pi of x is at most 5x over log x.

Hope
For the lower bound, we use Lemmas one and two. Lemma one tells us that '2n choose n' is at least 4 to the n divided by 2n plus 1. Meanwhile, by Lemma two, each prime-power factor in its prime factorization is at most 2n.

Michael
There are at most pi of 2n distinct primes that can occur in that factorization. So the whole binomial coefficient is at most 2n raised to pi of 2n. Combining the lower and upper estimates gives 4 to the n over 2n plus 1, at most, 2n to the power pi of 2n.

Hope
Taking base-2 logarithms and rearranging gives pi of 2n at least the quantity 2n minus log of 2n plus 1, divided by log of 2n. The subtracted logarithm is a small correction compared with 2n, so this is essentially 2n over log of 2n, with a small additive loss.

Michael
For a general x, choose an even number just below it. The prime count up to x is at least the count up to that even number, and the two arguments differ by less than 2. Substituting that nearby even number gives the same kind of lower estimate: x over log x, up to a small additive error.

Hope
Together, the estimates say that the number of primes up to x grows roughly like x over log x. Dividing by x, the proportion of primes is roughly 1 over log x. The bounds are not an exact formula, but they give the right scale and are useful for estimating how many primes are available.

Michael
They also tell us something about intervals. For sufficiently large x, the lower estimate for pi of 6x and the upper estimate for pi of x can be compared to show that there are primes between x and 6x. For small values, you can check the remaining cases directly.

Hope
It’s important not to confuse that with a claim about a fixed-width interval. You cannot expect a prime between x and x plus 10 for every x, or even between x and x plus some fixed billion. Prime gaps can be long, so a fixed additive width cannot guarantee a prime.

Michael
A stronger famous result is Bertrand’s postulate, proved by Chebyshev: for every integer n greater than 1, there is a prime between n and 2n. That interval grows in proportion to n, unlike an interval of fixed width.

Hope
And there’s a much tighter famous conjecture that remains open: between every pair of consecutive squares, n squared and the next square, there should be a prime. The gap between those squares is only 2n plus 1, which is small compared with the size n squared. Bertrand’s result doesn’t reach that short an interval.

Michael
That gives us a bridge to cryptography, one of the applications of number theory. Cryptography is about letting two parties communicate over a channel that an adversary may observe, while preventing the adversary from understanding the message.

Hope
There are two separate concerns in transmitting information. First, the channel or storage medium can introduce errors, so we need error-correcting methods. Second, even if the message arrives without errors, we may want to keep an observer from reading it. Today we’re focusing on secrecy, not error correction.

Michael
The standard characters are Alice, who wants to send a message; Bob, who receives it; and Eve, the adversary. The channel could be a wire, a radio signal, a message passed by hand, or stored data. The essential assumption is that Eve can observe the communication.

Hope
Alice’s original message is called the plaintext, usually written M. We can represent it as a string of bits, since text and symbols can be encoded in binary. An encryption algorithm uses a shared secret S together with M to produce a ciphertext C.

Michael
Bob receives C and applies a decryption algorithm using the secret S to recover M. For the system to be useful, decryption must recover the intended message uniquely. The hope is that Eve can see C but, without the secret, cannot feasibly work out the plaintext.

Hope
The simplest historical example is a substitution cipher. Choose a permutation of the alphabet as the secret key, and replace every letter by its partner under that permutation. For a cyclic shift, A becomes B, B becomes C, and so on, with Z wrapping around to A.

Michael
Encryption applies the permutation to the message. Decryption applies the inverse permutation, which takes each letter back to its original one. That does recover the message exactly, provided Alice and Bob share the same permutation.

Hope
But exact recovery is not the same as security. For an arbitrary substitution on 26 letters, there are 26 factorial possible permutations, which sounds like a huge search space. A cyclic Caesar shift is a much simpler special case with far fewer possibilities.

Michael
The bigger problem is that meaningful language is not a sequence of completely arbitrary letters. In English, some letters occur much more often than others. Eve can inspect ciphertexts, look for frequent symbols and patterns, and compare those statistics with what is typical in English.

Hope
With enough meaningful messages, those frequency clues can help Eve infer which plaintext letters correspond to which ciphertext letters. Context and knowledge of how language works can help fill in the rest. So even a large key space does not save a cipher if the messages themselves reveal exploitable patterns.

Michael
That’s why the substitution cipher is considered broken: it may look difficult to attack by trying every key, but frequency analysis can expose the secret in practice. Modern cryptography uses more sophisticated methods, including ideas from number theory and algebra.

Hope
So the path today ran from counting primes to why number theory matters in applications. We built a prime-count estimate from three lemmas, saw what it can and cannot tell us about prime gaps, and then set up the basic language of encryption: plaintext, ciphertext, a secret key, and the limits of simple substitution.

Ask anything
We've read this whole study set, so questions about it get the best answers.