A happy number is found with this process:
- Start with a positive whole number.
- Square each of its digits and add the squares. The total is the new number.
- Do the same again and again. Either you reach
1 (and you stay at 1), or you go round and round in a loop that never contains 1.
A number is happy when the process reaches 1.
Given n, return true if it is a happy number. Otherwise return false.
Examples
Example 1:
Input: n = 23
Output: true
Explanation: 2^2 + 3^2 = 13, then 1^2 + 3^2 = 10, then 1^2 + 0^2 = 1.
Example 2:
Input: n = 20
Output: false
Explanation: 20 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20. It comes back to 20 and loops forever.
Example 3:
Input: n = 68
Output: true
Explanation: 6^2 + 8^2 = 100. Then 1 + 0 + 0 = 1.