I'm writing some software that takes a group of users and compares each user with every other user in the group. I need to display the amount of comparisons needed for a countdown type feature.
For example, this group [1,2,3,4,5] would be analysed like this:
1-2, 1-3, 1-4, 1-5
2-3, 2-4, 2-5
3-4, 3-5
4-5
By creating little diagrams like this I've figured out the pattern which is as follows:
Users - Comparisons
2 - 1
3 - 3 (+2)
4 - 6 (+3)
5 - 10 (+4)
6 - 15 (+5)
7 - 21 (+6)
8 - 28 (+7)
9 - 36 (+8)
I need to be able to take any number of users, and calculate how many comparisons it will take to compare every user with every other user.
Can someone please tell me what the formula for this is?
The sum of $0+\cdots + n-1$ is $$\frac12(n-1)n.$$
Here $n$ is the number of users; there are 0 comparisons needed for the first user alone, 1 for the second user (to compare them to the first), 2 for the third user, and so on, up to the $n$th user who must be compared with the $n-1$ previous users.
For example, for $9$ people you are adding up $0+1+2+3+4+5+6+7+8$, which is equal to $$\frac12\cdot 8\cdot 9= \frac{72}{2} = 36$$ and for $10$ people you may compute $$\frac12\cdot9\cdot10 = \frac{90}2 = 45.$$