TheAlgorithms/Python · error · ValueError
multiplicative_persistence() does not accept negative values
Error message
multiplicative_persistence() does not accept negative values
What it means
multiplicative_persistence() in maths/persistence.py rejects negative inputs after confirming the value is an int: if num < 0 it raises ValueError('multiplicative_persistence() does not accept negative values'). Digit-product persistence is defined for non-negative integers, and str(-39) would inject a '-' character that breaks the int(i) digit parsing, so negatives are refused rather than mangled.
Source
Thrown at maths/persistence.py:22
https://en.wikipedia.org/wiki/Persistence_of_a_number
>>> multiplicative_persistence(217)
2
>>> multiplicative_persistence(-1)
Traceback (most recent call last):
...
ValueError: multiplicative_persistence() does not accept negative values
>>> multiplicative_persistence("long number")
Traceback (most recent call last):
...
ValueError: multiplicative_persistence() only accepts integral values
"""
if not isinstance(num, int):
raise ValueError("multiplicative_persistence() only accepts integral values")
if num < 0:
raise ValueError("multiplicative_persistence() does not accept negative values")
steps = 0
num_string = str(num)
while len(num_string) != 1:
numbers = [int(i) for i in num_string]
total = 1
for i in range(len(numbers)):
total *= numbers[i]
num_string = str(total)
steps += 1
return steps
def additive_persistence(num: int) -> int:View on GitHub (pinned to f5988cc097)
Solutions
- Pass the absolute value if sign is irrelevant: multiplicative_persistence(abs(num)).
- Add an upstream range check (num >= 0) with your own error message if negatives indicate a bug in caller data.
- Catch ValueError if negative input is expected occasionally and handle it explicitly.
Example fix
# before multiplicative_persistence(value) # ValueError when value == -39 # after multiplicative_persistence(abs(value))
Defensive patterns
Strategy: validation
Validate before calling
if num < 0:
raise ValueError(f'persistence undefined for negative {num}')
multiplicative_persistence(num) Try / catch
try:
multiplicative_persistence(num)
except ValueError as exc:
if 'negative' in str(exc):
num = abs(num) # only if sign is truly irrelevant
else:
raise Prevention
- abs() signed values before digit math when sign is noise.
- Treat a negative input as a data-quality bug and log it.
- Validate numeric ranges at the boundary, not inside math loops.
When it happens
Trigger: Calling multiplicative_persistence(-1) or any negative int such as multiplicative_persistence(-39). Only reached when the argument is an int; a negative float raises the integral-values error instead because the type check runs first.
Common situations: Feeding signed deltas or temperatures into the function; forgetting to abs() a value that can be negative by domain; off-by-one underflow producing -1 in a loop.
Related errors
- number must be an integer
- multiplicative_persistence() only accepts integral values
- additive_persistence() only accepts integral values
- additive_persistence() does not accept negative values
- is_prime() only accepts positive integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a99c32854c7a769d.
Report an issue: GitHub.