| | |
Approximation of Pi (Python)
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
For those of you who are inquisitive, here is a little Python program to approximate the value of pi to 77 digits. You can easily go further, just change the value n in range(n+1).
# a generator to approximate pi to n decimals # the result is a string # tested with Python24 def pi_generate(): """generator to approximate pi""" q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 while True: if 4 * q + r - t < m * t: yield m q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x) else: q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2) n = 77 digits = pi_generate() # build a list of characters, the leading 3 and n decimals pi_list = [] for i in range(n+1): pi_list.append(str(digits.next())) # insert the missing period at index 1 (after the 3) pi_list.insert(1, '.') #print pi_list # test # convert the list of characters to a string pi_str = "".join(pi_list) print "pi approximated to %d decimals (below it is the official pi):" % n print pi_str # official pi value pi_pub = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620" print pi_pub """ result --> pi approximated to 77 decimals (below it is the official pi): 3.14159265358979323846264338327950288419716939937510582097494459230781640628620 3.14159265358979323846264338327950288419716939937510582097494459230781640628620 """
0
•
•
•
•
Sorry Jeff, don't have a link. This is from one of the many series expansions that float around the net. I know it is accurate for at least 77 places. I did add a comparison with the published real pi to it.
In the mean time a happy '4*math.atan(1)' to you!
In the mean time a happy '4*math.atan(1)' to you!
0
•
•
•
•
Here's a link to something that looks like it might have been the original.
http://mail.python.org/pipermail/edu...ly/006810.html
This page contains a link to a math paper (PDF) where the idea was developed.
http://mail.python.org/pipermail/edu...ly/006810.html
This page contains a link to a math paper (PDF) where the idea was developed.
Similar Threads
- how to run a python script from another python sript (Python)
- Python Code, plz help noobie with python (Python)
- python code security or set up a complied python code with a password (Python)
- Square Root approximation (C)
- Pi Approximation (C++)
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython



