1
$\begingroup$

I am populating a matrix diagonally. Check out these three examples for clarification:

1  2  4  7  11 16
3  5  8  12 17 22
6  9  13 18 23 27
10 14 19 24 28 31
15 20 25 29 32 34
21 26 30 33 35 36

1  2  4  7  11 16 22 28
3  5  8  12 17 23 29 34
6  9  13 18 24 30 35 39
10 14 19 25 31 36 40 43
15 20 26 32 37 41 44 46
21 27 33 38 42 45 47 48

1  2  4
3  5  7
6  8  10
9  11 13
12 14 16
15 17 19
18 20 21

Is it possible to create a formula, fn(columns, row, i) = (x, y), so that you can derive what column and row a specific number in the sequence resides in? So for instance, looking at the first example, we would get something like fn(6, 6, 29) = (4,5)

Any help would be greatly appreciated, thanks.

$\endgroup$
1
$\begingroup$

This is going to be a tedious process. Split the rectangle into three parts: where diagonals are growing in size, where diagonals have the same length, and where diagonals are shrinking in size.

For the first section where diagonals are growing in size, number the diagonals with the variable $d$ so that $d = x + y - 1$. Now $d$ must be the smallest integer for which $1 + 2 + \ldots + d \geq i$. A bit of algebra gives you the following expression for $d$.

$$d = \left\lceil{\frac{\sqrt{8i +1} - 1}{2}}\right\rceil$$

The value of $x$ can be calculated similarly so that you can calculate $y$. The other sections can be computed similarly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.