diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index 4ed23dd1d..39a1464a9 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -41,6 +41,14 @@ def armstrong_number(n: int) -> bool: temp //= 10 return n == sum +def narcissistic_number(n:int) -> bool: + """Return True if n is a narcissistic number or False if it is not""" + + expo = len(str(n)) #power, all number will be raised to + temp = [(int(i)**expo) for i in str(n)] # each digit will be multiplied expo times + + # check if sum of cube of each digit is equal to number + return n == sum(temp) def main(): """ @@ -48,6 +56,7 @@ def main(): """ num = int(input("Enter an integer to see if it is an Armstrong number: ").strip()) print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.") + print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.") if __name__ == "__main__":