Editor

Digital Root

#29 · Math · Beginner · 30 XP · about 5 min

Add up the digits of a number. If the total has more than one digit, add up its digits too, and keep going until a single digit is left. That last digit is the number's digital root.

Write digital_root(n) that takes a whole number n (zero or more) and returns its digital root as an integer.

Examples

digital_root(16)     # returns 7
digital_root(942)    # returns 6
digital_root(5)      # returns 5

For 942: 9 + 4 + 2 = 15, then 1 + 5 = 6, which is a single digit.

Note

A number with one digit is already its own digital root, and that includes 0. n can have up to 100 digits.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.