{"record":{"id":"a865bb1e719ccee4","repo":"TheAlgorithms/Python","slug":"the-value-of-input-must-not-be-a-negative-number","errorCode":null,"errorMessage":"the value of input must not be a negative number","messagePattern":"the value of input must not be a negative number","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/minimum_squares_to_represent_a_number.py","lineNumber":31,"sourceCode":"    >>> minimum_squares_to_represent_a_number(21)\n    3\n    >>> minimum_squares_to_represent_a_number(58)\n    2\n    >>> minimum_squares_to_represent_a_number(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: the value of input must not be a negative number\n    >>> minimum_squares_to_represent_a_number(0)\n    1\n    >>> minimum_squares_to_represent_a_number(12.34)\n    Traceback (most recent call last):\n        ...\n    ValueError: the value of input must be a natural number\n    \"\"\"\n    if number != int(number):\n        raise ValueError(\"the value of input must be a natural number\")\n    if number < 0:\n        raise ValueError(\"the value of input must not be a negative number\")\n    if number == 0:\n        return 1\n    answers = [-1] * (number + 1)\n    answers[0] = 0\n    for i in range(1, number + 1):\n        answer = sys.maxsize\n        root = int(math.sqrt(i))\n        for j in range(1, root + 1):\n            current_answer = 1 + answers[i - (j**2)]\n            answer = min(answer, current_answer)\n        answers[i] = answer\n    return answers[number]\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/minimum_squares_to_represent_a_number.py#L13-L49","documentation":"Raised by minimum_squares_to_represent_a_number(number) when number < 0, after the integrality check has already passed. Negative numbers would create a negative-size answers list ([-1] * (number + 1) with number + 1 <= 0), so they are rejected with this ValueError. The doctest documents minimum_squares_to_represent_a_number(-1) raising it.","triggerScenarios":"minimum_squares_to_represent_a_number(-1) or any negative integral value; -3.0 also reaches this check because -3.0 == int(-3.0) passes the first guard and then compares negative.","commonSituations":"Unvalidated user input; arithmetic that underflows below zero (a - b with b > a); sign errors when converting signed deltas to counts.","solutions":["Guard at the call site: only call when number >= 0, e.g. via max(0, number) if clamping suits your domain.","Fix the upstream computation producing the negative value.","Validate parsed input early: if number < 0: reject with your own error message."],"exampleFix":"# before\nsquares = minimum_squares_to_represent_a_number(delta)  # delta = -5 -> ValueError\n\n# after\nif delta < 0:\n    raise ValueError('delta must be non-negative')\nsquares = minimum_squares_to_represent_a_number(delta)","handlingStrategy":"validation","validationCode":"if number < 0:\n    raise ValueError(f'number must be >= 0, got {number}')\ncount = minimum_squares_to_represent_a_number(number)","typeGuard":"def is_non_negative_number(value: object) -> bool:\n    return isinstance(value, (int, float)) and value >= 0","tryCatchPattern":"try:\n    count = minimum_squares_to_represent_a_number(number)\nexcept ValueError as exc:\n    if 'negative number' in str(exc):\n        raise ValueError('input underflowed below zero; check upstream math') from exc\n    raise","preventionTips":["Validate sign at the boundary where numbers enter your system.","Audit subtraction-based computations for underflow below zero.","Test numeric helpers with -1, 0, 1 boundary triples."],"tags":["python","input-validation","boundary-check","dynamic-programming"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}