{"record":{"id":"55c00686e4e0368f","repo":"TheAlgorithms/C-Sharp","slug":"nameof-number-cannot-be-negative-keithnumberchecker","errorCode":null,"errorMessage":"{nameof(number)} cannot be negative","messagePattern":"(.+?) cannot be negative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/KeithNumberChecker.cs","lineNumber":19,"sourceCode":"namespace Algorithms.Numeric;\n\n/// <summary>\n///  In number theory, a Keith number or repfigit number is a natural number n in a given number base b with k digits such that\n///  when a sequence is created such that the first k terms are the k digits of n and each subsequent term is the sum of the\n///  previous k terms, n is part of the sequence.\n/// </summary>\npublic static class KeithNumberChecker\n{\n    /// <summary>\n    ///     Checks if a number is a Keith number or not.\n    /// </summary>\n    /// <param name=\"number\">Number to check.</param>\n    /// <returns>True if it is a Keith number; False otherwise.</returns>\n    public static bool IsKeithNumber(int number)\n    {\n        if (number < 0)\n        {\n            throw new ArgumentException($\"{nameof(number)} cannot be negative\");\n        }\n\n        var tempNumber = number;\n\n        var stringNumber = number.ToString();\n\n        var digitsInNumber = stringNumber.Length;\n\n        /* storing the terms of the series */\n        var termsArray = new int[number];\n\n        for (var i = digitsInNumber - 1; i >= 0; i--)\n        {\n            termsArray[i] = tempNumber % 10;\n            tempNumber /= 10;\n        }\n\n        var sum = 0;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/KeithNumberChecker.cs#L1-L37","documentation":"IsKeithNumber(number) determines whether a non-negative integer is a Keith (repfigit) number by iterating digit-sequence sums. Negative numbers are outside the definition domain, so the library throws ArgumentException with the parameter name interpolated into the message. Zero is allowed by this guard.","triggerScenarios":"Calling KeithNumberChecker.IsKeithNumber with any negative int, e.g. IsKeithNumber(-14).","commonSituations":"Subtracting offsets from parsed user input, or feeding signed values from a range scan without a lower bound check.","solutions":["Check number >= 0 before calling IsKeithNumber.","Use Math.Abs only if the sign is genuinely irrelevant to your use case.","Filter input collections to non-negative values prior to checking."],"exampleFix":"// before\nbool isKeith = KeithNumberChecker.IsKeithNumber(userValue);\n// after\nif (userValue < 0) throw new ArgumentOutOfRangeException(nameof(userValue));\nbool isKeith = KeithNumberChecker.IsKeithNumber(userValue);","handlingStrategy":"validation","validationCode":"bool isKeith = number >= 0 && KeithNumberChecker.IsKeithNumber(number);","typeGuard":"static bool IsNonNegative(int v) => v >= 0;","tryCatchPattern":"try { return KeithNumberChecker.IsKeithNumber(n); }\ncatch (ArgumentException) { return false; // negatives are never Keith numbers }","preventionTips":["Filter signed ranges to non-negative values before number-theory checks","Check where negative values originate (offset subtraction, diffs)","Prefer unsigned or validated positive types at the API boundary"],"tags":["argument-validation","negative-number","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}