java算法_快乐数


    public static void main(String[] args) {
        Week2 w=new Week2();
        boolean a=w.isHappy(999);
        System.out.println(a);
    }
    public boolean isHappy(int n) { int temp = n;
        while (true) {
            temp = getNext(temp);
            if (temp > 243) {
                continue;
            } else if (temp == 4 || temp == 16 || temp == 37 || temp == 58 ||
                    temp == 89 || temp == 145 || temp == 42 || temp == 20) {
                return false;
            } else if (temp == 1) {
                return true;
            }
        }
    }
    /**
     * 用取余和整除的方式,获取一个数字中的每个
     */
    private int getNext(int num) {
        int result = 0;
        while (num > 0) {
            result += (num % 10) * (num % 10);
            num = num / 10;
        }
        return result;
    }

你可能感兴趣的:(java算法_快乐数)