中級 0件解決済み 正答率0%

Digital Root

Sum the digits repeatedly until one digit remains.

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.

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。