From c710f385d88a4c57b4023f4be3d09c38cba562a0 Mon Sep 17 00:00:00 2001 From: Pranav M S Krishnan Date: Fri, 17 Oct 2025 21:29:42 +0530 Subject: [PATCH 1/3] Add is_armstrong_number function Implement function to check Armstrong numbers with examples. --- maths/is_armstrong_number.py | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maths/is_armstrong_number.py diff --git a/maths/is_armstrong_number.py b/maths/is_armstrong_number.py new file mode 100644 index 000000000000..0b9849380619 --- /dev/null +++ b/maths/is_armstrong_number.py @@ -0,0 +1,38 @@ +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 df9eac0d60c13e235a1913ef556a67cf70c0b5cc Mon Sep 17 00:00:00 2001 From: Pranav M S Krishnan Date: Fri, 17 Oct 2025 21:40:56 +0530 Subject: [PATCH 2/3] Rename parameter in is_armstrong_number function Rename parameter n to number for clarity --- maths/is_armstrong_number.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/is_armstrong_number.py b/maths/is_armstrong_number.py index 0b9849380619..793fa8d838dd 100644 --- a/maths/is_armstrong_number.py +++ b/maths/is_armstrong_number.py @@ -1,4 +1,4 @@ -def is_armstrong_number(n: int) -> bool: +def is_armstrong_number(number: int) -> bool: """ Check whether a non-negative integer is an Armstrong (narcissistic) number. @@ -25,14 +25,15 @@ def is_armstrong_number(n: int) -> bool: False """ # Only non-negative integers are considered - if n < 0: + if number < 0: return False # Convert to string to count digits - digits = str(n) + digits = str(number) power = len(digits) # Sum of each digit raised to the 'power' total = sum(int(d) ** power for d in digits) - return total == n + return total == number + From cf68d3216d9e353cfcf7381040b424e7502411b5 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 16:11:16 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/is_armstrong_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/is_armstrong_number.py b/maths/is_armstrong_number.py index 793fa8d838dd..dfc5e6e7b7f0 100644 --- a/maths/is_armstrong_number.py +++ b/maths/is_armstrong_number.py @@ -36,4 +36,3 @@ def is_armstrong_number(number: int) -> bool: total = sum(int(d) ** power for d in digits) return total == number -