{"record":{"id":"a984948698cf9161","repo":"TheAlgorithms/Go","slug":"arguments-must-be-positive","errorCode":null,"errorMessage":"arguments must be positive","messagePattern":"arguments must be positive","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/binomialcoefficient.go","lineNumber":18,"sourceCode":"// binomialcoefficient.go\n// description: Returns C(n, k)\n// details:\n// a binomial coefficient C(n,k) gives number ways\n// in which k objects can be chosen from n objects.\n// wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient\n// time complexity: O(k) or O(n-k) whichever is smaller (O(n) in worst case)\n// space complexity: O(1)\n// author: Akshay Dubey (https://github.com/itsAkshayDubey)\n// see binomialcoefficient_test.go\n\npackage math\n\nimport (\n\t\"errors\"\n)\n\nvar ErrPosArgsOnly error = errors.New(\"arguments must be positive\")\n\n// C is Binomial Coefficient function\n// This function returns C(n, k) for given n and k\nfunc Combinations(n int, k int) (int, error) {\n\tif n < 0 || k < 0 {\n\t\treturn -1, ErrPosArgsOnly\n\t}\n\tif k > (n - k) {\n\t\tk = n - k\n\t}\n\tres := 1\n\tfor i := 0; i < k; i++ {\n\t\tres *= (n - i)\n\t\tres /= (i + 1)\n\t}\n\treturn res, nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/binomialcoefficient.go#L1-L36","documentation":"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.","triggerScenarios":"Thrown at math/binomialcoefficient.go:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"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"}