TheAlgorithms/Python · error · ValueError

Number {n} must instead be a positive integer

Error message

Number {n} must instead be a positive integer

What it means

Raised by is_carmichael_number() in maths/special_numbers/carmichael_number.py when n is not an int or is <= 0. Carmichael numbers are composite numbers satisfying Fermat's little theorem for all coprime bases; the definition only makes sense for positive integers >= 3 (the smallest is 561), so zero, negatives, and non-integers are rejected up front. The message interpolates n, e.g. 'Number -7 must instead be a positive integer'.

Source

Thrown at maths/special_numbers/carmichael_number.py:68

    >>> is_carmichael_number(5.1)
    Traceback (most recent call last):
         ...
    ValueError: Number 5.1 must instead be a positive integer

    >>> is_carmichael_number(-7)
    Traceback (most recent call last):
         ...
    ValueError: Number -7 must instead be a positive integer

    >>> is_carmichael_number(0)
    Traceback (most recent call last):
         ...
    ValueError: Number 0 must instead be a positive integer
    """

    if n <= 0 or not isinstance(n, int):
        msg = f"Number {n} must instead be a positive integer"
        raise ValueError(msg)

    return all(
        power(b, n - 1, n) == 1
        for b in range(2, n)
        if greatest_common_divisor(b, n) == 1
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    number = int(input("Enter number: ").strip())
    if is_carmichael_number(number):
        print(f"{number} is a Carmichael Number.")
    else:
        print(f"{number} is not a Carmichael Number.")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate and convert input: n = int(n) if isinstance(n, float) and n.is_integer() else n
  2. Filter candidates to ints >= 3 before testing: if isinstance(n, int) and n >= 3
  3. Fix the upstream producer to emit ints (use int parsing, // division)

Example fix

# before
is_carmichael_number(float(user_input))  # -> ValueError

# after
is_carmichael_number(int(user_input))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(n, int) and not isinstance(n, bool) and n > 0:
    print(is_carmichael_number(n))
else:
    print('n must be a positive integer')

Type guard

def is_positive_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool) and value > 0

Prevention

When it happens

Trigger: Calling is_carmichael_number(-7), is_carmichael_number(0), or is_carmichael_number(561.0) (float). Note the combined check n <= 0 or not isinstance(n, int) means floats are also rejected — a float 0.5 raises, and True (bool) would pass the type check but raise on <= 0 only if falsy.

Common situations: Testing edge cases at 0/1/2 (they return False, not errors, since they are valid positive ints); values from parsed text left as strings; float results from division. Remember 1 and 2 are valid inputs returning False.

Related errors


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