-1

What is sum of the finite series $1+\frac12+\frac13+\frac14+\frac15+\cdots +\frac1n,$ for finite value of $n\in \mathbb{N}.$

The above question arose in finding the worst-case time-complexity for the below C- code:

int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}

Here, the outer loop executes $\log_2n$ times, and need to sum up the number of times, the step for 'count += 1' executes.

Edit:
Am equally concerned about the correct series being derived.

My reasoning is:
For an input integer $n,$ the outermost loop of fun() is executed $log_2 (n)$ times. For each run of outer loop, the inner loop statement of fun() is executed the same number of times, as the value of $i:$
First run of the outer loop: $i = \frac{n}{log_2(2)} : n$ times,
Second run of the outer loop:$i = \frac{n}{log_2(4)} : \frac n2$ times,
Third run of the outer loop: $i = \frac{n}{log_2(8)} : \frac n3$ times,
Fourth run of the outer loop: $i = \frac{n}{log_2(16)} : \frac n4$ times,
$\cdots$
Last run of the outer loop:$i = \frac{n}{log_2(2^n)} : 1$ times.

So, need to sum up all the number of times, to get the worst case time complexity $n + \frac n2 + \frac n3 + \frac n4 +... + 1 $

$= n(1 + \frac12 + \frac13 + \frac14 + \frac15 + \frac16 + \cdots+ \frac1n)$

Edit #2:
Yes am wrong, in getting a wrong series, and the one suggested by @RossMillikan is correct. Mine would have been correct, if the outer loop had $i$ divided by a different quantity than $2,$ in each successive run. Though am unable to find that value.

Request to provide that value.

jiten
  • 4,960
  • 1
    https://math.stackexchange.com/q/52572/1118406 – Sine of the Time Dec 26 '24 at 14:26
  • 4
    Are you looking for https://en.wikipedia.org/wiki/Harmonic_number – Amrut Ayan Dec 26 '24 at 14:27
  • 2
    These are the harmonic numbers. They are closely approximated by $\ln n+0.57721\dots$, where $\ln n$ (or $\log_en$) is the natural logarithm and where $\gamma:=0.57721\dots$ is the Euler–Mascheroni constant. See here: https://en.wikipedia.org/wiki/Harmonic_number – Akiva Weinberger Dec 26 '24 at 14:29
  • Thanks, and hope that got derived the correct series, as a different one is derived at the problem's source, at: https://www.geeksforgeeks.org/questions/algorithms-analysis-of-algorithms-question-3/ You can see the series derived, by selecting any answer there. – jiten Dec 26 '24 at 14:31
  • You got a correct answer to the question you asked, but the sum you want is $\frac 12 + \frac 14 + \frac 18 + \ldots \frac 1{2^n}$. This is a geometric series that can be summed. – Ross Millikan Dec 26 '24 at 14:35
  • @RossMillikan I derived a different series, please see my edit, and suggest where I erred. – jiten Dec 26 '24 at 14:50
  • The first time through the outer loop you go through the inner loop $n$ times, then $\frac n2$, then $\frac n4$ and so on. I don't see where your $\log_2$s come from. It is true you go through the outer loop $\log_2 n$ times. – Ross Millikan Dec 26 '24 at 15:11
  • @RossMillikan Request a dividing factor for the variable i, in the outer loop; that makes my series correct. – jiten Dec 26 '24 at 15:14
  • 1
    I cannot understand the relationship between the title question and the code snippet. If you want to write code to sum the harmonic series, then can't you just do it directly instead of a nested loop? – ziggurism Dec 26 '24 at 15:40
  • 1
    You can just store $n$ and make your outer loop $i$ from $1$ to $n$ and the inner loop $j$ from $1$ to $n/i$ – Ross Millikan Dec 26 '24 at 16:01
  • @RossMillikan Thanks a lot for that. – jiten Dec 26 '24 at 16:05

3 Answers3

3

These are called the harmonic numbers, and can be approximated by $\sum_{i=1}^n \frac{1}{i} = H_n \approx \ln(n) + \gamma + O(1/n)$ where $\gamma$ is a constant called the Euler-Mascheroni constant.

1

In this article, you will find a graph that visually demonstrates the relationship between harmonic numbers and the natural logarithm.

So, if you

  • visually interpret the integral as the area under the curve of a non-negative function $~f(x),~$

  • define $~\log(n)~$ to refer to the natural log,

  • and define $~\displaystyle H(n) = \sum_{i=1}^n \frac{1}{i},~$

it is easy to see that

$$\int_1^n ~\frac{1}{x} ~dx < H(n) < 1 + \left[ ~\int_1^n ~\frac{1}{x} ~dx ~\right] \implies $$

$$ \log(n) < H(n) < 1 + \log(n) ~: ~\text{for all} ~n \in \Bbb{Z^+}.$$

user2661923
  • 42,303
  • 3
  • 21
  • 46
1

Let $n=2^m$, let's analyze the value of count in your code.

  • In the first outer iteration, $i = n=2^m$.
  • In the second iteration, $i = 2^{m-1}$.
  • In the third iteration, $i=2^{m-2}$.
  • In the $k$-th iteration, $i=2^{m-(k-1)}$.
  • In the $m$-th iteration, $i=2$
  • In the $(m+1)$-th iteration, $i=1$.

Thus, given that $n=2^m$, the value of count is

$$\sum_{j=0}^{m}2^j=2^{m+1}-1=2n-1=O(n)$$

Siong Thye Goh
  • 153,832
  • Seems the worst case time complexity is just $O(n),$ as the exact value of count is: $2n-1.$ It is not obvious why you changed it to $O(n log n)$? – jiten Dec 26 '24 at 16:25
  • Am not clear why there is a difference between the 'time complexity', & the 'count value'. Can you please show the difference for some higher value of $n?$ – jiten Dec 26 '24 at 16:32
  • 1
    hmm... you might be right, let's unaccept the solution and let me delete the post first? – Siong Thye Goh Dec 26 '24 at 16:32