問題
翻訳を表示Add up the digits of n. If the result has more than one digit, add up its digits again. Repeat until a single digit remains.
Implement digital_root(n).
Input
n: A positive integer.
Output
Return the final single digit.
制約
1 ≤ n ≤ 10¹⁸
例
例 1
入力:
n = 999
期待される出力:
expected = 9
入力:
long long n = 999;
期待される出力:
long long expected = 9;
入力:
long n = 999;
期待される出力:
long expected = 9;
解説
9 + 9 + 9 = 27, then 2 + 7 = 9.
例 2
入力:
n = 38
期待される出力:
expected = 2
入力:
long long n = 38;
期待される出力:
long long expected = 2;
入力:
long n = 38;
期待される出力:
long expected = 2;
解説
3 + 8 = 11, then 1 + 1 = 2.