K2Raygun11 - 1 year ago 106

Python Question

`def digit_sum(n):`

'''(int)->number

Returns the sum of all the digits in the given integer, n'''

if n<10:

return n

return n%10 + digit_sum(n//10)

def digital_root(n):

'''(int)->number

Returns the resulting sum of the digits in the given integer until it reaches a single digit number; via digit_sum'''

while n>9:

n=sum(digit_sum(n))

return n

Wrote the code for

`digit_sum`

`digital_root`

Answer Source

So the idea is that you have to use recursion for the last one as well? In that case, this should do the job:

```
def digital_root(n):
if n < 10:
return n
return digital_root(digit_sum(n))
```