Consider the following pseudo code. What is the total number of multiplications to be performed?
D = 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3 View worked solution
Answer: C
To find the total number of multiplications performed, we need to count the number of times the innermost statement D = D * 3 is executed. This is equivalent to finding the number of triples that satisfy the loop conditions:
We can transform this inequality to make all relations strictly less than ():
- Let . Since , we have .
- Since , we have .
- Let . Since , we have .
Substituting these, the condition becomes:
This is equivalent to choosing distinct integers from the set . The number of ways to do this is:
Here, , , and are three consecutive integers. Thus, the total number of multiplications is one-sixth of the product of consecutive integers.
Free practice · Your answers on this page are not saved.
