Every time I learn a new programming language, my first piece of code is a program to compute prime numbers. This leads to the kind of programs you would expect in Pascal, C or Lisp, but recently I decided to learn bits of PostScript.

PostScript, as a language designed for graphics, has all the
arithmetic needed to compute prime numbers. Not surprisingly,
it's rather easy to write a PostScript file that prints prime
number tables, with the particularity that the *printer
computes the primes*. Here
is such a file. It computes the primes between 2 and 9973, but
you can edit the file to compute larger tables.

What is probably more interesting is to combine PostScript's graphics ability with prime number computations. A long time ago, I saw in a book a picture looking like this (I think it was in a book by Martin Gardner):

In that picture, called a Ulam's spiral, natural numbers are placed along a squared spiral and a dot is printed for each prime number, like this (for a mysterious reason, I decided to start my spiral with 1 instead of 0; please don't tell Dijkstra):

x -x-99-98-97-96-95-94-93-92-91 @-----------@-----------------| | | | | x 65-64-63-62-61-60-59-58-57 90 | |-----------@-----@-----| | | | | | | | | | x 66 37-36-35-34-33-32-31 56 89 @ | @-----------------@ | @ | | | | | | | | | | | | x 67 38 17-16-15-14-13 30 55 88 | @ | @-----------@ | | | | | | | | | | | | | | | | | | | x 68 39 18 5--4--3 12 29 54 87 | | | | @-----@ | @ | | | | | | | | | | | | | | | | | | | | | | x 69 40 19 6 1--2 11 28 53 86 | | | @ | --@ @ | @ | | | | | | | | | | | | | | | | | | | x 70 41 20 7--8--9-10 27 52 85 @ | @ | @--------| | | | | | | | | | | | | | | | | | x 71 42 21-22-23-24-25-26 51 84 | @ | |-----@--------| | | | | | | | | | | | | x 72 43-44-45-46-47-48-49-50 83 @ | @-----------@--------| @ | | | | | | x 73-74-75-76-77-78-79-80-81-82 | @-----------------@--------| | | x--x--x--x--x--x--x--x--x--x--x... |-----@------------------------...

If you zoom in the center of the previous picture, you actually get something like this (use the mouse to see the numbers and the lines of the spiral):

The nice thing is, when you consider a large enough number of primes, you begin to see some patterns and especially alignments appear. In the first picture, you can see some diagonal lines.

Here is a Postscript file that draws exactly that. It goes arround the spiral and prints a black disc for each prime. You can edit the source to change the resolution (in dpi) and to choose to have the lines of the spiral and the numbers appear or not.

With a resolution of 72 dpi, the rendering with ghostscript is rather fast. The actual printing, on the other hand (especially on a HP printer) ...

Here are some nice links I found related to Ulam's spiral:

- http://mathworld.wolfram.com/PrimeSpiral.html
- http://yoyo.cc.monash.edu.au/~bunyip/primes
- http://platon.lacitec.on.ca/~dmorin/applet/village
- Even more links with Google...

Michel Charpentier <> Last modified: Wed Feb 14 14:53:10 EST 2001