判断是否是快乐数

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);
}
原文地址:https://www.cnblogs.com/wuOverflow/p/4708084.html