TheAlgorithms/Go · error
can't have a negative n-th catalan number
Error message
can't have a negative n-th catalan number
What it means
A package-level sentinel (errCatalan) returned by NthCatalanNumber when n < 0; Catalan numbers are only defined for non-negative indices, so this generic negative-argument guard rejects the offending n.
Source
Thrown at dynamic/catalan.go:11
//The Catalan numbers are a sequence of positive integers that appear in many counting
// problems in combinatorics.
// time complexity: O(n²)
// space complexity: O(n)
//reference: https://brilliant.org/wiki/catalan-numbers/
package dynamic
import "fmt"
var errCatalan = fmt.Errorf("can't have a negative n-th catalan number")
// NthCatalan returns the n-th Catalan Number
// Complexity: O(n²)
func NthCatalanNumber(n int) (int64, error) {
if n < 0 {
//doesn't accept negative number
return 0, errCatalan
}
var catalanNumberList []int64
catalanNumberList = append(catalanNumberList, 1) //first value is 1
for i := 1; i <= n; i++ {
catalanNumberList = append(catalanNumberList, 0) //append 0 and calculate
for j := 0; j < i; j++ {
catalanNumberList[i] += catalanNumberList[j] * catalanNumberList[i-j-1]
}View on GitHub (pinned to 5ba447ec5f)
Solutions
- Validate n>=0 before calling
- Return 0 for negative n at the call site if a default is acceptable
- Compare with errors.Is(err, errCatalan) to handle specifically
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at dynamic/catalan.go:11 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/cdf5ea1168e3419b.
Report an issue: GitHub.