Happy Number

2017/12/8 posted in  leetcode

class Solution {
public:
    int happy(int num){
        int a = 0;
        while (num!=0) {
            a += int(pow(num%10, 2));
            num/=10;
        }
        return a;
    }


    bool isHappy(int n) {
        int slow, fast;
        slow = fast = n;
        do {
            slow = happy(slow);
            fast = happy(fast);
            fast = happy(fast);
        } while(slow != fast);

        return slow==1?1:0;
    }
};