From f80ffe1f54a0a3caacc14efb93b61393052d9f14 Mon Sep 17 00:00:00 2001 From: Akash Date: Sun, 3 May 2020 23:26:33 +0530 Subject: [PATCH] added method for checking armstrong number (#1936) * added method for checking armstrong number * Update comment Co-authored-by: Christian Clauss --- maths/armstrong_numbers.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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__":