From 860a036c124e2dfbd1c95233ff96d47bc18b5ad3 Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Sat, 30 May 2026 00:09:48 +0530 Subject: [PATCH 1/4] maths: add Armstrong numbers algorithm --- maths/armstrong_numbers.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maths/armstrong_numbers.py diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py new file mode 100644 index 000000000000..fb96b58c6589 --- /dev/null +++ b/maths/armstrong_numbers.py @@ -0,0 +1,43 @@ +"""Armstrong Numbers - checking if a number equals sum of its digits raised to power of digit count.""" + + +def is_armstrong(number: int) -> bool: + """ + Returns True if number is an Armstrong number, False otherwise. + + >>> is_armstrong(0) + True + >>> is_armstrong(1) + True + >>> is_armstrong(153) + True + >>> is_armstrong(370) + True + >>> is_armstrong(9474) + True + >>> is_armstrong(100) + False + >>> is_armstrong(25) + False + """ + if not isinstance(number, int) or number < 0: + raise ValueError("is_armstrong() only accepts non-negative integers") + + digits = str(number) + power = len(digits) + return sum(int(d) ** power for d in digits) == number + + +def get_armstrongs_up_to(limit: int) -> list[int]: + """ + Returns list of all Armstrong numbers up to limit. + + >>> get_armstrongs_up_to(500) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407] + """ + return [i for i in range(limit + 1) if is_armstrong(i)] + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 1c815a28f22b6ad00fc623281a1a82371c01eeb1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 May 2026 18:56:46 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/armstrong_numbers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index fb96b58c6589..44bde7d197c3 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -22,7 +22,7 @@ def is_armstrong(number: int) -> bool: """ if not isinstance(number, int) or number < 0: raise ValueError("is_armstrong() only accepts non-negative integers") - + digits = str(number) power = len(digits) return sum(int(d) ** power for d in digits) == number @@ -40,4 +40,5 @@ def get_armstrongs_up_to(limit: int) -> list[int]: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 9e442030a14db3dea7c94ec9484e3bac4c23900b Mon Sep 17 00:00:00 2001 From: Ayushi Jain Date: Sat, 30 May 2026 00:41:39 +0530 Subject: [PATCH 3/4] maths: fix line too long in armstrong_numbers --- maths/armstrong_numbers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index 44bde7d197c3..b2573bc365e4 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -1,4 +1,4 @@ -"""Armstrong Numbers - checking if a number equals sum of its digits raised to power of digit count.""" +"""Armstrong Numbers.""" def is_armstrong(number: int) -> bool: @@ -10,7 +10,7 @@ def is_armstrong(number: int) -> bool: >>> is_armstrong(1) True >>> is_armstrong(153) - True + True >>> is_armstrong(370) True >>> is_armstrong(9474) From 145543cf1b78c42bb4bb9e7ca5071424cce61a5c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 May 2026 19:13:25 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- 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 b2573bc365e4..e79b361a75d6 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -10,7 +10,7 @@ def is_armstrong(number: int) -> bool: >>> is_armstrong(1) True >>> is_armstrong(153) - True + True >>> is_armstrong(370) True >>> is_armstrong(9474)