Last active
January 31, 2023 10:40
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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