java judge whether it is a happy number

Definition of happiness number:

happy number has the following characteristics:
In a given carry system, the sum of squares of all the digits of the number is obtained. The new number is again calculated as the sum of squares of all the digits. If this is repeated, the final result must be 1.

Take the decimal system for example:
2 8 → 2²+8²=68 → 6²+8²=100 → 1²+0²+0²=1
3 2 → 3²+2²=13 → 1²+3²=10 → 1²+0²=1
3 7 → 3²+7²=58 → 5²+8²=89 → 8²+9²=145 → 1²+4²+5²=42 → 4²+2²=20 → 2²+0²=4 → 4²=16 → 1²+6²=37......

Therefore, 28 and 32 are happy numbers. In the calculation process of 37, 37 appears repeatedly. The result of continuous calculation will only be the cycle of the above numbers, and there will be no 1. Therefore, 37 is not a happy number.
The numbers that are not happy numbers are called unhappiness numbers. The sum of the squares of all unhappiness numbers will finally enter the cycle of 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4.

Obviously, this can be solved by recursion
If a number is 1, it's happiness number. If it's not 1, then judge whether it's 4, 16, 37 Wait for the number in the above loop. If it is false, it will inherit the recursive judgment.
Method 1:

 public static int sqrt(int n){
        if(n < 10){
            return n * n;
        }

        int sum = 0;
        while ( n >= 10){       //If n is two digits
            int t = n % 10;     //Seek lower bits
            sum += t * t;       //Sum up the squares of the lower bits into sum

            n = n / 10;         // n remove low digits
        }

        sum += n * n;           // When n is one digit, the above condition is not satisfied, but it needs to be added to sum

        return sum;
    }

Then write isHappyNumber(int n)

  public static boolean isHappyNumber(int number){
        //The following are recursive exit conditions
        if(number <= 0 ){
            return false;
        }

        if(number == 1){
            return true;
        }

        //4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
        if(number == 4 || number == 16 || number == 37 || number == 58
            || number == 89 || number == 145 || number == 42 || number == 20){
            return false;
        }
        int result = sqrt(number);
        if(result == 1){
            return true;
        }

        return isHappyNumber(result);//Recursive call
    }

Method two

public class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet();
        return isHappyNum(n, set);    
    }
    
    public boolean isHappyNum(int n, Set set){
        if (set.contains(n)){
            return false;
        }
        set.add(n);//In fact, we still use the fact that the number of unhappiness will have a cycle to judge
        int num = 0;
        while (n != 0){
            num += (n % 10) * (n % 10);
            n /= 10;
        }
        if (num == 1){
            return true;
        } else {
            return isHappyNum(num, set);
        }
    }
}

Posted by Dani34 on Mon, 25 Nov 2019 13:19:30 -0800