Walking the Ulam Spiral

After reading Unwinding the Ulam Spiral from @montyxcantsin, I was fascinated by the prime-number based pattern it produces.
This site may serve you as an explanation to how apparently randomly spaced prime numbers can produce a regular pattern. Continue reading after the triangle for details on how it is build and the role prime number play here. Enjoy,
-- tessi

Pause computation
Save as PNG

Each red or black box in the triangle above contains a set of numbers. If any number in a box is prime, the box is painted black. Click on a box to find out which numbers were checked and whether they are primes.

Given each box holds five numbers (it has a boxSize of 5), the very first box will get numbers 1 to 5. In the second row, numbers 6 to 15 are distributed between two boxes in an alternating pattern. This continues as shown in the following listing:

Box 1 (Row 1)

  • 1
  • 2
  • 3
  • 4
  • 5

Box 2 (Row 2)

  • 6
  • 8
  • 10
  • 12
  • 14

Box 3 (Row 2)

  • 7
  • 9
  • 11
  • 13
  • 15

Box 3 (Row 3)

  • 16
  • 19
  • 22
  • 25
  • 28

Box 4 (Row 3)

  • 17
  • 20
  • 23
  • 26
  • 29

Box 5 (Row 3)

  • 18
  • 21
  • 24
  • 27
  • 30

Each row will get an additional box between which numbers are distributed. Feel free to play with different box sizes by changing the boxSize parameter to this site. Here are some interesting sizes for you to play with:

We observe that for larger boxSizes, patterns start to emerge in the triangle. I wonder whether we can use them as knitting patterns :)

If you want to keep playing, this site supports some additional settings which you can change by adding URL params:

Note that prime numbers are calculated here with the Miller-Rabin primality test, which does not give a 100% answer on whether a number is prime or not (it rather gives a certain likelihood that a number is prime). Since I'm not pursuing a math degree with this website, I figured this is good enough and it's fast to calculate especially for large numbers.

If you want to know more don't forget to visit Novas Blog about this phenomenon.