Latest recipes tagged "pi"http://code.activestate.com/recipes/tags/pi/new/2013-08-25T06:24:56-07:00ActiveState Code RecipesCalculating PI using trigonometric iterations (Python) 2013-08-25T06:24:56-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578651-calculating-pi-using-trigonometric-iterations/ <p style="color: grey"> Python recipe 578651 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pi/">pi</a>). </p> <p>Calculating PI using trigonometric iterations</p> Complex Methods to Compute Pi in Python (Python) 2013-06-03T18:08:19-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578548-complex-methods-to-compute-pi-in-python/ <p style="color: grey"> Python recipe 578548 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote about computing Pi with Python. This module uses the Decimal library. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p> Simple Method to Compute Pi in Python (Python) 2013-06-03T18:06:43-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578547-simple-method-to-compute-pi-in-python/ <p style="color: grey"> Python recipe 578547 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote about computing Pi with Python. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p> Archimedes Method for PI (arbitrary precision) (Python) 2013-03-02T21:56:42-08:00Bjorn Madsenhttp://code.activestate.com/recipes/users/4176352/http://code.activestate.com/recipes/578478-archimedes-method-for-pi-arbitrary-precision/ <p style="color: grey"> Python recipe 578478 by <a href="/recipes/users/4176352/">Bjorn Madsen</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/precision/">precision</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>The code below incorporates the newly revised python 3.3.0 decimal module to permit arbitrary precision (<a href="http://docs.python.org/3/library/decimal.html%29" rel="nofollow">http://docs.python.org/3/library/decimal.html)</a></p> <p>The recipe was originally by FB35 from <a href="http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/" rel="nofollow">http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/</a> </p> Pi Circle (Python) 2012-05-13T04:35:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578130-pi-circle/ <p style="color: grey"> Python recipe 578130 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/art/">art</a>, <a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/pi/">pi</a>). Revision 5. </p> <p>Computes Pi to many decimal places and prints the digits in a circle.</p> Amazing estimation of Pi using the Mandlebrot set (Python) 2011-02-20T07:34:56-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577584-amazing-estimation-of-pi-using-the-mandlebrot-set/ <p style="color: grey"> Python recipe 577584 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/approximation/">approximation</a>, <a href="/recipes/tags/mandlebrot/">mandlebrot</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/strange/">strange</a>). </p> <p>Miraculously, you can estimate π just by counting the number of iterations for a point near the neck to escape. The method has a rigorous error bound.</p> Calculate Pi using Monte Carlo Simulations in Python (Vectorized) (Python) 2011-02-17T22:35:46-08:00Zach Pacehttp://code.activestate.com/recipes/users/4177048/http://code.activestate.com/recipes/577578-calculate-pi-using-monte-carlo-simulations-in-pyth/ <p style="color: grey"> Python recipe 577578 by <a href="/recipes/users/4177048/">Zach Pace</a> (<a href="/recipes/tags/calculate/">calculate</a>, <a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>I saw something like this in C++ as an introduction to Monte Carlo, so I decided to make something similar in Python. My original code used for loops, but I vectorized it with no small amount of effort, and it now runs orders of magnitude faster. For example, I can calculate pi to .002% error with 100,000,000 randomized coordinates in approximately 15 seconds. Careful to start small, because memory fills up quickly, and the computer will run slow if you overstep your RAM. I'm able to go up to a bit more than 150 million without compromising speed and functionality in other tasks.</p> <p>For those who are curious, vectorization is a technique whereby numpy (or similar) arrays replace things like loops. They're a bit tricky to write (at least for me), but they work beautifully.</p> <p>It might be useful for visualization to plot the occurrence of data points, and observe the randomness</p> Calculating PI using random numbers (Monte Carlo method) (Python) 2010-03-27T00:58:24-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577154-calculating-pi-using-random-numbers-monte-carlo-me/ <p style="color: grey"> Python recipe 577154 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>Calculating PI using random numbers (Monte Carlo method)</p> Archimedes method for calculating PI (Python) 2009-12-09T15:13:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/ <p style="color: grey"> Python recipe 576981 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>). </p> <p>Archimedes method for calculating PI.</p>