0

sum $\gets 0$
for $i\gets 1$ to $n$ do
$~~~~$ for $j \gets 1$ to $\left\lfloor\dfrac{n}{i}\right\rfloor$ do
$~~~~~~~~~$ sum $\gets$ sum + ary$[i]$

I know $\sum_{i=1}^{n} = \frac{n(n+1)}{2}$ so Im pretty sure I can just say $O(n^2)$ but Im not sure if this is the answer because perhaps its not precise enough. Im not sure how to incorporate the floor function. I also feel like the sum doesnt go to n it goes to $\left\lfloor\dfrac{n}{i}\right\rfloor$.

yako
  • 311

1 Answers1

0

Up to the rounding of the upper bound what you are looking for is the sum $n+\frac{n}{2}+\frac{n}{3}+...+\frac{n}{n}$, which is $n$ times a partial sum of the harmonic series. You can find an approximation here, Is there a partial sum formula for the Harmonic Series?, and basically, your complexity will be $O(n\log n)$.