Subscribe to MentalFloss - PyBlog

Leibniz Formula

OK. I saw a neat post on Java forum (Leibniz series) and I just had to do it. I'm a novice with mathematical stuff but the output seems right. Check it out.

```#sum series of numbers to aprox pi
# I have no idea what I'm doing and I don't know math very well...
# 2.7

def main():
ops = ["-","+"]
op  = False
iteration = int(raw_input("How many terms?: "))*2

total = 1.0
for i in xrange(3,iteration,2):
print "%f%s1.0/%d.0" % (total, ops[op], i)
total = eval("%f%s1.0/%d.0" % (total, ops[op], i))
op = not op
print total*4

if __name__ == '__main__':
main()

```

So, here's what happens when it runs for 1000 iterations.

output said:

How many terms?: 1000
1.000000-1.0/3.0
0.666667+1.0/5.0
0.866667-1.0/7.0
0.723810+1.0/9.0
0.834921-1.0/11.0
0.744012+1.0/13.0
0.820935-1.0/15.0
0.754268+1.0/17.0
0.813092-1.0/19.0
0.760460+1.0/21.0
0.808079-1.0/23.0
0.764601+1.0/25.0
0.804601-1.0/27.0
0.767564+1.0/29.0
0.802047-1.0/31.0
0.769789+1.0/33.0
0.800092-1.0/35.0
0.771521+1.0/37.0
0.798548-1.0/39.0
0.772907+1.0/41.0
0.797297-1.0/43.0
0.774041+1.0/45.0
0.796263-1.0/47.0
0.774986+1.0/49.0
0.795394-1.0/51.0
0.775786+1.0/53.0
0.794654-1.0/55.0
0.776472+1.0/57.0
0.794016-1.0/59.0
0.777067+1.0/61.0
0.793460-1.0/63.0
0.777587+1.0/65.0
0.792972-1.0/67.0

#-- snip stuff --

0.785646-1.0/1991.0
0.785144+1.0/1993.0
0.785646-1.0/1995.0
0.785145+1.0/1997.0
0.785646-1.0/1999.0
3.1405829995

So, I mean... it ends up being 3.14 so I think I got the algorithm right.

(PS: Sorry if I sound like a total fool. I really am very dense when it comes to math)

EDIT:
OK here it is running for 10M iterations:

output said:

How many terms?: 10000000
3.1416758

I am blown away at this thing. I have NEVER calculated pi before and now I can -- how fun!

Page 1 of 1

andrewsw

18 March 2014 - 03:30 PM
Looks right.

http://en.wikipedia....mula_for_%CF%80

Why not disable the printing temporarily and increase the number of iterations? It will be reassuring to see the number approaching nearer to pi.

Mind you, there will be floating-point errors that the theoretical basis for this doesn't help with.
2

MentalFloss

18 March 2014 - 03:53 PM

andrewsw, on 18 March 2014 - 10:30 PM, said:

Looks right.

http://en.wikipedia....mula_for_%CF%80

Why not disable the printing temporarily and increase the number of iterations? It will be reassuring to see the number approaching nearer to pi.

Mind you, there will be floating-point errors that the theoretical basis for this doesn't help with.

OK. Thank you. I am running it for 10million.

So, this problem seems kind of interesting. What it seems like it's doing is taking the difference between increasing fractions by buffering a middle sized fraction in between.

Can't I just take a segment and calculate it and then calculate a segment equally far away for both iterations (one where it starts at + and then the other where it starts at -) and see which one is closer to pi? I don't understand why I have to calculate starting at 1.
0

andrewsw

18 March 2014 - 04:21 PM
Sorry, I'm not sure what you are asking. Perhaps you should post this as a question in the forum.

Good luck.
0

MentalFloss

18 March 2014 - 04:40 PM

andrewsw, on 18 March 2014 - 11:21 PM, said:

Sorry, I'm not sure what you are asking. Perhaps you should post this as a question in the forum.

Good luck.

Yeah. I might. Thanks.
0
Page 1 of 1

There are no Trackbacks for this entry

S M T W T F S
1 2
3456789
10111213141516
17181920212223
24252627282930
31