TheAlgorithms/Python · error · ValueError
The input value cannot be less than 2
Error message
The input value cannot be less than 2
What it means
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.
Source
Thrown at maths/pollard_rho.py:39
274177
>>> pollard_rho(97546105601219326301)
9876543191
>>> pollard_rho(100)
2
>>> pollard_rho(17)
>>> pollard_rho(17**3)
17
>>> pollard_rho(17**3, attempts=1)
>>> pollard_rho(3*5*7)
21
>>> pollard_rho(1)
Traceback (most recent call last):
...
ValueError: The input value cannot be less than 2
"""
# A value less than 2 can cause an infinite loop in the algorithm.
if num < 2:
raise ValueError("The input value cannot be less than 2")
# Because of the relationship between ``f(f(x))`` and ``f(x)``, this
# algorithm struggles to find factors that are divisible by two.
# As a workaround, we specifically check for two and even inputs.
# See: https://math.stackexchange.com/a/2856214/165820
if num > 2 and num % 2 == 0:
return 2
# Pollard's Rho algorithm requires a function that returns pseudorandom
# values between 0 <= X < ``num``. It doesn't need to be random in the
# sense that the output value is cryptographically secure or difficult
# to calculate, it only needs to be random in the sense that all output
# values should be equally likely to appear.
# For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num``
# However, the success of Pollard's algorithm isn't guaranteed and is
# determined in part by the initial seed and the chosen random function.
# To make retries easier, we will instead use ``f(x) = (x**2 + C) % num``
# where ``C`` is a value that we can modify between each attempt.View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
factor = pollard_rho(n) # ValueError when n == 1
# after
if n < 2:
return [] # no prime factors below 2
factor = pollard_rho(n) Defensive patterns
Strategy: validation
Validate before calling
if num < 2:
return [] # nothing to factor
pollard_rho(num) Type guard
def is_factorable(v) -> bool:
return isinstance(v, int) and v >= 2 Try / catch
try:
pollard_rho(num)
except ValueError:
handle_trivial_input(num) # 0, 1, negatives Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- number must be an integer
- multiplicative_persistence() only accepts integral values
- multiplicative_persistence() does not accept negative values
- additive_persistence() only accepts integral values
- additive_persistence() does not accept negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/34f267acd15f89d0.
Report an issue: GitHub.