-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathdisarium_number.py
More file actions
38 lines (29 loc) · 743 Bytes
/
disarium_number.py
File metadata and controls
38 lines (29 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def is_disarium(num: int) -> bool:
"""
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.
Args:
num (int): The number to check.
Returns:
bool: True if num is a Disarium number, False otherwise.
Examples:
>>> is_disarium(135)
True
>>> is_disarium(89)
True
>>> is_disarium(75)
False
>>> is_disarium(9)
True
"""
digits = str(num)
total = 0
position = 1
for i in digits:
total += int(i) ** position
position += 1
return total == num
if __name__ == "__main__":
import doctest
doctest.testmod()