Happy Number

 

public boolean isHappy(int n) {
        // Write your code here
        if(n <= 0)
            return false;
        long nn = (long)n;
        Set<Long> set = new HashSet<Long>();
        while(true) {
            if(nn == 1)
                return true;
            if(set.contains(nn))
                return false;
            else
                set.add(nn);
            long tmp = 0;
            while(nn != 0){
                tmp+= (nn % 10)*(nn % 10);
                nn/=10;
            }
            nn = tmp;
        }
    }