#!/usr/bin/perl # This is a quick program to calculate pi using the Monte Carlo method. # I recomend inputting a value for $cycles greater that 1000. # I am working on a detailed explanation of how and why this works. # I will add it as soon as I'm done. use strict; my ($cycles, $i, $yespi, $pi) = 0; srand; #print "Please enter number of cycles\n"; print "Please enter the amount of cycles:"; chomp($cycles = ); while ($i < $cycles) { my ($x, $y, $cdnt) = 0; $x = rand; $y = rand; $cdnt = $x**2 + $y**2; if ($cdnt <= 1) { ++$yespi; } ++$i; } $pi = ($yespi / $cycles) * 4; print "Pi is $pi\n";