{"record":{"id":"20ccebc5154e7a82","repo":"TheAlgorithms/Python","slug":"input-must-be-a-positive-integer","errorCode":null,"errorMessage":"Input must be a positive integer","messagePattern":"Input must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/liouville_lambda.py","lineNumber":39,"sourceCode":"    -1\n    >>> liouville_lambda(0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a positive integer\n    >>> liouville_lambda(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a positive integer\n    >>> liouville_lambda(11.0)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value of [number=11.0] must be an integer\n    \"\"\"\n    if not isinstance(number, int):\n        msg = f\"Input value of [number={number}] must be an integer\"\n        raise TypeError(msg)\n    if number < 1:\n        raise ValueError(\"Input must be a positive integer\")\n    return -1 if len(prime_factors(number)) % 2 else 1\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":21,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/liouville_lambda.py#L21-L47","documentation":"Raised by liouville_lambda in maths/liouville_lambda.py when number is an int but less than 1. The Liouville function is defined only for positive integers (lambda(1) = 1, computed as the empty factorization); 0 and negatives have no prime factorization, so the guard rejects them with ValueError after the type check and before prime_factors is called.","triggerScenarios":"Calling liouville_lambda(0) or liouville_lambda(-1). Any int < 1 hits the raise; 1 is valid and returns 1 (len([]) % 2 == 0 -> 1).","commonSituations":"Enumerating from 0 in benchmark loops; signed differences or offsets producing non-positive values; reusing validation from a function whose domain starts at 0.","solutions":["Enumerate candidates from 1: range(1, n + 1).","Validate bounds on user/computed input before the call.","Skip non-positive entries when mapping over mixed-sign data."],"exampleFix":"// before\nvalues = [liouville_lambda(i) for i in range(len(data))]  # i=0 raises\n\n// after\nvalues = [liouville_lambda(i) for i in range(1, len(data) + 1)]","handlingStrategy":"validation","validationCode":"if number < 1:\n    raise ValueError(f\"liouville_lambda needs number >= 1, got {number}\")\nlam = liouville_lambda(number)","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":"try:\n    lam = liouville_lambda(i)\nexcept ValueError:\n    lam = None  # or skip index","preventionTips":["Enumerate from 1, not 0","Filter non-positive entries when mapping over mixed-sign data"],"tags":["math","number-theory","liouville","valueerror","domain-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}