# Thread: Help me with this series

1. ## Help me with this series

Alright I'm having lots of trouble estimating pi using this series. I must be missing something.

My book says you can estimate pi using this series:

Code:
`pi = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ... (-1 / 2i - 1) + (1 / 2i + 1) )`
I'm sure a lot of you are familiar with this.

This is my code:

Code:
```double sum = 0;
for(int i = 0; i <= data2; i++){
sum +=  ((- (1.0 / (2.0 * i - 1.0)))  + (1.0 / (2.0 * i + 1.0)));
}
sum *= 4.0;```
My outcomes are always like 4.45264 ... I'm thinking it has to do with my parenthesis, but I've rearranged them so many times I can't figure it out. Also, data2 is just some arbitrary value that the user enters.

Any help would be greatly appreciated. I'm pretty stuck on this.

2. Since the series is
Code:
`pi = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ... (+1 / 2i - 1) - (1 / 2i + 1) )`
that may help. Also i starts at 1 and goes by twos.

3. Use a debugger to watch how sum changes on each iteration of the loop

What I suggest is that on each iteration compute the current term, i.e., 1, then -1/3, then 1/5, etc. It looks like you are trying to do two terms at a time, but when i = 0 you get 2, which definitely is not the result of 1 - 1/3.

4. Okay so like tabstop said, it looks like the book f-up the series by putting the negative on the wrong one. I also incremented by two and am getting better answers. here's the code:

Code:
```double sum = 0;
for(int i = 1; i <= data2; i+= 2){
sum +=  (( (1.0 / (2.0 * i - 1.0)))  + (-(1.0 / (2.0 * i + 1.0))));
}
sum *= 4.0;```
answer when data2 = 10: 3.0418

answer when data2 = 100: 3.1315

answer when data2 = 1000: 3.14059

answer when data2 = 20000: 3.14154

Is this how the series is suppose to work? Does this look correct?

I think it's right but I just want to be sure, since I changed the formula that the book gave.

5. Originally Posted by NoUse
I also incremented by two and am getting better answers.
Ah, but if you want to group pairs of terms together instead then incrementing by four makes more sense:
1/1 - 1/3
1/5 - 1/7
1/9 - 1/11
Notice the 1, 5, 9, ... progression. The denominator of the second term in the pair is easily obtained by adding 2 to the denominator of the first.

6. Originally Posted by laserlight
Ah, but if you want to group pairs of terms together instead then incrementing by four makes more sense:
1/1 - 1/3
1/5 - 1/7
1/9 - 1/11
Notice the 1, 5, 9, ... progression. The denominator of the second term in the pair is easily obtained by adding 2 to the denominator of the first.
Thanks for your help and quick responses.

We're suppose to be going by the book. One of the questions we have to answer using this program is how many terms of the series until you get your first 3.14159. Incrementing by 4 would ultimately change the series thus changing the amount of terms it takes to get there, would it not?

7. Originally Posted by NoUse
We're suppose to be going by the book. One of the questions we have to answer using this program is how many terms of the series until you get your first 3.14159. Incrementing by 4 would ultimately change the series thus changing the amount of terms it takes to get there, would it not?
No, it won't. The series itself is the same, of course, but what concerns the number of iterations is the fact that you are grouping consecutive terms into pairs. If your book told you to do this, then the number of iterations would be the same whether you increment by 2 or 4. The difference is that if you increment by 2, you would compute the terms as 1/(2i-1) and -1/(2i+1), but if you increment by four you would compute the terms as 1/i and -1/(i+2) respectively.