#15The Digital Alchemy Experiment
An alchemist is experimenting with a mysterious number n. During each experiment, the alchemist:
- Breaks the current number into its individual digits.
- Squares each digit.
- Adds all the squared values together.
- Uses the resulting sum as the number for the next experiment.
The experiment is considered a success if the number eventually transforms into the legendary number 1. However, if the transformation starts repeating numbers in a cycle and never reaches 1, the experiment fails. Determine whether the alchemist's experiment will succeed. Return true if the number eventually reaches 1; otherwise, return false.
Real-World Applications: Cycle Detection in Iterative Systems; Digital Signal Processing; State Transition Analysis;
Examples
Example 1
Input: n = 19
Output: true
Explanation:
19 → 1² + 9² = 82 82 → 8² + 2² = 68 68 → 6² + 8² = 100 100 → 1² + 0² + 0² = 1
Example 2
Input: n = 2
Output: false
Explanation:
2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 ... The transformation enters a repeating cycle and never reaches 1.
Example 3
Input: n = 1
Output: true
Explanation:
The number is already the legendary number 1.
Constraints
- 1 <= n <= 2³¹ - 1