Time Complexity Nested Loop Inner Loop + Outer Loop

Dicky Geraldi

Can anyone explain what the time complexity is for this algorithm?

for (i = 1; i <= n; i++){
    for(j = 1; j <= n; j += i) {   // note: not j++
        printf("Iteration %d : %d\n", i, j);   
    }
}
Antti Haapala

The printf in the inner loop is called exactly ceil(n) + ceil(n/2) + ceil(n/3) + ... ceil(n/n) times. To get rid of ceil, we know that ceil(y/n) is bounded above by y/n + 1, so we know that the number of executions is >= n + n/2 + n/3 ... n/n but is < n + 1 + n/2 + 1 + n/3 + 1 + n/4 + 1... + n/n + 1. The former can be factored to n(1 + 1/2 + 1/3 + 1/4 ... 1/n) and the latter can be refactored into to n(1 + 1/2 + 1/3 + 1/4 ... 1/n) + n.

The latter factor is of the first addend to infinity is the the harmonic series, which diverges. The sum of the first k terms from the Wikipedia page is known to be bounded:

https://wikimedia.org/api/rest_v1/media/math/render/svg/c92abcc9592300b3eca1aac9748346649ba86af9

which means that 1 + 1/2 + 1/3 + 1/4 ... 1/n is Ө(ln n) = Ө(log n); we can give strict bounds for the number of times that printf is called (c(n): n log n <= c(n) < n log n + 2n. Since n log n grows faster than 2n, we can keep only the former and notice that both bounds belong to Ө(n log n) and hence c(n) belongs to Ө(n log n) as well (Ө(F) means that the function is both Ω(F) and O(F)).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Time complexity of nested for loops where inner loop depends on outer loop

Time complexity of nested loop dependent on the outer loop

What is the time complexity of this nested loop where the inner loop grows by a factor of the outer?

Time complexity where inner loop increases according to outer loop

Time complexity of the inner loop

Complexity where inner loop depends on outer loop

Time complexity nested loop

Time complexity of nested for loop

Time complexity for a nested for loop

Time Complexity - nested for loop

Time complexity on nested for loop

Nested parallel for loop: "Parallel outer for loop" in "parallel inner for loop as a function"

Back to inner for loop from outer loop - Python Nested Loop

What is the time complexity of this for loop nested in a while loop?

Time complexity with for loop nested in while loop

Time complexity of an unorthodox nested for loop

reduce the time complexity of nested for loop

Average Time Complexity of Nested Loop

Time complexity of a triple nested for loop

Time complexity of nested while loop

Complexity Of Nested Loop, With Outer Loop Variable Diminishing By Half Each Iteration

What is the Time Complexity of this code sample? like nested loop, but inner loop is a fixed number

Time complexity for inner loop from 0 to i

Time complexity of loop with multiple inner loops

R nested foreach %dopar% in outer loop and %do% in inner loop

Time complexity of nested while loop inside nested for loop

How to find the time complexity of nested for-loop

How to calculate time complexity of nested loop?

What is the time complexity of the following nested loop dependency?