From ddd4efd7689494f52f837064f5abb76186a41bdd Mon Sep 17 00:00:00 2001 From: Nefelibata <124799179+MeiSiristhebest@users.noreply.github.com> Date: Sat, 1 Aug 2026 13:46:47 +0800 Subject: [PATCH 1/2] Add digital root algorithm --- maths/digital_root.py | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 maths/digital_root.py diff --git a/maths/digital_root.py b/maths/digital_root.py new file mode 100644 index 000000000000..ac529e53b940 --- /dev/null +++ b/maths/digital_root.py @@ -0,0 +1,46 @@ +from __future__ import annotations + +""" +Digital Root. +The digital root (also known as the repeated digital sum) of a non-negative +integer is the (single digit) value obtained by an iterative process of summing +digits: on each iteration the digits of the previous result are summed, and the +process continues until a single-digit number is reached. + +For more information see: +https://en.wikipedia.org/wiki/Digital_root +""" + + +def digital_root(n: int) -> int: + """ + Return the digital root of a non-negative integer n. + + The digital root is the single-digit value obtained by repeatedly summing + the decimal digits of n until only one digit remains. The input is taken by + its absolute value, so negative numbers behave like their positive + counterpart. + + >>> digital_root(0) + 0 + >>> digital_root(9) + 9 + >>> digital_root(38) + 2 + >>> digital_root(12345) + 6 + >>> digital_root(-45) + 9 + >>> digital_root(999999999999) + 9 + """ + n = abs(n) + while n >= 10: + n = sum(int(digit) for digit in str(n)) + return n + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 7f85add0a977446573b363d552b0e8ca863ee7b8 Mon Sep 17 00:00:00 2001 From: Nefelibata <124799179+MeiSiristhebest@users.noreply.github.com> Date: Sat, 1 Aug 2026 14:50:53 +0800 Subject: [PATCH 2/2] refactor: use descriptive parameter name `number` instead of `n` (algorithms-keeper) --- maths/digital_root.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/maths/digital_root.py b/maths/digital_root.py index ac529e53b940..bb809c98bd35 100644 --- a/maths/digital_root.py +++ b/maths/digital_root.py @@ -12,12 +12,12 @@ """ -def digital_root(n: int) -> int: +def digital_root(number: int) -> int: """ - Return the digital root of a non-negative integer n. + Return the digital root of a non-negative integer number. The digital root is the single-digit value obtained by repeatedly summing - the decimal digits of n until only one digit remains. The input is taken by + the decimal digits of number until only one digit remains. The input is taken by its absolute value, so negative numbers behave like their positive counterpart. @@ -34,10 +34,10 @@ def digital_root(n: int) -> int: >>> digital_root(999999999999) 9 """ - n = abs(n) - while n >= 10: - n = sum(int(digit) for digit in str(n)) - return n + number = abs(number) + while number >= 10: + number = sum(int(digit) for digit in str(number)) + return number if __name__ == "__main__":