From 106d04f318ca3041688a9769ad8b4257ba2274b7 Mon Sep 17 00:00:00 2001 From: Akash Date: Sun, 3 May 2020 22:40:06 +0530 Subject: [PATCH 1/2] added method for checking armstrong number --- maths/armstrong_numbers.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index 4ed23dd1d1d7..77f3aa77292d 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 armstrong 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__": From 3f6be56b7c80649702fee3f6dafdf0951db15a65 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 3 May 2020 19:55:48 +0200 Subject: [PATCH 2/2] Update comment --- maths/armstrong_numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index 77f3aa77292d..39a1464a9aa6 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -42,7 +42,7 @@ def armstrong_number(n: int) -> bool: return n == sum def narcissistic_number(n:int) -> bool: - """Return True if n is a armstrong number or False if it is not""" + """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