Results 1 to 5 of 5
  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Summing a Series: Is this correct??

    Write a program to sum the following series:
    (1/3) + (3/5) + (5/7) + (7/9) + (9/11) + (11/13) + ... + (95/97) + (97/99)

    This is my attempt.
    Java Code:
    public class SummingSeries {
    	public static void main(String[] args) {
    		double sum = 0.0;
    		for(double i = 1; i <= 99; i++) {
    			sum += ((i+2)/(i+4));
    		}
    		System.out.println("The sum is " + sum);
    	}
    }
    This is the output I get.
    The sum is 92.73308433232398
    Is my output correct? I have no idea what the desired output is supposed to be so I need to know if 92.73308433232398 is actually the sum of the series mentioned above. Is there anything wrong with my code? I think my code is correct but I'm not sure.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Summing a Series: Is this correct??

    Quote Originally Posted by son012189 View Post
    Write a program to sum the following series:
    (1/3) + (3/5) + (5/7) + (7/9) + (9/11) + (11/13) + ... + (95/97) + (97/99)

    This is my attempt.
    Java Code:
    public class SummingSeries {
    	public static void main(String[] args) {
    		double sum = 0.0;
    		for(double i = 1; i <= 99; i++) {
    			sum += ((i+2)/(i+4));
    		}
    		System.out.println("The sum is " + sum);
    	}
    }
    This is the output I get.

    Is my output correct? I have no idea what the desired output is supposed to be so I need to know if 92.73308433232398 is actually the sum of the series mentioned above. Is there anything wrong with my code? I think my code is correct but I'm not sure.
    Take a few examples for 'i', say 1, 2 and 3 and see if you get the correct terms: 3/5, 4/6, 5/7; what is that 4/6 doing there?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Summing a Series: Is this correct??

    I see my mistake now thanks. I fixed my code.

    Java Code:
    public class SummingSeries {
    	public static void main(String[] args) {
    		double sum = 0.0;
    		for(double i = 1; i <= 49; i++) {
    			sum += ((2*i-1)/(2*i+1));
    		}
    		System.out.println("The sum is " + sum);
    	}
    }
    Is my code correct now? or is there something else I need to fix?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Summing a Series: Is this correct??

    Quote Originally Posted by son012189 View Post
    I see my mistake now thanks. I fixed my code.

    Java Code:
    public class SummingSeries {
    	public static void main(String[] args) {
    		double sum = 0.0;
    		for(double i = 1; i <= 49; i++) {
    			sum += ((2*i-1)/(2*i+1));
    		}
    		System.out.println("The sum is " + sum);
    	}
    }
    Is my code correct now? or is there something else I need to fix?
    It's fine now; you also could've done this:

    Java Code:
    for (double i= 1; i <= 97; i+= 2)
       sum+= i/(i+2);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Summing the digit
    By gozuhair in forum New To Java
    Replies: 13
    Last Post: 07-18-2011, 07:09 AM
  2. summing value within Hashtable
    By dc0m in forum New To Java
    Replies: 4
    Last Post: 09-23-2010, 12:17 AM
  3. Perrin series
    By moamen in forum New To Java
    Replies: 6
    Last Post: 12-04-2009, 05:27 PM
  4. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 07:37 PM
  5. 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

Posting Permissions

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