Skip to content

Commit 5d35869

Browse files
authored
Update Armstrong_number.py
Script can now work with all the numbers of lenght n and base 10
1 parent 7aafe26 commit 5d35869

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Armstrong_number.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
def is_armstrong_number(number):
2-
total = 0
1+
"""
2+
In number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number),
3+
in a given number base b, is a number that is the total of its own digits each raised to the power of the number of digits.
4+
Source: https://en.wikipedia.org/wiki/Narcissistic_number
5+
NOTE:
6+
this scripts only works for number in base 10
7+
"""
38

4-
# find the sum of the cube of each digit
5-
temp = number
6-
while temp > 0:
7-
digit = temp % 10
8-
total += digit ** 3
9-
temp //= 10
9+
def is_armstrong_number(number:str):
10+
total:int = 0
11+
exp:int = len(number) #get the number of digits, this will determinate the exponent
12+
13+
digits:list[int] = []
14+
for digit in number: digits.append(int(digit)) #get the single digits
15+
for x in digits: total += x ** exp #get the power of each digit and sum it to the total
1016

11-
# return the result
12-
if number == total:
13-
return True
17+
# display the result
18+
if int(number) == total:
19+
print(number,"is an Armstrong number")
1420
else:
15-
return False
21+
print(number,"is not an Armstrong number")
1622

17-
number = int(input("Enter the number: "))
18-
if is_armstrong_number(number):
19-
print(number,"is an Armstrong number")
20-
else:
21-
print(number,"is not an Armstrong number")
23+
number = input("Enter the number : ")
24+
is_armstrong_number(number)

0 commit comments

Comments
 (0)