# Compute pi by using Gregory series

• (2 Pages)
• 1
• 2

## 16 Replies - 4255 Views - Last Post: 02 October 2012 - 01:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293586&amp;s=2c5d02d1082797eb9ec4c0b497732805&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

# Compute pi by using Gregory series

Posted 28 September 2012 - 11:30 PM

Gregory series is
pi = 4( 1 - (1/3) + (1/5) - (1/7) + (1/9) - (1/11)+ ........... (-1)^(i+1) / (2i -1))

I have to write a program that displays the pi value for i = 10000, 20000,.....and 100000

```def main():
i = 1
sum = 0
for i in range(10000, 100000 +1):
pi = 4 * ( 1 - ((-1) ** ( i + 1) / (2 * i - 1)))

if i == 10000 or i == 20000 or i == 30000 or i == 40000 or \
i == 50000 or i == 60000 or i == 70000 or i == 80000 or \
i == 90000 or i == 100000:

sum = pi

print("pi is", sum)

main()
```

my code is only able to calculate the last term from the formula instead of the summation of series

can anyone help?

As a beginner of python, I am not good in loop stuff.

Is This A Good Question/Topic? 0

## Replies To: Compute pi by using Gregory series

### #2 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 12:04 AM

You are setting sum to the new value every time you loop so it's as simple as adding to sum, instead of replacing it with the last value:
```sum = sum + pi
```

You want to keep adding to the sum anyway, even if it doesn't divide by 10000 but only print it when it divides by 10000 so, your program could be:
```def main():
i = 1 # Don't need this definition of i
sum = 0
for i in range(10000, 100000 +1):
pi = 4 * ( 1 - ((-1) ** ( i + 1) / (2 * i - 1)))

sum = sum + pi

# Standard programming arithmetic, if i is a multiple of 10000 (so 20000, 30000 etc..) without writing them all out
if(i % 10000 == 0):
print("pi is", sum)

main()

```

This post has been edited by Simown: 29 September 2012 - 12:04 AM

### #3 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 03:06 AM

Hmm.... you're actually doing the Leibniz series, Gregory's series where x=1. References are good: http://en.wikipedia..../Leibniz_series

So, your math ain't going to hack it. The series builds. You definitely don't want to be multiplying by 4 all the time. The value is pi/4, so you only want sum*4 for your output.

### #4 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 03:21 AM

I tried this out, so a few gotchas. In my python version, -1 ** n always yields -1, which is wrong. Make sure you're working in float. This means you'll want to use 1.0 rather than just 1. You must start at 1, not 10000.

### #5 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 03:35 AM

My Python version too, but strangely pow(-1, n) will yield the correct answer which I thought was just different syntax.

### #6 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 11:24 AM

baavgai, on 29 September 2012 - 03:06 AM, said:

Hmm.... you're actually doing the Leibniz series, Gregory's series where x=1. References are good: http://en.wikipedia..../Leibniz_series

So, your math ain't going to hack it. The series builds. You definitely don't want to be multiplying by 4 all the time. The value is pi/4, so you only want sum*4 for your output.

I understand what you said, but I don't know how to convert that into a code.

when I try I am not getting any answer close to 3.14 (pi).

```def main():

import math

sum = 0

for i in range(1, 100000 +1):

pi = (math.pi /4)

sum  = pi

if(i % 10000 == 0):

print("pi is", sum)

main()
```

i DON'T KNOW what I am doing....

### #7 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 11:33 AM

Do it on paper.
```sum = 1.0
sum += -1.0 / 3.0
sum += 1.0 / 5.0
sum += -1.0 / 7.0
sum += 1.0 / 9.0
sum += -1.0 / 11.0
sum += 1.0 / 13.0
...

```

Now, you really just need to do those sum += steps in a loop. Clearly, the numerator simply toggles the sign. The denominator just grows by 2. It's not much more complex than that. The value for pi, at any moment, is sum*4.

Now that I've described it in English, you try to describe it in Python. Good luck.

### #8 sepp2k

• D.I.C Lover

Reputation: 2226
• Posts: 3,429
• Joined: 21-June 11

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 11:58 AM

baavgai, on 29 September 2012 - 12:21 PM, said:

