TheAlgorithms/Python · error · TypeError
recursive_lucas_number accepts only integer arguments.
Error message
recursive_lucas_number accepts only integer arguments.
What it means
Raised by recursive_lucas_number in maths/lucasas_series.py (maths/lucas_series.py) when n_th_number is not an int. The naive doubly-recursive Lucas implementation (L(0)=2, L(1)=1, L(n)=L(n-1)+L(n-2)) decrements its argument by 1 and 2 each step, which only terminates for integer arguments; the type guard raises TypeError before the first recursive call. Note there is no negative-argument guard, so negative ints recurse until a TypeError is eventually raised by hitting a non-int.
Source
Thrown at maths/lucas_series.py:23
def recursive_lucas_number(n_th_number: int) -> int:
"""
Returns the nth lucas number
>>> recursive_lucas_number(1)
1
>>> recursive_lucas_number(20)
15127
>>> recursive_lucas_number(0)
2
>>> recursive_lucas_number(25)
167761
>>> recursive_lucas_number(-1.5)
Traceback (most recent call last):
...
TypeError: recursive_lucas_number accepts only integer arguments.
"""
if not isinstance(n_th_number, int):
raise TypeError("recursive_lucas_number accepts only integer arguments.")
if n_th_number == 0:
return 2
if n_th_number == 1:
return 1
return recursive_lucas_number(n_th_number - 1) + recursive_lucas_number(
n_th_number - 2
)
def dynamic_lucas_number(n_th_number: int) -> int:
"""
Returns the nth lucas number
>>> dynamic_lucas_number(1)
1
>>> dynamic_lucas_number(20)
15127
>>> dynamic_lucas_number(0)View on GitHub (pinned to f5988cc097)
Solutions
- Convert to non-negative int before calling: recursive_lucas_number(int(n)).
- Prefer dynamic_lucas_number for larger n (the recursive version is exponential-time).
- Guard n >= 0 at the call site since negative ints are not properly handled.
Example fix
// before val = recursive_lucas_number(idx) # idx is 20.0 // after idx = int(idx) val = recursive_lucas_number(idx) if idx >= 0 else None
Defensive patterns
Strategy: validation
Validate before calling
n = int(n_th_number)
if n < 0:
raise ValueError(f"Lucas index must be >= 0, got {n}")
val = dynamic_lucas_number(n) # prefer dynamic for speed Type guard
def is_nonneg_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) and v >= 0 Try / catch
try:
val = recursive_lucas_number(n)
except TypeError:
val = dynamic_lucas_number(int(n)) Prevention
- Always enforce n >= 0 yourself; the recursive variant mishandles negative ints
- Use the dynamic variant for n above ~30 to avoid exponential runtime
When it happens
Trigger: Calling recursive_lucas_number(-1.5), recursive_lucas_number(2.0), or recursive_lucas_number('5'). Also indirectly: calling with a negative int recurses down past 0 until n_th_number - 2 produces a non-int transition point and eventually trips the isinstance check.
Common situations: Float indices from formulas; string input from prompts (the __main__ block does int(input()) correctly, but reuse elsewhere may not); passing a negative index expecting generalized Lucas numbers.
Related errors
- dynamic_lucas_number accepts only integer arguments.
- Number should not be negative.
- find_max_recursive() arg is an empty sequence
- list index out of range
- find_min_recursive() arg is an empty sequence
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/031dba8539f99f94.
Report an issue: GitHub.