Skip to content

Instantly share code, notes, and snippets.

@perigrin
Created November 11, 2010 16:59
Show Gist options
  • Select an option

  • Save perigrin/672801 to your computer and use it in GitHub Desktop.

Select an option

Save perigrin/672801 to your computer and use it in GitHub Desktop.
perl glyph_odds [ministry level] [trials]
#!/usr/bin/perl
use 5.12.2;
use List::Util qw(reduce sum);
sub factorial {
my ($in) = @_;
return 1 unless $in;
return reduce { $a * $b } ( 1 .. $in );
}
sub nCx {
my ( $n, $x ) = @_;
return factorial($n) / ( factorial($x) * factorial( $n - $x ) );
}
# P(x) = nCx * p^x * q^(n-x)
#
# n = number of trials
# x = number of successes among n trials
# p = probability of success in any one trial
# q = probability of failure in any one trial (q = 1 - p)
sub single_trial {
my ( $n, $x, $p ) = @_;
my $q = 1 - $p;
my $out = nCx( $n, $x ) * ( $p**$x ) * $q**( $n - $x );
# say "nCx( $n, $x ) * ( $p**$x ) * $q**( $n - $x ) = " . $out;
return $out;
}
my ( $level, $trials ) = @ARGV;
my $odds = $level / 100;
say sum map { single_trial( $trials, $_, $odds ) } 1 .. $trials;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment