Skip to content

Commit 4b720f4

Browse files
authored
Create is_armstrong.py
1 parent 8934bab commit 4b720f4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

maths/is_armstrong.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def is_armstrong(n: int) -> bool:
2+
"""
3+
Check if a number is an Armstrong number.
4+
5+
An Armstrong number (Narcissistic number) is a number that is equal
6+
to the sum of its digits each raised to the power of the number of digits.
7+
8+
Example:
9+
153 = 1^3 + 5^3 + 3^3
10+
"""
11+
12+
digits = list(map(int, str(n)))
13+
num_digits = len(digits)
14+
return n == sum(d ** num_digits for d in digits)
15+
16+
17+
if __name__ == "__main__":
18+
print(is_armstrong(153)) # True
19+
print(is_armstrong(370)) # True
20+
print(is_armstrong(10)) # False

0 commit comments

Comments
 (0)