Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions maths/digitial_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
digital_root.py
----------------
Calculates the digital root of a given number.

A digital root is obtained by summing the digits of a number repeatedly
until only a single-digit number remains.

Example:
>>> digital_root(942)
6
>>> digital_root(132189)
6
>>> digital_root(493193)
2
"""

def digital_root(n: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

"""Return the digital root of a non-negative integer."""
while n >= 10:
n = sum(map(int, str(n)))
return n


if __name__ == "__main__":
num = int(input("Enter a number: "))
print(digital_root(num))