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.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.