Условие
Показать перевод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.