Sum all the digits of number N (>0), if result has more than one digit, repeat summing all the digits of the result of each round, until reaching a single digit number R.
For example:
$$N = 365 => 3 + 6 + 5 = 14 => R = 1 + 4 = 5 $$
Algorithm: $$R = (N – 1) mod 9 + 1$$
Proof
Represent each digit in N
as a_{k}
k
: digit position from 0 to n
, n = total digits – 1
$$N = {a_{n}}*10^{n}+{a_{n-1}}*10^{n-1}…+{a_{1}}*10^{1}+{a_{0}}*10^{0}$$
$$∵ {a_{k}}*10^{k} ≡ {a_{k}} (mod 9) (because any 10^{k} mod 9 = 1)$$
$$∴ a_{n}*10^{n}+a_{n-1}*10^{n-1}…+{a_{1}}*10^{1}+{a_{0}}*10^{0} ≡ {a_{n}}+{a_{n-1}}…+{a_{1}}+{a_{0}} (mod 9)$$
$$=> N ≡ R (mod 9)$$
Modulo 9 produces a natural number ∈ [0, 8]
, to achieve a result ∈ [1, 9]
:
$$(N – 1) mod 9 + 1 = (R – 1) mod 9 + 1 = R$$
$$=> R = (N – 1) mod 9 + 1$$