title: Given an N-side square matrix, is there a way to find the ring value of a cell without using loops or if conditions?
body: <p>For instance, imagine you have a 6-side square matrix.
  These are the cells cartesian indices:</p>
  
  <pre><code>(0,0) (0,1) (0,2) (0,3) (0,4) (0,5)
  (1,0) (1,1) (1,2) (1,3) (1,4) (1,5)
  (2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
  (3,0) (3,1) (3,2) (3,3) (3,4) (3,5)
  (4,0) (4,1) (4,2) (4,3) (4,4) (4,5)
  (5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
  </code></pre>
  
  <p>A 6-side square has 3 rings: a </p>
  
  <pre><code>A A A A A A
  A B B B B A
  A B C C B A
  A B C C B A
  A B B B B A
  A A A A A A
  </code></pre>
  
  <p><strong>QUESTION</strong>: What's the function that takes the coordinates of a cell, the side N of the square and returns the ring value accordingly? Ex:</p>
  
  <pre><code>f(x = 1, y  2, N = 6) = B
  </code></pre>
  
  <p>A,B,C... can be any numerical value: 1,2,3 ... or 0,1,2 ... or whatever. What matters is that they are congruent for any N. Ex:</p>
  
  <pre><code>N = 1   =&gt;   A = 1
  N = 2   =&gt;   A = 1
  N = 3   =&gt;   A = 1, B = 2
  N = 4   =&gt;   A = 1, B = 2
  N = 5   =&gt;   A = 1, B = 2, C = 3
  N = 6   =&gt;   A = 1, B = 2, C = 3
  N = 7   =&gt;   A = 1, B = 2, C = 4, D = 4
  ...
  </code></pre>
  
  <p>Using if conditions the problem is easily solved.
  Given a pair (x,y) and the square side N:</p>
  
  <pre><code># N//2 is the number of rings in a N-side square
  for k in range(1,N//2+1):
      if x == 0+k-1 or y== 0+k-1 or x == N-k or y == N-1:
          return k
  </code></pre>
  
  <p>This seems like a very expensive way to find the ring value of the cell though.
  I have been trying to find the function using diagonals, sum of the coordinates, difference of the coordinates ... of the cells, but I still couldn't find anything.
  Has anyone ever encountered this problem?
  Is there a way to solve it?</p>