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.
1 parent 8934bab commit 4b720f4Copy full SHA for 4b720f4
maths/is_armstrong.py
@@ -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