Quick summary: I am a programmer who has an equation using integer division and modulus operators, and just need to solve it for a variable.
I'm working on a C++ program to distribute a matrix across several computers, and the matrix is split up by columns based on the number of computers and the size of the matrix. I have an equation for determining the start and end columns that should be on a given computer. The parameters are:
$\;\;\;\;n$ $-$ the number of columns or rows of an $n \times n$ matrix,
$\;\;\;\;id$ $-$ the id of a computer (e.g. $0, 1, 2, \cdots$)
$\;\;\;\;c$ $-$ the number of computers.
The equations for start and end columns are
$$startColumn(id, n, c) = id \times\left(\frac{n}{c}\right)+\min\left(id, n\%c \right)$$ $$endColumn(id, n, c) = startColumn(id+1, n, c) - 1$$
Note that division in this case is integer division in C++, e.g. 7/4=1, and $\%$ represents the modulus operator.
What I want to do is solve these equations for $id$. That is, given a column (called col), n, and c, you can find which id (computer) that column belongs to. I'm having trouble with the mathematics of this given the min function, modulus, and integer division involved. Perhaps it is quite simple, the closest I've gotten is this very simple formula, but it seems to be off by 1 in some cases.
$$id(col, n, c ) = \frac{col\times \min(c,n)}{(n)}$$
Of course I could solve experimentally, by generating the $startColumn$ and $endColumn$ for each computer until I find the computer that holds the column I'm looking for, but I'm trying to do this directly, using a single equation.
In your setup, the first $n\%c$ blocks of columns have width $\frac nc+1$, and the remainder have width $\frac nc$. So one can write $$ id(col,n,c) = \begin{cases} \displaystyle \frac{col}{\frac nc+1} , & \text{if } \displaystyle 0 \le col < (n\%c)\bigg( \frac nc+1 \bigg) , \\ \displaystyle n\%c + \frac{col - (n\%c)\big( \frac nc+1 \big)}{\frac nc} , & \text{if } \displaystyle (n\%c)\bigg( \frac nc+1 \bigg) \le col < n .\end{cases} $$