1325. Does It Settle On One
Repeatedly replace a number by the sum of the squares of its digits. Print
yes if this eventually reaches 1, and no otherwise.
19 -> 1 + 81 = 82 -> 64 + 4 = 68 -> 36 + 64 = 100 -> 1 yes
4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 no
A number that does not reach 1 falls into a cycle, so the loop never ends by itself and needs a stopping rule.
The straightforward rule is to remember every value seen and stop on a repeat.
Keep the running value, not the string: extracting digits with % 10 and / 10
avoids converting to text on every step.
There is a neater rule worth knowing. Every number that fails eventually reaches 4, so a single check against 4 is enough and needs no memory at all. Both are correct here, and the memoryless one is what an interviewer is hoping you notice after the first version works.
The value never grows without bound: any number below 10^9 has at most 10
digits, so one step can produce at most 10 * 81 = 810, and everything after the
first step is small.
Constraints - `1 ≤ n ≤ 1000000000`
Input
One line containing the integer n.
Output
Print yes if the process reaches 1, or no otherwise.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.