I have to compare M
items where a single item should not be compared to itself. In this case, I would like to design an algorithm to find the nth
comparison. If, for example, I am comparing 2 items then the list of comparisons should be:
2: (1,2)
Likewise if I am comparing 3 items the list of comparisons should be:
3: (1,2), (1,3), (2,3)
Following this pattern:
4: (1,2), (1,3), (1,4), (2,3), (2,4), (3,4)
5: (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5)
and so on.
My question is, what is the nth
item (i,j)
if the input is M
?
M: (1,2), ..., (i,j), ..., (M-1,M)
While I can easily write a simple program to calculate this ad-hoc, I am wondering if there is a closed form solution to this so that it will not scale with M
.
EDIT: To make this more clear cut (and to have an example that can be implemented for testing), I'd like the code to be in C
with the following template:
void findIJ(int M, int n) {
int i = 0;
int j = 0;
/* Do work to find i and j*/
printf("(i,j) = (%i,%i)\n", i, j);
}