Description

Given a number n, return the sequence of numbers when repeatedly replacing the number by the sum of the squares of its digits until it reaches 1 or loops endlessly.

Examples

Input:n = 19
Output:[19,82,68,100,1]
Explanation:

19 → 82 → 68 → 100 → 1 (happy number).

Input:n = 7
Output:[7,49,97,130,10,1]
Explanation:

7 → 7² = 49 → 4² + 9² = 16 + 81 = 97 → 9² + 7² = 81 + 49 = 130 → 1² + 3² + 0² = 1 + 9 + 0 = 10 → 1² + 0² = 1 + 0 = 1 (happy number).

Input:n = 4
Output:[4,16,37,58,89,145,42,20,4]
Explanation:

4 → 4² = 16 → 1² + 6² = 1 + 36 = 37 → 3² + 7² = 9 + 49 = 58 → 5² + 8² = 25 + 64 = 89 → 8² + 9² = 64 + 81 = 145 → 1² + 4² + 5² = 1 + 16 + 25 = 42 → 4² + 2² = 16 + 4 = 20 → 2² + 0² = 4 + 0 = 4. The sequence loops back to 4 (unhappy number).

Constraints

  • 1 ≤ n ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!