Problema
Mostra traduzioneAdd 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.
Vincoli
1 ≤ n ≤ 10¹⁸
Esempi
Esempio 1
Input:
n = 999
Output atteso:
expected = 9
Input:
long long n = 999;
Output atteso:
long long expected = 9;
Input:
long n = 999;
Output atteso:
long expected = 9;
Spiegazione
9 + 9 + 9 = 27, then 2 + 7 = 9.
Esempio 2
Input:
n = 38
Output atteso:
expected = 2
Input:
long long n = 38;
Output atteso:
long long expected = 2;
Input:
long n = 38;
Output atteso:
long expected = 2;
Spiegazione
3 + 8 = 11, then 1 + 1 = 2.