{"record":{"id":"c5a0958b4e7b51f1","repo":"TheAlgorithms/C-Sharp","slug":"0-is-not-a-positive-integer","errorCode":null,"errorMessage":"{0} is not a positive integer","messagePattern":"(.+?) is not a positive integer","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/ModularExponentiation.cs","lineNumber":30,"sourceCode":"    /// </summary>\n    /// <param name=\"b\">Base.</param>\n    /// <param name=\"e\">Exponent.</param>\n    /// <param name=\"m\">Modulus.</param>\n    /// <returns>Modular Exponential.</returns>\n    public int ModularPow(int b, int e, int m)\n    {\n        // initialize result in variable res\n        int res = 1;\n        if (m == 1)\n        {\n            // 1 divides every number\n            return 0;\n        }\n\n        if (m <= 0)\n        {\n            // exponential not defined in this case\n            throw new ArgumentException(string.Format(\"{0} is not a positive integer\", m));\n        }\n\n        for (int i = 0; i < e; i++)\n        {\n            res = (res * b) % m;\n        }\n\n        return res;\n    }\n}\n","sourceCodeStart":12,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/ModularExponentiation.cs#L12-L41","documentation":"ModularPow computes (b^e) mod m using a naive loop; the modulus must be a positive integer because modular exponentiation is undefined for m <= 0 (and division by zero would occur for m = 0). The library throws ArgumentException with a formatted message naming the modulus. Base and exponent are not range-checked by this guard.","triggerScenarios":"Calling ModularPow(b, e, m) with m <= 0, e.g. ModularPow(2, 10, 0) or ModularPow(2, 10, -5).","commonSituations":"A modulus read from config defaulting to 0, an unsigned/signed conversion bug yielding negative m, or mistakenly passing the exponent as the third argument.","solutions":["Ensure the modulus m is a positive integer (m >= 1) before calling.","Check argument order: the modulus is the third parameter.","If a zero modulus is legitimate in your domain, guard with a special case (result defined as 0) instead of calling."],"exampleFix":"// before\nvar r = ModularPow(baseVal, exp, mod); // mod may be 0\n// after\nif (mod <= 0) throw new ArgumentOutOfRangeException(nameof(mod));\nvar r = ModularPow(baseVal, exp, mod);","handlingStrategy":"validation","validationCode":"if (m <= 0) throw new ArgumentOutOfRangeException(nameof(m), \"Modulus must be a positive integer\");\nvar r = ModularPow(b, e, m);","typeGuard":null,"tryCatchPattern":"try { var r = ModularPow(b, e, m); }\ncatch (ArgumentException ex) when (ex.Message.EndsWith(\"is not a positive integer\")) { /* supply m >= 1 or define domain result */ }","preventionTips":["Verify argument order (base, exponent, modulus) before calling","Default modulus config values to a valid positive constant, not 0","Watch signed/unsigned conversions that can turn a modulus negative"],"tags":["modular-arithmetic","argument-validation","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"}