**There’s a thing in mathematics known as the Ulam Spiral, which is created by writing all the numbers from 1 upwards in a rotating pattern around a grid. This spiral was discovered by Stanislaw Ulam in 1963 while doodling during the presentation of a ”long and very boring paper”. What’s interesting about the Ulam spiral is that when you circle all the prime numbers, many of them surprisingly align along diagonal lines.**

You can try it for yourself if you have enough time and some paper and a pencil, or even enter the numbers into a spreadsheet. Just follow the procedure on the right, and then circle, underline, cross out, or otherwise highlight all the prime numbers. I considered doing the same when I was first intrigued by the Ulam spiral but, being a programmer, decided to take things further by writing a program to map all the prime numbers onto an HTML5 canvas element.

At the same time I decided to write the spiral in such a way that as well as following a grid-like form, the distances between the numbers could be changed and the spiral allowed to grow in arcs (not straight lines). I also chose to allow the spacing between arms of the spiral to be variable so that the resulting spiral could be formatted in a wide range of different ways. For example, a grid pattern supports only 8 potential directions for straight lines to appear (N, NE, E, SE, S, SW, W, and NW), but by allowing variable distances, fewer or more directions become available. So here (with apologies if you don’t program) is a JavaScript program to do just as I described:

<!DOCTYPE html> <html> <head> <title>Prime Spirals - By Robin Nixon</title> </head> <body> <canvas id='canvas' width='900' height='900'></canvas> <script> canvas = document.getElementById('canvas') context = canvas.getContext('2d') w = canvas.width h = canvas.height x = w / 2 // Center of spiral... y = h / 2 // ...X & Y coordinates r = 0.0002 // Spiral radius increment d = Math.PI / (360 * 7) // Gap between potential plots n = 1 // Numbers being plotted loop: while (n < 2200000) { n += 1 for (i = 2 ; i <= Math.sqrt(n) ; i += 1) if (n % i == 0) continue loop context.fillRect(x + Math.sin(n * d) * r * n, y + Math.cos(n * d) * r * n, 1, 1) } </script> </body> </html>

In this program, the canvas width and height are set to 900 pixels then the variables x and y are set to point to the center of the canvas. After that r is given a value of 0.0002. This is the outward movement in pixels of the spiral. For every degree around the spiral it will move outward by that amount.

The variable d was then initially set to a value of π ÷ 360, so that each new plot point on the spiral would be at intervals of one degree. However, on the outer edges of the spiral this made the gaps very wide indeed so I changed this distance to a 7th of a degree to get much more compact results by choosing a division factor of 360 × 7. Both r and d are the values that can be changed to play with different types of spiral if you wish to run this code yourself. The last value initialized is n, which is the equivalent of the numbers counting upwards around the spiral, and will be continually incremented and checked for primality each time.

Finally there is a loop with a label of loop. It counts up to 2.2 million, locating all prime numbers up to that value by using a for loop to check for any factors. If a factor is found then n is not prime and so the continue statement resumes execution at the start of the loop, where n is incremented and then again checked for primality.

If n passes the factoring tests then the canvas is drawn on with a call to fillRect(), using x and y as the base polar coordinates, with n (the prime number) multiplied by d (the distance on the spiral between each number) and then passed through functions to obtain the sine and cosine of these values to assign as coordinates, which are then multiplied by both r (the distance between spiral arms) and n (the prime number), to finalize their positions in the spiral.

When you run this program, the result you get will be similar to the image on the left. At first glance you may not think much of it, but look closely and notice how there is an amazingly consistent repeating pattern of radial lines all the way around this image. Some places there are gaps of white, and in others there are distinct lines. What’s more when you study the lines close up you can see that they are created with primes all falling near each other, creating a line wider than one pixel, as if they are drawn towards that ‘spoke’ of the spiral.

Now, I know what many of you will be thinking, because this looks like the kind of result you might get from interference of one kind or another, such as imprecise floating point arithmetic – I also thought the same. So I modified the program to use random numbers instead of primes. As you will know, almost all prime numbers end with the digits 1, 3, 7, or 9. The only one to end with 5 is 5 itself, and we can similarly discount the number 2. So, I swapped out the code to check for factors with some to increment n (which starts off with a value of 1) by random even amounts of between 2 and 20 (to get another odd number). Then, if the resulting number doesn’t end with 5, it is plotted on the spiral. Here’s the code:

