bool isHappy(int n) {
// If not happy, it will finally recycle in the checkList.
array<int, 8> checkList{4,16,37,58,89,145,42,20};
auto caculate = [](string&& s)
{
int sum = 0;
for (auto&& ch : s){
auto n = static_cast<int>(ch - '0');
sum += n * n;
}
return sum;
};
function<bool(int)> check;
check = [&](int n)
{
if (n == 1){
return true;
}
if (find(checkList.cbegin(), checkList.cend(), n) != checkList.cend()){
return false;
}
return check(caculate(to_string(n)));
};
return check(n);
}