Tutte le sfide
Media 0 risolte 0% di accettazione

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.

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.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.