...

/

Happy Number (medium)

Happy Number (medium)

Problem Statement

Any number will be called a happy number if, after repeatedly replacing it with a number equal to the sum of the square of all of its digits, leads us to number ‘1’. All other (not-happy) numbers will never reach ‘1’. Instead, they will be stuck in a cycle of numbers which does not include ‘1’.

Example 1:

Input: 23   
Output: true (23 is a happy number)  
Explanations: Here are the steps to find out that 23 is a happy number:
  1. 22+322^2 + 3 ^2 = 4 + 9 = 13
  2. 12+321^2 + 3^2 = 1 + 9 = 10
  3. 12+021^2 + 0^2 = 1 + 0 = 1

Example 2:

Input: 12   
Output: false (12 is not a happy number)  
Explanations: Here are the steps to find out that 12 is not a happy number:
  1. 12+221^2 + 2 ^2 = 1 + 4 = 5
  2. 525^2
...