{"record":{"id":"81c221dfa71b207b","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-greater-than-or-equal-to-one-81c221","errorCode":null,"errorMessage":"Parameter n must be greater than or equal to one.","messagePattern":"Parameter n must be greater than or equal to one\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_003/sol1.py","lineNumber":86,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    \"\"\"\n\n    try:\n        n = int(n)\n    except TypeError, ValueError:\n        raise TypeError(\"Parameter n must be int or castable to int.\")\n    if n <= 0:\n        raise ValueError(\"Parameter n must be greater than or equal to one.\")\n    max_number = 0\n    if is_prime(n):\n        return n\n    while n % 2 == 0:\n        n //= 2\n    if is_prime(n):\n        return n\n    for i in range(3, int(math.sqrt(n)) + 1, 2):\n        if n % i == 0:\n            if is_prime(n // i):\n                max_number = n // i\n                break\n            elif is_prime(i):\n                max_number = i\n    return max_number\n\n\nif __name__ == \"__main__\":","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_003/sol1.py#L68-L104","documentation":"Documented ValueError from project_euler/problem_003/sol1.py:solution when n <= 0 after int conversion; the largest-prime-factor algorithm only accepts positive integers. CAVEAT: unreachable as shipped because the file's 'except TypeError, ValueError:' is Python 2 syntax and the module raises SyntaxError under Python 3 before this check runs.","triggerScenarios":"After the syntax fix: solution(0) or solution(-10). As written today: importing the module under Python 3 fails immediately with SyntaxError.","commonSituations":"Generic benchmark harnesses that call every solution with 0; negative values from subtracting user input without clamping; running the legacy file under a modern interpreter where the py2 syntax breaks first.","solutions":["Fix the except clause at project_euler/problem_003/sol1.py:84 to use parenthesized exceptions so the module imports.","Pass n >= 1 (1 is accepted; 2 returns 2 immediately).","Clamp user-derived input: n = max(n, 1) only if 1 is semantically acceptable, otherwise reject.","Catch ValueError for batch enumeration over possibly-invalid inputs."],"exampleFix":"# before\nsolution(0)\n\n# after\nn = int(input_value)\nif n <= 0:\n    raise ValueError('n must be positive')\nresult = solution(n)","handlingStrategy":"validation","validationCode":"n = int(n)\nif n < 1:\n    raise ValueError('n must be a positive integer')\nsolution(n)","typeGuard":"def is_valid_factorization_input(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":"try:\n    solution(n)\nexcept ValueError as e:\n    if 'greater than or equal to one' in str(e):\n        ...","preventionTips":["Reject n <= 0 at the call site; do not rely on the library check (module is unparseable until syntax fixed).","Remember solution(1) returns 1 - decide if that is meaningful for you.","Validate user/CLI input before batch runs."],"tags":["python-euler","python2-syntax","valueerror","validation","prime-factorization"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}