happyNum的判断

happyNum的描述:


A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the 

squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not 

include 1. Those numbers for which this process ends in 1 are happy numbers.


example:1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 这些数都是happyNum


具体代码:

public static boolean judgeHappyNum(int n){
		String s = Integer.valueOf(n).toString();
		HashSet<Integer> hashSet = new HashSet<>();
		while(true){
			int temp = 0;
			for (int i = 0; i < s.length(); i++) {
				temp = temp +(int)Math.pow(Integer.valueOf(String.valueOf(s.charAt(i))), 2);
			}
			if (temp == 1) {
				return true;
			}
			if (!hashSet.contains(temp)) {
				hashSet.add(temp);
				s = String.valueOf(temp);
			}else {
				return false;
			}
		}
		
		
	}


你可能感兴趣的:(判断,快乐数,happynum)