A way to determine in which one of the boxes a number in Sudoku is
<p>I recently started implementing a Sudoku solver and it was done with the help of Algorithm X. This means that there is a cover matrix with lots of different possible solutions to a given Sudoku and it is algorithm's task to find the correct ones. </p>

<p>But the problem is in the generator of that matrix. I'll simplify the problem as much as possible. I am using a cover matrix which helps me solve Sudokus. Each line in that matrix is a unique entry. In order to compose a single line, we need to determine in which cell, row, column and box a given number in Sudoku is.</p>

<p>Since Sudoku dimensions are defined by the dimensions of the inner boxes (image below), I'm using those dimensions to determine in which row and column a pregiven number is.</p>

<p>Every box has a dimension of width and height or if you prefer m and n or any other notation - classic 9x9 Sudoku has a m = 3 and n = 3 (the dimension of inner box). </p>

<p><a href="https://i.stack.imgur.com/P8bRB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P8bRB.png" alt="one of sudoku boxes"></a></p>

<p>As you can see this is a custom sudoku with dimensions m = 2 and n = 3 and it has 6 boxes. </p>

<p>I am having a problem with creating a formula that would give me the information in which box a number is. So basically input should be the position (index) of the number in the Sudoku and the output the number of the box in which that number is.</p>

<p>Below is the code i tried polishing so far but it still doesn't calculate the indexes quite correctly. I used the index of the row in which the given number is and tried using that to determine index of the column and tried combining the two to determine the number of the box. Feel free to tell me where i am making a mistake.</p>

<pre><code>     public static void assignSudokuBox(int rowIndex, int m, int n){

     double x = Math.ceil(Math.ceil((rowIndex+1)*1.0/(m*n))/m);

     double columnIndex = (rowIndex%(m*n))*m*n + (rowIndex/(m*n));

     double y = Math.ceil(Math.ceil(columnIndex*1.0/(m*n))/n);

     double number = Math.ceil((x-1)*m+y);

     System.out.println("Box of index " + rowIndex + " is: " + number + ".");

 }
</code></pre>