A number and the sum of its digits have the same remainder when divided by 9. Why? Because 10 ≡ 1 (mod 9), so 10^k ≡ 1 (mod 9) for any k.
This means num mod 9 equals the digital root, with one exception: if num mod 9 = 0 and num ≠ 0, the digital root is 9, not 0.
Formula: if num = 0, return 0. Otherwise return 1 + (num - 1) mod 9. This handles the exception simplely.