lib/chunky_png/canvas/drawing.rb
# Calculates the binomial coefficient for n over k.
#
# @param [Integer] n first parameter in coeffient (the number on top when
#   looking at the mathematic formula)
# @param [Integer] k k-element, second parameter in coeffient (the number
#   on the bottom when looking at the mathematic formula)
# @return [Integer] The binomial coeffcient of (n,k)
Calculates the binomial coefficient for n over k.
Calculates the binomial coefficient for n over k .
def binomial_coefficient(n, k)
        return  1 if n == k || k == 0
        return  n if k == 1
        return -1 if n < k
        fact_n = (2..n).inject(1) { |carry, i| carry * i }
        fact_k = (2..k).inject(1) { |carry, i| carry * i }
        fact_n_sub_k = (2..(n - k)).inject(1) { |carry, i| carry * i }
        fact_n / (fact_k * fact_n_sub_k)
      end
def binomial_coefficient ( n , k ) return 1 if n == k || k == 0 return n if k == 1 return - 1 if n < k fact_n = ( 2 .. n ) . inject ( 1 ) { | carry , i | carry * i } fact_k = ( 2 .. k ) . inject ( 1 ) { | carry , i | carry * i } fact_n_sub_k = ( 2 .. ( n - k ) ) . inject ( 1 ) { | carry , i | carry * i } fact_n / ( fact_k * fact_n_sub_k ) end
Calculates the binomial coefficient for n over k.
Calculates the binomial coefficient for n over k .
# calculate factorials
The binomial coeffcient of (n,k)
The binomial coeffcient of ( n k )
Integer
n
<not_specific>
k
<not_specific>
<not_specific>
k k-element, second parameter in coeffient (the number on the bottom when looking at the mathematic formula)
k k - element second parameter in coeffient ( the number on the bottom when looking at the mathematic formula )