Implement a recursive python pyhton 3 function digitsumn to
Implement a recursive python (pyhton 3) function digit_sum(n) to calculate the sum of all digits of the given integer n. Then, implement a recursive python function to compute the digital root digital_root(n) of the given integer n. Your function must use the digit_sum function. The digital root of a number is calculated by taking the sum of all of the digits in a number, and repeating the process with the resulting sum until only a single digit remains. For example, if you start with 1969, you must first add 1+9+6+9 to get 25. Since the value 25 has more than a single digit, you must repeat the operation to obtain 7 as a final answer. Your function digital_root(n) must use digit_sum function.
Solution
def digit_sum(n):
if n <= 0:
return 0;
else:
return n%10 + digit_sum(n/10);
def digital_root(n):
if n < 10:
return n;
else:
return digital_root(digit_sum(n));
print digital_root(1969)
Output:
sh-4.3$ python main.py
7
