Skip to main content
9 of 11
Rollback to Revision 5
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

Can square tree rings be generated from primes?

Apparently yes! In three easy steps.

Step 1

Let f(n) denote the prime-counting function (number of primes less than or equal to n).

Define the integer sequence s(n) as follows. For each positive integer n,

  • Initiallize t to n.
  • As long as t is neither prime nor 1, replace t by f(t) and iterate.
  • The number of iterations is s(n).

The iterative process is guaranteed to end because f(n) < n for all n.

Consider for example n=25. We initiallize t = 25. Since this is not a prime nor 1, we compute f(25), which is 9. This becomes the new value for t. This is not a prime nor 1, so we continue: f(9) is 4. We continue again: f(4) is 2. Since this is a prime we stop here. We have done 3 iterations (from 25 to 9, then to 4, then to 2). Thus s(25) is 3.

The first 40 terms of the sequence are as follows. The sequence is not in OEIS.

0 0 0 1 0 1 0 2 2 2 0 1 0 2 2 2 0 1 0 3 3 3 0 3 3 3 3 3 0 3 0 1 1 1 1 1 0 2 2 2

Step 2

Given an odd positive integer N, build an N×N array (matrix) by winding the finite sequence s(1), s(2), ..., s(N2) to form a square outward spiral. For example, given N = 5 the spiral is

s(21)   s(22)   s(23)   s(24)   s(25)
s(20)   s(7)    s(8)    s(9)    s(10)
s(19)   s(6)    s(1)    s(2)    s(11)
s(18)   s(5)    s(4)    s(3)    s(12)
s(17)   s(16)   s(15)   s(14)   s(13)

or, substituting the values,

 3       3       0       3       3
 3       0       2       2       2
 0       1       0       0       0
 1       0       1       0       1
 0       2       2       2       0

Step 3

Represent the N×N array as an image with a grey colour map, or with some other colour map of your taste. The map should be gradual, so that the order of numbers corresponds to some visually obvious order of the colours. The test cases below show some example colour maps.

#The challenge

Given an odd positive integer N, produce the image described above.

#Rules

  • The spiral must be outward, but can be clockwise or counter-clockwise, and can start moving right (as in the above example), left, down or up.

  • The scales of the horizontal and vertical axes need not be the same. Also axis labels, colorbar and similar elements are optional. As long as the spiral can be clearly seen, the image is valid.

  • Images can be output by any of the standard means. In particular, the image may be displayed on screen, or a graphics file may be produced, or an array of RGB values may be output. If outputting a file or an array, please post an example of what it looks like when displayed.

  • Input means and format are flexible as usual. A program or a function can be provided. Standard loopholes are forbidden.

  • Shortest code in bytes wins.

#Test cases

The following images (click for full resolution) correspond to several values of N. A clock-wise, rightward-first spiral is used, as in the example above. The images also illustrate several valid colour maps.

  • N = 301: enter image description here

  • N = 501: enter image description here

  • N = 701: enter image description here

Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372