From 8ba7fc52e13f6b1e270c6ea4da087956bcdb6d20 Mon Sep 17 00:00:00 2001 From: Pranav M S Krishnan Date: Fri, 17 Oct 2025 21:20:15 +0530 Subject: [PATCH 1/2] Add Armstrong number check algorithm Implement function to check Armstrong numbers with examples. --- maths/armstrong_number.py | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 maths/armstrong_number.py diff --git a/maths/armstrong_number.py b/maths/armstrong_number.py new file mode 100644 index 000000000000..e1850ff95ddd --- /dev/null +++ b/maths/armstrong_number.py @@ -0,0 +1,40 @@ +from typing import Union + +def is_armstrong_number(n: int) -> bool: + """ + Check whether a non-negative integer is an Armstrong (narcissistic) number. + + An Armstrong number is a number that is the sum of its own digits each raised + to the power of the number of digits in the number. + + Reference: + Narcissistic number (Armstrong number) — Wikipedia + https://en.wikipedia.org/wiki/Narcissistic_number + + >>> is_armstrong_number(0) + True + >>> is_armstrong_number(1) + True + >>> is_armstrong_number(153) + True + >>> is_armstrong_number(370) + True + >>> is_armstrong_number(9474) + True + >>> is_armstrong_number(9475) + False + >>> is_armstrong_number(-1) # negative numbers are not considered Armstrong + False + """ + # Only non-negative integers are considered + if n < 0: + return False + + # Convert to string to count digits + digits = str(n) + power = len(digits) + + # Sum of each digit raised to the 'power' + total = sum(int(d) ** power for d in digits) + + return total == n From 6ca69acf22a3b246f795e5900db4c5bbe74beaa3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Oct 2025 15:54:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/armstrong_number.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/armstrong_number.py b/maths/armstrong_number.py index e1850ff95ddd..5e3170529941 100644 --- a/maths/armstrong_number.py +++ b/maths/armstrong_number.py @@ -1,5 +1,6 @@ from typing import Union + def is_armstrong_number(n: int) -> bool: """ Check whether a non-negative integer is an Armstrong (narcissistic) number. @@ -37,4 +38,4 @@ def is_armstrong_number(n: int) -> bool: # Sum of each digit raised to the 'power' total = sum(int(d) ** power for d in digits) - return total == n + return total == n