Lawrence Wong
Lawrence Wong

Reputation: 1139

Time complexity (in big-O notation) of the following code?

I was just wondering what is the time complexity of the following code.

The time complexity (Big O) of the code below in my opinion would be O(n^4)

What do you guys think?

int result = 0;
for(int i =1; i<n*n; i++){
  for (int j=i; j*j <n; j++){
    for(int k =j; k*k <n; k++){
      result++;
     }
  }
}

Upvotes: 3

Views: 831

Answers (2)

Formal steps (need to be verified) using Sigma Notation would yield the following:

enter image description here enter image description here

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

Looks like n^(2.75) to me:

- outer loop: n^2
- first inner loop is sqrt(n)
- second inner loop is sqrt(sqrt(n))

Total of:

n^2 * sqrt(n) *  sqrt(sqrt(n)) = n^(2+ 0.5 + 0.25) = n^(2.75)

Upvotes: 2

Related Questions