MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Combinations

Function Combinations

math/binomialcoefficient.go:22–35  ·  view source on GitHub ↗

C is Binomial Coefficient function This function returns C(n, k) for given n and k

(n int, k int)

Source from the content-addressed store, hash-verified

20// C is Binomial Coefficient function
21// This function returns C(n, k) for given n and k
22func Combinations(n int, k int) (int, error) {
23 if n < 0 || k < 0 {
24 return -1, ErrPosArgsOnly
25 }
26 if k > (n - k) {
27 k = n - k
28 }
29 res := 1
30 for i := 0; i < k; i++ {
31 res *= (n - i)
32 res /= (i + 1)
33 }
34 return res, nil
35}

Callers 2

TestCombinationsFunction · 0.92
BenchmarkCombinationsFunction · 0.92

Calls

no outgoing calls

Tested by 2

TestCombinationsFunction · 0.74
BenchmarkCombinationsFunction · 0.74