class Solution {

public:

int loop[8] = {4,16,37,58,89,145,42,20};

```
bool inLoop(int n){
for(auto x: loop){
if(x == n) return true;
}
return false;
}
bool isHappy(int n) {
if(n == 1) return true;
if(inLoop(n)) return false;
int next = 0;
while(n){
next += (n%10)*(n%10);
n /= 10;
}
return isHappy(next);
}
```

};

proof:

1.loop number is less than 162.

Assume f(x) is the sum of the squares of x's digits. let's say 0 < x <= 9,999,999,999 which is larger than the range of an int. f(x) <= 9^2 * 10 = 810. So no mater how big x is, after one step of f(x), it will be less than 810.The most large f(x) value (x < 810) is f(799) = 211. And do this several times: f(211) < f(199) = 163. f(163) < f(99) = 162. So no mater which x you choose after several times of f(x),it finally fall in the range of [1,162] and can never get out.

2.I check every unhappy number in range of [1,162] , they all fall in loop {4,16,37,58,89,145,42,20} ,which means every unhappy number fall in this loop.