From 967957c5c49f05c6f39d9788ece4ae443ecb5f65 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Mon, 15 Sep 2025 23:52:14 +0000 Subject: [PATCH] [Sync Iteration] python/perfect-numbers/2 --- .../perfect-numbers/2/perfect_numbers.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 solutions/python/perfect-numbers/2/perfect_numbers.py diff --git a/solutions/python/perfect-numbers/2/perfect_numbers.py b/solutions/python/perfect-numbers/2/perfect_numbers.py new file mode 100644 index 0000000..3b99946 --- /dev/null +++ b/solutions/python/perfect-numbers/2/perfect_numbers.py @@ -0,0 +1,35 @@ +""" +Determine if a number is perfect, abundant, or deficient based on +Nicomachus' (60 - 120 CE) classification scheme for positive integers. +""" + +import math + + +def classify(number: int) -> str: + """ + A perfect number equals the sum of its positive divisors. + + :param number: int a positive integer + :return: str the classification of the input integer + """ + if number < 1: + raise ValueError( + "Classification is only possible for positive integers." + ) + + total_divisors: int = 1 + + for n in range(2, int(math.sqrt(number)) + 1): + if number % n == 0: + total_divisors += n + if n != number // n: + total_divisors += number // n + + if number == 1 or number > total_divisors: + return "deficient" + + if total_divisors == number: + return "perfect" + + return "abundant"