Skip to content

Instantly share code, notes, and snippets.

@colomon
Created February 8, 2015 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/c03e8202e1604482c955 to your computer and use it in GitHub Desktop.
Save colomon/c03e8202e1604482c955 to your computer and use it in GitHub Desktop.
Probability of randomly choosing an integer power
use v6;
sub is-integer-power($n) {
for 2..($n.sqrt.floor) -> $m {
my $c = $n;
my $count = 0;
while $c > 1 {
last unless $c %% $m;
$c div= $m;
$count++;
}
if $c == 1 {
# say "$n == $m ** $count (check { $m ** $count })";
return True;
}
}
return False;
}
my @powers-of-ten = 1, 10, 100 ... *;
for @powers-of-ten -> $pot {
my $number-of-integer-powers = (1..$pot).grep({ is-integer-power($_) });
say "$pot samples, { $number-of-integer-powers / $pot } probability";
}
10 samples, 0.3 probability
100 samples, 0.12 probability
1000 samples, 0.04 probability
10000 samples, 0.0124 probability
100000 samples, 0.00366 probability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment