0

I've been trying to wrap my head around the following question for ages, its part of my revision for the upcoming end of year exam, however I fail to see how there is a link from the number to the letter, how will one cycle from 1 to a and iterate to get there? if that makes any sense! We were told that when estimating execution time of a loop, it is likely to be the number of times the loop executes, so therefor a list of length n would take approximately n units of time to execute, that makes sense, its just the cycle from 1-a, then 1-b? like, how many iterations will the loop run to get from 1 to a? it just confuses me! anyhow here it is as on the practice paper:

z := 0
for i from 1 to a
  for j from 1 to b
    for k from 1 to c
      z := z + a*b*c

• How long does it take to compute the k-loop?

• How long does it take to compute the j-loop?

• How long does it take to compute the i-loop?

• How long does it take to compute the algorithm?

Wandering Logic
  • 17,743
  • 1
  • 44
  • 87

1 Answers1

1

There's no list here. In this problem, $a, b, c$ are simply numbers, so the $k$ loop will iterate $c$ times, taking the values $k=1, k=2, \dotsc,k=c$ for any values of $i$ and $j$. Working outwards, the $j$ loop will iterate $b$ times and each time will perform a complete $k$ loop. In other words, the total amount of work done by the $j$ loop will be $bc$: $b$ iterations of the $j$ loop, each involving $c$ steps of the inner $k$ loop, no matter what the $i$ value is. Finally, in a similar way, the outer $i$ loop will iterate $a$ times, each time doing $bc$ work. The total number of iterations, then, will just be $abc$.

For example, with $a=2, b=3, c=2$ the $(i, j, k)$ values will be, in order, $$\begin{array}{c} (1,1,1) \\ (1,1,2) \\ (1,2,1) \\ (1,2,2) \\ (1,3,1) \\ (1,3,2) \\ (2,1,1) \\ (2,1,2) \\ (2,2,1) \\ (2,2,2) \\ (2,3,1) \\ (2,3,1) \\ \end{array}$$

Rick Decker
  • 14,826
  • 5
  • 42
  • 54