코드로 우주평화

Sum of Digits / Digital Root 본문

나는 이렇게 학습한다/Algorithm & SQL

Sum of Digits / Digital Root

daco2020 2022. 3. 23. 10:45
반응형

Description:

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. The input will be a non-negative integer.

Examples

    16  -->  1 + 6 = 7
   942  -->  9 + 4 + 2 = 15  -->  1 + 5 = 6
132189  -->  1 + 3 + 2 + 1 + 8 + 9 = 24  -->  2 + 4 = 6
493193  -->  4 + 9 + 3 + 1 + 9 + 3 = 29  -->  2 + 9 = 11  -->  1 + 1 = 2

 

 

Solution:

1. If 'n' is less than 10, add each digit.
2. Repeat until a single digit appears.
3. When a single digit is returned, it is returned.

 

def digital_root(n):    
    return (digital_root(sum([int(i) for i in str(n)])) if n >= 10 else n)

 

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Is n divisible by x and y?  (0) 2022.03.25
Find the divisors!  (0) 2022.03.24
Dashatize it  (0) 2022.03.22
Data Reverse  (0) 2022.03.21
Persistent Bugger  (0) 2022.03.20