{"record":{"id":"2bfed6a634e66063","repo":"TheAlgorithms/Go","slug":"input-argument-must-be-non-negative-integer","errorCode":null,"errorMessage":"input argument must be non-negative integer","messagePattern":"input argument must be non-negative integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/factorial/factorial.go","lineNumber":17,"sourceCode":"// factorial.go\n// description: Calculating factorial\n// details:\n// The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n - [Factorial](https://en.wikipedia.org/wiki/Factorial)\n// time complexity: O(n)\n// space complexity: O(1)\n// author(s) [red_byte](https://github.com/i-redbyte)\n// see factorial_test.go\n\n// Package factorial describes algorithms Factorials calculations.\npackage factorial\n\nimport (\n\t\"errors\"\n)\n\nvar ErrNegativeArgument = errors.New(\"input argument must be non-negative integer\")\n\n// Iterative returns the iteratively brute forced factorial of n\nfunc Iterative(n int) (int, error) {\n\tif n < 0 {\n\t\treturn 0, ErrNegativeArgument\n\t}\n\tresult := 1\n\tfor i := 2; i <= n; i++ {\n\t\tresult *= i\n\t}\n\treturn result, nil\n}\n\n// Recursive This function recursively computes the factorial of a number\nfunc Recursive(n int) (int, error) {\n\tif n < 0 {\n\t\treturn 0, ErrNegativeArgument\n\t}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/factorial/factorial.go#L1-L35","documentation":"Sentinel error returned by both Iterative and Recursive factorial when n < 0; the factorial is only defined for non-negative integers, so any negative input triggers this guard before computation begins.","triggerScenarios":"Thrown at math/factorial/factorial.go:17 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Validate n>=0 before computing","Use the gamma function if you need factorials of negative/non-integer values","Return a zero value with a domain-specific message for invalid input"],"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"}