<!DOCTYPE html> <html> <head> <title>Random Spirals - By Robin Nixon</title> </head> <body> <canvas id='canvas' width='900' height='900'></canvas> <script> canvas = document.getElementById('canvas') context = canvas.getContext('2d') w = canvas.width h = canvas.height x = w / 2 // Center of spiral... y = h / 2 // ...X & Y coordinates r = 0.0002 // Spiral radius increment d = Math.PI / (360 * 7) // Gap between potential plots n = 1 // Numbers being plotted while (n < 2200000) { n += Math.floor(Math.random() * 10) * 2 if (n % 5 != 0) context.fillRect(x + Math.sin(n * d) * r * n, y + Math.cos(n * d) * r * n, 1, 1) } </script> </body> </html>

So, very little has changed here, other than that there will now be a combination of primes and non-primes plotted on the spiral. So what does the output look like? Well, as you can see from the image on the right, it’s pretty much as you’d expect, a smooth and random-looking image, with only marginal demonstration of radial lines, caused only by the fact that the distance between plot locations is one 7th of a degree. The smaller this value is made, the less obvious these lines become.

If you try, as I earlier suggested, modifying the values of r and d with either of these programs, you will see very little difference in the output from the latter one, but a wide variety of different sets of radial lines from the former, indicating, as far as I can tell, that there is a lot more order and repetition in prime numbers than we may have hitherto realized.

I have Googled for prime spirals and searched for similar images to those I have been getting and not come across any as yet, so maybe this is something new to add to the Ulam Spiral. At least, it looks that way to me and I would welcome input from any mathematicians on the subject. Even if it’s not new, it’s still new to me, and lots of fun to play with, and I hope you’ll find it the same.

I have uploaded both examples to my website where you can try them for yourself. If you’re curious I do recommend you download them and try playing with the code to see what else you can discover:

**Update**: My latest work in progress which includes both prime and random animations, with a number of keyboard options for fine control is now here:

Have fun, and please let me know what you think.

Note: It has been suggested to me that 360 has multiple divisors and therefore using this value will cause erroneous results. However, 360 × 7 is 2520, and when I swap that out for primes such as 2503, 2521, or other primes sufficiently large to not leave too wide a gap between ‘spokes’, I still frequently see these radial lines, although they often show up as spiral lines. And, again, comparing with the random version of the program, I have been so far unable to replicate these radial lines in a similar fashion with any combination of different values. So I’m still scratching my head…

## And there’s more!

I’ve been playing around a lot more with this idea and decided to pick out subsets of prime numbers such as only those that are two numbers away from each other (also known as twin primes), or primes that come in groups of two or more separated by either two or four numbers between each, and so on. And the result of this has further amazed me.

So let me show you what kind of spirals you get when you do this, starting off with the spiral of all primes for comparison.

### Spiral of all primes:

### Spiral of twin primes:

### Spiral of triplet primes:

### Spiral of primes separated by 4 numbers:

### Spiral of primes separated by 6 numbers:

### Spiral of primes separated by 12 numbers:

### (And for comparison) Spiral of random odd numbers not ending in 5:

In all these prime number cases only the final prime in a group is plotted. What is amazing to me is that no-matter which way you try to make it unlikely that the prime number sequence you select will reveal a pattern, there is always a pattern somewhere – even when you only plot groups of three primes near each other, or primes separated by the value 12. If these radial line patterns were an anomaly I don’t think they would interact so directly with the prime sequences selected.

*I mean, look at the spiral for primes separated by 6 numbers. This isn’t every 6th number that’s a prime or something as semi-ordered as that. It shows only those primes that have a separation of 6 numbers between them (and only the latter of each pair, so that no sub-pattern can form) – and a distinct pattern is generated from it. I’m dieing to be put out of my misery and be proved wrong on this – but it does seem like we are seeing something new here at the moment.*

Here are the new files to create these spirals:

PS: I tried plotting the Mersenne Primes but there are too few of them to tell whether there’s a pattern evolving. I’ll see if I can plot some other types of prime, though.