Skip to content

Commit f86a6bd

Browse files
author
ARRVINDH PK
committed
Add algorithm to check Armstrong number
1 parent e2a78d4 commit f86a6bd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

maths/is_armstrong_number.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def is_armstrong_number(number: int) -> bool:
2+
"""
3+
Check if a number is an Armstrong number.
4+
An Armstrong number is a number that is equal to the sum
5+
of its own digits each raised to the power of the number of digits.
6+
7+
>>> is_armstrong_number(153)
8+
True
9+
>>> is_armstrong_number(370)
10+
True
11+
>>> is_armstrong_number(371)
12+
True
13+
>>> is_armstrong_number(9474)
14+
True
15+
>>> is_armstrong_number(123)
16+
False
17+
"""
18+
digits = str(number)
19+
power = len(digits)
20+
return number == sum(int(digit) ** power for digit in digits)
21+
22+
23+
if __name__ == "__main__":
24+
import doctest
25+
26+
doctest.testmod()

0 commit comments

Comments
 (0)