Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

a_perfect_number #13991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Sayamgarg1 wants to merge 3 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Sayamgarg1:master
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /