Write an algorithm to determine if a number is happy.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
19 is a happy number
1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1
According to the Wikipedia entry Happy Number, an non-happy number will eventually reach the sequence
4, 16, 37, 58, 89, 145, 42, 20, 4,... and loop in this sequence forever.
In our solution, we just have to check if the current square sum has already been considered. If the sum has already been considered, then we’re in a loop and the current number is not a happy number. We check this look a
HashSet<Integer>. For each sum we have considered for the number
n, we add the sum to the hash set. When we compute a new sum, we check the hash set to see if the sum is already in the set; if it is, we immediately return
false. If the sum reaches
1, we return
The code of this solution follows.