Equation to locate a square in a square

313 Views Asked by At

Good evening,

I have been experimenting with different Sudoku checker and have come across a problem:

For a nxn Sudoku where n is a square number (4,6,19,25 etcc.), there would be an n number of sub-squares in a square. For example for a 9 by 9 there are 9 3x3 equal sized sub squares:

enter image description here

let column = j

let row = i

Now what I am trying to do is to come up with an equation in terms of n and sub-square number needed such that it would equal the first row number.

e.g for sub square number 3 the first row is number 3, for sub square 0 first row is 0 etc.

the issue with this is that the sub squares 0,1 and 2 all start at the same row 0 and therefore does this mean it is impossible to find an equation/algorithm or any other way to do this?

would the same apply for columns for sub squares?

2

There are 2 best solutions below

0
On BEST ANSWER

Assuming the rows and columns are the numbers you wrote outside the square: If $n=m^2$ then the first row in the $k$'th sub-square would be row $m \cdot\lfloor \frac{k}{m} \rfloor$ (notice the floor function) and the first column would be $m \cdot (k \bmod m)$.

So for $n=9$ the first row in e.g. the $7$'th sub-square would be row $3 \cdot\lfloor \frac{7}{3} \rfloor = 6$ and the first column would be $3 \cdot (7 \bmod 3)=3$.

6
On

In your example, square $k$ is in row $\lfloor \frac k3 \rfloor$ and in column $k \bmod 3$. For general $n$, replace $3$ by $n$. This shows the nice side of counting starting with zero.