The direct approach: while , extract each digit using , add them up, and repeat with the sum.
For : extract and , sum is . For : extract and , sum is . Now , so return .
This works but takes multiple iterations. There's a faster way using modulo arithmetic.