Write a method to determine whether a postive number is Happy.
A number is Happy (Yes, it is a thing!) if it follows a sequence that ends in 1 after following the steps given below :
Beginning with the number itself, replace it by the sum of the squares of its digits until either the number becomes 1 or loops endlessly in a cycle that does not include 1.
For instance, 19 is a happy number.
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Numbers aren't Happy if they "Loop endlessly in a cycle". Knowing that, you should immediately guess that you'll need a buffer of some sort to keep a history of visited numbers. A HashSet is a good choice!
Loop until a number already exists in the visited set. Inside the loop, compute the expression as stated in the problem. If the result is 1, return true. Otherwise, add this number to the visited set. Outside the loop, simply return false.