Happy Number
EasyMath & Geometry
A positive integer is happy if the following process eventually reaches 1:
- Replace the number with the sum of the squares of its decimal digits.
- Repeat.
If the process never reaches 1, it falls into a repeating cycle that does not include 1, and the number is not happy.
Given n, return True if n is happy and False otherwise.
Example 1
- Input:
- n = 23
- Output:
- True
- Explanation
- 22 + 32 = 13, then 12 + 32 = 10, then 12 + 02 = 1, so 23 is happy.
Example 2
- Input:
- n = 4
- Output:
- False
- Explanation
- 4 goes to 16, 37, 58, 89, 145, 42, 20 and then back to 4. The sequence repeats forever without reaching 1.
Example 3
- Input:
- n = 100000
- Output:
- True
- Explanation
- Only the leading 1 contributes, so the first step already gives 1.
Constraints
1 <= n <= 2^31 - 1