# Ulam Spiral

Do you see any pattern in this graph? You will not believe at first glance that it is generated using prime numbers.

In order to generate it, the numbers are arranged in a spiral, as follows:

13 -- 12 -- 11 -- 10 25 | | | 14 03 -- 02 09 24 | | | | | 15 04 01 08 23 | | | | 16 05 -- 06 -- 07 22 | | 17 -- 18 -- 19 -- 20 -- 21

Then the prime numbers are marked. Note the abundance of diagonals.

In the graphic above you can see the spiral made up to 10^{18}. The prime numbers are marked in green.

Move the graph by dragging the spiral. If the spiral has the focus, you can also move the graph by using the keyboard arrows. You can obtain more detail or see a larger area by using the Zoom buttons.

You can also see the position (`x`, `y`) in the spiral and the number `n` of any point of the graph by moving the cursor to that point.

Below the spiral you can see the equations of the diagonal lines that intersect in the point marked by the cursor (see explanation below).

Move the center by typing a new number (up to 18 digits) in the upper input box and press the return key.

Change the starting number in the center of the spiral by typing a new number (up to 18 digits) in the lower input box and press the return key.

The formula that gives the numbers in the diagonal lines can be expressed using quadratic polynomials. The quadratic polynomials are shown in the screen along with their factorization (if possible). In this case the diagonal cannot contain primes, except in exceptional cases (when one factor is 1 and the other is prime).

When the quadratic polynomial cannot be factorized, then its diagonal will contain primes. A special property of quadratic polynomials is that about half of the primes cannot divide any of the values of the polynomial.
For instance, when we select the center of the spiral equal to 41 by entering this number in the right input box, we will find a diagonal with a lot of primes in the direction NW-SE.
This is because the values that take the polynomial 4t^{2} + 2t + 41 cannot divide 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 or 37 among other prime numbers.
Any number in the diagonal line less than 41^{2} = 1681 must be prime, because it is not multiple of any prime less than its square root.

The numbers shown at the right of the polynomial are the primes less than 100 that cannot divide any value of the polynomial. The diagonals whose numbers cannot be divided by small prime numbers have more prime numbers in them, because most composite numbers are multiples of small prime numbers.

Written by Dario Alpern. Last updated 15 May 2022.

## Source code

You can download the source of the current program and the old Ulam spiral visualization applet from GitHub. Notice that the source code is in C language and you need the Emscripten environment in order to generate JavaScript.