Find the row/column of the $n^\text{th}$ word in a list of words arranged in a grid

50 Views Asked by At

Here is a list of twelve words arranged in a grid with four rows and three words per row

alpha   betta    gamma
one     two      three
better  faster   stronger
major   captain  colonel

How can I find out which row and column the $11^\text{th}$ word, the word "captain", is in? Like, knowing that it's the $11^\text{th}$ word, how can I tell mathematically that it's the second word in the forth row? Or similarly how can I tell the position of the word "better" knowing only that it's the $7^\text{th}$ word in the list?

1

There are 1 best solutions below

1
On BEST ANSWER

To answer this, you need to be familiar with modular arithmetic and the floor function.

If your grid has $m$ words per row, then the $k^{\text{th}}$ word will be the $(k \bmod m)^{\text{th}}$ word in the $(\lfloor (k-1)/m \rfloor +1)^{\text{th}}$ row. I think the best way to realize why these formulas work is to try them on a few example words, so for a few specific cases of $k$, and see what happens. A quick point though, the $+1$ and $-1$ in the formula for the row only need to be there because we are indexing our rows and columns starting at $1$ instead of $0$ (i.e. in the above grid we're saying that the word alpha is the first word of the first row, and not the zeroth word of the zeroth row).