28 - Sum of Digits / Digital Root
Q.
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
A)
function digital_root(n) {
// ...
let result = n.toString().split('')
result = result.reduce((a,b) => (+a)+(b))
while(result.toString().length>1) {
result = result.toString().split('').reduce((a,b) => (+a)+(+b))
}
return +result
}
function digital_root(n) {
// ...
let result = n.toString().split('')
result = result.reduce((a,b) => (+a)+(b))
while(result.toString().length>1) {
result = result.toString().split('').reduce((a,b) => (+a)+(+b))
}
return +result
}생각하기 좋은 문제인 것 같다
Author And Source
이 문제에 관하여(28 - Sum of Digits / Digital Root), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@developerjhp/알고리즘-28-Sum-of-Digits-Digital-Root저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)