关于快乐数

所谓的快乐数,就是每一位上的数拆开平方再相加再拆开平方相加循环,最后等于1的就是快乐数,否则为非快乐数,但是求出来结果和带入的数有个1到230的范围(虽然不知道哪儿来的)

循环写法

package methodTest;

public class Happy {

    public static boolean getHappy(int num){
        boolean ifHappy = false;

        int mayHappyNum = num;

        while (mayHappyNum>=1&&mayHappyNum<=230){

            int hund = mayHappyNum%1000/100;

            int ten = mayHappyNum%100/10;

            int dig = mayHappyNum%10;

            System.out.println(mayHappyNum);

            mayHappyNum = hund*hund+ten*ten+dig*dig;

            if(mayHappyNum == 1){
                ifHappy = true;
                break;
            }
        }
        return ifHappy;
    }

    public static void main(String[] args) {
        System.out.println(getHappy(19));
    }
}

递归写法

    /**
     * 因为测试了下有20这种无限循环导致爆出栈溢出错误的情况,所以特地加上了个限制循环次数的计数器,一般来说限制为1000应该是不会栈溢出的
     */
    public static int count = 0;
    /**
     * 因为迭代不能同时返回Boolean值和int值,所以得拆开分成两个方法来做
     */
    public static boolean getHappyIterate(int num){
        count = 0;
        boolean ifHappy = false;
        if(iterateBody(num) == 1){ifHappy = true;}
        return ifHappy;
    }

    public static int iterateBody(int num){
        int hund = num%1000/100;

        int ten = num%100/10;

        int dig = num%10;
        num = hund*hund+ten*ten+dig*dig;
        count++;
        System.out.println("第"+count+"次运算为"+num);
        if(count< 1000) {
            if (num == 1 || (num > 230 || num < 1)) {
                return num;
            } else {
                return iterateBody(num);
            }
        }else{
            return num;
        }
    }

整个程序外加测试的main方法

package methodTest;

public class Happy {

    /**
     * 因为测试了下有20这种无限循环导致爆出栈溢出错误的情况,所以特地加上了个限制循环次数的计数器,一般来说限制为1000应该是不会栈溢出的
     */
    public static int count = 0;

    public static boolean getHappy(int num){
        boolean ifHappy = false;

        count = 0;

        int mayHappyNum = num;

        while (mayHappyNum>=1&&mayHappyNum<=230){

            int hund = mayHappyNum%1000/100;

            int ten = mayHappyNum%100/10;

            int dig = mayHappyNum%10;

            mayHappyNum = hund*hund+ten*ten+dig*dig;

             count++;

            System.out.println("第"+count+"次运算为"+mayHappyNum);

            if(mayHappyNum == 1){
                ifHappy = true;
                break;
            }
        }
        return ifHappy;
    }

    /**
     * 因为迭代不能同时返回Boolean值和int值,所以得拆开分成两个方法来做
     */
    public static boolean getHappyIterate(int num){
        count = 0;
        boolean ifHappy = false;
        if(iterateBody(num) == 1){ifHappy = true;}
        return ifHappy;
    }

    public static int iterateBody(int num){
        int hund = num%1000/100;

        int ten = num%100/10;

        int dig = num%10;
        num = hund*hund+ten*ten+dig*dig;
        count++;
        System.out.println("第"+count+"次运算为"+num);
        if(count< 1000) {
            if (num == 1 || (num > 230 || num < 1)) {
                return num;
            } else {
                return iterateBody(num);
            }
        }else{
            return num;
        }
    }

    public static void main(String[] args) {
//        System.out.println(getHappy(19));
        System.out.println(getHappyIterate(19));
        System.out.println(getHappyIterate(20));
    }
}

你可能感兴趣的:(关于快乐数)