A spiral drawn on a hexagon grid with the prime numbers colored in red or black. The numbers go from 1 to 90000. The red dots are twin primes.

Motivation:

The Ulam spiral is generated by drawing a spiral on a square grid and highlighting the prime numbers.

source: Wikimedia

I tried to find if this had been performed using a hexagonal grid and could only find this image:

source: Mathworld

The Mathworld image seemed interesting but one can not see apparent patterns from this visualization.

Implementation:

Hex Grid System:

I decided to use an x and a slanted y axis to index the hexagons. Doing some math I was able to derive the center of a hexagon given an x and y offset.

float center_x = radius * cos_30 * (2 * x - y);

float center_y = -y * (radius * 3/2);

The Spiral:

From the diagram one can see a pattern of movement for the spiral. For the direction I created an array:

var directions = [[0,-1], [-1,-1], [-1,0], [0,1], [1,1], [1,0]];

The magnitudes of the direction of travel follows the pattern n,n,n,n,n+1,n where n is the current ring you are on. This excludes the first move where I move right one hex. The magnitude of travel can be expressed with the following:

int mag = Math.floor((counter-1)/6) + 1

```
```

`if ((hex_loc_counter+1) % 6 == 0){`

mag += 1

}

Red vs Black Dots:

I am interested in twin primes so I decided to highlight the twin primes in red.

if (isPrime(n+2) || isPrime(n-2)){

red_hexagon

}

else{

black_hexagon

}

Future Work:

I would be interested to see how the 6n+1 and 6n-1 primes appear on the hex grid.