File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
3+ """
4+ Digital Root.
5+ The digital root (also known as the repeated digital sum) of a non-negative
6+ integer is the (single digit) value obtained by an iterative process of summing
7+ digits: on each iteration the digits of the previous result are summed, and the
8+ process continues until a single-digit number is reached.
9+
10+ For more information see:
11+ https://en.wikipedia.org/wiki/Digital_root
12+ """
13+
14+
15+ def digital_root (n : int ) -> int :
16+ """
17+ Return the digital root of a non-negative integer n.
18+
19+ The digital root is the single-digit value obtained by repeatedly summing
20+ the decimal digits of n until only one digit remains. The input is taken by
21+ its absolute value, so negative numbers behave like their positive
22+ counterpart.
23+
24+ >>> digital_root(0)
25+ 0
26+ >>> digital_root(9)
27+ 9
28+ >>> digital_root(38)
29+ 2
30+ >>> digital_root(12345)
31+ 6
32+ >>> digital_root(-45)
33+ 9
34+ >>> digital_root(999999999999)
35+ 9
36+ """
37+ n = abs (n )
38+ while n >= 10 :
39+ n = sum (int (digit ) for digit in str (n ))
40+ return n
41+
42+
43+ if __name__ == "__main__" :
44+ import doctest
45+
46+ doctest .testmod ()
You can’t perform that action at this time.
0 commit comments