TheAlgorithms/Python · error · TypeError

Input value of [number={number}] must be an integer

Error message

Input value of [number={number}] must be an integer

What it means

Raised by liouville_lambda in maths/liouville_lambda.py when number is not an int. The Liouville function lambda(n) = (-1)^k where k is the number of prime factors of n (with multiplicity), computed here via len(prime_factors(number)) % 2, which requires an exact integer to factor; non-ints are rejected with TypeError before factorization. Note 11.0 fails even though it is whole.

Source

Thrown at maths/liouville_lambda.py:37

    1
    >>> liouville_lambda(11)
    -1
    >>> liouville_lambda(0)
    Traceback (most recent call last):
        ...
    ValueError: Input must be a positive integer
    >>> liouville_lambda(-1)
    Traceback (most recent call last):
        ...
    ValueError: Input must be a positive integer
    >>> liouville_lambda(11.0)
    Traceback (most recent call last):
        ...
    TypeError: Input value of [number=11.0] must be an integer
    """
    if not isinstance(number, int):
        msg = f"Input value of [number={number}] must be an integer"
        raise TypeError(msg)
    if number < 1:
        raise ValueError("Input must be a positive integer")
    return -1 if len(prime_factors(number)) % 2 else 1


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert before calling: liouville_lambda(int(number)).
  2. If truncation would be wrong, validate integrality: assert number == int(number).
  3. Cast numpy scalars with int() at the boundary.

Example fix

// before
lam = liouville_lambda(n)  # n is 11.0

// after
lam = liouville_lambda(int(n))
Defensive patterns

Strategy: type-guard

Validate before calling

number = int(number)  # after confirming number == int(number) if truncation matters
lam = liouville_lambda(number)

Type guard

def is_int_value(v) -> bool:
    return isinstance(v, int) or (isinstance(v, float) and v.is_integer())

Try / catch

try:
    lam = liouville_lambda(n)
except TypeError:
    lam = liouville_lambda(int(n))

Prevention

When it happens

Trigger: Calling liouville_lambda(11.0), liouville_lambda('7'), or liouville_lambda(2+0j). The isinstance check fires first; ints >= 1 proceed to prime factorization.

Common situations: Number-theory pipelines where values passed through float operations lose int type; JSON/config-sourced numbers; numpy integer scalars failing isinstance(x, int).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d8b5786d499a83dc. Report an issue: GitHub.