{"record":{"id":"cb72e021e7caa688","repo":"TheAlgorithms/Go","slug":"integer-must-be-between-1-and-3999","errorCode":null,"errorMessage":"integer must be between 1 and 3999","messagePattern":"integer must be between 1 and 3999","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/inttoroman.go","lineNumber":25,"sourceCode":"package conversion\n\nimport (\n\t\"errors\"\n)\n\nvar (\n\t// lookup arrays used for converting from an int to a roman numeral extremely quickly.\n\tr0 = []string{\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"} // 1 - 9\n\tr1 = []string{\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"} // 10 - 90\n\tr2 = []string{\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"} // 100 - 900\n\tr3 = []string{\"\", \"M\", \"MM\", \"MMM\"}                                       // 1,000 - 3,000\n)\n\n// IntToRoman converts an integer value to a roman numeral string. An error is\n// returned if the integer is not between 1 and 3999.\nfunc IntToRoman(n int) (string, error) {\n\tif n < 1 || n > 3999 {\n\t\treturn \"\", errors.New(\"integer must be between 1 and 3999\")\n\t}\n\t// Concatenate strings for each of 4 lookup array categories.\n\t//\n\t// Key behavior to note here is how math with integers is handled. Values are floored to the\n\t// nearest int, not rounded up. For example, 26/10 = 2 even though the actual result is 2.6.\n\t//\n\t// For example, lets use an input value of 126:\n\t// `r3[n%1e4/1e3]` --> 126 % 10_000 = 126 --> 126 / 1_000 = 0.126 (0 as int) --> r3[0] = \"\"\n\t// `r2[n%1e3/1e2]` --> 126 % 1_000 = 126 --> 126 / 100 = 1.26 (1 as int) --> r2[1] = \"C\"\n\t// `r1[n%100/10]` --> 126 % 100 = 26 --> 26 / 10 = 2.6 (2 as int) --> r1[2] = \"XX\"\n\t// `r0[n%10]` --> 126 % 10 = 6 --> r0[6] = \"VI\"\n\t// FINAL --> \"\" + \"C\" + \"XX\" + \"VI\" = \"CXXVI\"\n\t//\n\t// This is efficient in Go. The 4 operands are evaluated,\n\t// then a single allocation is made of the exact size needed for the result.\n\treturn r3[n%1e4/1e3] + r2[n%1e3/1e2] + r1[n%100/10] + r0[n%10], nil\n}\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/inttoroman.go#L7-L43","documentation":"Sentinel-style guard in IntToRoman: the lookup-table approach only covers roman numerals for 1-3999, so any integer outside that range (including 0 and negatives) is rejected rather than producing a wrong numeral.","triggerScenarios":"Thrown at conversion/inttoroman.go:25 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Clamp or reject the value at the call site before calling IntToRoman","For values >3999, use an overline/extended notation library or format as repeated 'M' prefix manually","Return a domain-specific error to the caller explaining the supported range"],"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"}