TheAlgorithms/Python · error · TypeError
number must be integer and greater than zero
Error message
number must be integer and greater than zero
What it means
Raised by get_factors in maths/gcd_of_n_numbers.py when the input does not match a positive integer. The function uses a match statement: int 1 returns Counter({1:1}), positive ints proceed to trial-division factoring, and everything else (str, float, None, negative ints, bools that fail the guard, etc.) falls to the wildcard case raising TypeError. It exists because prime factorization is only defined for positive integers.
Source
Thrown at maths/gcd_of_n_numbers.py:44
...
TypeError: number must be integer and greater than zero
>>> get_factors(1.5)
Traceback (most recent call last):
...
TypeError: number must be integer and greater than zero
factor can be all numbers from 2 to number that we check if number % factor == 0
if it is equal to zero, we check again with number // factor
else we increase factor by one
"""
match number:
case int(number) if number == 1:
return Counter({1: 1})
case int(num) if number > 0:
number = num
case _:
raise TypeError("number must be integer and greater than zero")
factors = factors or Counter()
if number == factor: # break condition
# all numbers are factors of itself
factors[factor] += 1
return factors
if number % factor > 0:
# if it is greater than zero
# so it is not a factor of number and we check next number
return get_factors(number, factors, factor + 1)
factors[factor] += 1
# else we update factors (that is Counter(dict-like) type) and check again
return get_factors(number // factor, factors, factor)
View on GitHub (pinned to f5988cc097)
Solutions
- Convert and validate inputs before calling: use int(x) inside a try/except and check x > 0.
- If calling via get_greatest_common_divisor, ensure every element of numbers is a positive int.
- Reject or normalize float-typed whole numbers (e.g. 4.0 -> 4) at your API boundary.
Example fix
// before
factors = get_factors(raw_input) # raw_input may be '12' or -3
// after
try:
n = int(raw_input)
except (TypeError, ValueError) as e:
raise TypeError(f"expected positive integer, got {raw_input!r}") from e
if n <= 0:
raise TypeError(f"expected positive integer, got {n}")
factors = get_factors(n) Defensive patterns
Strategy: type-guard
Validate before calling
def coerce_positive_int(value):
try:
n = int(value)
except (TypeError, ValueError) as e:
raise TypeError(f"expected positive integer, got {value!r}") from e
if isinstance(value, float) and value != n:
raise TypeError(f"non-integer float: {value!r}")
if n <= 0:
raise TypeError(f"must be > 0, got {n}")
return n Type guard
def is_positive_int(value) -> bool:
return isinstance(value, int) and not isinstance(value, bool) and value > 0 Try / catch
try:
factors = get_factors(value)
except TypeError as e:
# log and reject the bad value
... Prevention
- Convert string inputs to int at the system boundary
- Exclude 0 and negatives before factorization
When it happens
Trigger: Calling get_factors('12'), get_factors(3.5), get_factors(-4), get_factors(0), or get_factors(None). The wildcard 'case _' arm raises TypeError('number must be integer and greater than zero').
Common situations: Passing unvalidated user input (CLI args, form fields are strings) into gcd computation; negative numbers from subtraction logic reaching the factorizer; data pipelines mixing int and float representations of whole numbers.
Related errors
- Input value must be a positive integer. Input value: {number
- Input value of [number={number}] must be an integer
- mod inverse of {a!r} and {m!r} does not exist
- factorial() not defined for negative values
- surface_area_cube() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7f6adcb3b7e1d02d.
Report an issue: GitHub.