Skip to content
Closed
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
27 changes: 27 additions & 0 deletions maths/special_numbers/disarium_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def is_disarium(num):
"""
Check if a number is a Disarium number.
A Disarium number is a number in which the sum of its digits
powered with their respective positions is equal to the number itself.

Example:
135 -> 1¹ + 3² + 5³ = 135 ✅
"""
digits = str(num)
total = 0
position = 1

for i in digits:
total += int(i) ** position
position += 1

if total == num:
return True
else:
return False

Check failure on line 21 in maths/special_numbers/disarium_number.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM103)

maths/special_numbers/disarium_number.py:18:5: SIM103 Return the condition `total == num` directly


if __name__ == "__main__":
test_numbers = [89, 135, 175, 518, 9, 10]
for n in test_numbers:
print(f"{n} → {'Disarium' if is_disarium(n) else 'Not Disarium'}")
Loading