Skip to content

Instantly share code, notes, and snippets.

@hmbr
Created December 21, 2011 17:43
Show Gist options
  • Save hmbr/1506932 to your computer and use it in GitHub Desktop.
Save hmbr/1506932 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
my $point = 0;
my $radius = 10000;
my $SQ_RADIUS = $radius * $radius;
my @squares = map { $_ * $_ } ( 1 .. $radius);
foreach my $xp (@squares) {
foreach my $y (@squares) {
$point ++ if ( $xp + $y <= $SQ_RADIUS )
}
}
my $circle = 4.0* ( $point + $radius) - 1.0;
my $square = 4.0 * $SQ_RADIUS;
my $pi = 4.0 * $circle/$square;
print "pi = $pi\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment