<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Calculating Pi</title>
	<atom:link href="http://programmingpraxis.com/2009/10/09/calculating-pi/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/10/09/calculating-pi/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Berechnung von Pi&#160;&#124;&#160;ComSubVies Creative Content</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-1101</link>
		<dc:creator><![CDATA[Berechnung von Pi&#160;&#124;&#160;ComSubVies Creative Content]]></dc:creator>
		<pubDate>Wed, 17 Mar 2010 13:18:44 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-1101</guid>
		<description><![CDATA[[...] (diesmal von Programming Praxis). Eine sehr beliebte Aufgabe ist die Berechnung von Pi, eine Übung die ich auch meinen Schülern gerne gebe &#8211; oft mit der Zusatzaufgabe zu [...]]]></description>
		<content:encoded><![CDATA[<p>[...] (diesmal von Programming Praxis). Eine sehr beliebte Aufgabe ist die Berechnung von Pi, eine Übung die ich auch meinen Schülern gerne gebe &#8211; oft mit der Zusatzaufgabe zu [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-998</link>
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Mon, 08 Feb 2010 21:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-998</guid>
		<description><![CDATA[Hi-

What is the advantage of the monte carlo method - randomly picking n points where the x,y are independent and uniformly distributed - and the grid method, where you set up points on some defined lattice and then just keep making the lattice finer and finer?

Thanks!]]></description>
		<content:encoded><![CDATA[<p>Hi-</p>
<p>What is the advantage of the monte carlo method &#8211; randomly picking n points where the x,y are independent and uniformly distributed &#8211; and the grid method, where you set up points on some defined lattice and then just keep making the lattice finer and finer?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Calculating Sines &#171; Programming Praxis</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-894</link>
		<dc:creator><![CDATA[Calculating Sines &#171; Programming Praxis]]></dc:creator>
		<pubDate>Tue, 12 Jan 2010 09:01:54 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-894</guid>
		<description><![CDATA[[...] calculated the value of pi, and logarithms to seven digits, in two previous exercises. We continue that thread in the current exercise with a function that calculates [...]]]></description>
		<content:encoded><![CDATA[<p>[...] calculated the value of pi, and logarithms to seven digits, in two previous exercises. We continue that thread in the current exercise with a function that calculates [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ronald Kaiser</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-761</link>
		<dc:creator><![CDATA[Ronald Kaiser]]></dc:creator>
		<pubDate>Thu, 12 Nov 2009 02:37:25 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-761</guid>
		<description><![CDATA[Monte Carlo method in python:

from random import random
def pi(n):
    return 4*float(sum(1 if (random()**2 + random()**2) &lt;= 1 else 0 for i in range(n)))/n]]></description>
		<content:encoded><![CDATA[<p>Monte Carlo method in python:</p>
<p>from random import random<br />
def pi(n):<br />
    return 4*float(sum(1 if (random()**2 + random()**2) &lt;= 1 else 0 for i in range(n)))/n</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wally Glutton</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-731</link>
		<dc:creator><![CDATA[Wally Glutton]]></dc:creator>
		<pubDate>Fri, 30 Oct 2009 05:10:09 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-731</guid>
		<description><![CDATA[Just for fun, a visual approach to the Monte Carlo simulation using Processing (http://processing.org). Unfortunately this program will never truly converge towards Pi as the whitespace is only a pixel-based approximation of a quarter-circle.

You can view the results as an applet here: http://stungeye.com/processing/1009/

[sourcecode lang=&quot;java&quot;]
final int R = 400;
final color BLUE  = color(0, 0, 255);
final color WHITE = color(255, 255, 255);
final color RED   = color(255, 0, 0);
final color BLACK = color(0, 0, 0);

double n = 0;
double count = 0;

void setup () {
  size(R,R);
  background(0);
  fill(255);
  stroke(255);
  ellipse(0, R, R*2, R*2);
}

void draw() {
  count++;  
  
  int random_x = (int)random(R);
  int random_y = (int)random(R);
  
  loadPixels();
  color current_pixel_colour = pixels[random_y * R + random_x];
  
  if ((current_pixel_colour == WHITE) &#124;&#124; (current_pixel_colour == BLUE)) {
    n++;
    set(random_x, random_y, BLUE);
    println(4 * n / count);
  } else {
    set(random_x, random_y, RED);
  }  
}
[/sourcecode]

(The applet version of the code is slightly more complex: http://stungeye.com/processing/1009/monte_carlo_pi.pde)]]></description>
		<content:encoded><![CDATA[<p>Just for fun, a visual approach to the Monte Carlo simulation using Processing (<a href="http://processing.org" rel="nofollow">http://processing.org</a>). Unfortunately this program will never truly converge towards Pi as the whitespace is only a pixel-based approximation of a quarter-circle.</p>
<p>You can view the results as an applet here: <a href="http://stungeye.com/processing/1009/" rel="nofollow">http://stungeye.com/processing/1009/</a></p>
<pre class="brush: java;">
final int R = 400;
final color BLUE  = color(0, 0, 255);
final color WHITE = color(255, 255, 255);
final color RED   = color(255, 0, 0);
final color BLACK = color(0, 0, 0);

double n = 0;
double count = 0;

void setup () {
  size(R,R);
  background(0);
  fill(255);
  stroke(255);
  ellipse(0, R, R*2, R*2);
}

void draw() {
  count++;  

  int random_x = (int)random(R);
  int random_y = (int)random(R);

  loadPixels();
  color current_pixel_colour = pixels[random_y * R + random_x];

  if ((current_pixel_colour == WHITE) || (current_pixel_colour == BLUE)) {
    n++;
    set(random_x, random_y, BLUE);
    println(4 * n / count);
  } else {
    set(random_x, random_y, RED);
  }
}
</pre>
<p>(The applet version of the code is slightly more complex: <a href="http://stungeye.com/processing/1009/monte_carlo_pi.pde" rel="nofollow">http://stungeye.com/processing/1009/monte_carlo_pi.pde</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rudi Angela</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-705</link>
		<dc:creator><![CDATA[Rudi Angela]]></dc:creator>
		<pubDate>Tue, 20 Oct 2009 22:01:59 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-705</guid>
		<description><![CDATA[Here&#039;s an implementation in Erlang. 
Approach: 
Function calc() takes parameter Tally that is the number of times to produce a random coordinate (X and Y between 0 and 1).
Calculate the distance to the origin and if it is less than 1 then count it as a match (inside circle with radius 1).
Keep a count on the total number of tries (Done) executed.
At the end (when Tally = 0) just calculate Pi as 4 * Matches / Done.

calc(Tally) -&gt;
	calc(Tally, 0, 0).
	
calc(0, Matches, Done) -&gt; 4 * Matches / Done;

calc(Tally, Matches, Done) -&gt;
	X = random:uniform(), Y = random:uniform(),
	calc (Tally - 1, if X*X + Y*Y  Matches + 1; true -&gt; Matches end, Done + 1).]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s an implementation in Erlang.<br />
Approach:<br />
Function calc() takes parameter Tally that is the number of times to produce a random coordinate (X and Y between 0 and 1).<br />
Calculate the distance to the origin and if it is less than 1 then count it as a match (inside circle with radius 1).<br />
Keep a count on the total number of tries (Done) executed.<br />
At the end (when Tally = 0) just calculate Pi as 4 * Matches / Done.</p>
<p>calc(Tally) -&gt;<br />
	calc(Tally, 0, 0).</p>
<p>calc(0, Matches, Done) -&gt; 4 * Matches / Done;</p>
<p>calc(Tally, Matches, Done) -&gt;<br />
	X = random:uniform(), Y = random:uniform(),<br />
	calc (Tally &#8211; 1, if X*X + Y*Y  Matches + 1; true -&gt; Matches end, Done + 1).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maurits</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-667</link>
		<dc:creator><![CDATA[Maurits]]></dc:creator>
		<pubDate>Mon, 12 Oct 2009 16:52:56 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-667</guid>
		<description><![CDATA[&gt;perl -e &quot;$m=pop;for(1..$m){$n+=rand()**2+rand()**2&lt;1}print$n*4/$m&quot; 5000000
3.1419256]]></description>
		<content:encoded><![CDATA[<p>&gt;perl -e &#8220;$m=pop;for(1..$m){$n+=rand()**2+rand()**2&lt;1}print$n*4/$m&quot; 5000000<br />
3.1419256</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kbob</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-666</link>
		<dc:creator><![CDATA[kbob]]></dc:creator>
		<pubDate>Sat, 10 Oct 2009 03:58:34 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-666</guid>
		<description><![CDATA[Instead of the Monte Carlo method, I started by generating a uniform grid of n x n points on the first quadrant (0 &lt;= x, y &lt;= 1).  pi/4 of them fell inside the unit circle.  That ran rather slowly and wasn&#039;t converging very well.

Then I realized I could use the Bresenham circle algorithm and just calculate one point in each row.  Since the basic BCA only finds points through a 45 degree angle, I ran it for 1/8th of the circle to find pi/8.

Here&#039;s how quickly it converged, using up to a billion iterations.

&lt;pre&gt;
1: -8
10: 2.8
100: 3.1
1000: 3.137576
10000: 3.1411928
100000: 3.1415526136
1000000: 3.141588652488
10000000: 3.14159225361224
100000000: 3.14159261359012
1000000000: 3.14159264958976
&lt;/pre&gt;

The code is deceptively simple.  In C, based on the sample code in Wikipedia.

&lt;pre&gt;
#include 

double pi(int n)
{
    int f = 1 - n;
    int ddF_x = 1;
    int ddF_y = -2 * n;
    int x = 0;
    int y = n;
    long long int in;
    while (x &lt; y) {
	if (f &gt;= 0) {
	    --y;
	    ddF_y += 2;
	    f += ddF_y;
	}
	x++;
	ddF_x += 2;
	f += ddF_x;
	in += y - x;
    }
    return 8.0 * in / ((double)n * n);
}

main()
{
    int i;

    for (i = 1; i &lt;= 1000000000; i *= 10)
	printf(&quot;%d: %.15g\n&quot;, i, pi(i));
}

&lt;/pre&gt;

(I wonder if the formatting will survive...)]]></description>
		<content:encoded><![CDATA[<p>Instead of the Monte Carlo method, I started by generating a uniform grid of n x n points on the first quadrant (0 &lt;= x, y &lt;= 1).  pi/4 of them fell inside the unit circle.  That ran rather slowly and wasn&#039;t converging very well.</p>
<p>Then I realized I could use the Bresenham circle algorithm and just calculate one point in each row.  Since the basic BCA only finds points through a 45 degree angle, I ran it for 1/8th of the circle to find pi/8.</p>
<p>Here&#039;s how quickly it converged, using up to a billion iterations.</p>
<pre>
1: -8
10: 2.8
100: 3.1
1000: 3.137576
10000: 3.1411928
100000: 3.1415526136
1000000: 3.141588652488
10000000: 3.14159225361224
100000000: 3.14159261359012
1000000000: 3.14159264958976
</pre>
<p>The code is deceptively simple.  In C, based on the sample code in Wikipedia.</p>
<pre>
#include 

double pi(int n)
{
    int f = 1 - n;
    int ddF_x = 1;
    int ddF_y = -2 * n;
    int x = 0;
    int y = n;
    long long int in;
    while (x &lt; y) {
	if (f &gt;= 0) {
	    --y;
	    ddF_y += 2;
	    f += ddF_y;
	}
	x++;
	ddF_x += 2;
	f += ddF_x;
	in += y - x;
    }
    return 8.0 * in / ((double)n * n);
}

main()
{
    int i;

    for (i = 1; i &lt;= 1000000000; i *= 10)
	printf("%d: %.15g\n", i, pi(i));
}
</pre>
<p>(I wonder if the formatting will survive&#8230;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-665</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 09 Oct 2009 18:09:11 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-665</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/10/09/programming-praxis-calculating-pi/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Control.Monad
import System.Random

monteCarloPi :: Int -&gt; IO ()
monteCarloPi n = do
    hits &lt;- fmap sum $ liftM2 (zipWith checkHit) rs rs
    print $ fromIntegral hits / fromIntegral n
    where rs = replicateM n $ randomRIO (0,1 :: Double)
          checkHit x y = if x*x + y*y &lt; 1 then 4 else 0

boundPi :: Int -&gt; (Double, Double)
boundPi n = iterate f (3/2 * sqrt 3, 3 * sqrt 3) !! (n - 1)
            where f (b, a) = let x = 2 / (1 / a + 1 / b)
                             in (sqrt $ b * x, x)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/10/09/programming-praxis-calculating-pi/" rel="nofollow">http://bonsaicode.wordpress.com/2009/10/09/programming-praxis-calculating-pi/</a> for a version with comments):</p>
<pre class="brush: css;">
import Control.Monad
import System.Random

monteCarloPi :: Int -&gt; IO ()
monteCarloPi n = do
    hits &lt;- fmap sum $ liftM2 (zipWith checkHit) rs rs
    print $ fromIntegral hits / fromIntegral n
    where rs = replicateM n $ randomRIO (0,1 :: Double)
          checkHit x y = if x*x + y*y &lt; 1 then 4 else 0

boundPi :: Int -&gt; (Double, Double)
boundPi n = iterate f (3/2 * sqrt 3, 3 * sqrt 3) !! (n - 1)
            where f (b, a) = let x = 2 / (1 / a + 1 / b)
                             in (sqrt $ b * x, x)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Calculating Pi &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/10/09/calculating-pi/#comment-664</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Calculating Pi &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 09 Oct 2009 18:08:56 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1500#comment-664</guid>
		<description><![CDATA[[...] Praxis &#8211; Calculating&#160;Pi By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to implement two ways of calculating pi. Let&#8217;s get [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Calculating&nbsp;Pi By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to implement two ways of calculating pi. Let&#8217;s get [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

