views:

167

answers:

4

Is there any formula for this series? I think it is a harmonic number in a form of sum(1/k) for k = 1 to n

+1  A: 

Here's one way to look at it:

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

duffymo
+2  A: 

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

domsterr
+2  A: 

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;
You
+2  A: 
function do(int n) 
{
    if(n==1)
        return n;

    return 1/n + do(--n); 
}
Nico
While recursive solutions look elegant, in this case it's inappropriate.
You
If the number is large enough you will get a stack overflow, or you will be adding basically zero, and not really changing the value much.
James Black
I figured he'd be using small sample numbers
Nico