Calculates N choose K, N! / (K! * (N - K)!), in a mathematical operations efficient way.
Ali Hamdi Ali Fadel · @AliOsm · about 3 years
Calculates N choose K, N! / (K! * (N - K)!), in a mathematical operations efficient way.
def n_choose_k(n, k)raise ArgumentError.new 'K should be less than or equal to N' if k > nresult = 1a = n - k + 1b = 1while a <= n doresult = result * a / ba += 1b += 1endresultend