{"record":{"id":"cdf5ea1168e3419b","repo":"TheAlgorithms/Go","slug":"can-t-have-a-negative-n-th-catalan-number","errorCode":null,"errorMessage":"can't have a negative n-th catalan number","messagePattern":"can't have a negative n-th catalan number","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dynamic/catalan.go","lineNumber":11,"sourceCode":"//The Catalan numbers are a sequence of positive integers that appear in many counting\n// problems in combinatorics.\n// time complexity: O(n²)\n// space complexity: O(n)\n//reference: https://brilliant.org/wiki/catalan-numbers/\n\npackage dynamic\n\nimport \"fmt\"\n\nvar errCatalan = fmt.Errorf(\"can't have a negative n-th catalan number\")\n\n// NthCatalan returns the n-th Catalan Number\n// Complexity: O(n²)\nfunc NthCatalanNumber(n int) (int64, error) {\n\tif n < 0 {\n\t\t//doesn't accept negative number\n\t\treturn 0, errCatalan\n\t}\n\n\tvar catalanNumberList []int64\n\tcatalanNumberList = append(catalanNumberList, 1) //first value is 1\n\n\tfor i := 1; i <= n; i++ {\n\t\tcatalanNumberList = append(catalanNumberList, 0) //append 0 and calculate\n\n\t\tfor j := 0; j < i; j++ {\n\t\t\tcatalanNumberList[i] += catalanNumberList[j] * catalanNumberList[i-j-1]\n\t\t}","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/dynamic/catalan.go#L1-L29","documentation":"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.","triggerScenarios":"Thrown at dynamic/catalan.go:11 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}