# Harmonic sums - Recursive

• 03-25-2011, 11:22 AM
überfuzz
Harmonic sums - Recursive
Gnarly in a recursive function!

I can't see where I'm messing things up. Maybe it's time for a coffee break..? Anyhow, I'm trying to calculate the harmonic sum of a series.
Code:

```  public static double harmonicSum(int n)   {       double temp = 0;       if( n == 1)       {         return 1;       } //System.out.println("Test");    Seems to give the right number of out puts.       temp = 1/n;       return temp + harmonicSum(n-1);   }```
Please point out the faulty stuff.
• 03-25-2011, 11:25 AM
sunde887
what happens if you make the recursive line
Code:

`return ((double)1 / n) + harmonicSum(n - 1);`
If that works, great, if not, what exactly is your problem?
• 03-25-2011, 11:27 AM
JosAH
Quote:

Originally Posted by sunde887
what happens if you make the recursive line
Code:

`return ((double)1 / n) + harmonicSum(n - 1);`
If that works, great, if not, what exactly is your problem?

Why not just do this?

Code:

`return 1.0 / n + harmonicSum(n - 1);`
kind regards,

Jos
• 03-25-2011, 11:29 AM
sunde887
That works too I suppose :) (sometimes I don't enjoy thinking logically)
• 03-25-2011, 11:42 AM
überfuzz
Thanks!

Not the first time I preform an int division. Probably not the last either...