adding reciprocals of odd nos. with + & - alternatively

R

Robocop

Hi Folks!
heres my question

adding: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15....

this is what i tried

to get ODD nos. i wrote 2*i+1 when *first line* is executed it adds
up all the odd deno. fraction, and in second sum it subtracts 1/3, 1/7
....., so shouldn't i multiple the second line by 2? so that it
subtract 1/3 , 1/7 and then again subtracts from the remaining sum?
like this::: sum = sum - 2*sign/(2*i+1) ; // MODIFIED SECOND LINE

sum = sum + sign /i; //----FIRST LINE
sum = sum - sign / (2*i+1); //----SECOND LINE



Code:
public class Strtry
{
public static void main (String[] parameters)
{

double sum = 0;
double sign = 1;
long start = System.currentTimeMillis();
for (int i = 1; i < 1000000000; i = 2*i +1)
{
sum = sum + sign /i;
sum = sum - sign / (2*i+1);

}
long finish = System.currentTimeMillis();
IO.println((finish - start)/1000);
IO.println(4*sum);
}
}
 
A

Andrew Hobbs

Robocop said:
Hi Folks!
heres my question

adding: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15....

this is what i tried

to get ODD nos. i wrote 2*i+1 when *first line* is executed it adds
up all the odd deno. fraction, and in second sum it subtracts 1/3, 1/7
...., so shouldn't i multiple the second line by 2? so that it
subtract 1/3 , 1/7 and then again subtracts from the remaining sum?
like this::: sum = sum - 2*sign/(2*i+1) ; // MODIFIED SECOND LINE

sum = sum + sign /i; //----FIRST LINE
sum = sum - sign / (2*i+1); //----SECOND LINE



Code:
public class Strtry
{
public static void main (String[] parameters)
{

double sum = 0;
double sign = 1;
long start = System.currentTimeMillis();
for (int i = 1; i < 1000000000; i = 2*i +1)
{
sum = sum + sign /i;
sum = sum - sign / (2*i+1);
[/QUOTE]

Run through the first few cycles and plug the numbers in.  You will find
that you are producing the sum of

1 - 1/3 + 1/3 - 1/7 + 1/7 - 1/15  Not quite what you expect.

In fact just looking at the equation it should be clear that i and 2*i + 1
are not consecutive odd numbers.

try i + 4 in the for loop and

i and (i + 2) as the denominators in two arithmetic expressions.

Andrew


--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
12 Ashover Grove
Carine   W.A.
Australia         6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
D

Dale King

Andrew Hobbs said:
Run through the first few cycles and plug the numbers in. You will find
that you are producing the sum of

1 - 1/3 + 1/3 - 1/7 + 1/7 - 1/15 Not quite what you expect.

And on a side note, due to the fact that you are using floating point with
limited precision, you will get a much more accurate answer if you start
from the smallest terms and work toward the biggest ones.

Here is a page that might help explain it:

http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/overview/reals.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top