for (int i=2; i<=e; i++)
for (int j=i; j<=e; j+=i)
Loop i from 2 to e, and for each i, start j at i and keep adding i until e.
You can think of it as running e/i times for each i.
(e is an arbitrary positive integer, not 2.71)
Written as a formula:
i=2∑eie=ei=2∑ei1
It becomes the harmonic series multiplied by e.
Now, for a monotonically increasing function, the following holds.
∫n−1mf(x)dx≤i=n∑mf(x)≤∫nm+1f(x)dx
Let f(x) = 1/x; since f(x) = 1/x is a monotonically increasing function,
let's substitute it in and find the time complexity. (substitute f(x) = 1/x, n = 2, m = e)
Lower bound :
e∫1ex1dx=e(loge−log1)=eloge=Ω(eloge)
Upper bound :
e∫2e+1x1dx=e(log(e+1)−log2)=O(eloge)
We usually care about the upper bound, so we can conclude the following.
O(eloge)
Comments
Leaving a comment stores your nickname (or GitHub profile info). Privacy policy