# Thread: Harmonic sums - Recursive

1. Member
Join Date
Feb 2011
Posts
43
Rep Power
0

## 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.
Java 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.

2. what happens if you make the recursive line
Java Code:
`return ((double)1 / n) + harmonicSum(n - 1);`
If that works, great, if not, what exactly is your problem?

3. Originally Posted by sunde887
what happens if you make the recursive line
Java Code:
`return ((double)1 / n) + harmonicSum(n - 1);`
If that works, great, if not, what exactly is your problem?
Why not just do this?

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

Jos

4. That works too I suppose :) (sometimes I don't enjoy thinking logically)

5. Member
Join Date
Feb 2011
Posts
43
Rep Power
0
Thanks!

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•