As a function of n , what is the flop count of the following code,
s=0
for k=1:n ,
s=s-j-1
for j=k:n ,
s=s+k+j^2*(k-1)
end
end
Ans ). n(n-1)/2 flops.
Well lets see, the outer loop runs from 1 to n. the inner loop runs from k to n. where k is the outer loop variable. Also the floaing point opetation is happening inside the inner loop. therefore the no of flops will be equal to the no of times the inner loop runs. So the inner loop run basically from k to n. Thus for k =1 ,it run n-1 times. for k=2 it run n-2 times until for k=n-1, it runs once. Thus, basically the no of flops are 1+2+3+....+n-1 = n(n-1)/2.
Hope it helps. Feel free to ask any doubt in comments.Don't forget to upvote if it helped :).
Get Answers For Free
Most questions answered within 1 hours.