Happy number trace
O(log n) per stepA happy number eventually reaches 1 when you repeatedly replace it with the sum of the squares of its digits. Any number that is not happy falls into a cycle through 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. The concept was introduced in the 1990s and later made famous by recreational mathematicians. About 14.3% of positive integers are happy. The visualizer traces each replacement step and marks the terminal value green if 1 is reached, or highlights the repeated value if a cycle is detected.
Sequence
Press ▶ to run
Edit the input and press Play
How it works
- Write out n.
- Replace by the sum of squares of its digits.
- Check: is it 1 (happy) or a repeat (unhappy)?
- Continue until one condition triggers.
Pseudocode
1happyTrace(n): # O(log n) per step2 seen ← {n}3 while n ≠ 1:4 n ← Σ(digit² for digit in n)5 emit n6 if n in seen: break # unhappy cycle7 seen.add(n)8 happy ← (n == 1)