{"record":{"id":"66218fe516305aee","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-greater-than-or-equal-to-one-66218f","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/sol2.py","lineNumber":50,"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    prime = 1\n    i = 2\n    while i * i <= n:\n        while n % i == 0:\n            prime = i\n            n //= i\n        i += 1\n    if n > 1:\n        prime = n\n    return int(prime)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":32,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_003/sol2.py#L32-L65","documentation":"Documented ValueError from project_euler/problem_003/sol2.py:solution when n <= 0 after int(n) succeeds; the trial-division factorization loop requires a positive integer. CAVEAT: the module is unparseable under Python 3 ('except TypeError, ValueError:' at line 48), so it raises SyntaxError at import and this ValueError is unreachable until the syntax is repaired.","triggerScenarios":"After the fix: solution(0) or solution(-1). Today: any import of project_euler/problem_003/sol2.py under Python 3 raises SyntaxError('multiple exception types must be parenthesized') instead.","commonSituations":"Calling the legacy solution from a py3 driver or doctest; batch scripts enumerating n from a range that includes 0; input parsing that yields 0 for empty strings (int('') raises TypeError instead - a related trap).","solutions":["Repair the except clause at project_euler/problem_003/sol2.py:48 to parenthesized form so the module loads.","Pass n >= 1.","Validate bounds before calling: if not isinstance(n, int) or n < 1: reject.","Catch ValueError when iterating over externally supplied bounds."],"exampleFix":"# before\nsolution(0)\n\n# after\nn = max(int(user_input), 1) if str(user_input).strip().isdigit() else None\nif n is None:\n    raise ValueError('n must be a positive integer')\nsolution(n)","handlingStrategy":"validation","validationCode":"n = int(n)\nif n <= 0:\n    raise ValueError(f'n must be >= 1, got {n}')\nsolution(n)","typeGuard":"def is_valid_factorization_input(n) -> bool:\n    return isinstance(n, int) and n >= 1","tryCatchPattern":"try:\n    solution(n)\nexcept ValueError:\n    ...","preventionTips":["Start sweep ranges at 1, not 0.","Fix the module's py2 except syntax before relying on any of its errors.","Treat empty-string input separately: int('') raises, it does not become 0."],"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-15T22:17:37.221Z"}