Question

As a function of n , what is the flop count of the following code, s=0...

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

Homework Answers

Answer #1

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 :).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Why it cannot display flop? you need to explain rather than just show me another code...
Why it cannot display flop? you need to explain rather than just show me another code MatLab function b=Matrixvector(A,x) n=length(x); b=zeros(n,1); flop=0; for i=1:n for j=1:n b(i)=b(i)+A(i,j)*x(j); flop=flop+2; end end disp(b); disp(flop);
What is the time complexity of the following code? (in big-O notaion) sum = 0 count...
What is the time complexity of the following code? (in big-O notaion) sum = 0 count = 0 var1 = 0 for: i=1 to n{ sum = sum+i for j=1 to [(3i+2i)(5n)(2i)]{ var1 = sum+count} } for m = i to j*j{ sum = sum*sum var2 = sum - (count+3)}
1. Give the BigO notation for each of the following pseudocode fragments. (A) k = 0...
1. Give the BigO notation for each of the following pseudocode fragments. (A) k = 0 loop( k < n ) s = s + ary[k] k = k + 2 end loop (B) j = 1 k = 1 loop( k <= n ) s = s + j j = j + 1 k = k * 2 end loop (C) Show work! k = 0 loop( k < n ) s = s + aryA[k] k =...
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and...
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and B was given from above, what is for i = 1:n U(1,i) = A(1,i); L(i,1) = A(i,1)/U(1,1); %u(1,1)=1 end for j = 2:n for k = j:n U(j,k) = A(j,k)-L(j,1:j-1)*U(1:j-1,k); L(k,j) = (A(k,j)-L(k,1:j-1)*U(1:j-1,j))/U(j,j); end end for i = 1:n x(i) = B(i) / L(i,i:n)*U(i:n,i); end fprintf("Ans x = \n"); fprintf("%f \n", x);
#2. Write a function Triangle which has one parameter n, it will print a Triangle of...
#2. Write a function Triangle which has one parameter n, it will print a Triangle of stars with nxn. By default, n is set to 3. For example, calling Triangle(3) or Triangle() will print the following: # * # * * # * * * #my code down below is not working and I have no idea why. Could someone help? Triangle <- function(n){ for(i in 0:(n-1)){ s <- "" for(k in 0:(n-1)) s <- paste(s, " ", sep =...
Design 2 bits counter that count down by using T flip flop when input x =1...
Design 2 bits counter that count down by using T flip flop when input x =1 and counts up when x=0. Find the following 1. Derive the state table 2. Derive the K‐map simplifications. 3. Draw the logic diagram
Show that the running time for the following segment of code is O(N3) without using the...
Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to count each statement that takes one unit of time. sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ )             for( k = 0; k < n; k++ ) sum++;
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code...
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code fragment. Algorithm test int counter, i, j; counter := 0; for (i:= 1; i < n; i*=2) { for (j:= 0; j < i; j++) { counter := counter +1 } } 3)Let f(n) = 2lg 8n + 3 be a function.
What is the tilde time complexity for the following code fragments: 1) int sum = 0;...
What is the tilde time complexity for the following code fragments: 1) int sum = 0; for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) sum++; 2) int sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i; j+=2) sum++;
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT