Time Complexity Nested Loop Inner Loop + Outer Loop
问题 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); } } 回答1: 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