From e34007450adcec8441b7a48613c0386defd58fad Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 24 Jun 2016 13:23:03 +0200 Subject: ruby: binomial --- ruby/binom.rb | 17 +++++++++++++++++ ruby/binom.sh | 4 ++++ 2 files changed, 21 insertions(+) create mode 100755 ruby/binom.rb create mode 100755 ruby/binom.sh 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 + -- cgit v1.2.3