The digital root comes from summing a number's digits repeatedly until one digit is left: 9875 → 29 → 11 → 2.
You can loop it, but there's a gem of number theory: a number and its digit sum leave the same remainder mod 9, because 10 ≡ 1 (mod 9). So:
root(0) = 0
root(n) = 1 + (n − 1) % 9 for n > 0
Recognising an invariant (here, "mod 9 is preserved") often collapses a loop into a formula.