Skip to content

Commit 35a668f

Browse files
Create digitial_root.py
1 parent e2a78d4 commit 35a668f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

maths/digitial_root.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
digital_root.py
3+
----------------
4+
Calculates the digital root of a given number.
5+
6+
A digital root is obtained by summing the digits of a number repeatedly
7+
until only a single-digit number remains.
8+
9+
Example:
10+
>>> digital_root(942)
11+
6
12+
>>> digital_root(132189)
13+
6
14+
>>> digital_root(493193)
15+
2
16+
"""
17+
18+
def digital_root(n: int) -> int:
19+
"""Return the digital root of a non-negative integer."""
20+
while n >= 10:
21+
n = sum(map(int, str(n)))
22+
return n
23+
24+
25+
if __name__ == "__main__":
26+
num = int(input("Enter a number: "))
27+
print(digital_root(num))

0 commit comments

Comments
 (0)