Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2011, 08:36 PM   PM User | #1
sackstein
Regular Coder

 
Join Date: Jan 2009
Posts: 160
Thanks: 40
Thanked 1 Time in 1 Post
sackstein is an unknown quantity at this point
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..
sackstein is offline   Reply With Quote
Old 03-08-2011, 12:21 AM   PM User | #2
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 PM.


Advertisement
Log in to turn off these ads.