Finding out the number of times innermost loop will iterate using combinations

49 Views Asked by At

Whenever I try to search for this question I seem to find methods using summations, but according to book I can do this with combinations? The book gives me two examples of two different situations on how to set up the combination, then gives me a general formula which I believe would be too easy to apply?

when you have a loop situation like

for i:=1 to n
     for j:=1 to i
       for k:=1 to j

$$1\le k\le j\le k \le n$$

Then it is concluded that the generalized formula is $$C(n-1+r,r)$$ where r is the number of nested loops

I'm guessing this is only for the situation above as right now I am dealing with

for k:=1 to n-1
 for j:=1 to k+1

and I have no clue how to come up with the expression for the combination

1

There are 1 best solutions below

2
On BEST ANSWER

You could just do the sums.

$$ \sum_{k=1}^{n-1} \sum_{j=1}^{k+1} 1 = \sum_{k=1}^{n-1} k+1 = \frac{1}{2}(n+2)(n-1) \text{.} $$

$$ \sum_{i=1}^n \sum_{j=1}^i \sum_{k=1}^j 1 = \sum_{i=1}^n \sum_{j=1}^i j = \sum_{i=1}^n \frac{1}{2}i(i+1) = \frac{1}{6}n(n+1)(n+2) \text{.} $$

Notice that the second one is $\frac{(n+2)!}{(n-1)! 3!} = C(n+2,3)$, as provided. But the first one does not have consecutive multiplicands; it is not a single binomial coefficient.