Created
February 8, 2015 18:21
-
-
Save colomon/c03e8202e1604482c955 to your computer and use it in GitHub Desktop.
Probability of randomly choosing an integer power
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
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