Math Fundamentals18 sections · 814 units
Open in Course

Add Digits - Digital Root

Modulo 9 pattern

There's a mathematical property called the digital root. For any number n>0n > 0, the digital root equals 1+((n1)mod9)1 + ((n - 1) \bmod 9).

Why does this work? A number and the sum of its digits have the same remainder when divided by 99. Repeatedly summing digits preserves this remainder until you get a single digit.

Special case: if n=0n = 0, the answer is 00. Otherwise, use the formula. This gives you O(1)O(1) time instead of O(logn)O(\log n).