module Combinatorics

Public Instance Methods

arrangement(n, k) click to toggle source

Function A(n,k)

# File lib/theory_of_probability.rb, line 25
def arrangement(n, k)
  f = fact(n, k)
  if n < k or k <= 0
    -1
  else
    f[0] / f[1]
  end
end
combination(n, k) click to toggle source

Function C(n,k)

# File lib/theory_of_probability.rb, line 14
def combination(n, k)
  f = fact(n, k)
  if n < k or k <= 0
    -1
  else
    f[0] / (f[2] * f[1])
  end
end
factorial(n) click to toggle source
# File lib/theory_of_probability.rb, line 8
def factorial(n)
  (1..n).inject(:*) || 1
end

Private Instance Methods

determining_i(arr, res) click to toggle source
# File lib/theory_of_probability.rb, line 51
def determining_i(arr, res)
  res[1] = arr[3] if arr[0] == arr[1] - arr[2]
  res[2] = arr[3] if arr[0] == arr[2]
end
fact(n, k) click to toggle source
# File lib/theory_of_probability.rb, line 36
def fact(n, k)
  res = [1,1,1]
  fact_n_greater_1(n, k, res) if n > 1
  res
end
fact_n_greater_1(n,k, res) click to toggle source
# File lib/theory_of_probability.rb, line 42
def fact_n_greater_1(n,k, res)
  c = 1
  for i in 2..n
    c *= i
    determining_i([i, n, k, c], res)
  end
  res[0] = c
end