Happy Number

Please refer to the problem statements through this link. It is a problem hard to think of a solution but easy to implement. To solve this problem, one should pick some numbers to simulate the process of generating Happy Number and after tried several numbers it is likely that we can find:

  1. Every number will iterate into a number with only one digit in finite steps.
  2. For 1 - 9, only 1 and 7 is Happy Number, so if a number generate a number in 1 - 9 but not equals to 1 or 7 in its iteration process, it is not a Happy Number.
  3. In fact every Non-HappyNumber will finally generate a 4. This law is not easy to find.
  4. For a 32-bit integer, in the first generation step it will turn to a integer less than 9922 (see the explanation comment in the sample code below)
public class HappyNumber {
    public boolean isHappy(int n) {
        while (true) {
            int next = 0;

            while (n > 0) {
                next += (n % 10) * (n % 10);
                n /= 10;
            }
            // from 1 -> 9 only 1 and 7 are happy numbers, and for any digits,
            // after infinite iteration it will product a sum in one digit.
            // So if this digit is happy, then it's happy. 
            //
            // In fact, 7 will finally turned into 1, so we can just check
            // if the sum is one. It is quite amazing!
            //
            // All positive numbers in 32 bit at most take 11 digits in this desimal representation.
            // So sum of those positive numbers' digits is at most 11*9^2 = 9911. We can only check these
            // 9911 numbers to prove that all their sequence will finally came into a one digit number,
            // and thus if it is a happy number is the same as that digit number.

            if (next < 10) {
                if (next == 1 || next == 7) return true;
                else return false;
            }
            n = next;
        }
    }
}