Happy Number
A happy number is defined by repeatedly replacing it with the sum of squares of its digits until it either reaches 1 (happy) or loops endlessly (not happy).
Return True if n is a happy number.
Example
19
True
- The input
nis a positive integer, here . Each step replaces the number with the sum of the squares of its decimal digits. - Starting at : .
- From : . From : .
- From : . The sequence reached , so is happy and the output is
True. - Notice what the other ending looks like: for the sequence runs , landing back on a value it already produced, so it repeats forever without touching and the answer is
False. - The input is already , so it counts as happy with zero steps.
Constraints
- 1 <= n <= 2^31 - 1