TheAlgorithms/Python · error · Exception
numbers must be integer and greater than zero
Error message
numbers must be integer and greater than zero
What it means
Raised by get_greatest_common_divisor in maths/gcd_of_n_numbers.py when any element of numbers fails factorization. The function maps get_factors over all inputs inside a try block; get_factors raises TypeError for non-positive-integers, and the except clause re-raises it as a generic Exception('numbers must be integer and greater than zero') chained from the original. One bad element aborts the whole GCD computation.
Source
Thrown at maths/gcd_of_n_numbers.py:92
...
Exception: numbers must be integer and greater than zero
>>> get_greatest_common_divisor(1.5, 2)
Traceback (most recent call last):
...
Exception: numbers must be integer and greater than zero
>>> get_greatest_common_divisor(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
1
>>> get_greatest_common_divisor("1", 2, 3, 4, 5, 6, 7, 8, 9, 10)
Traceback (most recent call last):
...
Exception: numbers must be integer and greater than zero
"""
# we just need factors, not numbers itself
try:
same_factors, *factors = map(get_factors, numbers)
except TypeError as e:
raise Exception("numbers must be integer and greater than zero") from e
for factor in factors:
same_factors &= factor
# get common factor between all
# `&` return common elements with smaller value (for Counter type)
# now, same_factors is something like {2: 2, 3: 4} that means 2 * 2 * 3 * 3 * 3 * 3
mult = 1
# power each factor and multiply
# for {2: 2, 3: 4}, it is [4, 81] and then 324
for m in [factor**power for factor, power in same_factors.items()]:
mult *= m
return mult
if __name__ == "__main__":
print(get_greatest_common_divisor(18, 45)) # 9
View on GitHub (pinned to f5988cc097)
Solutions
- Sanitize the whole collection before calling: nums = [int(n) for n in numbers] and assert all n > 0.
- Filter out invalid entries if they are expected noise: nums = [n for n in numbers if isinstance(n, int) and n > 0] (then require nums non-empty).
- Catch Exception (the wrapper is generic, not TypeError) around the call if you must handle it, and inspect __cause__ for the original error.
Example fix
// before
gcd = get_greatest_common_divisor(*values) # values may contain '1' or 0
// after
values = [int(v) for v in values]
if any(v <= 0 for v in values):
raise ValueError(f"all inputs must be positive integers: {values}")
gcd = get_greatest_common_divisor(*values) Defensive patterns
Strategy: validation
Validate before calling
nums = [int(n) for n in numbers]
if not nums or any(n <= 0 for n in nums):
raise ValueError(f"all inputs must be positive integers: {numbers!r}")
gcd = get_greatest_common_divisor(*nums) Type guard
def all_positive_ints(seq) -> bool:
return bool(seq) and all(isinstance(n, int) and n > 0 for n in seq) Try / catch
try:
g = get_greatest_common_divisor(*nums)
except Exception as e: # wrapper raises generic Exception, not TypeError
if isinstance(e.__cause__, TypeError):
# invalid element detected
... Prevention
- Validate the whole collection, not each call
- Remember the re-raise is a bare Exception, so catching TypeError will miss it
When it happens
Trigger: Calling get_greatest_common_divisor('1', 2, 3) or with any single non-int, zero, or negative element among the arguments. The map(get_factors, numbers) call raises TypeError which is wrapped and re-raised.
Common situations: Spreads of mixed data (e.g. a list containing a string from parsing); forgetting that a 0 in the dataset is invalid for factorization; assuming the function skips bad values instead of failing fast.
Related errors
- Limit for the Catalan sequence must be ≥ 0
- Number should not be negative.
- Negative arguments are not supported
- the value of input must be a natural number
- abs_min() arg is an empty sequence
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/38a7ea61bb7e0e0f.
Report an issue: GitHub.