What will be the time complexity of the following function? is it O(n^3) or O(n^4)?
i am getting O(n^3) in the first for
loop, it will undergo n times. in the second for
loop, for every nth element it will go n^2 times, therefore the total complexity till here is O(n^3) now, the if
statement will only hold true value only for n out of n^2 values, and for every n values the k- for
loop will go till n^2 elements and hence the complexity is O(n^3). I have taken few values of n: for n=3 ,c=25
for n=10,c=1705
for n=50,c=834275
for(i=1;i<=n;++i)
for(j=1;j<=(i*i);++j)
if((j%i)==0)
for(k=1;k<=j;++k)
c=c+1;
这种程序的时间复杂度是O(n^3)
量级。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.