Happy Number
A "Happy Number" is defined as a number that after finite number of "steps" - where
we sum the square of each digit each time - the result is a 1
. Given a number n
,
determine whether it is a happy number.
As a challenge, complete this question under constant space.
Parameters
n
: The number to check.
Result
true
orfalse
, depending on whether this number is a happy number.
Examples
Example 1
Input: n = 19
Output: true
Explanation:
11^2 + 9^2 = 82 28^2 + 2^2 = 68 36^2 + 8^2 = 100 41^2 + 0^2 + 0^2 = 1
Example 2
Input: n = 2
Output: false
Explanation:
12^2 = 4 24^2 = 16 31^2 + 6^2 = 37 43^2 + 7^2 = 58 55^2 + 8^2 = 89 68^2 + 9^2 = 145 71^2 + 4^2 + 5^2 = 42 84^2 + 2^2 = 20 92^2 + 0^2 = 4 10...
Constraints
1 <= n < 2^31