Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-130

Digital Root

Problem

Return the digital root of a non-negative integer n: repeatedly sum its digits until a single digit remains.

Use the closed form 1 + (n - 1) mod 9 for n > 0, and return 0 when n is 0. The modulo operation must not be replaced by digit loops if you want the constant-time version.

Hint

A positive number is congruent to its digit sum modulo 9.

Test cases

case 1
in: [0]
expected: 0
case 2
in: [9]
expected: 9
case 3
in: [38]
expected: 2
case 4
in: [12345]
expected: 6
case 5
in: [9999]
expected: 9
Starter codePython
def digital_root(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor