Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    160
    Thanks
    40
    Thanked 1 Time in 1 Post

    Question calculating series

    The question I am having problems with is the following:

    Write a method to compute the following series:

    m(i) = 4(1 - 1/3 + 1/5 -1/7 + 1/9 - 1/11 + ... + 1/(2i-1) - 1/(2i+1)



    here is the code I have put together so far:

    Code:
    public class series {
    	
        public static double m(int i){
        	
        	double num = 0;
        	
        	for (i = i;i>0;i--){ //infinite times starting at i
        		num += 4*((1/(2*((double)i)-1))-(1/(2*((double)i)+1)));
        	}
        	return num;
        }
        public static void main(String args[]){
        	System.out.println("i     m(i)");
        	for (int i=10;i<=100;i=i+10)
        		System.out.println(i+"     "+(m(i)));
        }
    }
    for some reason it is not displaying the correct results what am I missing?

    The expected print out is :

    i m(i)
    10 3.04184
    20 3.09162

    etc but mine is starting at 3.809
    Last edited by sackstein; 03-06-2011 at 08:46 PM.

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Is the formula correct?

    With your logic in code I a1ways get this:
    i=10; num = 4(1/19 -1/21)
    i=9; num=4(1/17-1/19)
    i=8; num=4(1/15-1/17)
    i=7; num=4(1/13-1/15)
    i=6; num=4(1/11-1/13)
    i=5; num=4(1/9-1/11)
    i=4; num=4(1/7-1/9)
    i=3; num=4(1/5-1/7)
    i=2; num=4(1/3-1/5)
    i=1; num=4(1-1/3)

    Total will be 3.80952380952381

    And with m(i) formula you gave above with m= 10, I always get m(10) = 1-1/3+1/5-1/7+1/9-1/11+1/13-1/15+1/17-1/19+1/21

    it is not matched to 1/(2i-1) + 1/(2i+1). Please revise the formula once..
    Last edited by spchinta; 03-08-2011 at 12:30 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
    •