Skip to content

Instantly share code, notes, and snippets.

@heyajulia
Last active January 31, 2023 10:40
Show Gist options
  • Save heyajulia/9afc2472db74ce02928112951bfc51d9 to your computer and use it in GitHub Desktop.
Save heyajulia/9afc2472db74ce02928112951bfc51d9 to your computer and use it in GitHub Desktop.
Inspired by Matt Parker’s video “Generating π from 1,000 random numbers” (https://youtu.be/RZBhSi_PwHU)
unit sub MAIN(UInt :s(:$sides) = 120, UInt :r(:$rolls) = 1_000);
sub roll-die { (1..$sides).roll }
my $coprimes = 0;
for ^$rolls {
my $a = roll-die();
my $b = roll-die();
$coprimes++ if $a gcd $b == 1;
}
say "π ≈ &sqrt(6 * $rolls / $coprimes)";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment