find the big oh characterization
input: n
s<-0
for i<-1 to n^2 do
for j<-1 to i do
s<-s+i
Both loops iterate n^2 times. I’m not sure if I should add or multiply the loops. Right now I think the answer is O(n^4).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The outer loops
ifrom 0 ton^2, while the inner one loopsjfrom 1 toi.Thus, the inner loop has complexity of
i(inner loop needsisteps,iis varying). Let us denote the runtime of the inner loop byIL(i)=i.To get the total runtime, we see that the inner loop is executed
n^2times with varying “parameter”i. Thus, we obtain as total runtime:That is, you have to sum up all numbers from 1 to
n^2. There are many basic textbooks explaining how to evaluate this.The result is
(n^2)(n^2+1)/2, which leads to an overal complexity ofO(n^4).