TheAlgorithms/Go · error
arguments must be positive
Error message
arguments must be positive
What it means
Returned by C (binomial coefficient) when either n or k is negative; the guard requires both arguments to be positive before computing C(n,k), since factorials/binomials are undefined for negative inputs.
Source
Thrown at math/binomialcoefficient.go:18
// 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
// time complexity: O(k) or O(n-k) whichever is smaller (O(n) in worst case)
// space complexity: O(1)
// 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
}
View on GitHub (pinned to 5ba447ec5f)
Solutions
- Check the argument is >0 before calling
- Return 0 or a domain-appropriate default for negative inputs at the call site
- Document the domain restriction in your API
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at math/binomialcoefficient.go:18 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/a984948698cf9161.
Report an issue: GitHub.