From 740f82a87a6f5573e80b3263f540efa831e05100 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Tue, 4 Jul 2017 21:09:27 +0200 Subject: multinom --- ruby/multinom.rb | 21 +++++++++++++++++++++ ruby/multinom.sh | 4 ++++ 2 files changed, 25 insertions(+) create mode 100755 ruby/multinom.rb create mode 100644 ruby/multinom.sh diff --git a/ruby/multinom.rb b/ruby/multinom.rb new file mode 100755 index 0000000..afee4b7 --- /dev/null +++ b/ruby/multinom.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +def fak n + r = 1 + (1).upto(n) { |i| r = r*i } + return r +end + +def multinom n, ks, ps + b = fak(n) + prod = 1 + ks.each { |k| prod = prod*fak(k) } + b = b/prod + ps.each_with_index { |p,j| + b = b*p**ks[j] + } + return b +end + +puts multinom(ARGV[0].to_f, ARGV[1].split.map{|i| i.to_f}, ARGV[2].split.map{|i| i.to_f}) + diff --git a/ruby/multinom.sh b/ruby/multinom.sh new file mode 100644 index 0000000..f78d4d5 --- /dev/null +++ b/ruby/multinom.sh @@ -0,0 +1,4 @@ +#!/bin/zsh -x + +./multinom.rb 10 "0 10" "0.1 0.9" + -- cgit v1.2.3