Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

algorithm - Asymptotic analysis

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) {

I realize what happens, for every i++ you have 1 level of multiplication less of j.

i = 1, you get j = 1, 2, 3, ..., 100

i = 2, you get j = 1, 3, 5, ..., 100

I'm not sure how to think this in terms of Big-theta.

The total of j is N, N/2, N/3, N/4..., N/N (My conclusion)

How would be best to try and think this as a function of N?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...