






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to counting the number of iterations in nested loops with different conditions and introduces the concept of r-combinations with repetition allowed. It also includes formulas for calculating the number of combinations and the binomial theorem.
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1
2
Consider the following nested loop:
for i:=1 to n
for j:=1 to i-
for k:=1 to j-
[ Statements]
next k
next j
next i
Question: How many times the statements in the innermost
loop will be executed?
Solution: Each iteration corresponds to
a triple of integers (i, j, k) where i > j > k.
The set of all this kind of triples corresponds to
all 3 -combinations of {1, …, n}.
Thus, the total number of iterations is C(n,3).
Each triple corresponds to a string of crosses and vertical bars.
how many items from each category are chosen.
Examples when n=5:
5
Which formula to use?
Order matters Order does not
matter
Repetition allowed
Repetition not
allowed
r
8
Useful formulas for special cases
10
Binomial Theorem
n-k
k
n-k
k is C(n,k). ■
2
2
2
2
2
3
3
2
2
3
3
2
2
3
11
Using the Binomial Theorem
4,
4,
4,
4000
3999