The equation should be,
f(n) = n*(n+1) / 2, f(n) is length
f(4) = 4 * 5 / 2 = 10
Question
I need a formula to define the relation between the length of a list in this example (2) and the number of elements which is here (4) to calculate the number of combinations which is (10).
Example:
(1,1)
(1,2)
(1,3)
(1,4)
(2,2)
(2,3)
(2,4)
(3,3)
(3,4)
(4,4)
Solution
The equation should be,
f(n) = n*(n+1) / 2, f(n) is length
f(4) = 4 * 5 / 2 = 10
OTHER TIPS
Here's how you derive the formula.
There are 4 entries with one as the first component: (1,1), (1,2), (1,3), (1,4), 3 entries with two in the first component, 2 with three and 1 with four.
That's 1 + 2 + 3 + 4. If you have 5 elements, it's 1 + 2 + 3 + 5, if you have 2 elements, it's 1 + 2.
It's clear we need a formula to evaluate 1 + 2 + 3 + ... + n
By rearranging the sum, we get terms that all evaluate to n + 1
, since we always grouped two numbers, we have n/2
such terms. Hence n/2 * (n + 1)