From 73999757796359ede1555ebebaa725c02cb99edb Mon Sep 17 00:00:00 2001 From: sayam garg Date: Sun, 7 Dec 2025 15:44:22 +0530 Subject: [PATCH 1/2] Create is_perfect_number.py --- maths/is_perfect_number.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 maths/is_perfect_number.py diff --git a/maths/is_perfect_number.py b/maths/is_perfect_number.py new file mode 100644 index 000000000000..34ebfbe4c732 --- /dev/null +++ b/maths/is_perfect_number.py @@ -0,0 +1,29 @@ +def is_perfect_number(n: int) -> bool: + """ + Check if a number is a Perfect Number. + + A perfect number is a positive integer that is equal to the sum + of its positive divisors, excluding the number itself. + + Example: + 6 = 1 + 2 + 3 + 28 = 1 + 2 + 4 + 7 + 14 + """ + + if n < 2: + return False + + total = 1 # 1 is always a divisor + for i in range(2, int(n ** 0.5) + 1): + if n % i == 0: + total += i + if i != n // i: + total += n // i + + return total == n + + +if __name__ == "__main__": + print(is_perfect_number(6)) # True + print(is_perfect_number(28)) # True + print(is_perfect_number(10)) # False From ba441ddfca908c943bbf69e37914cdba240c82fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 7 Dec 2025 10:20:21 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/is_perfect_number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/is_perfect_number.py b/maths/is_perfect_number.py index 34ebfbe4c732..072726ea480c 100644 --- a/maths/is_perfect_number.py +++ b/maths/is_perfect_number.py @@ -2,7 +2,7 @@ def is_perfect_number(n: int) -> bool: """ Check if a number is a Perfect Number. - A perfect number is a positive integer that is equal to the sum + A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. Example: @@ -14,7 +14,7 @@ def is_perfect_number(n: int) -> bool: return False total = 1 # 1 is always a divisor - for i in range(2, int(n ** 0.5) + 1): + for i in range(2, int(n**0.5) + 1): if n % i == 0: total += i if i != n // i: @@ -24,6 +24,6 @@ def is_perfect_number(n: int) -> bool: if __name__ == "__main__": - print(is_perfect_number(6)) # True + print(is_perfect_number(6)) # True print(is_perfect_number(28)) # True print(is_perfect_number(10)) # False