Consider the following algorithm. for i is in {1, 2, 3, 4} do beep for j is in {1, 2, 3} do beep for k is in {1, 2, 3, 4} do for l is in {1, 2, 3, 4, 5} do beep for m is in {1, 2, 3, 4, 5, 6} do beep How many times does a beep statement get executed?
start with the inner most loops: for L beeps 5 times, and for m beeps 6 times that is 11 total within the k loop k executes 4 times, so 4*11 beeps = 44 at the same level for j loops 3 times = 3 beeps 47 total beeps plus 1 standalone beep gives 48 beeps per step in the for i loop i has 4 steps, so 4*48 = 192 beeps
i get 188
i is in {1, 2, 3, 4} do {beep for j is in {1, 2, 3} do beep for k is in {1, 2, 3, 4} do {for l is in {1, 2, 3, 4, 5} do beep for m is in {1, 2, 3, 4, 5, 6} do beep } } 4(3+4(5+6)) 4(3+4(11)) 4(3+44) 4(47) 188
i see, i droped the 1
in my defense, they should have said: for this {1} :)
thanks guys :)
=152
Join our real-time social learning platform and learn together with your friends!