I was asked a question about the following problem (maybe puzzle would be a better word), which originally came from programming. The person, who asked me this, was programming something involving 3-by-3 square. He decided to index the positions on the square like this:
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
Later he found out that he wants to rotate the grid clockwise. So he needed map working like this: $1\mapsto 3$, $2\mapsto 6$, $3\mapsto9$, $4\mapsto2$, $5\mapsto5$, $6\mapsto8$ etc. And he noticed that he can get this map by using $n\mapsto 3n\bmod{10}$. His first question was:
Why does this work?
I'll add the way I was able to explain this, but feel free to include better explanation, if you have one.
I have added a coordinate system to the table:
-1 0 1
-------------
-1 | 1 | 2 | 3 |
-------------
0 | 4 | 5 | 6 |
-------------
1 | 7 | 8 | 9 |
-------------
In these coordinates, the number in the square can be calculated as $x+3y+5$, where $x$ is the number of the of column and $y$ is the number of the row.
The clockwise rotation is given in this coordinate system by $(x,y)\mapsto(-y,x)$. Hence we want to have $f(x+3y+5)=-y+3x+5$. And the difference $$3(x+3y+5)-(-y+3x+5)=10y+10$$ is indeed a multiple of 10.
A very natural question was:
Can this be generalized?
The natural generalization I was able to think of was taking $n\times n$-square for $n=2k+1$. Again, I can index rows and columns by numbers $-k,\dots,-1,0,1,\dots,k$. Now the number in each row will be $x+ny+(n+1)k+1$. And multiplying by $n$ and taking reminder modulo $n^2+1$ will work: $$n(x+ny+(n+1)k+1)-(-y+nx+(n+1)k+1)=(n^2+1)y+(n^2-1)k+(n-1)=(n^2+1)(y+k)$$
I would be interested to see other generalizations (if there are some):
Another natural question is:
Can something similar be done, if I do not start indexing by 1, but by some other number (for example 0). If starting with 1 is the "best" or "most natural choice", what are the reasons behind it?
Can I get similar simple formula for rotating this table? (Since the numbers used here are 0,1,...,8; I would naturally expect something using modulus 9.)
-------------
| 0 | 1 | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
-------------