Asymptotic analysis

后端 未结 2 1960
再見小時候
再見小時候 2020-12-19 10:59

I\'m having trouble understanding how to make this into a formula.

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j += i) {
         


        
相关标签:
2条回答
  • 2020-12-19 11:24

    Well, you can methodically use Sigma notation:

    enter image description here

    0 讨论(0)
  • 2020-12-19 11:25

    So your question can be actually reduced to "What is the tight bound for the harmonic series 1/1 + 1/2 + 1/3 + ... + 1/N?" For which the answer is log N (you can consider it as continuous sum instead of discrete, and notice that the integral of 1/N is log N)

    Your harmonic series is the formula of the whole algorithm (as you have correctly concluded)

    So, your sum:

    N + N/2 + N/3 + ... + N/N = N * (1 + 1/2 + 1/3 + ... + 1/N) = Theta(N * log N)
    

    So the tight bound for the algorithm is N*log N

    See the [rigorous] mathematical proof here (see the "Integral Test" and "Rate of Divergence" part)

    0 讨论(0)
提交回复
热议问题