Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions maths/is_perfect_number.py
Original file line number Diff line number Diff line change
@@ -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