开发者

Any efficient way to calculate the sum of harmonic series upto nth term? 1 + 1/2 + 1/3 + --- + 1/n =? [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 12 years ago.

Improve this question

Is there any formula for this series "1 +开发者_JAVA百科 1/2 + 1/3 + --- + 1/n = ?" I think it is a harmonic number in a form of sum(1/k) for k = 1 to n.


As it is the harmonic series summed up to n, you're looking for the nth harmonic number, approximately given by γ + ln[n], where γ is the Euler-Mascheroni constant.

For small n, just calculate the sum directly:

double H = 0;
for(double i = 1; i < (n+1); i++) H += 1/i;


If I understood you question correctly, reading this should help you: http://en.wikipedia.org/wiki/Harmonic_number


Here's one way to look at it:

http://www.wolframalpha.com/input/?i=sum+1/j,+j%3D1+to+n


function do(int n) 
{
    if(n==1)
        return n;

    return 1/n + do(--n); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