---
# System prepended metadata

title: \#258 Add Digits
tags: [leetcode, leetcode-easy]

---

# \#258 Add Digits
## *給定一個數字,加總此數的每一位,不斷重複直到剩下一位數為止*
## Log
 - build 20210601 by syhuang

## 數學式解法
 - 見參考
```javascript=
var addDigits = function(num) {
    return 1 + (num - 1) % 9;
};
```
## 初見
 - 轉字串陣列+reduce解
```javascript=
var addDigits = function(num) {
    if(num<10) return num;
    return addDigits(
            num.toString().split('').reduce((a,b)=>parseInt(a)+parseInt(b),0)
        );
};
```
## 備註
 - 學習javascript reduce用法
## 參考
 - [參考解](https://leetcode.com/problems/add-digits/discuss/68580/Accepted-C%2B%2B-O(1)-time-O(1)-space-1-Line-Solution-with-Detail-Explanations)
 - [wiki-數根](https://en.wikipedia.org/wiki/Digital_root#Congruence_formula)
###### tags: `leetcode`, `leetcode-easy`