Compute pi by using Gregory series

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2322 Views - Last Post: 02 October 2012 - 01:17 PM Rate Topic: -----

#1 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 q81101  Icon User is offline

  • New D.I.C Head

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

Re: Compute pi by using Gregory series

Posted 29 September 2012 - 11:24 AM

View Postbaavgai, 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....
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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.
Was This Post Helpful? 1
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Compute pi by using Gregory series

Posted 29 September 2012 - 11:58 AM

View Postbaavgai, 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.



View PostSimown, 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

Was This Post Helpful? 2
  • +
  • -

#9 q81101  Icon User is offline

  • New D.I.C Head

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

Re: Compute pi by using Gregory series

Posted 29 September 2012 - 12:11 PM

View Postbaavgai, 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?
Was This Post Helpful? 0
  • +
  • -

#10 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 q81101  Icon User is offline

  • New D.I.C Head

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

Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:22 PM

View Postbaavgai, 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()
    

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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)
>>> 


Was This Post Helpful? 0
  • +
  • -

#14 q81101  Icon User is offline

  • New D.I.C Head

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

Re: Compute pi by using Gregory series

Posted 02 October 2012 - 12:32 PM

View Postbaavgai, 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.
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • 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

Was This Post Helpful? 3
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2