After hearing the idea of a probabilistic algorithm that is calculating pi
I decided to implement it in perl,and the following is what I obtained:
23 # 24 # pi*($d/2)**2 in circle 25 # ------------ ~ ----------- ~ pi/4 26 # $d**2 in total 27 # 28 29 30 31 use strict; 32 use warnings; 33 34 my $d = 1000;#diameter 35 my $no_total = 1000000;#number of points 36 my $no_in=0; 37 38 sub gen_xy { ( int(rand($d)),int(rand($d)) ); } 39 40 sub in_circle { 41 my ($x,$y) = @_; 42 ($x - $d/2)**2 + ($y - $d/2)**2 <= ($d/2)**2 43 ? 1 44 : 0; 45 } 46 47 48 $no_in += in_circle gen_xy 49 for 1..$no_total; 50 51 warn "Pi approx: ". ( $no_in/$no_total ) *4 ;
I'm ignoring the fact that this code is useless,it's really slow and there are
C programs which can be found through google which are able to get pi to 10000 decimals
It does confirm that pi is ~3.14 after 10 seconds of running time

In reply to probabilistic pi algorithm by spx2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.