We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8934bab + 7399975 commit 29e9047Copy full SHA for 29e9047
maths/is_perfect_number.py
@@ -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