The tricky part is detecting when you're in a cycle. If you see the same number twice, you're looping and will never reach .
Use a set to track all numbers you've seen. Before computing the next sum, check if the current number is already in the set.
To extract digits, use the pattern from earlier: , then . Repeat until .