Results 1 to 9 of 9
  1. #1
    xplsivo is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Exclamation 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.

  2. #2
    xplsivo is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default The table:

    The table should resemble this:

    i m(i)
    ---------------
    1 99
    2 45645
    ...
    29 456
    30 30
    Last edited by xplsivo; 11-17-2009 at 03:44 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #4
    xplsivo is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Hmmm...

    This is what I have so far:


    Quote Originally Posted by xplsivo View Post
    for(int i=1; i<=20; i++){
    System.out.println(i + "\t\t" + convert(i,i));
    The code seems to be computing i/(i + 1) but it will not add the previous back to it.
    Last edited by xplsivo; 11-17-2009 at 03:43 PM.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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);
    }

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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);
             }
          }
       }

  7. #7
    xplsivo is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default :)

    Quote Originally Posted by collin389 View Post
    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);
             }
          }
       }
    This rocks, thanks! It does everything I need it to do, If I could just figure out how to write it where it computes it within its own method, then I'll be good to go. Could you point me in the right direction please?

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  9. #9
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Give us some hints on what your method needs to do. What are its parameters and return type?

Similar Threads

  1. Generating Fibonacci Series with a Multithreaded Java Program
    By firesauce in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-20-2009, 07:26 AM
  2. How to add a second series in jfreechart
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 11:16 AM
  3. Replies: 2
    Last Post: 02-17-2009, 03:20 PM
  4. No Fluff Just Stuff Software Symposium Series 2007,
    By orchid in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-08-2007, 08:13 PM

Tags for this Thread

Posting Permissions

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