{"record":{"id":"e94b4671d30db200","repo":"TheAlgorithms/Python","slug":"modulus-must-be-a-positive-integer","errorCode":null,"errorMessage":"Modulus must be a positive integer","messagePattern":"Modulus must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binary_exponentiation.py","lineNumber":113,"sourceCode":"    >>> binary_exp_mod_recursive(3, 4, 5)\n    1\n    >>> binary_exp_mod_recursive(11, 13, 7)\n    4\n    >>> binary_exp_mod_recursive(1.5, 4, 3)\n    2.0625\n    >>> binary_exp_mod_recursive(7, -1, 10)\n    Traceback (most recent call last):\n        ...\n    ValueError: Exponent must be a non-negative integer\n    >>> binary_exp_mod_recursive(7, 13, 0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Modulus must be a positive integer\n    \"\"\"\n    if exponent < 0:\n        raise ValueError(\"Exponent must be a non-negative integer\")\n    if modulus <= 0:\n        raise ValueError(\"Modulus must be a positive integer\")\n\n    if exponent == 0:\n        return 1\n\n    if exponent % 2 == 1:\n        return (binary_exp_mod_recursive(base, exponent - 1, modulus) * base) % modulus\n\n    r = binary_exp_mod_recursive(base, exponent // 2, modulus)\n    return (r * r) % modulus\n\n\ndef binary_exp_mod_iterative(base: float, exponent: int, modulus: int) -> float:\n    \"\"\"\n    Computes a^b % c iteratively, where a is the base, b is the exponent, and c is the\n    modulus\n\n    >>> binary_exp_mod_iterative(3, 4, 5)\n    1","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binary_exponentiation.py#L95-L131","documentation":"In binary_exp_mod_recursive, after the exponent passes validation, a modulus <= 0 raises ValueError('Modulus must be a positive integer'). Modular arithmetic requires a positive modulus for well-defined residues.","triggerScenarios":"binary_exp_mod_recursive(7, 13, 0); binary_exp_mod_recursive(7, 13, -5); modulus read from config as 0 default, or a modulus variable never initialized from its argument.","commonSituations":"Default-initialized modulus left at 0; passing (base, modulus, exponent) in the wrong order so 0 or a negative lands in the modulus slot; modulus computed as a difference that can be 0.","solutions":["Pass a positive modulus (>= 1); modulus 1 is legal and always yields 0.","Check argument order — signature is (base, exponent, modulus).","Validate the modulus at configuration load time."],"exampleFix":"# before\nresult = binary_exp_mod_recursive(base, modulus, exponent)  # wrong order\n\n# after\nresult = binary_exp_mod_recursive(base, exponent, modulus)","handlingStrategy":"validation","validationCode":"if modulus <= 0:\n    raise ValueError(f\"modulus must be >= 1, got {modulus}\")\nresult = binary_exp_mod_recursive(base, exponent, modulus)","typeGuard":"def is_positive_int(m: object) -> bool:\n    return isinstance(m, int) and m >= 1","tryCatchPattern":null,"preventionTips":["Signature order is (base, exponent, modulus) — double-check swaps","Never default a modulus config to 0"],"tags":["math","modular-arithmetic","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}