6

Why do prime numbers make these spirals?

 1 year ago
source link: https://www.3blue1brown.com/lessons/prime-spirals
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Why Do Prime Numbers Make These Spirals?

I've had people ask me before why it is that mathematicians care so much about prime numbers. The role they play in math is similar to the role atoms play in chemistry. They're the fundamental building blocks of the integers, at least when multiplication is involved, and quite often solving some problem can be reduced to first solving it for primes. But honestly, a big part of why mathematicians care so much about primes is that they're hard to understand. Math is riddled with unsolved problems about primes, so for personality types who are drawn to difficult puzzles, prime numbers have a certain allure that's almost independent of the practical importance they have in math and related fields, like cryptography.

Part of the beauty of mathematics is how two seemingly unrelated concepts can be interconnected through an arbitrary choice. I first saw this pattern in a question on the Math Stack Exchange. It was asked by a user under the name dwymark, and answered by Greg Martin, and it relates to the distribution of prime numbers, as well as rational approximations for π\piπ.

ppp_writing.png

This user had been playing around with plotting data in polar coordinates. As a quick reminder, this means labeling points in 2D space, not with the usual xyxyxy-coordinates, but instead with a distance from the origin, commonly called rrr for radius, together with the angle that line makes with the horizontal, commonly called theta, θ\thetaθ.

polar_coords.png

The angle is typically given in radians; that means an angle of π\piπ is halfway around, and 2π2\pi2π gives a full circle. Notice, polar coordinates are not unique, in the sense that adding 2π2\pi2π to the angle doesn't change the location.

Which other point in polar coordinates does this point not equal? (3,7π2)(3, \frac{7\pi}{2})(3,27π​)

(3,3π2)(3, \frac{3\pi}{2})(3,23π​)

(−3,5π2)(-3, \frac{5\pi}{2})(−3,25π​)

(3,−π2)(3, -\frac{\pi}{2})(3,−2π​)

(3,5π2)(3, \frac{5\pi}{2})(3,25π​)

The pattern we'll look at centers around plotting points where both these coordinates are a given prime number.

(r,θ)=(p,p)(r,\theta)=(p,p)(r,θ)=(p,p)
plotting_primes.png

There's no practical reason to do this. It's just fun! We're frolicking in the playground of data visualization. Let's get a feel for this with all whole numbers, rather than just primes. The point (1,1)(1, 1)(1,1) sits a distance 1 away from the origin, with an angle of 1 radian. (2,2)(2, 2)(2,2) has twice the angle, and twice the distance. Similarly, to get to (3,3)(3, 3)(3,3), you rotate one more radian, with a total angle now slightly less than π\piπ, and you step one unit farther from the origin.

integer_plot.png

Make sure it's clear what's being plotted, because everything that follows depends on understanding it. Each step forward is like the tip of a clock hand which rotates 1 radian, a little less than 16\frac1661​ of a turn, and grows longer by 1 unit. As you continue, these points spiral outward, forming what's known in the business as an "Archimedean spiral".

archimedean_spiral.png

If you knock out everything except the prime numbers, it initially looks quite random. After all, primes are famous for their chaotic and difficult-to-predict behavior. But when you zoom out, you see these very clear galactic seeming spirals. What's weird is that some of the arms seem to be missing.

galactic_zoomout.png

Zooming out even farther, those spirals give way to a different pattern: these many different outward rays. Those rays seem to come mostly in clumps of 4, but with an occasional gap here and there, like a comb missing some teeth

galactic_comb.png

The question, naturally, is what on Earth is going on here? Where do these spirals come from, and why do we instead get straight lines at a larger scale? You could be more quantitative and count that there are 20 spirals, and up at the larger scale if you patiently went through each ray you'd count a total of 280. But of course, this just raises further questions on where these numbers come from, and why they'd arise from primes.

ray_count.png

These patterns are certainly beautiful, but they don't have a hidden, divine message about primes. I should say upfront, the fact the math exchange question jumped right into primes makes the puzzle a bit misleading. If you look at all the whole numbers, not just the primes, you see very similar spirals.

integer_rays.png

