Results 1 to 9 of 9
Thread: Help with summing series
- 11-17-2009, 02:56 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Help with summing series
I am trying to write a method to compute the series:
m(i) = 1/2 + 2/3 + ... + i/(i + 1)
I am trying to display them in a table but I just can't get it to come out right. I would prefer the table to look like the following:
i m(i)
---------------
1 99
2 45645
...
29 456
30 30
Can anyone out there help me do this? I would be much obliged!Last edited by xplsivo; 11-17-2009 at 03:44 PM.
- 11-17-2009, 02:58 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
The table:
The table should resemble this:
i m(i)
---------------
1 99
2 45645
...
29 456
30 30Last edited by xplsivo; 11-17-2009 at 03:44 PM.
- 11-17-2009, 04:03 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Use a for loop to produce each row. The printf() method will allow you to print the accumulating total so that it has 4 decimal places.
As no-one is likely to actually write the code for you, you will get the best help if you post your code and describe whatever problem you have with it.
- 11-17-2009, 05:03 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
- 11-17-2009, 05:18 AM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
you could say:
int value = 0;
System.out.println("i - m(i)");
System.out.println("---------------------");
for( int i = 0; i < 21; i++)
{
System.out.println( i + " - " + value + "\n" );
value = (i+1)/(i+2);
}
- 11-17-2009, 05:34 AM #6
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Oh, and something to notice, then you divide int you wont get a double you have to divide them as doubles. Also i forgot the +=, it should be:
value += ((double) i + 1)/(i+2);
the second one is automatically casted as a double.
here is the exact code, tested:
Java Code:public class series { public static void main( String args[] ) { double value = 0; System.out.println("i - m(i)"); System.out.println("---------------------"); for( int i=0; i < 21; i++) { System.out.println( i + " - " + value + "\n" ); value += ((double) i + 1)/(i+2); } } }
- 11-17-2009, 05:50 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
- 11-17-2009, 05:28 PM #8
You mean recursively? Instead of having a for loop you return value and your counter and then have a base case to break if your counter reaches 21 or your value reaches a certain spot.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 11-23-2009, 07:37 PM #9
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Similar Threads
-
Generating Fibonacci Series with a Multithreaded Java Program
By firesauce in forum Threads and SynchronizationReplies: 1Last Post: 10-20-2009, 07:26 AM -
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03-23-2009, 11:16 AM -
Getting ExceptionInInitializer exception when i am trying to code a time series chart
By neeraj.singh in forum AWT / SwingReplies: 2Last Post: 02-17-2009, 03:20 PM -
No Fluff Just Stuff Software Symposium Series 2007,
By orchid in forum Reviews / AdvertisingReplies: 0Last Post: 04-08-2007, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks