Skip to content

Commit f80ffe1

Browse files
Akashcclauss
Akash
andauthored
added method for checking armstrong number (TheAlgorithms#1936)
* added method for checking armstrong number * Update comment Co-authored-by: Christian Clauss <[email protected]>
1 parent b6fcee3 commit f80ffe1

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

maths/armstrong_numbers.py

+9
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ def armstrong_number(n: int) -> bool:
4141
temp //= 10
4242
return n == sum
4343

44+
def narcissistic_number(n:int) -> bool:
45+
"""Return True if n is a narcissistic number or False if it is not"""
46+
47+
expo = len(str(n)) #power, all number will be raised to
48+
temp = [(int(i)**expo) for i in str(n)] # each digit will be multiplied expo times
49+
50+
# check if sum of cube of each digit is equal to number
51+
return n == sum(temp)
4452

4553
def main():
4654
"""
4755
Request that user input an integer and tell them if it is Armstrong number.
4856
"""
4957
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
5058
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
59+
print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.")
5160

5261

5362
if __name__ == "__main__":

0 commit comments

Comments
 (0)