{"record":{"id":"f5a027db3dd2f84f","repo":"TheAlgorithms/Python","slug":"number-n-must-instead-be-a-positive-integer-f5a027","errorCode":null,"errorMessage":"Number {n} must instead be a positive integer","messagePattern":"Number (.+?) must instead be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/carmichael_number.py","lineNumber":68,"sourceCode":"    >>> is_carmichael_number(5.1)\n    Traceback (most recent call last):\n         ...\n    ValueError: Number 5.1 must instead be a positive integer\n\n    >>> is_carmichael_number(-7)\n    Traceback (most recent call last):\n         ...\n    ValueError: Number -7 must instead be a positive integer\n\n    >>> is_carmichael_number(0)\n    Traceback (most recent call last):\n         ...\n    ValueError: Number 0 must instead be a positive integer\n    \"\"\"\n\n    if n <= 0 or not isinstance(n, int):\n        msg = f\"Number {n} must instead be a positive integer\"\n        raise ValueError(msg)\n\n    return all(\n        power(b, n - 1, n) == 1\n        for b in range(2, n)\n        if greatest_common_divisor(b, n) == 1\n    )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n\n    number = int(input(\"Enter number: \").strip())\n    if is_carmichael_number(number):\n        print(f\"{number} is a Carmichael Number.\")\n    else:\n        print(f\"{number} is not a Carmichael Number.\")","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/carmichael_number.py#L50-L86","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate and convert input: n = int(n) if isinstance(n, float) and n.is_integer() else n","Filter candidates to ints >= 3 before testing: if isinstance(n, int) and n >= 3","Fix the upstream producer to emit ints (use int parsing, // division)"],"exampleFix":"# before\nis_carmichael_number(float(user_input))  # -> ValueError\n\n# after\nis_carmichael_number(int(user_input))","handlingStrategy":"validation","validationCode":"if isinstance(n, int) and not isinstance(n, bool) and n > 0:\n    print(is_carmichael_number(n))\nelse:\n    print('n must be a positive integer')","typeGuard":"def is_positive_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value > 0","tryCatchPattern":null,"preventionTips":["Filter candidates to ints >= 3 in sieve loops (561 is the smallest)","Convert parsed strings with int() at the boundary","One ValueError covers both type and range failures here"],"tags":["python","math","number-theory","validation","value-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}