Digital Root without loops Python

后端 未结 6 857
无人及你
无人及你 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:23

    def droot(a):
        while a>=10:
             b =int(a/10) #other digit(s)
             c =a-(b*10)  #the rightmost digit
             a =b+c #add together
        return a
    

    I have not seen this solution anywhere, so I thought it would be nice to share, i discovered it myself! Just 1 looping structure.

提交回复
热议问题