Skip to content

Commit

Permalink
algorithm: binomial coefficient (TheAlgorithms#539)
Browse files Browse the repository at this point in the history
* feat: Add binomial coefficient implementation

* test: Add tests for binomial coefficient

* fix: Fix typing error in binomialcoefficient.go

* fix: Add linter fixes to BenchmarkBinomialCoefficient()

* fix: Fix import statement in binomialcoefficient_test.go

* fix: Fix return statement

* test: Add detailed unit tests for binomial coefficient

* fix: Replace all occurrences of C with Combinations

* fix: Move error variable outside Combinations function

* refactor: Add t.Run block

* refactor: Remove log statement

* refactor: Refactor unit tests

* fix: Change error variable name from error to err
  • Loading branch information
itsAkshayDubey authored Sep 15, 2022
1 parent 6c8e822 commit 94e8490
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
33 changes: 33 additions & 0 deletions math/binomialcoefficient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// binomialcoefficient.go
// description: Returns C(n, k)
// details:
// a binomial coefficient C(n,k) gives number ways
// in which k objects can be chosen from n objects.
// wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see binomialcoefficient_test.go

package math

import (
"errors"
)

var ErrPosArgsOnly error = errors.New("arguments must be positive")

// C is Binomial Coefficient function
// This function returns C(n, k) for given n and k
func Combinations(n int, k int) (int, error) {
if n < 0 || k < 0 {
return -1, ErrPosArgsOnly
}
if k > (n - k) {
k = n - k
}
res := 1
for i := 0; i < k; i++ {
res *= (n - i)
res /= (i + 1)
}
return res, nil
}
42 changes: 42 additions & 0 deletions math/binomialcoefficient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// binomialcoefficient_test.go
// description: Returns C(n, k)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see binomialcoefficient.go

package math_test

import (
"testing"

"github.com/TheAlgorithms/Go/math"
)

func TestCombinations(t *testing.T) {
var tests = []struct {
name string
n int
k int
expectedValue int
expectedError error
}{
{"n = 5, k = 2", 5, 2, 10, nil},
{"n = 7, k = 4", 7, 4, 35, nil},
{"n = 0, k = 0", 0, 0, 1, nil},
{"n = -1, k = 1", -1, 1, -1, math.ErrPosArgsOnly},
{"n = 1, k = -1", 1, -1, -1, math.ErrPosArgsOnly},
{"n = -1, k = -1", -1, -1, -1, math.ErrPosArgsOnly},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := math.Combinations(test.n, test.k)
if result != test.expectedValue || test.expectedError != err {
t.Errorf("expected error: %s, got: %s; expected value: %v, got: %v", test.expectedError, err, test.expectedValue, result)
}
})
}
}
func BenchmarkCombinations(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = math.Combinations(65536, 65536)
}
}

0 comments on commit 94e8490

Please sign in to comment.