Module: Math

Defined in:
lib/bernoulli/math.rb

Overview

Extend the Ruby Math module with common functions used in statstical distributions

Class Method Summary collapse

Class Method Details

.binomial(n, k) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/bernoulli/math.rb', line 16

def self.binomial(n, k)
  if n < 0 or k < 0 or n < k
    0
  else
    factorial(n)/(factorial(k) * factorial(n - k))
  end
end

.factorial(n) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/bernoulli/math.rb', line 5

def self.factorial(n)
  if n < 0
    raise Math::DomainError, 'Numerical argument is out of domain - "factorial"'
  end
  if n == 0
    1
  else
    (1..n).inject(:*)
  end
end