We recursively sum the digits of the number. If the result is a single digit, return it. Otherwise, recurse again on the sum.
- Base case: If
num < 10, returnnum. - Compute digit sum by taking
num % 10(last digit) + recursive call onnum // 10. - If the digit sum is >= 10, recurse on the digit sum.
- Time Complexity: O(log N) per pass, O(log N) passes in worst case.
- Space Complexity: O(log N) (recursion stack).
def digital_root(num):
if num < 10:
return num
def digit_sum(n):
if n == 0:
return 0
return n % 10 + digit_sum(n // 10)
return digital_root(digit_sum(num))