You are given a positive integer n.
Your task is to determine whether n is a happy number. A happy number is defined by the following process: starting with the given number, replace it with the sum of the squares of its digits, and repeat until the number either equals 1 or enters an endless cycle.
Note that:
1.1.Return true if n is a happy number, and false otherwise.
19
True
1² + 9² = 82 → 8² + 2² = 68 → 6² + 8² = 100 → 1² + 0² + 0² = 1. The process reaches 1, so 19 is a happy number.
2
False
2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → … The process enters a cycle and never reaches 1, so 2 is not a happy number.
1
True
1² = 1. The number is already 1, so it is a happy number.
19
True