Skip to main content
3 of 6
added 54 characters in body
flawr
  • 44.1k
  • 42
  • 43

#Prime Spiral

This challenge was inspired by this question on math.SE by @Karl

You might be familiar with the Ulam spiral. What we're doing here is similar, but a little bit different, given a positive integer N we generate an image:

On an empty equally spaced rectangular 2d grid choose an arbitrary point and place an 1 then place a 2 on the grid point to the right of the 1. Then continue as follows:

If you're at number n<N:

  • If n is composite: Go straight in the same direction to place n+1
  • If n is prime: Turn right to place n+1 (overwrite, if there was another from a previous step)
  • Repeat this for n:=n+1.

Now consider the grid as a pixel image that is plain white. Then colour each pixel where we placed a number on the corresponding grid point. Then choose a finite rectangle that contains all the coloured pixels, and output it as an image.

###Examples

For N=10^5 we get following output (by @Wojowu)

flawr
  • 44.1k
  • 42
  • 43