输入一个数字,求每一位相加之和
题目描述:
# In this kata, you must create a digital root function.
#
# A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n.
If that value has more than one digit, continue reducing in this way until a single-digit number is produced.
This is only applicable to the natural numbers.
我的解答:
def sum_digits(n):
if n < 10:
return n
else:
return sum_digits(n // 10) + sum_digits(n % 10)
def digital_root(n):
if n < 10:
return n
else:
n = (n // 10) + (n % 10)
return digital_root(n)
def sum_digits(n):
if n < 10:
return n
else:
return sum_digits(sum(map(int, str(n))))