Skip to content

Commit 29e9047

Browse files
authored
Merge pull request #1 from Sayamgarg1/add-perfect-number
Create is_perfect_number.py
2 parents 8934bab + 7399975 commit 29e9047

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

maths/is_perfect_number.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def is_perfect_number(n: int) -> bool:
2+
"""
3+
Check if a number is a Perfect Number.
4+
5+
A perfect number is a positive integer that is equal to the sum
6+
of its positive divisors, excluding the number itself.
7+
8+
Example:
9+
6 = 1 + 2 + 3
10+
28 = 1 + 2 + 4 + 7 + 14
11+
"""
12+
13+
if n < 2:
14+
return False
15+
16+
total = 1 # 1 is always a divisor
17+
for i in range(2, int(n ** 0.5) + 1):
18+
if n % i == 0:
19+
total += i
20+
if i != n // i:
21+
total += n // i
22+
23+
return total == n
24+
25+
26+
if __name__ == "__main__":
27+
print(is_perfect_number(6)) # True
28+
print(is_perfect_number(28)) # True
29+
print(is_perfect_number(10)) # False

0 commit comments

Comments
 (0)