I tried this out, so a few gotchas. In my python version, -1 ** n always yields -1, which is wrong.

Simown, on 29 September 2012 - 12:35 PM, said:

My Python version too, but strangely pow(-1, n) will yield the correct answer which I thought was just different syntax.

It's a precedence issue. -1 ** n is parsed as -(1 ** n). ** binds more tightly than unary -¹. (-1) ** n will give the intended result.

¹ At first this was completely counter-intuitive to me, but on second thought it kind of makes sense because it's the same as in maths: -xy is the same as -(xy), not (-x)y.

This post has been edited by sepp2k: 29 September 2012 - 12:09 PM

### #9 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 12:11 PM

baavgai, on 29 September 2012 - 11:33 AM, said:

Do it on paper.
```sum = 1.0
sum += -1.0 / 3.0
sum += 1.0 / 5.0
sum += -1.0 / 7.0
sum += 1.0 / 9.0
sum += -1.0 / 11.0
sum += 1.0 / 13.0
...

```

Now, you really just need to do those sum += steps in a loop. Clearly, the numerator simply toggles the sign. The denominator just grows by 2. It's not much more complex than that. The value for pi, at any moment, is sum*4.

Now that I've described it in English, you try to describe it in Python. Good luck.

```def main():

sum = 0

for i in range(1, 100000 +1):
sum  = ( (-1) ** (i + 1) / (2 * i - 1) )  # The series

sum += (sum * 4)

if(i % 10000 == 0):

print("pi is", sum)

main()
```

Not anywhere close to the pi value

which part I did wrong?

### #10 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: Compute pi by using Gregory series

Posted 29 September 2012 - 05:47 PM

I got it.

The only thing wrong is I should that the equation be something else instead of sum

thanks everyone

### #11 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: Compute pi by using Gregory series

Posted 30 September 2012 - 03:14 AM

So what did your final solution look like? You were nowhere close, before.

### #12 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:22 PM

baavgai, on 30 September 2012 - 03:14 AM, said:

So what did your final solution look like? You were nowhere close, before.

```def main():

sum = 0

for i in range(1, 100000 +1):
n  = ( (-1) ** (i + 1) / (2 * i - 1) )

sum += n * 4

if(i % 10000 == 0):

print("pi is", sum)

main()

```

### #13 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:29 PM

Thanks for coming back with an answer. Unfortunately, that still looks like the one that ain't working:
```>>> def pi1():
...     sum = 0
...     for i in range(1, 100000 +1):
...             n  = ( (-1) ** (i + 1) / (2 * i - 1) )
...             sum += n * 4
...             if(i % 10000 == 0):
...                     print("pi is", sum)
...
>>> pi1()
('pi is', -19996)
('pi is', -39996)
('pi is', -59996)
('pi is', -79996)
('pi is', -99996)
('pi is', -119996)
('pi is', -139996)
('pi is', -159996)
('pi is', -179996)
('pi is', -199996)
>>>

```

### #14 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:32 PM

baavgai, on 02 October 2012 - 12:29 PM, said:

Thanks for coming back with an answer. Unfortunately, that still looks like the one that ain't working:
```>>> def pi1():
...     sum = 0
...     for i in range(1, 100000 +1):
...             n  = ( (-1) ** (i + 1) / (2 * i - 1) )
...             sum += n * 4
...             if(i % 10000 == 0):
...                     print("pi is", sum)
...
>>> pi1()
('pi is', -19996)
('pi is', -39996)
('pi is', -59996)
('pi is', -79996)
('pi is', -99996)
('pi is', -119996)
('pi is', -139996)
('pi is', -159996)
('pi is', -179996)
('pi is', -199996)
>>>

```

I don't know why you are getting those output. As I run my code in cmd, it's work correctly.

### #15 sepp2k

• D.I.C Lover

Reputation: 2226
• Posts: 3,429
• Joined: 21-June 11

## Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:36 PM

It's a Python 2 vs. Python 3 issue.

Apparently baavgai is using Python 2. That's why his print output shows up as tuples. More importantly it's why on his system the calculations use integer division, which leads to wrong results.

The code does indeed work perfectly fine using Python 3.

This post has been edited by sepp2k: 02 October 2012 - 12:36 PM

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }