{"record":{"id":"34f267acd15f89d0","repo":"TheAlgorithms/Python","slug":"the-input-value-cannot-be-less-than-2","errorCode":null,"errorMessage":"The input value cannot be less than 2","messagePattern":"The input value cannot be less than 2","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/pollard_rho.py","lineNumber":39,"sourceCode":"    274177\n    >>> pollard_rho(97546105601219326301)\n    9876543191\n    >>> pollard_rho(100)\n    2\n    >>> pollard_rho(17)\n    >>> pollard_rho(17**3)\n    17\n    >>> pollard_rho(17**3, attempts=1)\n    >>> pollard_rho(3*5*7)\n    21\n    >>> pollard_rho(1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The input value cannot be less than 2\n    \"\"\"\n    # A value less than 2 can cause an infinite loop in the algorithm.\n    if num < 2:\n        raise ValueError(\"The input value cannot be less than 2\")\n\n    # Because of the relationship between ``f(f(x))`` and ``f(x)``, this\n    # algorithm struggles to find factors that are divisible by two.\n    # As a workaround, we specifically check for two and even inputs.\n    #   See: https://math.stackexchange.com/a/2856214/165820\n    if num > 2 and num % 2 == 0:\n        return 2\n\n    # Pollard's Rho algorithm requires a function that returns pseudorandom\n    # values between 0 <= X < ``num``.  It doesn't need to be random in the\n    # sense that the output value is cryptographically secure or difficult\n    # to calculate, it only needs to be random in the sense that all output\n    # values should be equally likely to appear.\n    # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num``\n    # However, the success of Pollard's algorithm isn't guaranteed and is\n    # determined in part by the initial seed and the chosen random function.\n    # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num``\n    # where ``C`` is a value that we can modify between each attempt.","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/pollard_rho.py#L21-L57","documentation":"pollard_rho() in maths/pollard_rho.py implements Pollard's rho integer factorization; it raises ValueError('The input value cannot be less than 2') when num < 2. The comment in source explains why: values below 2 (0, 1, negatives) cause an infinite loop in the rho iteration because the pseudo-random walk f(x) = (x^2 + c) % num degenerates. There is no nontrivial factorization for numbers below 2, so the guard is mathematical, not stylistic.","triggerScenarios":"Calling pollard_rho(1), pollard_rho(0), or pollard_rho(-15). Also reachable in a factorization loop if a remainder/composite shrinks to 1 unexpectedly (e.g. fully factored earlier than the loop expects).","commonSituations":"Reusing a generic 'factor n' driver that does not special-case n < 2; feeding results of a previous factorization step (which can be 1) back into the function; unvalidated CLI args.","solutions":["Special-case small inputs before calling: n < 2 has no factors; return/raise in your own driver.","In factorization loops, break when the cofactor reaches 1 instead of recursing/calling again.","Validate n >= 2 at the entry point of your factorization utility."],"exampleFix":"# before\nfactor = pollard_rho(n)  # ValueError when n == 1\n\n# after\nif n < 2:\n    return []  # no prime factors below 2\nfactor = pollard_rho(n)","handlingStrategy":"validation","validationCode":"if num < 2:\n    return []  # nothing to factor\npollard_rho(num)","typeGuard":"def is_factorable(v) -> bool:\n    return isinstance(v, int) and v >= 2","tryCatchPattern":"try:\n    pollard_rho(num)\nexcept ValueError:\n    handle_trivial_input(num)  # 0, 1, negatives","preventionTips":["Special-case n < 2 in every factorization driver.","Break factorization loops when the cofactor hits 1.","Validate num >= 2 once at the entry of your factor utility."],"tags":["python","value-error","factorization","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}