Results 1 to 5 of 5
Thread: Harmonic sums - Recursive
- 03-25-2011, 10:22 AM #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.
Please point out the faulty stuff.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); }
- 03-25-2011, 10:25 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
what happens if you make the recursive line
If that works, great, if not, what exactly is your problem?Java Code:return ((double)1 / n) + harmonicSum(n - 1);
- 03-25-2011, 10:27 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-25-2011, 10:29 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That works too I suppose :) (sometimes I don't enjoy thinking logically)
- 03-25-2011, 10:42 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
recursive function
By jayden in forum New To JavaReplies: 11Last Post: 09-02-2010, 03:00 PM -
Problem with Sums and Averages in Sales Report
By DavidEvans in forum New To JavaReplies: 9Last Post: 04-21-2010, 08:57 PM -
recursive method
By michail in forum New To JavaReplies: 0Last Post: 01-31-2010, 01:50 PM -
basic help with Recursive
By syntrax in forum New To JavaReplies: 3Last Post: 12-15-2009, 06:19 AM -
Help with recursive implementation
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks