Digital Root without loops Python

后端 未结 6 859
无人及你
无人及你 2021-01-25 07:58
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         


        
6条回答
  •  被撕碎了的回忆
    2021-01-25 08:44

    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))
    

提交回复
热议问题