summaryrefslogtreecommitdiff
path: root/ruby
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2016-06-24 13:23:03 +0200
committerPatrick Simianer <p@simianer.de>2016-06-24 13:23:03 +0200
commite34007450adcec8441b7a48613c0386defd58fad (patch)
treed147bc2f7ba1338aae0d64e3ec9d6519fc7f9699 /ruby
parent348bc9e6c0a30844b268fc5dbd431deb65daea9a (diff)
ruby: binomial
Diffstat (limited to 'ruby')
-rwxr-xr-xruby/binom.rb17
-rwxr-xr-xruby/binom.sh4
2 files changed, 21 insertions, 0 deletions
diff --git a/ruby/binom.rb b/ruby/binom.rb
new file mode 100755
index 0000000..1a2dc1b
--- /dev/null
+++ b/ruby/binom.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+
+def binom n, k
+ prod = 1
+ (1..k).each { |j|
+ prod *= (n+1-j)/j
+ }
+
+ return prod
+end
+
+def bdist k, p, n
+ binom(n, k)*(p**k)*(1-p)**(n-k)
+end
+
+puts bdist(ARGV[0].to_f, ARGV[1].to_f, ARGV[2].to_f)
+
diff --git a/ruby/binom.sh b/ruby/binom.sh
new file mode 100755
index 0000000..fcb73e4
--- /dev/null
+++ b/ruby/binom.sh
@@ -0,0 +1,4 @@
+#!/bin/zsh
+
+for i in {0..10}; do ./binom.rb $i 0.1 10; done
+