They're much cleaner, and there are now 44 of them, but it means the question of where the spirals come from is, perhaps disappointingly, completely separate from what happens when we limit our view to primes.

Before you get too disappointed, the question of why we see spirals at all is still a great puzzle. And even if primes don't cause the spirals, asking what goes on when you filter for primes does lead you to one of the most important theorems on the distribution of prime numbers, known as Dirichlet's theorem.

Ingredients for a Spiral PI

To start, did you notice that at a much smaller scale there were 6 little spirals? This offers a good starting point to explain what's happening in the two larger patterns. Notice how all the multiples of 6 form one of the arms of this spiral. Then the next one is every number one above a multiple of 6, and the one after that includes all numbers two above a multiple of 6, and so on. Why is that?

6_spirals.png

Remember, each step forward in the sequence involves a turn of one radian, so when you count up by 6, you've turned a total of 6 radians, which is a little less than 2π2\pi2π, a full turn. So every time you count up 6, you've almost made a full turn, it's just a little less. Another six steps, a slightly smaller angle, six more, smaller still, and so on, with this angle changing gently enough to give the illusion of a single curving line.

6_steps.png

If you limit the view to prime numbers, all but two of these spiral arms go away. Think about it… a prime number can't be a multiple of 6. It also can't be 2 above a multiple of 6, unless it's 2, nor can it be 4 above a multiple of 6, since all those are even numbers. It also can't be 3 above a multiple of 6 (unless it's the number 3 itself) since all those numbers are divisible by 3.

6_prime_spiral.png

While we're in this simpler context, let me introduce some terminology that mathematicians use. Each of these sequences where you're counting up by 6 is called a "residue class, mod 6". The word "residue" in this context is a fancy way of saying "remainder", and mod means something like "from division by". The label "residue class mod 6" means "a set of remainders from division by 6."

residue_class_mod_6.png

For example, 6 goes into 20 three times, with a remainder of 2, so 20 has a "residue of 2 mod 6".

6_mod_20.png

Together with all other numbers leaving a remainder of 2 when the thing you divide by is 6, you have a full "residue class". I know that sounds like the world's most pretentious way of saying "everything 2 above a multiple of 6", and it is! But this is the standard jargon, and it is handy to have some words for the idea.

2nd Residue class mod 66k+2:{2,8,14,20,26,32,38,…}\text{2nd Residue class mod 6} \\ 6k+2: \{2,8,14,20,26,32,38,\dots \}2nd Residue class mod 66k+2:{2,8,14,20,26,32,38,…}

So in the lingo, each of these spiral arms corresponds to a residue class mod 6, and the reason we see them is that 6 is close to 2π2\pi2π; turning 6 radians is almost a full turn. And the reason we only see two of them when filtering for primes is that all prime numbers are either 1 or 5 above a multiple of 6 (with the exceptions of 2 and 3). The other four residue classes hold numbers which are either even or divisible by 3.

Which residue class mod 6 does the number 381 belong to?

6k+06k+06k+0

6k+16k+16k+1

6k+26k+26k+2

6k+36k+36k+3

6k+46k+46k+4

6k+56k+56k+5

With that as a warmup, let's think about the larger scale patterns. In the same way that 6 steps were close to a full turn, taking 44 steps is very close to a whole number of turns.

44_spiral.png

Since there are 2π2\pi2π radians per rotation, taking 44 steps gives a total of 442π\frac{44}{2\pi}2π44​ rotations, which comes out to be just barely above 7 full turns. You could also write this by saying 447\frac{44}{7}744​ is a close approximation for 2π2\pi2π, which some of you may better recognize as the famous 227\frac{22}{7}722​ approximation for π\piπ.

44_7_approx.png

So if you count by multiples of 44 in the diagram, each point has almost the same angle as the last, just a little bit bigger, so as you continue on with more and more we get this gentle spiral as that angle increases very slowly. All of the numbers 1 above a multiple of 44 make a similar spiral, but rotated one radian counterclockwise. Same for everything 2 above a multiple of 44, and so on.

44_residue_arm.png

To phrase it with the fancier language, each of these spiral arms is a residue class mod 44. And maybe now you can tell me what happens when we limit the view to prime numbers.

Prime numbers cannot be a multiple of 44, so that arm won't be visible. Similarly, you won't see primes 2 above a multiple of 44, or 4 above, and so on, since all those residue classes have nothing but even numbers.

prime_spiral_numbered_circle.png

Of the residual classes mod 44, only 20 arms are visible when filtering for primes.

Likewise, any multiple of 11 can't be prime, except for 11 itself, so the spiral of numbers 11 above a multiple of 44 won't be visible, and neither will the spiral of number 33 above a multiple of 44. Each spiral we're left with is a residue class that doesn't share any factors with 44. Within each of these spiral arms that we can't reject out of hand, the primes seem to be somewhat randomly distributed, a fact I'd like you to tuck away for later.

This is another good chance for a side note on jargon mathematicians use. What we care about here are all the numbers between 0 and 43 that don't share any prime factors with 44, right? The ones which aren't even, and aren't divisible by 11. Two numbers that don't share any factors like this are called "relatively prime", or "coprime".

coprime_definition.png

You can count that there are 20 numbers between 1 and 44 coprime to 44, a fact that a number theorist would compactly write as:

ϕ(44)=20\phi(44)=20ϕ(44)=20

The greek letter phi, ϕ\phiϕ, here refers to "Euler's totient function" (yet another needlessly fancy word). It is defined to be the number of integers from 1 up to NNN which are coprime to NNN. More obscurely, these numbers are sometimes called the "totatives" of NNN.

In short, what the user on math exchange was seeing are two unrelated pieces of number theory illustrated in one drawing: The first is that 447\frac{44}{7}744​ is a close rational approximation to 2π2\pi2π, which results in residue classes mod 44 being cleanly separated out. The second is that many of these residue classes contain either 0 or 1 primes, so won't show up, while primes do show up plentifully enough in the remaining 20 residue classes to make these spiral arms visible.

Perhaps now you can predict what's going on at a larger scale. Just as 6 radians is vaguely close to a full turn, and 44 radians is quite close to 7 full turns, it so happens that 710 radians is extremely close to a whole number of turns.

Specifically, 710 radians is 7102π\frac{710}{2\pi}2π710​ rotations, which works out to be 113 point zero zero zero zero zero nine. This is the same thing as saying that 710113\frac{710}{113}113710​ is a very close rational approximation to 2π2\pi2π, which may be recognizable as the 355113\frac{355}{113}113355​ approximation of π\piπ.

710_113_approx.png

If you want to understand where rational approximations like this come from, and what it means for something like this one to be "unusually good", take a look at this great mathologer video.

What this means is that if you move forward by steps of 710, the angle of each new point is almost exactly the same as the last, only microscopically bigger. Even very far out, such a sequence appears to be on a straight line. And of course, the other residue classes mod 710 also form nearly-straight lines.

710_spiral.png

710 radians is so close to a whole number of revolutions that the 710k residue class appears to be almost exactly on the x-axis.

With all 710 of them, and only so many pixels on the screen, it can be a bit hard to make them out. So in this case, it's actually easier to see once we limit the view to primes, where you don't see many of these residue classes.

Which quadrant would the 710k+60710k+60710k+60 class show up in if it were on the above graph?

1st (top right)

2nd (top left)

3rd (bottom left)

4th (bottom right)

In reality, with a little further zooming, you can see that there is actually a gentle spiral to these, but the fact that it takes so long to become prominent is a wonderful illustration, maybe the best illustration I've seen, for just how good an approximation 710113\frac{710}{113}113710​ is for 2π2\pi2π.

710_galaxy.png

To understand what happens when we filter for primes, it's entirely analogous to what we did before. The factors of 710 are 71, 5 and 2. So if the remainder is divisible by any of those, then so is your number.

710_factors.png

When you pull up all of the residue classes with odd numbers, it looks like every other ray in our crowded picture. Of those which remain, these are the ones divisible by five, which are nice and evenly spaced at every fifth line. Notice, the fact that primes never show up in these is what explains the pattern of these lines coming in clumps of four. And of those remaining, these four residue classes are divisible by 71, so the primes won't show up there. This explains why some of the clumps of four seem to be missing a tooth.

filtering_quad.png

The top left plots all of the residue classes. Each successive plot continues to filter for the prime factors of 710.

So if you were wondering where the number 280 came from earlier, it comes from counting how many numbers from 1 to 710 don't share any factors with 710; these are the ones that we can't rule out for including primes based on some obvious divisibility consideration.

280_calculation.png

This of course doesn't guarantee that any particular one will have prime numbers, but when you look at the picture, it actually seems like the primes are pretty evenly distributed among all these remaining classes, wouldn't you agree?

710_rainbow.png

Every color in the rainbow can be seen, showing that every remaining residue class has about the same density of primes.

Dirichlet's Theorem

That last point actually relates to a fairly deep fact, known in number theory as "Dirichlet's theorem". To take a simpler example than residue classes mod 710, think of those mod 10. Because we write numbers in base 10, this is the same thing as grouping numbers together by what their last digit is. So numbers ending with a digit 0 form one residue class, numbers ending with a digit 1 form another, and so on.

residue_class_mod_10.png

Other than 2, prime numbers can't have an even number as their last digit, since that means they're even. Similarly any prime bigger than 5 can't end in a 5. There's nothing surprising there, primes bigger than 5 must end in a 1, 3, 7 or 9.

primes_mod_10.png

How are the primes distributed between the residue classes 0 mod 2 and 1 mod 2?

All of the primes except 2 would be in the 1 mod 2 class, because it contains all the odd numbers. The 0 mod 2 class has all the even integers, and the only even prime is 2.

A much more nuanced question is how the primes are distributed among the remaining four groups. Let's make a quick histogram, counting through each prime, and showing what proportion of primes we've seen so far have a given last digit. What do you predict will happen as we go through more and more primes?

histogram_mod_10.png

The 2nd and 5th residue class's proportion go to zero as more primes are added to the histogram.

As we add more primes to the histogram, it seems like a pretty even spread between these four classes, about 25% for each. Maybe that's what you'd expect. After all, why would primes show any preference for one last digit over another? But it's highly nonobvious how you would prove such a thing. Or for that matter, how do you rigorously phrase what it is you want to prove?

A mathematician might go about it like this: If you look at all the prime numbers less than xxx for some large xxx, and consider what fraction of them are, say, one above a multiple of 10, that fraction should approach 14\frac1441​ as xxx approaches infinity.

lim⁡x→∞(# of primes p where p≤x and p≡1 mod 10# of primes p where p≤x)=14\lim_{x\to\infty} \left( \frac{\#\text{ of primes }p\text{ where }p\leq x \color{red}\text{ and }p\equiv 1 \text{ mod } 10} {\color{black}\#\text{ of primes }p\text{ where }p\leq x} \right) = \frac14x→∞lim​(# of primes p where p≤x# of primes p where p≤x and p≡1 mod 10​)=41​

Likewise for all the other allowable residue classes 3 and 7 and 9. And of course, there's nothing special about 10, a similar fact should hold for other numbers.

Consider our old friends the residue classes mod 44. For example, let's make a similar histogram, showing what proportion of the primes show up in each one. Again, as time goes on, we see an even spread between the 20 allowable residue classes, meaning each spiral arm from our diagram has about the same number of primes as the others. Again, perhaps this is what you'd expect, but it's shockingly hard to prove.

histogram_mod_44.png

All of the even classes and 11 + 33 quickly go to zero, just like the missing arms of the spiral.

How many primes will be in the 71st histogram bin for the larger spiral pattern (r mod 710)?

Just one! 71 is one of the prime factors of 710, so after 71 is put in the bin, no other primes will follow.

The histograms give a pretty good illustration of what we mean by an even distribution, but it might be enlightening to see how it would be phrased in a math text, fancy jargon and all. It's essentially what we just saw for 10, only more general. Again, look at all the primes up to some bound xxx, but instead of asking what proportion of them have a residue of, say, 1 mod 10, you ask what proportion have a residue of rrr mod NNN, where NNN is any number, and rrr is anything coprime to NNN.

N is any numberr is coprime to Nlim⁡x→∞(# of primes p where p≤x and p≡r mod N# of primes p where p≤x)=1ϕ(N)N\text{ is any number}\\ r\text{ is coprime to }N\\ \lim_{x\to\infty} \left( \frac {\#\text{ of primes }p\text{ where }p\le x \color{red}\text{ and }p\equiv r \text{ mod } N} {\color{black}\#\text{ of primes }p\text{ where }p\le x} \right) = \frac1{\phi(N)}N is any numberr is coprime to Nx→∞lim​(# of primes p where p≤x# of primes p where p≤x and p≡r mod N​)=ϕ(N)1​

Remember, to be "coprime" means they don't share factors bigger than 1. Instead of approaching 14\frac1441​, that proportion approaches 1ϕ(N)\frac1{\phi(N)}ϕ(N)1​, where ϕ\phiϕ is that special function I mentioned earlier that gives the number of residues coprime to NNN. For example:

ϕ(10)=∣{1,3,7,9}∣=4\phi(10) = |\left\{1,3,7,9\right\}| = 4ϕ(10)=∣{1,3,7,9}∣=4

In case this is too clear for the reader, you might even see it buried in more notation, where this denominator and numerator are written with a special prime counting function, which, rather confusingly, has the name π\color{blue}\piπ; totally unrelated to the number π\piπ.

angry pi creature

We're running out of symbols!

lim⁡x→∞(π(x;N,r)π(x))=1ϕ(N)\lim_{x\to\infty}\left(\frac{\color{blue}\pi\color{black}(x;N,r)}{\color{blue}\pi\color{black}(x)}\right)=\frac1{\phi(N)}x→∞lim​(π(x)π(x;N,r)​)=ϕ(N)1​

What does this equation equal? lim⁡x→∞(π(x;6,1)π(x))=?\lim_{x\to\infty}\left(\frac{\color{blue}\pi\color{black}(x;6,1)}{\color{blue}\pi\color{black}(x)}\right)=\ ? limx→∞​(π(x)π(x;6,1)​)= ?

12\frac1221​

13\frac1331​

14\frac1441​

15\frac1551​

16\frac1661​

It turns out to be rather difficult to prove that the primes are evenly distributed among residue classes like this. In 1837, Dirichlet published a result which is very close to this, but he used a slightly different definition of density. Instead of simply counting the primes up to a certain threshold, it involves looking at all primes and adding up the values 1ps\frac{1}{p^s}ps1​ for some real number s>0s > 0s>0. Specifically, in his notion, here's how the density of primes which are rrr mod NNN would look:

lim⁡s→1+∑p≡r mod n1ps∑p1ps\lim _{s \rightarrow 1^{+}} \frac{\displaystyle \sum_{p \equiv r \text{ mod } n} \frac{1}{p^{s}}}{\displaystyle \sum_{p} \frac{1}{p^{s}}}s→1+lim​p∑​ps1​p≡r mod n∑​ps1​​

This looks more complicated, but based on the approach Dirichlet used this turns out to be easier to wrangle with mathematically. The real significance of his result, though, was that it was the first time anyone could show that there are infinitely many primes in any residue class (assuming rrr and NNN are coprime).

For example, imagine you were asked to prove that infinitely many primes end in the digit 1, and the way you do it is by showing that a quarter of all primes end in a 1. Together with the fact that there are infinitely many primes, which we’ve known since Euclid, this gives a much stronger statement, and a much more interesting one.

So how did Dirichlet prove it? Well… it’s way more involved than what would be reasonable to show here, but one interesting fact worth mentioning is that it relies heavily on complex analysis, which is the study of doing calculus with functions whose inputs and outputs are complex numbers. That may seem surprising, given that prime numbers seem unrelated to the continuous world of calculus, much less when complex numbers end up in the mix. But since the early 19th century, that’s absolutely par for the course when it comes to understanding how primes are distributed.

This isn’t just antiquated technology. Understanding the distribution of primes in residue classes like this continues to be relevant in modern research, too. Some of the recent breakthroughs on small gaps between primes, edging towards that ever-elusive twin prime conjecture, have their basis in understanding how primes split up among these kinds of residue classes.

quad_histogram.png

From Arbitrary to Important

To close things off, I want to emphasize something. In some sense, the original bit of data visualization whimsy that led to these patterns... it doesn’t matter. No one cares. There’s nothing natural about plotting (p,p)(p, p)(p,p) in polar coordinates, and most of the initial mystery in these spirals resulted from artifacts that come from dealing with an integer number of radians.

But on the other hand, this kind of play is clearly worth it if the end result is a line of questions leading you to something like Dirichlet’s theorem, which is important, especially if it inspires you to learn enough to understand the tactics of the proof.

It’s not a coincidence that a fairly random question like this one can lead you to an important and deep fact from math. What it means for a piece of math to be important is that it connects to many other topics. So even arbitrary explorations of numbers, as long as they aren’t too arbitrary, have a good chance of stumbling into something meaningful.

many_connections.png

Sure, you’ll get a much more concentrated dosage of important facts by going through a textbook or a course, with far fewer uninteresting dead ends. But there’s something special about rediscovering these topics on your own. If you effectively reinvent Euler’s Totient function before ever seeing it defined, or start wondering about rational approximations before learning about continued fractions, or if you seriously explore how primes are divvied up between residue classes before you’ve even heard the name Dirichlet, then when you do learn those topics, you’ll see them as familiar friends, not as arbitrary definitions.

3b1b_spiral.png

A Challenging Exploration

As a demonstration for what it is like to explore an arbitrary path of mathematics, let's extend this problem into 3 dimensions. Spherical coordinates is a method of plotting a point in 3D space using the distance to the origin, the angle from the XXX axis, and the angle from the ZZZ axis. We can condense this formula into:

[xyz]=[pcos⁡(p)sin⁡(p)psin⁡2(p)pcos⁡(p)]\begin{bmatrix}x\\y\\z\end{bmatrix} =\begin{bmatrix}p\cos(p)\sin(p)\\p\sin^2(p)\\p\cos(p)\end{bmatrix}⎣⎡​xyz​⎦⎤​=⎣⎡​pcos(p)sin(p)psin2(p)pcos(p)​⎦⎤​

If we take the first few thousand prime numbers and plot them as (p,p,p)(p, p, p)(p,p,p) in spherical coordinates, what pattern emerges?

The spiral galaxy we saw on the 2D plane is still visible, but now it looks like some type of infinity spiral where the arms of the galaxy are weaving in and out of each other. The 11th11\text{th}11th and 33rd33\text{rd}33rd classes are still missing on either side of the center. The above image is actually an interactive applet, go ahead and click and drag on it to move it around.

The 2D plot gave us question like "why are there spirals?" and "why are some arms missing for primes?" The 3D plot gives us another question "why do the spirals go into an infinity pattern?" And just like the first two questions, this one is also unrelated to either of the previous questions. Take a moment to try and explain why this shape appears in spherical coordinates. I recommend to explore this new prompt with the math community in the comments below, what important topics arise from looking at this arbitrary choice? As you continue your journey into mathematics, keep in mind that sometimes a puzzle should be broken down into simpler components which are easier to deal with individually.

Relation to Ulam Spirals

There’s a great Numberphile video some of you may have seen entitled prime spirals, in which James Grimes describes a similar, but distinct, pattern with primes. If you haven’t seen it, I’d recommend taking a look. The idea is to write out all numbers in a grid, starting from the center, and spiraling out while circling all the primes. The pattern you get is called an “Ulam Spiral,” named after Stanislaw Ulam who first noticed this while doodling during a boring meeting.

What you find in the zoomed out pattern is a bias towards certain stripes. In our example, the spirals and rays corresponded to certain linear functions, things like 44x+144x + 144x+1, or 710x+13710x + 13710x+13, where you plug in some integer for xxx. The Ulam Spiral pattern highlighted in the Numberphile video is showing something one step more complicated, which is how certain quadratic functions seem to have more primes than others. There’s an analog to Dirichlet’s theorem, known as the Chebotarev density theorem, laying out exactly how dense you expect primes to be in certain polynomial patterns like these. Again, the details are a bit too technical for the scope here. The point, though, is that not only do primes have plenty of patterns within them, but mathematicians also understand many of those patterns quite well, despite the reputation primes have of being impenetrably complicated.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK