Ulam’s Prime Number Spiral

There is an infinite number of prime numbers, and yet the prime numbers themselves do not display any apparent pattern, nor does any formula exist that generates prime numbers. In fact, Legendre proved that there cannot be an algebraic function which always gives primes.

Prime SpiralHowever, prime numbers do exhibit a curious phenomenon when arranged in a spiral along with other consecutive integers, as in the figure to the right (in the figure, prime numbers are highlighted in white, twin primes are green, and Mersenne primes are red).

The Phenomenon

It was first noticed by the physicist StanisÅ‚aw Ulam in 1963, when he got bored in a meeting and started doodling spirals of numbers. He noticed that, if he makes a spiral of consecutive integers, and circles only the prime numbers, strange diagonal “lines” of prime numbers emerge.

This is quite surprising, since we would intuitively expect a random distribution of prime numbers. However, these diagonal segments occur on an impressively large scale, and arbitrarily far from the center of the spiral. The following image is a spiral containing about 4000 primes, and next to it is the same image with some of the diagonal paths highlighted.
Prime Spiral

Application

To explore this phenomenon on a large scale, I wrote a small program that generates arbitrarily large spirals, with configurable coloring and other options.

Download the program! (Or browse the source code on GitHub)

Prime Spiral Application
The program generates a spiral based on the total number of integers that you specify. It also allows you to specify the colors to use for the background, prime numbers, twin primes, and Mersenne primes.

In addition, the program allows you to use a custom polynomial (up to degree-2) for generating the spiral. By default, the polynomial is set to
\(f(n) = n\)
so the spiral will have the normal sequence 0, 1, 2, 3, etc. However, as an example, suppose you want the spiral to consist only of odd integers (1, 3, 5, 7, etc). You can simply set the polynomial to be
\(f(n) = 2n + 1\)
Odd Prime Spiral
by writing “2″ in the text box next to “n“, and “1″ in the last of the three text boxes. The image to the right shows a spiral constructed from odd integers only. Notice the prominent “patterns,” this time extending vertically and horizontally.

Conclusions

Ultimately, all that these patterns show is that certain polynomials are more “likely” to generate prime numbers than others. In fact, we can speculate that these kinds of patterns would emerge if we arrange the integers in any ordered design, not just a spiral. Even if we arrange the integers in a simple table, we would still see occasional “streaks” of prime numbers similar to the ones seen in the spiral.

The existence of “prime-generating” polynomials was known since the time of Euler, who discovered a polynomial that gives 40 consecutive prime numbers, namely
$$f(n) = n^2 – n + 41$$
The reason why some polynomials generate more primes than others is still not known.

Extreme Spirals

Using my program, you can generate extremely large spirals, limited only by the amount of memory on your computer. Here are some fairly large ones that I generated:

Share this article:
  • Facebook
  • LinkedIn
  • Reddit
  • Twitter

6 thoughts on “Ulam’s Prime Number Spiral

  1. KSR

    Dear Sir,
    I am performing some related studies which include to soem extent the prime numebrs. i would very much like to learn from you if you please about the prime numbers. this would help me a lot in my studies. theank you very much indeed sir.

    Reply
  2. mike tech

    Wrote [ updated ] program in turbo pascal to “generate” prime numbers,
    original program by Eratosthenes ~300 bc.
    not really algebraic, uses “casting out primes” method.

    Reply
  3. thomas

    hmm “There is an infinite number of prime numbers”, is there? ;) this is a mistake that even the best english speakers seem to make all the time; having said that i know yours is fine, and i thoroughly enjoyed your page about things americans say “wrong”!

    anyway sorry about that, i’ll continue reading this article (and the rest of your excellent site) now :)

    Reply
  4. sparge

    @thomas; I’m one of those best English (c.f english) speakers. I’m curious to know wherein you think the mistake lies (unless it’s a nitpick about the difference between “there is an infinite number of …” and “there is no largest …” or “… are countably infinite”). You appear to have missed the intentional irony in the title of the page about Americans (c.f. americans), though. Are you by any chance American? :-)

    Reply
  5. Bill McEachen

    I know an amateur named Hank Harrell has worked with such prime-generating quadratics. You may wish to run some of his best eq’ns thru your spiral graphics. The website link I have does not work but he had a self-published book as follows: ISBN 075967180X Prime Producing Equations. It’s dated (he uses ubasic) but the best polynomials are discussed.

    Reply
  6. Rachael

    I think Thomas is saying that the correct grammar would be ‘There are an infinite number of prime numbers’… even though the word ‘number’ is singular, you are actually talking about ‘numbers’.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>