Digital Roots

The digital root is defined as the sum of all the digits of a number, \(n\).

More concretely, suppose that,

\[n = d_0 + d_1 b + d_2 b^2 + \cdots + d_n b^n\, ,\]

therefore the digits are given as \(\{ d_0, d_1, d_2, \dots, d_n \}\) and so the digital root, \(D\), is defined by,

\[D \equiv \sum_{i=0}^n d_i \]
function digitalroot(n::Int)
    while (n = sum(digits(n))) > 9 end
    return n
end

or using recursion,

function digitalroot(n::Int)
    n < 10 && return n
    return digitalroot(n |> digits |> sum)
end